Skip to content

Commit 38ca69f

Browse files
authored
Return json.Unmarshal error when importing issues deferred (#2892)
1 parent 505b7ea commit 38ca69f

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

github/issue_import.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,11 @@ func (s *IssueImportService) Create(ctx context.Context, owner, repo string, iss
8686
if err != nil {
8787
aerr, ok := err.(*AcceptedError)
8888
if ok {
89-
decErr := json.Unmarshal(aerr.Raw, i)
90-
if decErr != nil {
91-
err = decErr
89+
if err := json.Unmarshal(aerr.Raw, i); err != nil {
90+
return i, resp, err
9291
}
93-
94-
return i, resp, nil
92+
return i, resp, err
9593
}
96-
9794
return nil, resp, err
9895
}
9996

github/issue_import_test.go

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func TestIssueImportService_Create(t *testing.T) {
4545
t.Errorf("Request body = %+v, want %+v", v, input)
4646
}
4747

48-
w.WriteHeader(http.StatusAccepted)
4948
w.Write(issueImportResponseJSON)
5049
})
5150

@@ -75,6 +74,93 @@ func TestIssueImportService_Create(t *testing.T) {
7574
})
7675
}
7776

77+
func TestIssueImportService_Create_defered(t *testing.T) {
78+
client, mux, _, teardown := setup()
79+
defer teardown()
80+
81+
createdAt := time.Date(2020, time.August, 11, 15, 30, 0, 0, time.UTC)
82+
input := &IssueImportRequest{
83+
IssueImport: IssueImport{
84+
Assignee: String("developer"),
85+
Body: "Dummy description",
86+
CreatedAt: &Timestamp{createdAt},
87+
Labels: []string{"l1", "l2"},
88+
Milestone: Int(1),
89+
Title: "Dummy Issue",
90+
},
91+
Comments: []*Comment{{
92+
CreatedAt: &Timestamp{createdAt},
93+
Body: "Comment body",
94+
}},
95+
}
96+
97+
mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) {
98+
v := new(IssueImportRequest)
99+
json.NewDecoder(r.Body).Decode(v)
100+
testMethod(t, r, "POST")
101+
testHeader(t, r, "Accept", mediaTypeIssueImportAPI)
102+
if !cmp.Equal(v, input) {
103+
t.Errorf("Request body = %+v, want %+v", v, input)
104+
}
105+
106+
w.WriteHeader(http.StatusAccepted)
107+
w.Write(issueImportResponseJSON)
108+
})
109+
110+
ctx := context.Background()
111+
got, _, err := client.IssueImport.Create(ctx, "o", "r", input)
112+
113+
if _, ok := err.(*AcceptedError); !ok {
114+
t.Errorf("Create returned error: %v (want AcceptedError)", err)
115+
}
116+
117+
want := wantIssueImportResponse
118+
if !cmp.Equal(got, want) {
119+
t.Errorf("Create = %+v, want %+v", got, want)
120+
}
121+
}
122+
123+
func TestIssueImportService_Create_badResponse(t *testing.T) {
124+
client, mux, _, teardown := setup()
125+
defer teardown()
126+
127+
createdAt := time.Date(2020, time.August, 11, 15, 30, 0, 0, time.UTC)
128+
input := &IssueImportRequest{
129+
IssueImport: IssueImport{
130+
Assignee: String("developer"),
131+
Body: "Dummy description",
132+
CreatedAt: &Timestamp{createdAt},
133+
Labels: []string{"l1", "l2"},
134+
Milestone: Int(1),
135+
Title: "Dummy Issue",
136+
},
137+
Comments: []*Comment{{
138+
CreatedAt: &Timestamp{createdAt},
139+
Body: "Comment body",
140+
}},
141+
}
142+
143+
mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) {
144+
v := new(IssueImportRequest)
145+
json.NewDecoder(r.Body).Decode(v)
146+
testMethod(t, r, "POST")
147+
testHeader(t, r, "Accept", mediaTypeIssueImportAPI)
148+
if !cmp.Equal(v, input) {
149+
t.Errorf("Request body = %+v, want %+v", v, input)
150+
}
151+
152+
w.WriteHeader(http.StatusAccepted)
153+
w.Write([]byte("{[}"))
154+
})
155+
156+
ctx := context.Background()
157+
_, _, err := client.IssueImport.Create(ctx, "o", "r", input)
158+
159+
if err == nil || err.Error() != "invalid character '[' looking for beginning of object key string" {
160+
t.Errorf("unexpected error: %v", err)
161+
}
162+
}
163+
78164
func TestIssueImportService_Create_invalidOwner(t *testing.T) {
79165
client, _, _, teardown := setup()
80166
defer teardown()

0 commit comments

Comments
 (0)