Skip to content

Commit ac46748

Browse files
committed
refactor(internal/librarianops/generate): move github functions to its own file
1 parent a29479b commit ac46748

File tree

3 files changed

+464
-29
lines changed

3 files changed

+464
-29
lines changed

internal/librarianops/generate.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func processRepo(ctx context.Context, repoName, repoDir, librarianBin string, ve
105105
err = cerr
106106
}
107107
}()
108-
if err := cloneRepo(ctx, repoDir, repoName); err != nil {
108+
if err := cloneRepoInDir(ctx, repoName, repoDir); err != nil {
109109
return err
110110
}
111111
}
@@ -117,8 +117,7 @@ func processRepo(ctx context.Context, repoName, repoDir, librarianBin string, ve
117117
return fmt.Errorf("failed to change directory to %q: %w", repoDir, err)
118118
}
119119
defer os.Chdir(originalWD)
120-
121-
if err := createBranch(ctx, time.Now()); err != nil {
120+
if err := createBranch(ctx, generateBranchName(branchPrefix, time.Now())); err != nil {
122121
return err
123122
}
124123
cfg, err := yaml.Read[config.Config]("librarian.yaml")
@@ -157,48 +156,33 @@ func processRepo(ctx context.Context, repoName, repoDir, librarianBin string, ve
157156
return err
158157
}
159158
}
160-
if err := commitChanges(ctx); err != nil {
159+
if err := commitChanges(ctx, commitTitle); err != nil {
161160
return err
162161
}
163162
if repoName != repoFake {
164-
if err := pushBranch(ctx); err != nil {
163+
if err := pushChanges(ctx); err != nil {
165164
return err
166165
}
167-
if err := createPR(ctx, repoName); err != nil {
166+
if err := createPR(ctx, createGithubDetails(repoName)); err != nil {
168167
return err
169168
}
170169
}
171170
return nil
172171
}
173172

174-
func cloneRepo(ctx context.Context, repoDir, repoName string) error {
175-
return command.Run(ctx, "gh", "repo", "clone", fmt.Sprintf("googleapis/%s", repoName), repoDir)
176-
}
177-
178-
func createBranch(ctx context.Context, now time.Time) error {
179-
branchName := fmt.Sprintf("%s%s", branchPrefix, now.UTC().Format("20060102T150405Z"))
180-
return command.Run(ctx, "git", "checkout", "-b", branchName)
181-
}
182-
183-
func commitChanges(ctx context.Context) error {
184-
if err := command.Run(ctx, "git", "add", "."); err != nil {
185-
return err
186-
}
187-
return command.Run(ctx, "git", "commit", "-m", commitTitle)
188-
}
189-
190-
func pushBranch(ctx context.Context) error {
191-
return command.Run(ctx, "git", "push", "-u", "origin", "HEAD")
192-
}
193-
194-
func createPR(ctx context.Context, repoName string) error {
173+
func createGithubDetails(repoName string) GithubDetails {
195174
sources := "googleapis"
196175
if repoName == repoRust {
197176
sources = "googleapis and discovery-artifact-manager"
198177
}
199178
title := fmt.Sprintf("chore: update %s and regenerate", sources)
200-
body := fmt.Sprintf("Update %s to the latest commit and regenerate all client libraries.", sources)
201-
return command.Run(ctx, "gh", "pr", "create", "--title", title, "--body", body)
179+
body := fmt.Sprintf(`
180+
Update %s to the latest commit and regenerate all client libraries.`, sources)
181+
return GithubDetails{
182+
PrTitle: title,
183+
PrBody: body,
184+
BranchName: generateBranchName(branchPrefix, time.Now()),
185+
}
202186
}
203187

204188
func runCargoUpdate(ctx context.Context) error {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package librarianops
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"time"
21+
22+
"github.com/googleapis/librarian/internal/command"
23+
)
24+
25+
var runCommand = command.Run
26+
27+
// uploadToGithub creates a branch, commits changes, pushes the changes, and creates a PR with the given details.
28+
func uploadToGithub(ctx context.Context, githubDetails GithubDetails) error {
29+
if err := createBranch(ctx, githubDetails.BranchName); err != nil {
30+
return err
31+
}
32+
if err := commitChanges(ctx, githubDetails.PrTitle); err != nil {
33+
return err
34+
}
35+
if err := pushChanges(ctx); err != nil {
36+
return err
37+
}
38+
if err := createPR(ctx, githubDetails); err != nil {
39+
return err
40+
}
41+
return nil
42+
}
43+
44+
func cloneRepoInDir(ctx context.Context, repoName string, repoDir string) error {
45+
return runCommand(ctx, "gh", "repo", "clone", fmt.Sprintf("googleapis/%s", repoName), repoDir)
46+
}
47+
48+
func generateBranchName(prefix string, time time.Time) string {
49+
return fmt.Sprintf("%s%s", prefix, time.Format("2006-01-02"))
50+
}
51+
52+
func createBranch(ctx context.Context, branchName string) error {
53+
return runCommand(ctx, "git", "checkout", "-b", branchName)
54+
}
55+
56+
func commitChanges(ctx context.Context, commitTitle string) error {
57+
if err := runCommand(ctx, "git", "add", "."); err != nil {
58+
return err
59+
}
60+
return runCommand(ctx, "git", "commit", "-m", commitTitle)
61+
}
62+
63+
func pushChanges(ctx context.Context) error {
64+
return runCommand(ctx, "git", "push", "-u", "origin", "HEAD")
65+
}
66+
67+
// GithubDetails contains the details for the github changes to be made.
68+
type GithubDetails struct {
69+
PrTitle string
70+
PrBody string
71+
BranchName string
72+
}
73+
74+
func createPR(ctx context.Context, githubDetails GithubDetails) error {
75+
return runCommand(ctx, "gh", "pr", "create", "--title", githubDetails.PrTitle, "--body", githubDetails.PrBody)
76+
}

0 commit comments

Comments
 (0)