Skip to content

Commit dd44dbf

Browse files
authored
feat: add a comment to generation PR if generation has a failure (#2291)
Fixes #2287
1 parent 3fe4e57 commit dd44dbf

File tree

5 files changed

+80
-36
lines changed

5 files changed

+80
-36
lines changed

internal/github/github.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ type PullRequestReview = github.PullRequestReview
4444
// RepositoryRelease is a type alias for the go-github type.
4545
type RepositoryRelease = github.RepositoryRelease
4646

47-
// MergeMethodRebase is a constant alias for the go-github constant.
48-
const MergeMethodRebase = github.MergeMethodRebase
49-
5047
// Client represents this package's abstraction of a GitHub client, including
5148
// an access token.
5249
type Client struct {

internal/librarian/command.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ import (
3838
)
3939

4040
const (
41-
generate = "generate"
42-
release = "release"
41+
generate = "generate"
42+
release = "release"
43+
defaultAPISourceBranch = "master"
44+
failedGenerationComment = `One or more libraries have failed to generate, please review PR description for a list of failed libraries.
45+
For each failed library, open a ticket in that library’s repository and then you may resolve this comment and merge.
46+
`
4347
)
4448

4549
var globalPreservePatterns = []string{
@@ -83,6 +87,7 @@ type commitInfo struct {
8387
repo gitrepo.Repository
8488
sourceRepo gitrepo.Repository
8589
state *config.LibrarianState
90+
failedGenerations int
8691
}
8792

8893
type commandRunner struct {
@@ -96,8 +101,6 @@ type commandRunner struct {
96101
workRoot string
97102
}
98103

99-
const defaultAPISourceBranch = "master"
100-
101104
func newCommandRunner(cfg *config.Config) (*commandRunner, error) {
102105
languageRepo, err := cloneOrOpenRepo(cfg.WorkRoot, cfg.Repo, cfg.APISourceDepth, cfg.Branch, cfg.CI, cfg.GitHubToken)
103106
if err != nil {
@@ -375,6 +378,12 @@ func commitAndPush(ctx context.Context, info *commitInfo) error {
375378
return fmt.Errorf("failed to create pull request: %w", err)
376379
}
377380

381+
if info.failedGenerations != 0 {
382+
if err := info.ghClient.CreateIssueComment(ctx, pullRequestMetadata.Number, failedGenerationComment); err != nil {
383+
return fmt.Errorf("failed to add pull request comment: %w", err)
384+
}
385+
}
386+
378387
return addLabelsToPullRequest(ctx, info.ghClient, info.pullRequestLabels, pullRequestMetadata)
379388
}
380389

internal/librarian/command_test.go

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,15 +1293,16 @@ func TestCompileRegexps(t *testing.T) {
12931293

12941294
func TestCommitAndPush(t *testing.T) {
12951295
for _, test := range []struct {
1296-
name string
1297-
setupMockRepo func(t *testing.T) gitrepo.Repository
1298-
setupMockClient func(t *testing.T) GitHubClient
1299-
state *config.LibrarianState
1300-
prType string
1301-
commit bool
1302-
push bool
1303-
wantErr bool
1304-
expectedErrMsg string
1296+
name string
1297+
setupMockRepo func(t *testing.T) gitrepo.Repository
1298+
setupMockClient func(t *testing.T) GitHubClient
1299+
state *config.LibrarianState
1300+
prType string
1301+
failedGenerations int
1302+
commit bool
1303+
push bool
1304+
wantErr bool
1305+
expectedErrMsg string
13051306
}{
13061307
{
13071308
name: "Push flag and Commit flag are not specified",
@@ -1576,19 +1577,48 @@ func TestCommitAndPush(t *testing.T) {
15761577
prType: "generate",
15771578
push: true,
15781579
},
1580+
{
1581+
name: "create_a_comment_on_generation_pr_error",
1582+
setupMockRepo: func(t *testing.T) gitrepo.Repository {
1583+
remote := git.NewRemote(memory.NewStorage(), &gogitConfig.RemoteConfig{
1584+
Name: "origin",
1585+
URLs: []string{"https://github.com/googleapis/librarian.git"},
1586+
})
1587+
status := make(git.Status)
1588+
status["file.txt"] = &git.FileStatus{Worktree: git.Modified}
1589+
return &MockRepository{
1590+
Dir: t.TempDir(),
1591+
AddAllStatus: status,
1592+
RemotesValue: []*git.Remote{remote},
1593+
}
1594+
},
1595+
setupMockClient: func(t *testing.T) GitHubClient {
1596+
return &mockGitHubClient{
1597+
createdPR: &github.PullRequestMetadata{Number: 123, Repo: &github.Repository{Owner: "test-owner", Name: "test-repo"}},
1598+
createIssueErr: errors.New("simulate comment creation error"),
1599+
}
1600+
},
1601+
state: &config.LibrarianState{},
1602+
prType: "generate",
1603+
failedGenerations: 1,
1604+
push: true,
1605+
wantErr: true,
1606+
expectedErrMsg: "failed to add pull request comment",
1607+
},
15791608
} {
15801609
t.Run(test.name, func(t *testing.T) {
15811610
repo := test.setupMockRepo(t)
15821611
client := test.setupMockClient(t)
15831612

15841613
commitInfo := &commitInfo{
1585-
commit: test.commit,
1586-
commitMessage: "",
1587-
ghClient: client,
1588-
prType: test.prType,
1589-
push: test.push,
1590-
repo: repo,
1591-
state: test.state,
1614+
commit: test.commit,
1615+
commitMessage: "",
1616+
ghClient: client,
1617+
prType: test.prType,
1618+
push: test.push,
1619+
repo: repo,
1620+
state: test.state,
1621+
failedGenerations: test.failedGenerations,
15921622
}
15931623

15941624
err := commitAndPush(context.Background(), commitInfo)

internal/librarian/generate.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ func (r *generateRunner) run(ctx context.Context) error {
8686
// The last generated commit is changed after library generation,
8787
// use this map to keep the mapping from library id to commit sha before the
8888
// generation since we need these commits to create pull request body.
89-
idToCommits := make(map[string]string, 0)
89+
idToCommits := make(map[string]string)
9090
var failedLibraries []string
91+
failedGenerations := 0
9192
if r.api != "" || r.library != "" {
9293
libraryID := r.library
9394
if libraryID == "" {
@@ -100,7 +101,6 @@ func (r *generateRunner) run(ctx context.Context) error {
100101
idToCommits[libraryID] = oldCommit
101102
} else {
102103
succeededGenerations := 0
103-
failedGenerations := 0
104104
blockedGenerations := 0
105105
for _, library := range r.state.Libraries {
106106
if r.librarianConfig != nil {
@@ -141,17 +141,18 @@ func (r *generateRunner) run(ctx context.Context) error {
141141
}
142142

143143
commitInfo := &commitInfo{
144-
branch: r.branch,
145-
commit: r.commit,
146-
commitMessage: "feat: generate libraries",
147-
failedLibraries: failedLibraries,
148-
ghClient: r.ghClient,
149-
idToCommits: idToCommits,
150-
prType: generate,
151-
push: r.push,
152-
repo: r.repo,
153-
sourceRepo: r.sourceRepo,
154-
state: r.state,
144+
branch: r.branch,
145+
commit: r.commit,
146+
commitMessage: "feat: generate libraries",
147+
failedLibraries: failedLibraries,
148+
ghClient: r.ghClient,
149+
idToCommits: idToCommits,
150+
prType: generate,
151+
push: r.push,
152+
repo: r.repo,
153+
sourceRepo: r.sourceRepo,
154+
state: r.state,
155+
failedGenerations: failedGenerations,
155156
}
156157

157158
return commitAndPush(ctx, commitInfo)

internal/librarian/mocks_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type mockGitHubClient struct {
4141
searchPullRequestsCalls int
4242
getPullRequestCalls int
4343
createReleaseCalls int
44+
createIssueCalls int
4445
createTagCalls int
4546
createPullRequestErr error
4647
addLabelsToIssuesErr error
@@ -49,6 +50,7 @@ type mockGitHubClient struct {
4950
searchPullRequestsErr error
5051
getPullRequestErr error
5152
createReleaseErr error
53+
createIssueErr error
5254
createTagErr error
5355
createdPR *github.PullRequestMetadata
5456
labels []string
@@ -104,6 +106,11 @@ func (m *mockGitHubClient) CreateRelease(ctx context.Context, tagName, releaseNa
104106
return m.createdRelease, m.createReleaseErr
105107
}
106108

109+
func (m *mockGitHubClient) CreateIssueComment(ctx context.Context, number int, comment string) error {
110+
m.createIssueCalls++
111+
return m.createIssueErr
112+
}
113+
107114
func (m *mockGitHubClient) CreateTag(ctx context.Context, tagName, commitish string) error {
108115
m.createTagCalls++
109116
return m.createTagErr

0 commit comments

Comments
 (0)