Skip to content

Commit

Permalink
Return json.Unmarshal error when importing issues deferred (google#2892)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin authored Aug 22, 2023
1 parent 505b7ea commit 38ca69f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
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

0 comments on commit 38ca69f

Please sign in to comment.