Skip to content

Commit 2a49ae6

Browse files
authored
fix(internal/librarian): should not push to github when no push flag specified (#2405)
now when commit = true and push = false, it does not push to GitHub. Updated test case to verify. Fixes #2390
1 parent 77c0475 commit 2a49ae6

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

internal/librarian/command.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,15 @@ func commitAndPush(ctx context.Context, info *commitInfo) error {
356356
return err
357357
}
358358

359-
if err := repo.Push(branch); err != nil {
360-
return err
361-
}
362-
363359
if !info.push {
364360
slog.Info("Push flag is not specified, skipping pull request creation")
365361
return nil
366362
}
367363

364+
if err := repo.Push(branch); err != nil {
365+
return err
366+
}
367+
368368
gitHubRepo, err := GetGitHubRepositoryFromGitRepo(info.repo)
369369
if err != nil {
370370
return fmt.Errorf("failed to get GitHub repository: %w", err)

internal/librarian/command_test.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,21 +1301,25 @@ func TestCommitAndPush(t *testing.T) {
13011301
push bool
13021302
wantErr bool
13031303
expectedErrMsg string
1304+
check func(t *testing.T, repo gitrepo.Repository)
13041305
}{
13051306
{
13061307
name: "Push flag and Commit flag are not specified",
13071308
setupMockRepo: func(t *testing.T) gitrepo.Repository {
1308-
repoDir := newTestGitRepoWithCommit(t, "")
1309-
repo, err := gitrepo.NewRepository(&gitrepo.RepositoryOptions{Dir: repoDir})
1310-
if err != nil {
1311-
t.Fatalf("Failed to create test repo: %v", err)
1309+
return &MockRepository{
1310+
Dir: t.TempDir(),
13121311
}
1313-
return repo
13141312
},
13151313
setupMockClient: func(t *testing.T) GitHubClient {
13161314
return nil
13171315
},
13181316
prType: "generate",
1317+
check: func(t *testing.T, repo gitrepo.Repository) {
1318+
mockRepo := repo.(*MockRepository)
1319+
if mockRepo.PushCalls != 0 {
1320+
t.Errorf("Push was called %d times, expected 0", mockRepo.PushCalls)
1321+
}
1322+
},
13191323
},
13201324
{
13211325
name: "create a commit",
@@ -1339,6 +1343,12 @@ func TestCommitAndPush(t *testing.T) {
13391343
},
13401344
prType: "generate",
13411345
commit: true,
1346+
check: func(t *testing.T, repo gitrepo.Repository) {
1347+
mockRepo := repo.(*MockRepository)
1348+
if mockRepo.PushCalls != 0 {
1349+
t.Errorf("Push was called %d times, expected 0", mockRepo.PushCalls)
1350+
}
1351+
},
13421352
},
13431353
{
13441354
name: "create a generate pull request",
@@ -1635,6 +1645,10 @@ func TestCommitAndPush(t *testing.T) {
16351645
t.Errorf("%s: commitAndPush() returned unexpected error: %v", test.name, err)
16361646
return
16371647
}
1648+
1649+
if test.check != nil {
1650+
test.check(t, repo)
1651+
}
16381652
})
16391653
}
16401654
}
@@ -1651,7 +1665,7 @@ func TestWritePRBody(t *testing.T) {
16511665
repo: &MockRepository{
16521666
Dir: t.TempDir(),
16531667
RemotesValue: []*gitrepo.Remote{
1654-
&gitrepo.Remote{
1668+
{
16551669
Name: "origin",
16561670
URLs: []string{"https://github.com/googleapis/librarian.git"},
16571671
},
@@ -1669,7 +1683,7 @@ func TestWritePRBody(t *testing.T) {
16691683
repo: &MockRepository{
16701684
Dir: t.TempDir(),
16711685
RemotesValue: []*gitrepo.Remote{
1672-
&gitrepo.Remote{
1686+
{
16731687
Name: "not-origin",
16741688
URLs: []string{"https://github.com/googleapis/librarian.git"},
16751689
},
@@ -1685,7 +1699,7 @@ func TestWritePRBody(t *testing.T) {
16851699
repo: &MockRepository{
16861700
Dir: t.TempDir(),
16871701
RemotesValue: []*gitrepo.Remote{
1688-
&gitrepo.Remote{
1702+
{
16891703
Name: "origin",
16901704
URLs: []string{"https://github.com/googleapis/librarian.git"},
16911705
},
@@ -1702,7 +1716,7 @@ func TestWritePRBody(t *testing.T) {
17021716
Dir: t.TempDir(),
17031717
AddAllStatus: make(git.Status),
17041718
RemotesValue: []*gitrepo.Remote{
1705-
&gitrepo.Remote{
1719+
{
17061720
Name: "origin",
17071721
URLs: []string{"https://github.com/googleapis/librarian.git"},
17081722
},

internal/librarian/mocks_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ type MockRepository struct {
323323
ChangedFilesInCommitValueByHash map[string][]string
324324
ChangedFilesInCommitError error
325325
CreateBranchAndCheckoutError error
326+
PushCalls int
326327
PushError error
327328
RestoreError error
328329
}
@@ -427,6 +428,7 @@ func (m *MockRepository) CreateBranchAndCheckout(name string) error {
427428
}
428429

429430
func (m *MockRepository) Push(name string) error {
431+
m.PushCalls++
430432
if m.PushError != nil {
431433
return m.PushError
432434
}

0 commit comments

Comments
 (0)