From 1d6192c1ac570661ccd33246cbf1d4a45bb58af9 Mon Sep 17 00:00:00 2001 From: Nikita Pivkin Date: Mon, 21 Aug 2023 22:38:26 +0700 Subject: [PATCH 1/2] fix: return error when importing issues deferred --- github/issue_import.go | 9 +++---- github/issue_import_test.go | 47 ++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/github/issue_import.go b/github/issue_import.go index 4bc8d5f1d23..04899772bd1 100644 --- a/github/issue_import.go +++ b/github/issue_import.go @@ -86,14 +86,11 @@ func (s *IssueImportService) Create(ctx context.Context, owner, repo string, iss if err != nil { aerr, ok := err.(*AcceptedError) if ok { - decErr := json.Unmarshal(aerr.Raw, i) - if decErr != nil { - err = decErr + if err := json.Unmarshal(aerr.Raw, i); err != nil { + return i, resp, err } - - return i, resp, nil + return i, resp, err } - return nil, resp, err } diff --git a/github/issue_import_test.go b/github/issue_import_test.go index a86ead898b4..2b29752e5c9 100644 --- a/github/issue_import_test.go +++ b/github/issue_import_test.go @@ -45,7 +45,6 @@ func TestIssueImportService_Create(t *testing.T) { t.Errorf("Request body = %+v, want %+v", v, input) } - w.WriteHeader(http.StatusAccepted) w.Write(issueImportResponseJSON) }) @@ -75,6 +74,52 @@ func TestIssueImportService_Create(t *testing.T) { }) } +func TestIssueImportService_Create_defered(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + createdAt := time.Date(2020, time.August, 11, 15, 30, 0, 0, time.UTC) + input := &IssueImportRequest{ + IssueImport: IssueImport{ + Assignee: String("developer"), + Body: "Dummy description", + CreatedAt: &Timestamp{createdAt}, + Labels: []string{"l1", "l2"}, + Milestone: Int(1), + Title: "Dummy Issue", + }, + Comments: []*Comment{{ + CreatedAt: &Timestamp{createdAt}, + Body: "Comment body", + }}, + } + + mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) { + v := new(IssueImportRequest) + json.NewDecoder(r.Body).Decode(v) + testMethod(t, r, "POST") + testHeader(t, r, "Accept", mediaTypeIssueImportAPI) + if !cmp.Equal(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + + w.WriteHeader(http.StatusAccepted) + w.Write(issueImportResponseJSON) + }) + + ctx := context.Background() + got, _, err := client.IssueImport.Create(ctx, "o", "r", input) + + if _, ok := err.(*AcceptedError); !ok { + t.Errorf("Create returned error: %v (want AcceptedError)", err) + } + + want := wantIssueImportResponse + if !cmp.Equal(got, want) { + t.Errorf("Create = %+v, want %+v", got, want) + } +} + func TestIssueImportService_Create_invalidOwner(t *testing.T) { client, _, _, teardown := setup() defer teardown() From 48a5fb2930e782b1f7f44423fe9e9c78c615d2a5 Mon Sep 17 00:00:00 2001 From: Nikita Pivkin Date: Mon, 21 Aug 2023 23:07:21 +0700 Subject: [PATCH 2/2] add test case --- github/issue_import_test.go | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/github/issue_import_test.go b/github/issue_import_test.go index 2b29752e5c9..48f96ab3a5c 100644 --- a/github/issue_import_test.go +++ b/github/issue_import_test.go @@ -120,6 +120,47 @@ func TestIssueImportService_Create_defered(t *testing.T) { } } +func TestIssueImportService_Create_badResponse(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + createdAt := time.Date(2020, time.August, 11, 15, 30, 0, 0, time.UTC) + input := &IssueImportRequest{ + IssueImport: IssueImport{ + Assignee: String("developer"), + Body: "Dummy description", + CreatedAt: &Timestamp{createdAt}, + Labels: []string{"l1", "l2"}, + Milestone: Int(1), + Title: "Dummy Issue", + }, + Comments: []*Comment{{ + CreatedAt: &Timestamp{createdAt}, + Body: "Comment body", + }}, + } + + mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) { + v := new(IssueImportRequest) + json.NewDecoder(r.Body).Decode(v) + testMethod(t, r, "POST") + testHeader(t, r, "Accept", mediaTypeIssueImportAPI) + if !cmp.Equal(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + + w.WriteHeader(http.StatusAccepted) + w.Write([]byte("{[}")) + }) + + ctx := context.Background() + _, _, err := client.IssueImport.Create(ctx, "o", "r", input) + + if err == nil || err.Error() != "invalid character '[' looking for beginning of object key string" { + t.Errorf("unexpected error: %v", err) + } +} + func TestIssueImportService_Create_invalidOwner(t *testing.T) { client, _, _, teardown := setup() defer teardown()