Skip to content

Commit af7b19e

Browse files
authored
chore: refactor commitAndPush func (#1907)
Refactor `commitAndPush` function so that it can be used to create generate and release pull requests. Updates #1703 Updates #1899
1 parent ee7391a commit af7b19e

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

internal/librarian/command.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ import (
3838
"github.com/googleapis/librarian/internal/gitrepo"
3939
)
4040

41+
const (
42+
generate = "generate"
43+
release = "release"
44+
)
45+
46+
type commitInfo struct {
47+
cfg *config.Config
48+
state *config.LibrarianState
49+
repo gitrepo.Repository
50+
ghClient GitHubClient
51+
additionalMessage string
52+
commitMessage string
53+
prType string
54+
}
55+
4156
type commandRunner struct {
4257
cfg *config.Config
4358
repo gitrepo.Repository
@@ -320,12 +335,14 @@ func coerceLibraryChanges(commits []*conventionalcommits.ConventionalCommit) []*
320335
// changes.
321336
// It uses the GitHub client to create a PR with the specified branch, title, and
322337
// description to the repository.
323-
func commitAndPush(ctx context.Context, cfg *config.Config, repo gitrepo.Repository, ghClient GitHubClient, commitMessage string) error {
338+
func commitAndPush(ctx context.Context, info *commitInfo) error {
339+
cfg := info.cfg
324340
if !cfg.Push && !cfg.Commit {
325341
slog.Info("Push flag and Commit flag are not specified, skipping committing")
326342
return nil
327343
}
328344

345+
repo := info.repo
329346
status, err := repo.AddAll()
330347
if err != nil {
331348
return err
@@ -343,6 +360,7 @@ func commitAndPush(ctx context.Context, cfg *config.Config, repo gitrepo.Reposit
343360
}
344361

345362
// TODO: get correct language for message (https://github.com/googleapis/librarian/issues/885)
363+
commitMessage := info.commitMessage
346364
slog.Info("Committing", "message", commitMessage)
347365
if err := repo.Commit(commitMessage); err != nil {
348366
return err
@@ -363,13 +381,12 @@ func commitAndPush(ctx context.Context, cfg *config.Config, repo gitrepo.Reposit
363381
return err
364382
}
365383

366-
// Create a new branch, set title and message for the PR.
367-
titlePrefix := "Librarian pull request"
368-
title := fmt.Sprintf("%s: %s", titlePrefix, datetimeNow)
384+
title := fmt.Sprintf("Librarian %s pull request: %s", info.prType, datetimeNow)
369385
slog.Info("Creating pull request", slog.String("branch", branch), slog.String("title", title))
370-
if _, err = ghClient.CreatePullRequest(ctx, gitHubRepo, branch, cfg.Branch, title, commitMessage); err != nil {
386+
if _, err = info.ghClient.CreatePullRequest(ctx, gitHubRepo, branch, cfg.Branch, title, commitMessage); err != nil {
371387
return fmt.Errorf("failed to create pull request: %w", err)
372388
}
389+
373390
return nil
374391
}
375392

internal/librarian/command_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,8 +1313,15 @@ func TestCommitAndPush(t *testing.T) {
13131313
Push: test.push,
13141314
Commit: test.commit,
13151315
}
1316-
1317-
err := commitAndPush(context.Background(), localConfig, repo, client, "")
1316+
commitInfo := &commitInfo{
1317+
cfg: localConfig,
1318+
state: nil,
1319+
repo: repo,
1320+
ghClient: client,
1321+
commitMessage: "",
1322+
prType: generate,
1323+
}
1324+
err := commitAndPush(context.Background(), commitInfo)
13181325

13191326
if test.wantErr {
13201327
if err == nil {

internal/librarian/generate.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (r *generateRunner) run(ctx context.Context) error {
127127
}
128128
slog.Info("Code will be generated", "dir", outputDir)
129129

130-
prBody := ""
130+
additionalMsg := ""
131131
if r.cfg.API != "" || r.cfg.Library != "" {
132132
libraryID := r.cfg.Library
133133
if libraryID == "" {
@@ -136,14 +136,13 @@ func (r *generateRunner) run(ctx context.Context) error {
136136
if err := r.generateSingleLibrary(ctx, libraryID, outputDir); err != nil {
137137
return err
138138
}
139-
prBody += fmt.Sprintf("feat: generated %s\n", libraryID)
139+
additionalMsg += fmt.Sprintf("feat: generated %s\n", libraryID)
140140
} else {
141141
failedGenerations := 0
142142
for _, library := range r.state.Libraries {
143143
if err := r.generateSingleLibrary(ctx, library.ID, outputDir); err != nil {
144-
// TODO(https://github.com/googleapis/librarian/issues/983): record failure and report in PR body when applicable
145144
slog.Error("failed to generate library", "id", library.ID, "err", err)
146-
prBody += fmt.Sprintf("%s failed to generate\n", library.ID)
145+
additionalMsg += fmt.Sprintf("%s failed to generate\n", library.ID)
147146
failedGenerations++
148147
}
149148
}
@@ -155,7 +154,17 @@ func (r *generateRunner) run(ctx context.Context) error {
155154
if err := saveLibrarianState(r.repo.GetDir(), r.state); err != nil {
156155
return err
157156
}
158-
if err := commitAndPush(ctx, r.cfg, r.repo, r.ghClient, prBody); err != nil {
157+
158+
commitInfo := &commitInfo{
159+
cfg: r.cfg,
160+
state: r.state,
161+
repo: r.repo,
162+
ghClient: r.ghClient,
163+
additionalMessage: additionalMsg,
164+
commitMessage: "",
165+
prType: generate,
166+
}
167+
if err := commitAndPush(ctx, commitInfo); err != nil {
159168
return err
160169
}
161170
return nil

internal/librarian/release_init.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,15 @@ func (r *initRunner) run(ctx context.Context) error {
105105
return err
106106
}
107107

108-
// TODO: https://github.com/googleapis/librarian/issues/1697
109-
// Add commit message after this issue is resolved.
110-
if err := commitAndPush(ctx, r.cfg, r.repo, r.ghClient, ""); err != nil {
108+
commitInfo := &commitInfo{
109+
cfg: r.cfg,
110+
state: r.state,
111+
repo: r.repo,
112+
ghClient: r.ghClient,
113+
commitMessage: "",
114+
prType: release,
115+
}
116+
if err := commitAndPush(ctx, commitInfo); err != nil {
111117
return fmt.Errorf("failed to commit and push: %w", err)
112118
}
113119

0 commit comments

Comments
 (0)