Skip to content

Commit 918507c

Browse files
authored
chore(internal/config): remove PushConfig flag (#1526)
Fixes #1517
1 parent b06989f commit 918507c

File tree

10 files changed

+124
-282
lines changed

10 files changed

+124
-282
lines changed

internal/config/config.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,6 @@ type Config struct {
128128
// Push is specified with the -push flag. No value is required.
129129
Push bool
130130

131-
// PushConfig specifies the email address and display name used in Git commits,
132-
// in the format "email,name".
133-
//
134-
// PushConfig is used in all commands that create commits in a language repository:
135-
// create-release-pr, configure and update-apis.
136-
//
137-
// PushConfig is optional. If unspecified, commits will use a default name of
138-
// "Google Cloud SDK" and a default email of [email protected].
139-
//
140-
// PushConfig is specified with the -push-config flag.
141-
PushConfig string
142-
143131
// Repo specifies the language repository to use, as either a local root directory
144132
// or a URL to clone from. If a local directory is specified, it can
145133
// be relative to the current working directory. The repository must
@@ -187,7 +175,6 @@ type Config struct {
187175
func New() *Config {
188176
return &Config{
189177
GitHubToken: os.Getenv("LIBRARIAN_GITHUB_TOKEN"),
190-
PushConfig: "",
191178
}
192179
}
193180

@@ -213,10 +200,6 @@ func (c *Config) IsValid() (bool, error) {
213200
return false, errors.New("no GitHub token supplied for push")
214201
}
215202

216-
if _, err := validatePushConfig(c.PushConfig, ""); err != nil {
217-
return false, err
218-
}
219-
220203
if _, err := validateHostMount(c.HostMount, ""); err != nil {
221204
return false, err
222205
}
@@ -236,16 +219,3 @@ func validateHostMount(hostMount, defaultValue string) (bool, error) {
236219

237220
return true, nil
238221
}
239-
240-
func validatePushConfig(pushConfig, defaultValue string) (bool, error) {
241-
if pushConfig == defaultValue {
242-
return true, nil
243-
}
244-
245-
parts := strings.Split(pushConfig, ",")
246-
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
247-
return false, errors.New("unable to parse push config")
248-
}
249-
250-
return true, nil
251-
}

internal/config/config_test.go

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ func TestNew(t *testing.T) {
3636
},
3737
want: Config{
3838
GitHubToken: "gh_token",
39-
PushConfig: "",
4039
},
4140
},
4241
{
4342
name: "No environment variables set",
4443
envVars: map[string]string{},
4544
want: Config{
4645
GitHubToken: "",
47-
PushConfig: "",
4846
},
4947
},
5048
{
@@ -54,7 +52,6 @@ func TestNew(t *testing.T) {
5452
},
5553
want: Config{
5654
GitHubToken: "gh_token",
57-
PushConfig: "",
5855
},
5956
},
6057
} {
@@ -137,71 +134,55 @@ func TestIsValid(t *testing.T) {
137134
wantErr bool
138135
}{
139136
{
140-
name: "Valid config - Push false, push config valid",
137+
name: "Valid config - Push false",
141138
cfg: Config{
142139
Push: false,
143140
GitHubToken: "",
144-
PushConfig: "[email protected],abc",
145141
},
146142
wantValid: true,
147143
wantErr: false,
148144
},
149145
{
150-
name: "Valid config - Push true, token present, push config valid",
146+
name: "Valid config - Push true, token present",
151147
cfg: Config{
152148
Push: true,
153149
GitHubToken: "some_token",
154-
PushConfig: "[email protected],abc",
155150
},
156151
wantValid: true,
157152
wantErr: false,
158153
},
159154
{
160-
name: "Invalid config - Push true, token missing, push config valid",
155+
name: "Invalid config - Push true, token missing",
161156
cfg: Config{
162157
Push: true,
163158
GitHubToken: "",
164-
PushConfig: "[email protected],abc",
165-
},
166-
wantValid: false,
167-
wantErr: true,
168-
},
169-
{
170-
name: "Invalid config - Push true, token present, push config invalid",
171-
cfg: Config{
172-
Push: true,
173-
GitHubToken: "some_token",
174-
PushConfig: "abc:[email protected]",
175159
},
176160
wantValid: false,
177161
wantErr: true,
178162
},
179163
{
180164
name: "Invalid config - host mount invalid, missing local-dir",
181165
cfg: Config{
182-
Push: false,
183-
PushConfig: "[email protected],abc",
184-
HostMount: "host-dir:",
166+
Push: false,
167+
HostMount: "host-dir:",
185168
},
186169
wantValid: false,
187170
wantErr: true,
188171
},
189172
{
190173
name: "Invalid config - host mount invalid, missing host-dir",
191174
cfg: Config{
192-
Push: false,
193-
PushConfig: "[email protected],abc",
194-
HostMount: ":local-dir",
175+
Push: false,
176+
HostMount: ":local-dir",
195177
},
196178
wantValid: false,
197179
wantErr: true,
198180
},
199181
{
200182
name: "Invalid config - host mount invalid, missing separator",
201183
cfg: Config{
202-
Push: false,
203-
PushConfig: "[email protected],abc",
204-
HostMount: "host-dir/local-dir",
184+
Push: false,
185+
HostMount: "host-dir/local-dir",
205186
},
206187
wantValid: false,
207188
wantErr: true,

internal/gitrepo/gitrepo.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@ package gitrepo
1818
import (
1919
"errors"
2020
"fmt"
21+
"github.com/go-git/go-git/v5"
22+
"github.com/go-git/go-git/v5/plumbing"
2123
"log/slog"
2224
"os"
2325
"strings"
24-
"time"
25-
26-
"github.com/go-git/go-git/v5"
27-
"github.com/go-git/go-git/v5/plumbing"
28-
"github.com/go-git/go-git/v5/plumbing/object"
2926
)
3027

3128
// Repository defines the interface for git repository operations.
3229
type Repository interface {
3330
AddAll() (git.Status, error)
34-
Commit(msg string, userName, userEmail string) error
31+
Commit(msg string) error
3532
IsClean() (bool, error)
3633
Remotes() ([]*git.Remote, error)
3734
GetDir() string
@@ -145,7 +142,7 @@ func (r *LocalRepository) AddAll() (git.Status, error) {
145142

146143
// Commit creates a new commit with the provided message and author
147144
// information.
148-
func (r *LocalRepository) Commit(msg string, userName, userEmail string) error {
145+
func (r *LocalRepository) Commit(msg string) error {
149146
worktree, err := r.repo.Worktree()
150147
if err != nil {
151148
return err
@@ -158,13 +155,8 @@ func (r *LocalRepository) Commit(msg string, userName, userEmail string) error {
158155
if status.IsClean() {
159156
return fmt.Errorf("no modifications to commit")
160157
}
161-
hash, err := worktree.Commit(msg, &git.CommitOptions{
162-
Author: &object.Signature{
163-
Name: userName,
164-
Email: userEmail,
165-
When: time.Now(),
166-
},
167-
})
158+
// The author of the commit will be read from git config.
159+
hash, err := worktree.Commit(msg, &git.CommitOptions{})
168160
if err != nil {
169161
return err
170162
}

internal/gitrepo/gitrepo_test.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/go-git/go-git/v5"
24-
gogitConfig "github.com/go-git/go-git/v5/config"
24+
goGitConfig "github.com/go-git/go-git/v5/config"
2525
"github.com/go-git/go-git/v5/plumbing/object"
2626
"github.com/google/go-cmp/cmp"
2727
)
@@ -367,15 +367,33 @@ func TestAddAll(t *testing.T) {
367367

368368
func TestCommit(t *testing.T) {
369369
t.Parallel()
370+
name, email := "tester", "[email protected]"
370371
// setupRepo is a helper to create a repository with an initial commit.
371372
setupRepo := func(t *testing.T) *LocalRepository {
372373
t.Helper()
373374
dir := t.TempDir()
374-
gogitRepo, err := git.PlainInit(dir, false)
375+
goGitRepo, err := git.PlainInit(dir, false)
375376
if err != nil {
376377
t.Fatalf("git.PlainInit failed: %v", err)
377378
}
378-
w, err := gogitRepo.Worktree()
379+
380+
author := struct {
381+
Name string
382+
Email string
383+
}{
384+
Name: name,
385+
Email: email,
386+
}
387+
config, err := goGitRepo.Config()
388+
if err != nil {
389+
t.Fatalf("gitRepo.Config failed: %v", err)
390+
}
391+
config.User = author
392+
if err := goGitRepo.SetConfig(config); err != nil {
393+
t.Fatalf("gitRepo.SetConfig failed: %v", err)
394+
}
395+
396+
w, err := goGitRepo.Worktree()
379397
if err != nil {
380398
t.Fatalf("Worktree() failed: %v", err)
381399
}
@@ -385,15 +403,13 @@ func TestCommit(t *testing.T) {
385403
}); err != nil {
386404
t.Fatalf("initial commit failed: %v", err)
387405
}
388-
return &LocalRepository{Dir: dir, repo: gogitRepo}
406+
return &LocalRepository{Dir: dir, repo: goGitRepo}
389407
}
390408

391409
for _, tc := range []struct {
392410
name string
393411
setup func(t *testing.T) *LocalRepository
394412
commitMsg string
395-
userName string
396-
userEmail string
397413
wantErr bool
398414
wantErrMsg string
399415
check func(t *testing.T, repo *LocalRepository, commitMsg string)
@@ -417,8 +433,6 @@ func TestCommit(t *testing.T) {
417433
return repo
418434
},
419435
commitMsg: "feat: add new file",
420-
userName: "tester",
421-
userEmail: "[email protected]",
422436
check: func(t *testing.T, repo *LocalRepository, commitMsg string) {
423437
head, err := repo.repo.Head()
424438
if err != nil {
@@ -446,8 +460,6 @@ func TestCommit(t *testing.T) {
446460
return setupRepo(t)
447461
},
448462
commitMsg: "no-op",
449-
userName: "tester",
450-
userEmail: "[email protected]",
451463
wantErr: true,
452464
wantErrMsg: "no modifications to commit",
453465
},
@@ -456,15 +468,13 @@ func TestCommit(t *testing.T) {
456468
setup: func(t *testing.T) *LocalRepository {
457469
dir := t.TempDir()
458470
// Create a bare repository which has no worktree.
459-
gogitRepo, err := git.PlainInit(dir, true)
471+
goGitRepo, err := git.PlainInit(dir, true)
460472
if err != nil {
461473
t.Fatalf("git.PlainInit failed: %v", err)
462474
}
463-
return &LocalRepository{Dir: dir, repo: gogitRepo}
475+
return &LocalRepository{Dir: dir, repo: goGitRepo}
464476
},
465477
commitMsg: "any message",
466-
userName: "tester",
467-
userEmail: "[email protected]",
468478
wantErr: true,
469479
wantErrMsg: "worktree not available",
470480
},
@@ -497,8 +507,6 @@ func TestCommit(t *testing.T) {
497507
return repo
498508
},
499509
commitMsg: "any message",
500-
userName: "tester",
501-
userEmail: "[email protected]",
502510
wantErr: true,
503511
wantErrMsg: "permission denied",
504512
},
@@ -507,7 +515,7 @@ func TestCommit(t *testing.T) {
507515
t.Parallel()
508516
repo := tc.setup(t)
509517

510-
err := repo.Commit(tc.commitMsg, tc.userName, tc.userEmail)
518+
err := repo.Commit(tc.commitMsg)
511519

512520
if tc.wantErr {
513521
if err == nil {
@@ -564,7 +572,7 @@ func TestRemotes(t *testing.T) {
564572
}
565573

566574
for name, urls := range tt.setupRemotes {
567-
if _, err := gogitRepo.CreateRemote(&gogitConfig.RemoteConfig{
575+
if _, err := gogitRepo.CreateRemote(&goGitConfig.RemoteConfig{
568576
Name: name,
569577
URLs: urls,
570578
}); err != nil {

internal/librarian/command.go

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -151,24 +151,22 @@ func createWorkRoot(t time.Time, workRootOverride string) (string, error) {
151151
return path, nil
152152
}
153153

154-
// commitAndPush creates a commit and push request to Github for the generated changes.
155-
// It uses the GitHub client to create a PR with the specified branch, title, and description to the repository.
156-
func commitAndPush(ctx context.Context, repo gitrepo.Repository, ghClient GitHubClient, pushConfig, commitMessage string) error {
157-
if pushConfig == "" {
158-
slog.Info("PushConfig flag not specified, skipping")
154+
// commitAndPush creates a commit and push request to GitHub for the generated
155+
// changes.
156+
// It uses the GitHub client to create a PR with the specified branch, title, and
157+
// description to the repository.
158+
func commitAndPush(ctx context.Context, r *generateRunner, commitMessage string) error {
159+
if !r.cfg.Push {
160+
slog.Info("Push flag is not specified, skipping")
159161
return nil
160162
}
161163
// Ensure we have a GitHub repository
162-
gitHubRepo, err := github.FetchGitHubRepoFromRemote(repo)
164+
gitHubRepo, err := github.FetchGitHubRepoFromRemote(r.repo)
163165
if err != nil {
164166
return err
165167
}
166168

167-
userEmail, userName, err := parsePushConfig(pushConfig)
168-
if err != nil {
169-
return err
170-
}
171-
status, err := repo.AddAll()
169+
status, err := r.repo.AddAll()
172170
if err != nil {
173171
return err
174172
}
@@ -178,7 +176,7 @@ func commitAndPush(ctx context.Context, repo gitrepo.Repository, ghClient GitHub
178176
}
179177

180178
// TODO: get correct language for message (https://github.com/googleapis/librarian/issues/885)
181-
if err := repo.Commit(commitMessage, userName, userEmail); err != nil {
179+
if err := r.repo.Commit(commitMessage); err != nil {
182180
return err
183181
}
184182

@@ -188,18 +186,8 @@ func commitAndPush(ctx context.Context, repo gitrepo.Repository, ghClient GitHub
188186
branch := fmt.Sprintf("librarian-%s", datetimeNow)
189187
title := fmt.Sprintf("%s: %s", titlePrefix, datetimeNow)
190188

191-
if _, err = ghClient.CreatePullRequest(ctx, gitHubRepo, branch, title, commitMessage); err != nil {
189+
if _, err = r.ghClient.CreatePullRequest(ctx, gitHubRepo, branch, title, commitMessage); err != nil {
192190
return fmt.Errorf("failed to create pull request: %w", err)
193191
}
194192
return nil
195193
}
196-
197-
func parsePushConfig(pushConfig string) (string, string, error) {
198-
parts := strings.Split(pushConfig, ",")
199-
if len(parts) != 2 {
200-
return "", "", fmt.Errorf("invalid pushConfig format: expected 'email,user', got %q", pushConfig)
201-
}
202-
userEmail := parts[0]
203-
userName := parts[1]
204-
return userEmail, userName, nil
205-
}

0 commit comments

Comments
 (0)