Skip to content

Commit 862c7d7

Browse files
authored
feat: write pr-body.txt to the work root when not pushing (#2395)
Fixes #2389
1 parent 00f7e92 commit 862c7d7

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

internal/librarian/command.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141
generate = "generate"
4242
release = "release"
4343
defaultAPISourceBranch = "master"
44+
prBodyFile = "pr-body.txt"
4445
failedGenerationComment = `One or more libraries have failed to generate, please review PR description for a list of failed libraries.
4546
For each failed library, open a ticket in that library’s repository and then you may resolve this comment and merge.
4647
`
@@ -87,6 +88,7 @@ type commitInfo struct {
8788
repo gitrepo.Repository
8889
sourceRepo gitrepo.Repository
8990
state *config.LibrarianState
91+
workRoot string
9092
failedGenerations int
9193
}
9294

@@ -330,6 +332,7 @@ func getDirectoryFilenames(dir string) ([]string, error) {
330332
func commitAndPush(ctx context.Context, info *commitInfo) error {
331333
if !info.push && !info.commit {
332334
slog.Info("Push flag and Commit flag are not specified, skipping committing")
335+
writePRBody(info)
333336
return nil
334337
}
335338

@@ -387,6 +390,35 @@ func commitAndPush(ctx context.Context, info *commitInfo) error {
387390
return addLabelsToPullRequest(ctx, info.ghClient, info.pullRequestLabels, pullRequestMetadata)
388391
}
389392

393+
// writePRBody attempts to log the body of a PR that would have been created if the
394+
// -push flag had been specified. This logs any errors (e.g. if the GitHub repo can't be determined)
395+
// but deliberately does not return them, as a failure here should not interfere with the flow.
396+
func writePRBody(info *commitInfo) {
397+
gitHubRepo, err := GetGitHubRepositoryFromGitRepo(info.repo)
398+
if err != nil {
399+
slog.Warn("Unable to create PR body; could not determine GitHub repo", "error", err)
400+
return
401+
}
402+
403+
prBody, err := createPRBody(info, gitHubRepo)
404+
if err != nil {
405+
slog.Warn("Unable to create PR body", "error", err)
406+
return
407+
}
408+
// Note: we can't accurately predict whether or not a PR would have been created,
409+
// as we're not checking whether the repo is clean or not. The intention is to be
410+
// as light-touch as possible.
411+
fullPath := filepath.Join(info.workRoot, prBodyFile)
412+
// Ensure that "cat [path-to-pr-body.txt]" gives useful output.
413+
prBody = prBody + "\n"
414+
err = os.WriteFile(fullPath, []byte(prBody), 0644)
415+
if err == nil {
416+
slog.Info("Wrote body of pull request that might have been created", "file", fullPath)
417+
} else {
418+
slog.Warn("Unable to save PR body", "error", err)
419+
}
420+
}
421+
390422
// addLabelsToPullRequest adds a list of labels to a single pull request (specified by the id number).
391423
// Should only be called on a valid Github pull request.
392424
// Passing in `nil` for labels will no-op and an empty list for labels will clear all labels on the PR.

internal/librarian/command_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,7 @@ func TestCommitAndPush(t *testing.T) {
16171617
repo: repo,
16181618
state: test.state,
16191619
failedGenerations: test.failedGenerations,
1620+
workRoot: t.TempDir(),
16201621
}
16211622

16221623
err := commitAndPush(context.Background(), commitInfo)
@@ -1638,6 +1639,96 @@ func TestCommitAndPush(t *testing.T) {
16381639
}
16391640
}
16401641

1642+
func TestWritePRBody(t *testing.T) {
1643+
for _, test := range []struct {
1644+
name string
1645+
info *commitInfo
1646+
wantFile bool
1647+
}{
1648+
{
1649+
name: "success",
1650+
info: &commitInfo{
1651+
repo: &MockRepository{
1652+
Dir: t.TempDir(),
1653+
RemotesValue: []*gitrepo.Remote{
1654+
&gitrepo.Remote{
1655+
Name: "origin",
1656+
URLs: []string{"https://github.com/googleapis/librarian.git"},
1657+
},
1658+
},
1659+
},
1660+
prType: "release",
1661+
state: &config.LibrarianState{},
1662+
workRoot: t.TempDir(),
1663+
},
1664+
wantFile: true,
1665+
},
1666+
{
1667+
name: "invalid repo",
1668+
info: &commitInfo{
1669+
repo: &MockRepository{
1670+
Dir: t.TempDir(),
1671+
RemotesValue: []*gitrepo.Remote{
1672+
&gitrepo.Remote{
1673+
Name: "not-origin",
1674+
URLs: []string{"https://github.com/googleapis/librarian.git"},
1675+
},
1676+
},
1677+
},
1678+
prType: "release",
1679+
workRoot: t.TempDir(),
1680+
},
1681+
},
1682+
{
1683+
name: "invalid-pr-type",
1684+
info: &commitInfo{
1685+
repo: &MockRepository{
1686+
Dir: t.TempDir(),
1687+
RemotesValue: []*gitrepo.Remote{
1688+
&gitrepo.Remote{
1689+
Name: "origin",
1690+
URLs: []string{"https://github.com/googleapis/librarian.git"},
1691+
},
1692+
},
1693+
},
1694+
prType: "invalid-pr-type",
1695+
workRoot: t.TempDir(),
1696+
},
1697+
},
1698+
{
1699+
name: "unable to save file",
1700+
info: &commitInfo{
1701+
repo: &MockRepository{
1702+
Dir: t.TempDir(),
1703+
AddAllStatus: make(git.Status),
1704+
RemotesValue: []*gitrepo.Remote{
1705+
&gitrepo.Remote{
1706+
Name: "origin",
1707+
URLs: []string{"https://github.com/googleapis/librarian.git"},
1708+
},
1709+
},
1710+
},
1711+
prType: "release",
1712+
state: &config.LibrarianState{},
1713+
workRoot: filepath.Join(t.TempDir(), "missing-directory"),
1714+
},
1715+
},
1716+
} {
1717+
t.Run(test.name, func(t *testing.T) {
1718+
writePRBody(test.info)
1719+
possibleFilePath := filepath.Join(test.info.workRoot, prBodyFile)
1720+
_, err := os.Stat(possibleFilePath)
1721+
if err != nil && !os.IsNotExist(err) {
1722+
t.Fatalf("error other than IsNotExist finding status of %s", possibleFilePath)
1723+
}
1724+
gotFile := err == nil
1725+
if test.wantFile != gotFile {
1726+
t.Errorf("writePRBody() wantFile = %t, gotFile = %t", test.wantFile, gotFile)
1727+
}
1728+
})
1729+
}
1730+
}
1731+
16411732
func TestAddLabelsToPullRequest(t *testing.T) {
16421733
for _, test := range []struct {
16431734
name string

internal/librarian/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func (r *generateRunner) run(ctx context.Context) error {
152152
repo: r.repo,
153153
sourceRepo: r.sourceRepo,
154154
state: r.state,
155+
workRoot: r.workRoot,
155156
failedGenerations: failedGenerations,
156157
}
157158

internal/librarian/release_init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func (r *initRunner) run(ctx context.Context) error {
105105
repo: r.repo,
106106
sourceRepo: r.sourceRepo,
107107
state: r.state,
108+
workRoot: r.workRoot,
108109
}
109110
if err := commitAndPush(ctx, commitInfo); err != nil {
110111
return fmt.Errorf("failed to commit and push: %w", err)

0 commit comments

Comments
 (0)