Skip to content

Commit ba05d12

Browse files
Loïc Dacharyrealaravinth
authored andcommitted
migration: milestone schema and validation
Signed-off-by: Loïc Dachary <[email protected]>
1 parent a850899 commit ba05d12

File tree

6 files changed

+116
-27
lines changed

6 files changed

+116
-27
lines changed

modules/migration/file_format.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,25 @@ func validate(bs []byte, datatype interface{}, isJSON bool) error {
6060
}
6161
}
6262

63+
var schemaFilename string
6364
switch datatype := datatype.(type) {
6465
case *[]*Issue:
65-
sch, err := getSchema("issue.json")
66-
if err != nil {
67-
return err
68-
}
69-
err = sch.Validate(v)
70-
if err != nil {
71-
log.Error("migration validation failed for\n%s", string(bs))
72-
}
73-
return err
66+
schemaFilename = "issue.json"
67+
case *[]*Milestone:
68+
schemaFilename = "milestone.json"
7469
default:
75-
return fmt.Errorf("file_format:validate: %T is not a known time", datatype)
70+
return fmt.Errorf("file_format:validate: %T has not a validation implemented", datatype)
71+
}
72+
73+
sch, err := getSchema(schemaFilename)
74+
if err != nil {
75+
return err
76+
}
77+
err = sch.Validate(v)
78+
if err != nil {
79+
log.Error("migration validation with %s failed for\n%s", schemaFilename, string(bs))
7680
}
81+
return err
7782
}
7883

7984
func toStringKeys(val interface{}) (interface{}, error) {

modules/migration/file_format_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ func TestMigrationJSON_IssueFail(t *testing.T) {
3131
t.Fatalf("got: type %T with value %s, want: *jsonschema.ValidationError", err, err)
3232
}
3333
}
34+
35+
func TestMigrationJSON_MilestoneOK(t *testing.T) {
36+
milestones := make([]*Milestone, 0, 10)
37+
err := Load("file_format_testdata/milestones.json", &milestones, true)
38+
assert.NoError(t, err)
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"title": "title_a",
4+
"description": "description_a",
5+
"deadline": "1988-04-12T23:20:50.52Z",
6+
"created": "1985-04-12T23:20:50.52Z",
7+
"updated": "1986-04-12T23:20:50.52Z",
8+
"closed": "1987-04-12T23:20:50.52Z",
9+
"state": "closed"
10+
},
11+
{
12+
"title": "title_b",
13+
"description": "description_b",
14+
"deadline": "1998-04-12T23:20:50.52Z",
15+
"created": "1995-04-12T23:20:50.52Z",
16+
"updated": "1996-04-12T23:20:50.52Z",
17+
"closed": null,
18+
"state": "open"
19+
}
20+
]

modules/migration/milestone.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import "time"
99

1010
// Milestone defines a standard milestone
1111
type Milestone struct {
12-
Title string
13-
Description string
14-
Deadline *time.Time
15-
Created time.Time
16-
Updated *time.Time
17-
Closed *time.Time
18-
State string // open, closed
12+
Title string `json:"title"`
13+
Description string `json:"description"`
14+
Deadline *time.Time `json:"deadline"`
15+
Created time.Time `json:"created"`
16+
Updated *time.Time `json:"updated"`
17+
Closed *time.Time `json:"closed"`
18+
State string `json:"state"`
1919
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"title": "Milestone",
3+
"description": "Milestone associated to a repository within a forge.",
4+
5+
"type": "array",
6+
"items": {
7+
"type": "object",
8+
"additionalProperties": false,
9+
"properties": {
10+
"title": {
11+
"description": "Short description.",
12+
"type": "string"
13+
},
14+
"description": {
15+
"description": "Long, multiline, description.",
16+
"type": "string"
17+
},
18+
"deadline": {
19+
"description": "Deadline after which the milestone is overdue.",
20+
"type": "string",
21+
"format": "date-time"
22+
},
23+
"created": {
24+
"description": "Creation time.",
25+
"type": "string",
26+
"format": "date-time"
27+
},
28+
"updated": {
29+
"description": "Last update time.",
30+
"type": "string",
31+
"format": "date-time"
32+
},
33+
"closed": {
34+
"description": "The last time 'state' changed to 'closed'.",
35+
"anyOf": [
36+
{
37+
"type": "string",
38+
"format": "date-time"
39+
},
40+
{
41+
"type": "null"
42+
}
43+
]
44+
},
45+
"state": {
46+
"description": "A 'closed' issue will not see any activity in the future, otherwise it is 'open'.",
47+
"enum": [
48+
"closed",
49+
"open"
50+
]
51+
}
52+
},
53+
"required": [
54+
"title",
55+
"description",
56+
"deadline",
57+
"created",
58+
"updated",
59+
"closed",
60+
"state"
61+
]
62+
},
63+
64+
"$schema": "http://json-schema.org/draft-04/schema#",
65+
"$id": "http://example.com/milestone.json",
66+
"$$target": "milestone.json"
67+
}

services/migrations/restore.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,14 @@ func (r *RepositoryRestorer) GetTopics() ([]string, error) {
113113
func (r *RepositoryRestorer) GetMilestones() ([]*base.Milestone, error) {
114114
milestones := make([]*base.Milestone, 0, 10)
115115
p := filepath.Join(r.baseDir, "milestone.yml")
116-
_, err := os.Stat(p)
116+
err := base.Load(p, &milestones, r.validation)
117117
if err != nil {
118118
if os.IsNotExist(err) {
119119
return nil, nil
120120
}
121121
return nil, err
122122
}
123123

124-
bs, err := os.ReadFile(p)
125-
if err != nil {
126-
return nil, err
127-
}
128-
129-
err = yaml.Unmarshal(bs, &milestones)
130-
if err != nil {
131-
return nil, err
132-
}
133124
return milestones, nil
134125
}
135126

0 commit comments

Comments
 (0)