Skip to content

Commit a8e0252

Browse files
committed
Fix exporting milestone for issues and PRs
There was a weird pointer bug which would cause a null milestone to erase "milestone" fields for previous entries in the list.
1 parent 5821065 commit a8e0252

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

api/export_pr.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
)
77

88
func (issue *Issue) ExportData(fields []string) *map[string]interface{} {
9-
v := reflect.ValueOf(issue).Elem()
109
data := map[string]interface{}{}
1110

1211
for _, f := range fields {
@@ -26,6 +25,7 @@ func (issue *Issue) ExportData(fields []string) *map[string]interface{} {
2625
case "projectCards":
2726
data[f] = issue.ProjectCards.Nodes
2827
default:
28+
v := reflect.ValueOf(issue).Elem()
2929
sf := fieldByName(v, f)
3030
data[f] = sf.Interface()
3131
}
@@ -35,7 +35,6 @@ func (issue *Issue) ExportData(fields []string) *map[string]interface{} {
3535
}
3636

3737
func (pr *PullRequest) ExportData(fields []string) *map[string]interface{} {
38-
v := reflect.ValueOf(pr).Elem()
3938
data := map[string]interface{}{}
4039

4140
for _, f := range fields {
@@ -76,6 +75,7 @@ func (pr *PullRequest) ExportData(fields []string) *map[string]interface{} {
7675
}
7776
data[f] = &requests
7877
default:
78+
v := reflect.ValueOf(pr).Elem()
7979
sf := fieldByName(v, f)
8080
data[f] = sf.Interface()
8181
}
@@ -86,16 +86,16 @@ func (pr *PullRequest) ExportData(fields []string) *map[string]interface{} {
8686

8787
func ExportIssues(issues []Issue, fields []string) *[]interface{} {
8888
data := make([]interface{}, len(issues))
89-
for i, issue := range issues {
90-
data[i] = issue.ExportData(fields)
89+
for i := range issues {
90+
data[i] = issues[i].ExportData(fields)
9191
}
9292
return &data
9393
}
9494

9595
func ExportPRs(prs []PullRequest, fields []string) *[]interface{} {
9696
data := make([]interface{}, len(prs))
97-
for i, pr := range prs {
98-
data[i] = pr.ExportData(fields)
97+
for i := range prs {
98+
data[i] = prs[i].ExportData(fields)
9999
}
100100
return &data
101101
}

api/export_pr_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ func TestIssue_ExportData(t *testing.T) {
3131
}
3232
`),
3333
},
34+
{
35+
name: "milestone",
36+
fields: []string{"number", "milestone"},
37+
inputJSON: heredoc.Doc(`
38+
{ "number": 2345, "milestone": {"title": "The next big thing"} }
39+
`),
40+
outputJSON: heredoc.Doc(`
41+
{
42+
"milestone": {
43+
"title": "The next big thing"
44+
},
45+
"number": 2345
46+
}
47+
`),
48+
},
3449
{
3550
name: "project cards",
3651
fields: []string{"projectCards"},
@@ -75,6 +90,31 @@ func TestIssue_ExportData(t *testing.T) {
7590
}
7691
}
7792

93+
func TestExportIssues(t *testing.T) {
94+
issues := []Issue{
95+
{Milestone: Milestone{Title: "hi"}},
96+
{},
97+
}
98+
exported := ExportIssues(issues, []string{"milestone"})
99+
100+
buf := bytes.Buffer{}
101+
enc := json.NewEncoder(&buf)
102+
enc.SetIndent("", "\t")
103+
require.NoError(t, enc.Encode(exported))
104+
assert.Equal(t, heredoc.Doc(`
105+
[
106+
{
107+
"milestone": {
108+
"title": "hi"
109+
}
110+
},
111+
{
112+
"milestone": null
113+
}
114+
]
115+
`), buf.String())
116+
}
117+
78118
func TestPullRequest_ExportData(t *testing.T) {
79119
tests := []struct {
80120
name string
@@ -95,6 +135,21 @@ func TestPullRequest_ExportData(t *testing.T) {
95135
}
96136
`),
97137
},
138+
{
139+
name: "milestone",
140+
fields: []string{"number", "milestone"},
141+
inputJSON: heredoc.Doc(`
142+
{ "number": 2345, "milestone": {"title": "The next big thing"} }
143+
`),
144+
outputJSON: heredoc.Doc(`
145+
{
146+
"milestone": {
147+
"title": "The next big thing"
148+
},
149+
"number": 2345
150+
}
151+
`),
152+
},
98153
{
99154
name: "status checks",
100155
fields: []string{"statusCheckRollup"},

0 commit comments

Comments
 (0)