Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 11 additions & 28 deletions internal/librarianops/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func processRepo(ctx context.Context, repoName, repoDir, librarianBin string, ve
err = cerr
}
}()
if err := cloneRepo(ctx, repoDir, repoName); err != nil {
if err := cloneRepoInDir(ctx, repoName, repoDir); err != nil {
return err
}
}
Expand All @@ -117,8 +117,7 @@ func processRepo(ctx context.Context, repoName, repoDir, librarianBin string, ve
return fmt.Errorf("failed to change directory to %q: %w", repoDir, err)
}
defer os.Chdir(originalWD)

if err := createBranch(ctx, time.Now()); err != nil {
if err := createBranch(ctx, generateBranchName(branchPrefix, time.Now())); err != nil {
return err
}
cfg, err := yaml.Read[config.Config]("librarian.yaml")
Expand Down Expand Up @@ -157,48 +156,32 @@ func processRepo(ctx context.Context, repoName, repoDir, librarianBin string, ve
return err
}
}
if err := commitChanges(ctx); err != nil {
if err := commitChanges(ctx, commitTitle); err != nil {
return err
}
if repoName != repoFake {
if err := pushBranch(ctx); err != nil {
if err := pushChanges(ctx); err != nil {
return err
}
if err := createPR(ctx, repoName); err != nil {
if err := createPR(ctx, createGithubDetails(repoName)); err != nil {
return err
}
}
return nil
}

func cloneRepo(ctx context.Context, repoDir, repoName string) error {
return command.Run(ctx, "gh", "repo", "clone", fmt.Sprintf("googleapis/%s", repoName), repoDir)
}

func createBranch(ctx context.Context, now time.Time) error {
branchName := fmt.Sprintf("%s%s", branchPrefix, now.UTC().Format("20060102T150405Z"))
return command.Run(ctx, "git", "checkout", "-b", branchName)
}

func commitChanges(ctx context.Context) error {
if err := command.Run(ctx, "git", "add", "."); err != nil {
return err
}
return command.Run(ctx, "git", "commit", "-m", commitTitle)
}

func pushBranch(ctx context.Context) error {
return command.Run(ctx, "git", "push", "-u", "origin", "HEAD")
}

func createPR(ctx context.Context, repoName string) error {
func createGithubDetails(repoName string) GithubDetails {
sources := "googleapis"
if repoName == repoRust {
sources = "googleapis and discovery-artifact-manager"
}
title := fmt.Sprintf("chore: update %s and regenerate", sources)
body := fmt.Sprintf("Update %s to the latest commit and regenerate all client libraries.", sources)
return command.Run(ctx, "gh", "pr", "create", "--title", title, "--body", body)
return GithubDetails{
PrTitle: title,
PrBody: body,
BranchName: generateBranchName(branchPrefix, time.Now()),
}
}

func runCargoUpdate(ctx context.Context) error {
Expand Down
76 changes: 76 additions & 0 deletions internal/librarianops/github_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2026 Google LLC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package librarianops

import (
"context"
"fmt"
"time"

"github.com/googleapis/librarian/internal/command"
)

var runCommand = command.Run

// uploadToGithub creates a branch, commits changes, pushes the changes, and creates a PR with the given details.
func uploadToGithub(ctx context.Context, githubDetails GithubDetails) error {
if err := createBranch(ctx, githubDetails.BranchName); err != nil {
return err
}
if err := commitChanges(ctx, githubDetails.PrTitle); err != nil {
return err
}
if err := pushChanges(ctx); err != nil {
return err
}
if err := createPR(ctx, githubDetails); err != nil {
return err
}
return nil
}

func cloneRepoInDir(ctx context.Context, repoName string, repoDir string) error {
return runCommand(ctx, "gh", "repo", "clone", fmt.Sprintf("googleapis/%s", repoName), repoDir)
}

func generateBranchName(prefix string, time time.Time) string {
return fmt.Sprintf("%s%s", prefix, time.UTC().Format("20060102T150405Z"))
}

func createBranch(ctx context.Context, branchName string) error {
return runCommand(ctx, "git", "checkout", "-b", branchName)
}

func commitChanges(ctx context.Context, commitTitle string) error {
if err := runCommand(ctx, "git", "add", "."); err != nil {
return err
}
return runCommand(ctx, "git", "commit", "-m", commitTitle)
}

func pushChanges(ctx context.Context) error {
return runCommand(ctx, "git", "push", "-u", "origin", "HEAD")
}

// GithubDetails contains the details for the github changes to be made.
type GithubDetails struct {
PrTitle string
PrBody string
BranchName string
}

func createPR(ctx context.Context, githubDetails GithubDetails) error {
return runCommand(ctx, "gh", "pr", "create", "--title", githubDetails.PrTitle, "--body", githubDetails.PrBody)
}
Loading
Loading