Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return json.Unmarshal error when importing issues deferred #2892

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 3 additions & 6 deletions github/issue_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
88 changes: 87 additions & 1 deletion github/issue_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down Expand Up @@ -75,6 +74,93 @@ 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_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()
Expand Down