Skip to content

Commit 035474d

Browse files
committed
label and assignee api call fixes
1 parent e6c0e93 commit 035474d

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

internal/cmd/combine_prs.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,47 @@ func createPullRequest(ctx context.Context, client RESTClientInterface, repo git
225225
"body": body,
226226
}
227227

228+
requestBody, err := encodePayload(payload)
229+
if err != nil {
230+
return fmt.Errorf("failed to encode payload: %w", err)
231+
}
232+
233+
// Create the pull request
234+
var prResponse struct {
235+
Number int `json:"number"`
236+
}
237+
err = client.Post(endpoint, requestBody, &prResponse)
238+
if err != nil {
239+
return fmt.Errorf("failed to create pull request: %w", err)
240+
}
241+
228242
// Add labels if provided
229243
if len(labels) > 0 {
230-
payload["labels"] = labels
244+
labelsEndpoint := fmt.Sprintf("repos/%s/%s/issues/%d/labels", repo.Owner, repo.Repo, prResponse.Number)
245+
labelsPayload, err := encodePayload(map[string][]string{"labels": labels})
246+
if err != nil {
247+
return fmt.Errorf("failed to encode labels payload: %w", err)
248+
}
249+
err = client.Post(labelsEndpoint, labelsPayload, nil)
250+
if err != nil {
251+
return fmt.Errorf("failed to add labels: %w", err)
252+
}
231253
}
232254

233255
// Add assignees if provided
234256
if len(assignees) > 0 {
235-
payload["assignees"] = assignees
257+
assigneesEndpoint := fmt.Sprintf("repos/%s/%s/issues/%d/assignees", repo.Owner, repo.Repo, prResponse.Number)
258+
assigneesPayload, err := encodePayload(map[string][]string{"assignees": assignees})
259+
if err != nil {
260+
return fmt.Errorf("failed to encode assignees payload: %w", err)
261+
}
262+
err = client.Post(assigneesEndpoint, assigneesPayload, nil)
263+
if err != nil {
264+
return fmt.Errorf("failed to add assignees: %w", err)
265+
}
236266
}
237267

238-
requestBody, err := encodePayload(payload)
239-
if err != nil {
240-
return fmt.Errorf("failed to encode payload: %w", err)
241-
}
242-
return client.Post(endpoint, requestBody, nil)
268+
return nil
243269
}
244270

245271
// encodePayload encodes a payload as JSON and returns an io.Reader

internal/cmd/combine_prs_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"strings"
56
"testing"
67

78
"github.com/github/gh-combine/internal/github"
@@ -11,6 +12,14 @@ import (
1112
func TestCreatePullRequest(t *testing.T) {
1213
client := &MockRESTClient{
1314
PostFunc: func(endpoint string, body interface{}, response interface{}) error {
15+
if strings.Contains(endpoint, "/pulls") {
16+
if prResponse, ok := response.(*struct{ Number int }); ok {
17+
prResponse.Number = 123 // Mock PR number
18+
}
19+
} else if strings.Contains(endpoint, "/labels") || strings.Contains(endpoint, "/assignees") {
20+
// Mock successful label/assignee addition
21+
return nil
22+
}
1423
return nil
1524
},
1625
}

0 commit comments

Comments
 (0)