Skip to content

Commit c590f20

Browse files
authored
feat: implement commit flag for release init (#1816)
Fixes #1008
1 parent 949f02f commit c590f20

File tree

6 files changed

+70
-22
lines changed

6 files changed

+70
-22
lines changed

internal/config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ type Config struct {
109109
// expected.
110110
CommandName string
111111

112+
// Commit determines whether to creat a commit for the release but not create
113+
// a pull request.
114+
//
115+
// This flag is ignored if Push is set to true.
116+
Commit bool
117+
112118
// GitHubToken is the access token to use for all operations involving
113119
// GitHub.
114120
//

internal/librarian/command.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,10 @@ func copyLibrary(dst, src string, library *config.LibraryState) error {
227227
// It uses the GitHub client to create a PR with the specified branch, title, and
228228
// description to the repository.
229229
func commitAndPush(ctx context.Context, cfg *config.Config, repo gitrepo.Repository, ghClient GitHubClient, commitMessage string) error {
230-
if !cfg.Push {
231-
slog.Info("Push flag is not specified, skipping")
230+
if !cfg.Push && !cfg.Commit {
231+
slog.Info("Push flag and Commit flag are not specified, skipping committing")
232232
return nil
233233
}
234-
// Ensure we have a GitHub repository
235-
gitHubRepo, err := github.FetchGitHubRepoFromRemote(repo)
236-
if err != nil {
237-
return err
238-
}
239234

240235
status, err := repo.AddAll()
241236
if err != nil {
@@ -263,6 +258,17 @@ func commitAndPush(ctx context.Context, cfg *config.Config, repo gitrepo.Reposit
263258
return err
264259
}
265260

261+
if !cfg.Push {
262+
slog.Info("Push flag is not specified, skipping pull request creation")
263+
return nil
264+
}
265+
266+
// Ensure we have a GitHub repository
267+
gitHubRepo, err := github.FetchGitHubRepoFromRemote(repo)
268+
if err != nil {
269+
return err
270+
}
271+
266272
// Create a new branch, set title and message for the PR.
267273
titlePrefix := "Librarian pull request"
268274
title := fmt.Sprintf("%s: %s", titlePrefix, datetimeNow)

internal/librarian/command_test.go

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -479,16 +479,16 @@ func TestCopyOneLibrary(t *testing.T) {
479479

480480
func TestCommitAndPush(t *testing.T) {
481481
for _, test := range []struct {
482-
name string
483-
setupMockRepo func(t *testing.T) gitrepo.Repository
484-
setupMockClient func(t *testing.T) GitHubClient
485-
push bool
486-
wantErr bool
487-
expectedErrMsg string
488-
validatePostTest func(t *testing.T, repo gitrepo.Repository)
482+
name string
483+
setupMockRepo func(t *testing.T) gitrepo.Repository
484+
setupMockClient func(t *testing.T) GitHubClient
485+
commit bool
486+
push bool
487+
wantErr bool
488+
expectedErrMsg string
489489
}{
490490
{
491-
name: "Push flag not specified",
491+
name: "Push flag and Commit flag are not specified",
492492
setupMockRepo: func(t *testing.T) gitrepo.Repository {
493493
repoDir := newTestGitRepoWithCommit(t, "")
494494
repo, err := gitrepo.NewRepository(&gitrepo.RepositoryOptions{Dir: repoDir})
@@ -502,14 +502,39 @@ func TestCommitAndPush(t *testing.T) {
502502
},
503503
},
504504
{
505-
name: "Happy Path",
505+
name: "create a commit",
506506
setupMockRepo: func(t *testing.T) gitrepo.Repository {
507507
remote := git.NewRemote(memory.NewStorage(), &gogitConfig.RemoteConfig{
508508
Name: "origin",
509509
URLs: []string{"https://github.com/googleapis/librarian.git"},
510510
})
511+
status := make(git.Status)
512+
status["file.txt"] = &git.FileStatus{Worktree: git.Modified}
513+
return &MockRepository{
514+
Dir: t.TempDir(),
515+
AddAllStatus: status,
516+
RemotesValue: []*git.Remote{remote},
517+
}
518+
},
519+
setupMockClient: func(t *testing.T) GitHubClient {
520+
return &mockGitHubClient{
521+
createdPR: &github.PullRequestMetadata{Number: 123, Repo: &github.Repository{Owner: "test-owner", Name: "test-repo"}},
522+
}
523+
},
524+
commit: true,
525+
},
526+
{
527+
name: "create a pull request",
528+
setupMockRepo: func(t *testing.T) gitrepo.Repository {
529+
remote := git.NewRemote(memory.NewStorage(), &gogitConfig.RemoteConfig{
530+
Name: "origin",
531+
URLs: []string{"https://github.com/googleapis/librarian.git"},
532+
})
533+
status := make(git.Status)
534+
status["file.txt"] = &git.FileStatus{Worktree: git.Modified}
511535
return &MockRepository{
512536
Dir: t.TempDir(),
537+
AddAllStatus: status,
513538
RemotesValue: []*git.Remote{remote},
514539
}
515540
},
@@ -523,8 +548,11 @@ func TestCommitAndPush(t *testing.T) {
523548
{
524549
name: "No GitHub Remote",
525550
setupMockRepo: func(t *testing.T) gitrepo.Repository {
551+
status := make(git.Status)
552+
status["file.txt"] = &git.FileStatus{Worktree: git.Modified}
526553
return &MockRepository{
527554
Dir: t.TempDir(),
555+
AddAllStatus: status,
528556
RemotesValue: []*git.Remote{}, // No remotes
529557
}
530558
},
@@ -675,7 +703,8 @@ func TestCommitAndPush(t *testing.T) {
675703
repo := test.setupMockRepo(t)
676704
client := test.setupMockClient(t)
677705
localConfig := &config.Config{
678-
Push: test.push,
706+
Push: test.push,
707+
Commit: test.commit,
679708
}
680709

681710
err := commitAndPush(context.Background(), localConfig, repo, client, "")
@@ -692,10 +721,6 @@ func TestCommitAndPush(t *testing.T) {
692721
t.Errorf("%s: commitAndPush() returned unexpected error: %v", test.name, err)
693722
return
694723
}
695-
696-
if test.validatePostTest != nil {
697-
test.validatePostTest(t, repo)
698-
}
699724
})
700725
}
701726
}

internal/librarian/flags.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func addFlagBuild(fs *flag.FlagSet, cfg *config.Config) {
3232
fs.BoolVar(&cfg.Build, "build", false, "whether to build the generated code")
3333
}
3434

35+
func addFlagCommit(fs *flag.FlagSet, cfg *config.Config) {
36+
fs.BoolVar(&cfg.Commit, "commit", false, "whether to create a commit for a release")
37+
}
38+
3539
func addFlagHostMount(fs *flag.FlagSet, cfg *config.Config) {
3640
defaultValue := ""
3741
fs.StringVar(&cfg.HostMount, "host-mount", defaultValue, "a mount point from Docker host and within the Docker. The format is {host-dir}:{local-dir}.")

internal/librarian/release_init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func init() {
5555
fs := cmdInit.Flags
5656
cfg := cmdInit.Config
5757

58+
addFlagCommit(fs, cfg)
5859
addFlagPush(fs, cfg)
5960
addFlagImage(fs, cfg)
6061
addFlagLibrary(fs, cfg)

internal/librarian/release_init_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"strings"
2323
"testing"
2424

25+
"github.com/go-git/go-git/v5"
26+
2527
"github.com/googleapis/librarian/internal/gitrepo"
2628

2729
"github.com/google/go-cmp/cmp"
@@ -79,6 +81,8 @@ func TestNewInitRunner(t *testing.T) {
7981

8082
func TestInitRun(t *testing.T) {
8183
t.Parallel()
84+
gitStatus := make(git.Status)
85+
gitStatus["file.txt"] = &git.FileStatus{Worktree: git.Modified}
8286
for _, test := range []struct {
8387
name string
8488
runner *initRunner
@@ -308,7 +312,9 @@ func TestInitRun(t *testing.T) {
308312
},
309313
},
310314
repo: &MockRepository{
311-
Dir: t.TempDir(),
315+
Dir: t.TempDir(),
316+
AddAllStatus: gitStatus,
317+
RemotesValue: []*git.Remote{}, // No remotes
312318
},
313319
librarianConfig: &config.LibrarianConfig{},
314320
partialRepo: t.TempDir(),

0 commit comments

Comments
 (0)