Skip to content

Commit c1f0285

Browse files
authored
feat(librarian): add ability to open pull request as a draft (#2604)
Fixes #2588
1 parent 7309cad commit c1f0285

File tree

6 files changed

+16
-8
lines changed

6 files changed

+16
-8
lines changed

internal/github/github.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (c *Client) GetRawContent(ctx context.Context, path, ref string) ([]byte, e
152152
// CreatePullRequest creates a pull request in the remote repo.
153153
// At the moment this requires a single remote to be configured,
154154
// which must have a GitHub HTTPS URL. We assume a base branch of "main".
155-
func (c *Client) CreatePullRequest(ctx context.Context, repo *Repository, remoteBranch, baseBranch, title, body string) (*PullRequestMetadata, error) {
155+
func (c *Client) CreatePullRequest(ctx context.Context, repo *Repository, remoteBranch, baseBranch, title, body string, isDraft bool) (*PullRequestMetadata, error) {
156156
if body == "" {
157157
slog.Warn("Provided PR body is empty, setting default.")
158158
body = "Regenerated all changed APIs. See individual commits for details."
@@ -166,6 +166,7 @@ func (c *Client) CreatePullRequest(ctx context.Context, repo *Repository, remote
166166
Base: &baseBranch,
167167
Body: github.Ptr(body),
168168
MaintainerCanModify: github.Ptr(true),
169+
Draft: github.Ptr(isDraft),
169170
}
170171
pr, _, err := c.PullRequests.Create(ctx, repo.Owner, repo.Name, newPR)
171172
if err != nil {

internal/github/github_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func TestCreatePullRequest(t *testing.T) {
321321
client := newClientWithHTTP("fake-token", repo, server.Client())
322322
client.BaseURL, _ = url.Parse(server.URL + "/")
323323

324-
metadata, err := client.CreatePullRequest(t.Context(), repo, test.remoteBranch, test.remoteBase, test.title, test.body)
324+
metadata, err := client.CreatePullRequest(t.Context(), repo, test.remoteBranch, test.remoteBase, test.title, test.body, false)
325325

326326
if test.wantErr {
327327
if err == nil {

internal/librarian/command.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ var globalPreservePatterns = []string{
7979
// GitHubClient is an abstraction over the GitHub client.
8080
type GitHubClient interface {
8181
GetRawContent(ctx context.Context, path, ref string) ([]byte, error)
82-
CreatePullRequest(ctx context.Context, repo *github.Repository, remoteBranch, remoteBase, title, body string) (*github.PullRequestMetadata, error)
82+
CreatePullRequest(ctx context.Context, repo *github.Repository, remoteBranch, remoteBase, title, body string, isDraft bool) (*github.PullRequestMetadata, error)
8383
AddLabelsToIssue(ctx context.Context, repo *github.Repository, number int, labels []string) error
8484
GetLabels(ctx context.Context, number int) ([]string, error)
8585
ReplaceLabels(ctx context.Context, number int, labels []string) error
@@ -126,8 +126,11 @@ type commitInfo struct {
126126
// api is the api path of a library, only set this value during api onboarding.
127127
api string
128128
// library is the ID of a library, only set this value during api onboarding.
129-
library string
129+
library string
130+
// prBodyBuilder is a callback function for building the pull request body
130131
prBodyBuilder func() (string, error)
132+
// isDraft declares whether or not to create the pull request as a draft.
133+
isDraft bool
131134
}
132135

133136
type commandRunner struct {
@@ -423,7 +426,7 @@ func commitAndPush(ctx context.Context, info *commitInfo) error {
423426
return fmt.Errorf("failed to create pull request body: %w", err)
424427
}
425428

426-
pullRequestMetadata, err := info.ghClient.CreatePullRequest(ctx, gitHubRepo, branch, info.branch, title, prBody)
429+
pullRequestMetadata, err := info.ghClient.CreatePullRequest(ctx, gitHubRepo, branch, info.branch, title, prBody, info.isDraft)
427430
if err != nil {
428431
return fmt.Errorf("failed to create pull request: %w", err)
429432
}

internal/librarian/mocks_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (m *mockGitHubClient) GetRawContent(ctx context.Context, path, ref string)
6666
return m.rawContent, m.rawErr
6767
}
6868

69-
func (m *mockGitHubClient) CreatePullRequest(ctx context.Context, repo *github.Repository, remoteBranch, remoteBase, title, body string) (*github.PullRequestMetadata, error) {
69+
func (m *mockGitHubClient) CreatePullRequest(ctx context.Context, repo *github.Repository, remoteBranch, remoteBase, title, body string, isDraft bool) (*github.PullRequestMetadata, error) {
7070
m.createPullRequestCalls++
7171
if m.createPullRequestErr != nil {
7272
return nil, m.createPullRequestErr

internal/librarian/update_image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ func (r *updateImageRunner) run(ctx context.Context) error {
133133
return formatUpdateImagePRBody(r.image, failedGenerations)
134134
}
135135
commitMessage := fmt.Sprintf("feat: update image to %s", r.image)
136-
// TODO(#2588): open PR as draft if there are failures
137136
return commitAndPush(ctx, &commitInfo{
138137
branch: r.branch,
139138
commit: r.commit,
@@ -148,6 +147,7 @@ func (r *updateImageRunner) run(ctx context.Context) error {
148147
workRoot: r.workRoot,
149148
failedGenerations: len(failedGenerations),
150149
prBodyBuilder: prBodyBuilder,
150+
isDraft: len(failedGenerations) > 0,
151151
})
152152
}
153153

system_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func TestPullRequestSystem(t *testing.T) {
175175

176176
// Create a pull request
177177
client := github.NewClient(testToken, repo)
178-
createdPullRequest, err := client.CreatePullRequest(t.Context(), repo, branchName, "main", "test: integration test", "do not merge")
178+
createdPullRequest, err := client.CreatePullRequest(t.Context(), repo, branchName, "main", "test: integration test", "do not merge", true)
179179
if err != nil {
180180
t.Fatalf("unexpected error in CreatePullRequest() %s", err)
181181
}
@@ -259,6 +259,10 @@ func TestPullRequestSystem(t *testing.T) {
259259
if pullRequest.GetComments() == 0 {
260260
t.Fatalf("Expected to have created a comment on the pull request.")
261261
}
262+
263+
if !pullRequest.GetDraft() {
264+
t.Fatalf("Expected created pull request to have been a draft.")
265+
}
262266
break
263267
}
264268
}

0 commit comments

Comments
 (0)