Skip to content

Commit 152b561

Browse files
authored
feat: create a pull request in release init command (#1793)
Updates #1008
1 parent fcba7b0 commit 152b561

File tree

5 files changed

+60
-19
lines changed

5 files changed

+60
-19
lines changed

internal/librarian/command.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,18 @@ func copyLibrary(dst, src string, library *config.LibraryState) error {
226226
// changes.
227227
// It uses the GitHub client to create a PR with the specified branch, title, and
228228
// description to the repository.
229-
func commitAndPush(ctx context.Context, r *generateRunner, commitMessage string) error {
230-
if !r.cfg.Push {
229+
func commitAndPush(ctx context.Context, cfg *config.Config, repo gitrepo.Repository, ghClient GitHubClient, commitMessage string) error {
230+
if !cfg.Push {
231231
slog.Info("Push flag is not specified, skipping")
232232
return nil
233233
}
234234
// Ensure we have a GitHub repository
235-
gitHubRepo, err := github.FetchGitHubRepoFromRemote(r.repo)
235+
gitHubRepo, err := github.FetchGitHubRepoFromRemote(repo)
236236
if err != nil {
237237
return err
238238
}
239239

240-
status, err := r.repo.AddAll()
240+
status, err := repo.AddAll()
241241
if err != nil {
242242
return err
243243
}
@@ -249,25 +249,25 @@ func commitAndPush(ctx context.Context, r *generateRunner, commitMessage string)
249249
datetimeNow := formatTimestamp(time.Now())
250250
branch := fmt.Sprintf("librarian-%s", datetimeNow)
251251
slog.Info("Creating branch", slog.String("branch", branch))
252-
if err := r.repo.CreateBranchAndCheckout(branch); err != nil {
252+
if err := repo.CreateBranchAndCheckout(branch); err != nil {
253253
return err
254254
}
255255

256256
// TODO: get correct language for message (https://github.com/googleapis/librarian/issues/885)
257257
slog.Info("Committing", "message", commitMessage)
258-
if err := r.repo.Commit(commitMessage); err != nil {
258+
if err := repo.Commit(commitMessage); err != nil {
259259
return err
260260
}
261261

262-
if err := r.repo.Push(branch); err != nil {
262+
if err := repo.Push(branch); err != nil {
263263
return err
264264
}
265265

266266
// Create a new branch, set title and message for the PR.
267267
titlePrefix := "Librarian pull request"
268268
title := fmt.Sprintf("%s: %s", titlePrefix, datetimeNow)
269269
slog.Info("Creating pull request", slog.String("branch", branch), slog.String("title", title))
270-
if _, err = r.ghClient.CreatePullRequest(ctx, gitHubRepo, branch, title, commitMessage); err != nil {
270+
if _, err = ghClient.CreatePullRequest(ctx, gitHubRepo, branch, title, commitMessage); err != nil {
271271
return fmt.Errorf("failed to create pull request: %w", err)
272272
}
273273
return nil

internal/librarian/command_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -674,15 +674,11 @@ func TestCommitAndPush(t *testing.T) {
674674
t.Run(test.name, func(t *testing.T) {
675675
repo := test.setupMockRepo(t)
676676
client := test.setupMockClient(t)
677-
r := &generateRunner{
678-
cfg: &config.Config{
679-
Push: test.push,
680-
},
681-
repo: repo,
682-
ghClient: client,
677+
localConfig := &config.Config{
678+
Push: test.push,
683679
}
684680

685-
err := commitAndPush(context.Background(), r, "")
681+
err := commitAndPush(context.Background(), localConfig, repo, client, "")
686682

687683
if test.wantErr {
688684
if err == nil {

internal/librarian/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (r *generateRunner) run(ctx context.Context) error {
149149
if err := saveLibrarianState(r.repo.GetDir(), r.state); err != nil {
150150
return err
151151
}
152-
if err := commitAndPush(ctx, r, prBody); err != nil {
152+
if err := commitAndPush(ctx, r.cfg, r.repo, r.ghClient, prBody); err != nil {
153153
return err
154154
}
155155
return nil

internal/librarian/release_init.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,17 @@ func (r *initRunner) run(ctx context.Context) error {
9898
return fmt.Errorf("failed to create output dir: %s", outputDir)
9999
}
100100
slog.Info("Initiating a release", "dir", outputDir)
101-
return r.runInitCommand(ctx, outputDir)
101+
if err := r.runInitCommand(ctx, outputDir); err != nil {
102+
return err
103+
}
104+
105+
// TODO: https://github.com/googleapis/librarian/issues/1697
106+
// Add commit message after this issue is resolved.
107+
if err := commitAndPush(ctx, r.cfg, r.repo, r.ghClient, ""); err != nil {
108+
return fmt.Errorf("failed to commit and push: %w", err)
109+
}
110+
111+
return nil
102112
}
103113

104114
func (r *initRunner) runInitCommand(ctx context.Context, outputDir string) error {
@@ -183,7 +193,7 @@ func updateLibrary(r *initRunner, state *config.LibrarianState, index int) error
183193
library := state.Libraries[index]
184194
updatedLibrary, err := getChangesOf(r.repo, library)
185195
if err != nil {
186-
return err
196+
return fmt.Errorf("failed to update library, %s: %w", library.ID, err)
187197
}
188198

189199
setReleaseTrigger(updatedLibrary, r.cfg.LibraryVersion, true)

internal/librarian/release_init_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func TestInitRun(t *testing.T) {
9393
containerClient: &mockContainerClient{},
9494
cfg: &config.Config{
9595
Library: "example-id",
96+
Push: false,
9697
},
9798
state: &config.LibrarianState{
9899
Libraries: []*config.LibraryState{
@@ -134,6 +135,7 @@ func TestInitRun(t *testing.T) {
134135
workRoot: t.TempDir(),
135136
containerClient: &mockContainerClient{},
136137
cfg: &config.Config{
138+
Push: false,
137139
Library: "example-id",
138140
},
139141
state: &config.LibrarianState{
@@ -288,7 +290,31 @@ func TestInitRun(t *testing.T) {
288290
partialRepo: t.TempDir(),
289291
},
290292
wantErr: true,
291-
wantErrMsg: "failed to fetch conventional commits for library",
293+
wantErrMsg: "failed to update library",
294+
},
295+
{
296+
name: "failed to commit and push",
297+
runner: &initRunner{
298+
workRoot: os.TempDir(),
299+
containerClient: &mockContainerClient{},
300+
cfg: &config.Config{
301+
Push: true,
302+
},
303+
state: &config.LibrarianState{
304+
Libraries: []*config.LibraryState{
305+
{
306+
ID: "example-id",
307+
},
308+
},
309+
},
310+
repo: &MockRepository{
311+
Dir: t.TempDir(),
312+
},
313+
librarianConfig: &config.LibrarianConfig{},
314+
partialRepo: t.TempDir(),
315+
},
316+
wantErr: true,
317+
wantErrMsg: "failed to commit and push",
292318
},
293319
{
294320
name: "failed to make partial repo",
@@ -325,6 +351,15 @@ func TestInitRun(t *testing.T) {
325351
t.Fatalf("os.MkdirAll() = %v", err)
326352
}
327353

354+
// Create the librarian state file.
355+
stateFile := filepath.Join(repoDir, ".librarian/state.yaml")
356+
if err := os.MkdirAll(filepath.Dir(stateFile), 0755); err != nil {
357+
t.Fatalf("os.MkdirAll() = %v", err)
358+
}
359+
if err := os.WriteFile(stateFile, []byte{}, 0644); err != nil {
360+
t.Fatalf("os.WriteFile() = %v", err)
361+
}
362+
328363
err := test.runner.run(context.Background())
329364
if test.wantErr {
330365
if err == nil {

0 commit comments

Comments
 (0)