From a8e025291f629f0f8dc31f9d6068e89f5dfeaad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 28 Apr 2021 18:28:18 +0200 Subject: [PATCH] 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. --- api/export_pr.go | 12 +++++----- api/export_pr_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/api/export_pr.go b/api/export_pr.go index c6fc3db05..dc70ee4fa 100644 --- a/api/export_pr.go +++ b/api/export_pr.go @@ -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 } diff --git a/api/export_pr_test.go b/api/export_pr_test.go index 9fd0ba848..eff3c157d 100644 --- a/api/export_pr_test.go +++ b/api/export_pr_test.go @@ -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"},