Skip to content

Commit 97720cd

Browse files
authored
feat(internal/librarian): record library generation failure message in PR message (#999)
Record library generation failure message in PR message Fix: #983
1 parent 28a51bf commit 97720cd

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

internal/librarian/command.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func createWorkRoot(t time.Time, workRootOverride string) (string, error) {
137137

138138
// commitAndPush creates a commit and push request to Github for the generated changes.
139139
// It uses the GitHub client to create a PR with the specified branch, title, and description to the repository.
140-
func commitAndPush(ctx context.Context, repo *gitrepo.Repository, ghClient GitHubClient, pushConfig string) error {
140+
func commitAndPush(ctx context.Context, repo *gitrepo.Repository, ghClient GitHubClient, pushConfig, commitMessage string) error {
141141
if pushConfig == "" {
142142
slog.Info("PushConfig flag not specified, skipping")
143143
return nil
@@ -157,16 +157,15 @@ func commitAndPush(ctx context.Context, repo *gitrepo.Repository, ghClient GitHu
157157
}
158158

159159
// TODO: get correct language for message (https://github.com/googleapis/librarian/issues/885)
160-
message := "Changes in this PR"
161-
repo.Commit(message, userName, userEmail)
160+
repo.Commit(commitMessage, userName, userEmail)
162161

163162
// Create a new branch, set title and message for the PR.
164163
datetimeNow := formatTimestamp(time.Now())
165164
titlePrefix := "Librarian pull request"
166165
branch := fmt.Sprintf("librarian-%s", datetimeNow)
167166
title := fmt.Sprintf("%s: %s", titlePrefix, datetimeNow)
168167

169-
_, err = ghClient.CreatePullRequest(ctx, gitHubRepo, branch, title, message)
168+
_, err = ghClient.CreatePullRequest(ctx, gitHubRepo, branch, title, commitMessage)
170169
if err != nil {
171170
return fmt.Errorf("failed to create pull request: %w", err)
172171
}

internal/librarian/command_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ func TestCommitAndPush(t *testing.T) {
496496
repo := test.setupMockRepo(t)
497497
client := test.setupMockClient(t)
498498

499-
err := commitAndPush(context.Background(), repo, client, test.pushConfig)
499+
err := commitAndPush(context.Background(), repo, client, test.pushConfig, "")
500500

501501
if err != nil && !strings.Contains(err.Error(), test.expectedErrMsg) {
502502
t.Errorf("commitAndPush() error = %v, expected to contain: %q", err, test.expectedErrMsg)

internal/librarian/generate.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func (r *generateRunner) run(ctx context.Context) error {
158158
}
159159
slog.Info("Code will be generated", "dir", outputDir)
160160

161+
prBody := ""
161162
if r.cfg.API != "" || r.cfg.Library != "" {
162163
libraryID := r.cfg.Library
163164
if libraryID == "" {
@@ -171,11 +172,12 @@ func (r *generateRunner) run(ctx context.Context) error {
171172
if err := r.generateSingleLibrary(ctx, library.ID, outputDir); err != nil {
172173
// TODO(https://github.com/googleapis/librarian/issues/983): record failure and report in PR body when applicable
173174
slog.Error("failed to generate library", "id", library.ID, "err", err)
175+
prBody += fmt.Sprintf("%s failed to generate\n", library.ID)
174176
}
175177
}
176178
}
177179

178-
if err := commitAndPush(ctx, r.repo, r.ghClient, r.cfg.PushConfig); err != nil {
180+
if err := commitAndPush(ctx, r.repo, r.ghClient, r.cfg.PushConfig, prBody); err != nil {
179181
return err
180182
}
181183
return nil

internal/librarian/generate_test.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,22 @@ import (
3737
// mockContainerClient is a mock implementation of the ContainerClient interface for testing.
3838
type mockContainerClient struct {
3939
ContainerClient
40-
generateCalls int
41-
buildCalls int
42-
configureCalls int
43-
generateErr error
44-
buildErr error
40+
generateCalls int
41+
buildCalls int
42+
configureCalls int
43+
generateErr error
44+
buildErr error
45+
failGenerateForID string
4546
}
4647

4748
func (m *mockContainerClient) Generate(ctx context.Context, request *docker.GenerateRequest) error {
4849
m.generateCalls++
50+
if m.failGenerateForID != "" {
51+
if request.LibraryID == m.failGenerateForID {
52+
return m.generateErr
53+
}
54+
return nil
55+
}
4956
return m.generateErr
5057
}
5158

@@ -472,6 +479,31 @@ func TestGenerateRun(t *testing.T) {
472479
build: true,
473480
wantErr: true,
474481
},
482+
{
483+
name: "generate all, partial failure does not halt execution",
484+
repo: newTestGitRepo(t),
485+
state: &config.LibrarianState{
486+
Image: "gcr.io/test/image:v1.2.3",
487+
Libraries: []*config.LibraryState{
488+
{
489+
ID: "lib1",
490+
APIs: []*config.API{{Path: "some/api1"}},
491+
},
492+
{
493+
ID: "lib2",
494+
APIs: []*config.API{{Path: "some/api2"}},
495+
},
496+
},
497+
},
498+
container: &mockContainerClient{
499+
failGenerateForID: "lib1",
500+
generateErr: errors.New("generate error"),
501+
},
502+
ghClient: &mockGitHubClient{},
503+
build: true,
504+
wantGenerateCalls: 2,
505+
wantBuildCalls: 1,
506+
},
475507
{
476508
name: "commit and push error",
477509
api: "some/api",

0 commit comments

Comments
 (0)