Merge pull request #3529 from cli/milestone-export-fix

Fix exporting `milestone` for issues and PRs
This commit is contained in:
Mislav Marohnić 2021-04-29 11:52:15 +02:00 committed by GitHub
commit d1d49c1810
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 6 deletions

View file

@ -6,7 +6,6 @@ import (
)
func (issue *Issue) ExportData(fields []string) *map[string]interface{} {
v := reflect.ValueOf(issue).Elem()
data := map[string]interface{}{}
for _, f := range fields {
@ -26,6 +25,7 @@ func (issue *Issue) ExportData(fields []string) *map[string]interface{} {
case "projectCards":
data[f] = issue.ProjectCards.Nodes
default:
v := reflect.ValueOf(issue).Elem()
sf := fieldByName(v, f)
data[f] = sf.Interface()
}
@ -35,7 +35,6 @@ func (issue *Issue) ExportData(fields []string) *map[string]interface{} {
}
func (pr *PullRequest) ExportData(fields []string) *map[string]interface{} {
v := reflect.ValueOf(pr).Elem()
data := map[string]interface{}{}
for _, f := range fields {
@ -76,6 +75,7 @@ func (pr *PullRequest) ExportData(fields []string) *map[string]interface{} {
}
data[f] = &requests
default:
v := reflect.ValueOf(pr).Elem()
sf := fieldByName(v, f)
data[f] = sf.Interface()
}
@ -86,16 +86,16 @@ func (pr *PullRequest) ExportData(fields []string) *map[string]interface{} {
func ExportIssues(issues []Issue, fields []string) *[]interface{} {
data := make([]interface{}, len(issues))
for i, issue := range issues {
data[i] = issue.ExportData(fields)
for i := range issues {
data[i] = issues[i].ExportData(fields)
}
return &data
}
func ExportPRs(prs []PullRequest, fields []string) *[]interface{} {
data := make([]interface{}, len(prs))
for i, pr := range prs {
data[i] = pr.ExportData(fields)
for i := range prs {
data[i] = prs[i].ExportData(fields)
}
return &data
}

View file

@ -31,6 +31,21 @@ func TestIssue_ExportData(t *testing.T) {
}
`),
},
{
name: "milestone",
fields: []string{"number", "milestone"},
inputJSON: heredoc.Doc(`
{ "number": 2345, "milestone": {"title": "The next big thing"} }
`),
outputJSON: heredoc.Doc(`
{
"milestone": {
"title": "The next big thing"
},
"number": 2345
}
`),
},
{
name: "project cards",
fields: []string{"projectCards"},
@ -75,6 +90,31 @@ func TestIssue_ExportData(t *testing.T) {
}
}
func TestExportIssues(t *testing.T) {
issues := []Issue{
{Milestone: Milestone{Title: "hi"}},
{},
}
exported := ExportIssues(issues, []string{"milestone"})
buf := bytes.Buffer{}
enc := json.NewEncoder(&buf)
enc.SetIndent("", "\t")
require.NoError(t, enc.Encode(exported))
assert.Equal(t, heredoc.Doc(`
[
{
"milestone": {
"title": "hi"
}
},
{
"milestone": null
}
]
`), buf.String())
}
func TestPullRequest_ExportData(t *testing.T) {
tests := []struct {
name string
@ -95,6 +135,21 @@ func TestPullRequest_ExportData(t *testing.T) {
}
`),
},
{
name: "milestone",
fields: []string{"number", "milestone"},
inputJSON: heredoc.Doc(`
{ "number": 2345, "milestone": {"title": "The next big thing"} }
`),
outputJSON: heredoc.Doc(`
{
"milestone": {
"title": "The next big thing"
},
"number": 2345
}
`),
},
{
name: "status checks",
fields: []string{"statusCheckRollup"},