Skip to content

Commit e32719c

Browse files
authored
feat: add onboarding PR body (#2552)
Fixes #2378
1 parent 0adeeac commit e32719c

File tree

10 files changed

+423
-68
lines changed

10 files changed

+423
-68
lines changed

e2e_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ func TestRunGenerate(t *testing.T) {
6666
workRoot := t.TempDir()
6767
repo := t.TempDir()
6868
apiSourceRepo := t.TempDir()
69-
if err := initRepo(t, repo, initialRepoStateDir); err != nil {
69+
if err := initRepo(t, repo, initialRepoStateDir, "initial commit"); err != nil {
7070
t.Fatalf("languageRepo prepare test error = %v", err)
7171
}
72-
if err := initRepo(t, apiSourceRepo, localAPISource); err != nil {
72+
if err := initRepo(t, apiSourceRepo, localAPISource, "initial commit"); err != nil {
7373
t.Fatalf("APISouceRepo prepare test error = %v", err)
7474
}
7575

@@ -197,10 +197,10 @@ func TestCleanAndCopy(t *testing.T) {
197197
workRoot := t.TempDir()
198198
repo := t.TempDir()
199199
apiSourceRepo := t.TempDir()
200-
if err := initRepo(t, repo, repoInitDir); err != nil {
200+
if err := initRepo(t, repo, repoInitDir, "initial commit"); err != nil {
201201
t.Fatalf("languageRepo prepare test error = %v", err)
202202
}
203-
if err := initRepo(t, apiSourceRepo, localAPISource); err != nil {
203+
if err := initRepo(t, apiSourceRepo, localAPISource, "initial commit"); err != nil {
204204
t.Fatalf("APISouceRepo prepare test error = %v", err)
205205
}
206206

@@ -277,10 +277,10 @@ func TestRunConfigure(t *testing.T) {
277277
workRoot := t.TempDir()
278278
repo := t.TempDir()
279279
apiSourceRepo := t.TempDir()
280-
if err := initRepo(t, repo, initialRepoStateDir); err != nil {
280+
if err := initRepo(t, repo, initialRepoStateDir, "initial commit"); err != nil {
281281
t.Fatalf("prepare test error = %v", err)
282282
}
283-
if err := initRepo(t, apiSourceRepo, test.apiSource); err != nil {
283+
if err := initRepo(t, apiSourceRepo, test.apiSource, "feat: add a new api\n\nPiperOrigin-RevId: 123456"); err != nil {
284284
t.Fatalf("APISouceRepo prepare test error = %v", err)
285285
}
286286

@@ -380,10 +380,10 @@ func TestRunGenerate_MultipleLibraries(t *testing.T) {
380380
repo := t.TempDir()
381381
apiSourceRepo := t.TempDir()
382382

383-
if err := initRepo(t, repo, test.initialRepoStateDir); err != nil {
383+
if err := initRepo(t, repo, test.initialRepoStateDir, "initial commit"); err != nil {
384384
t.Fatalf("languageRepo prepare test error = %v", err)
385385
}
386-
if err := initRepo(t, apiSourceRepo, localAPISource); err != nil {
386+
if err := initRepo(t, apiSourceRepo, localAPISource, "initial commit"); err != nil {
387387
t.Fatalf("APISouceRepo prepare test error = %v", err)
388388
}
389389

@@ -486,7 +486,7 @@ func TestReleaseInit(t *testing.T) {
486486
wantChangelog := filepath.Join(test.testDataDir, "CHANGELOG.md")
487487
commitMsgPath := filepath.Join(test.testDataDir, "commit_msg.txt")
488488

489-
if err := initRepo(t, repo, initialRepoStateDir); err != nil {
489+
if err := initRepo(t, repo, initialRepoStateDir, "initial commit"); err != nil {
490490
t.Fatalf("prepare test error = %v", err)
491491
}
492492

@@ -722,7 +722,7 @@ libraries:
722722
repo := test.repoURL
723723
if test.repoPath != "" {
724724
repo = t.TempDir()
725-
err := initRepo(t, repo, test.repoPath)
725+
err := initRepo(t, repo, test.repoPath, "initial commit")
726726
if err != nil {
727727
t.Fatalf("error initializing fake git repo %s", err)
728728
}
@@ -792,7 +792,7 @@ func newMockGitHubServer(t *testing.T, prTitleFragment string) *httptest.Server
792792

793793
// initRepo initiates a git repo in the given directory, copy
794794
// files from source directory and create a commit.
795-
func initRepo(t *testing.T, dir, source string) error {
795+
func initRepo(t *testing.T, dir, source, message string) error {
796796
t.Logf("initializing repo, dir %s, source %s", dir, source)
797797
if err := os.CopyFS(dir, os.DirFS(source)); err != nil {
798798
return err
@@ -801,7 +801,7 @@ func initRepo(t *testing.T, dir, source string) error {
801801
runGit(t, dir, "add", ".")
802802
runGit(t, dir, "config", "user.email", "test@github.com")
803803
runGit(t, dir, "config", "user.name", "Test User")
804-
runGit(t, dir, "commit", "-m", "init test repo")
804+
runGit(t, dir, "commit", "-m", message)
805805
runGit(t, dir, "remote", "add", "origin", "https://github.com/googleapis/librarian.git")
806806
return nil
807807
}

internal/gitrepo/gitrepo_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"os"
2121
"path/filepath"
22+
"slices"
2223
"strings"
2324
"testing"
2425
"time"
@@ -445,6 +446,8 @@ func TestChangedFiles(t *testing.T) {
445446
}
446447
return
447448
}
449+
450+
slices.Sort(gotFiles) // Sorting makes the test deterministic.
448451
if diff := cmp.Diff(test.wantFiles, gotFiles); diff != "" {
449452
t.Errorf("ChangedFiles() mismatch (-want +got):\n%s", diff)
450453
}

internal/librarian/command.go

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,37 @@ import (
3737
)
3838

3939
const (
40-
generate = "generate"
41-
release = "release"
4240
defaultAPISourceBranch = "master"
4341
prBodyFile = "pr-body.txt"
4442
failedGenerationComment = `One or more libraries have failed to generate, please review PR description for a list of failed libraries.
4543
For each failed library, open a ticket in that library’s repository and then you may resolve this comment and merge.
4644
`
4745
)
4846

47+
type pullRequestType int
48+
49+
const (
50+
pullRequestUnspecified pullRequestType = iota
51+
pullRequestOnboard
52+
pullRequestGenerate
53+
pullRequestRelease
54+
)
55+
56+
// String returns the string representation of a pullRequestType.
57+
// It returns unknown if the type is not a recognized constant.
58+
func (t pullRequestType) String() string {
59+
names := map[pullRequestType]string{
60+
pullRequestUnspecified: "unspecified",
61+
pullRequestOnboard: "onboard",
62+
pullRequestGenerate: "generate",
63+
pullRequestRelease: "release",
64+
}
65+
if name, ok := names[t]; ok {
66+
return name
67+
}
68+
return "unspecified"
69+
}
70+
4971
var globalPreservePatterns = []string{
5072
fmt.Sprintf(`^%s(/.*)?$`, regexp.QuoteMeta(config.GeneratorInputDir)), // Preserve the generator-input directory and its contents.
5173
}
@@ -79,17 +101,18 @@ type commitInfo struct {
79101
failedLibraries []string
80102
ghClient GitHubClient
81103
idToCommits map[string]string
82-
library string
83-
libraryVersion string
84-
prType string
104+
prType pullRequestType
85105
pullRequestLabels []string
86106
push bool
87107
languageRepo gitrepo.Repository
88108
sourceRepo gitrepo.Repository
89109
state *config.LibrarianState
90110
workRoot string
91-
apiOnboarding bool
92111
failedGenerations int
112+
// api is the api path of a library, only set this value during api onboarding.
113+
api string
114+
// library is the ID of a library, only set this value during api onboarding.
115+
library string
93116
}
94117

95118
type commandRunner struct {
@@ -447,9 +470,24 @@ func addLabelsToPullRequest(ctx context.Context, ghClient GitHubClient, pullRequ
447470

448471
func createPRBody(info *commitInfo, gitHubRepo *github.Repository) (string, error) {
449472
switch info.prType {
450-
case generate:
451-
return formatGenerationPRBody(info.sourceRepo, info.languageRepo, info.state, info.idToCommits, info.failedLibraries)
452-
case release:
473+
case pullRequestOnboard:
474+
req := &onboardPRRequest{
475+
sourceRepo: info.sourceRepo,
476+
state: info.state,
477+
api: info.api,
478+
library: info.library,
479+
}
480+
return formatOnboardPRBody(req)
481+
case pullRequestGenerate:
482+
req := &generationPRRequest{
483+
sourceRepo: info.sourceRepo,
484+
languageRepo: info.languageRepo,
485+
state: info.state,
486+
idToCommits: info.idToCommits,
487+
failedLibraries: info.failedLibraries,
488+
}
489+
return formatGenerationPRBody(req)
490+
case pullRequestRelease:
453491
return formatReleaseNotes(info.state, gitHubRepo)
454492
default:
455493
return "", fmt.Errorf("unrecognized pull request type: %s", info.prType)

internal/librarian/command_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ func TestCommitAndPush(t *testing.T) {
12941294
setupMockRepo func(t *testing.T) gitrepo.Repository
12951295
setupMockClient func(t *testing.T) GitHubClient
12961296
state *config.LibrarianState
1297-
prType string
1297+
prType pullRequestType
12981298
failedGenerations int
12991299
commit bool
13001300
push bool
@@ -1320,7 +1320,7 @@ func TestCommitAndPush(t *testing.T) {
13201320
return nil
13211321
},
13221322
state: &config.LibrarianState{},
1323-
prType: "release",
1323+
prType: pullRequestRelease,
13241324
check: func(t *testing.T, repo gitrepo.Repository) {
13251325
mockRepo := repo.(*MockRepository)
13261326
if mockRepo.PushCalls != 0 {
@@ -1347,7 +1347,7 @@ func TestCommitAndPush(t *testing.T) {
13471347
}
13481348
},
13491349
state: &config.LibrarianState{},
1350-
prType: "release",
1350+
prType: pullRequestRelease,
13511351
commit: true,
13521352
check: func(t *testing.T, repo gitrepo.Repository) {
13531353
mockRepo := repo.(*MockRepository)
@@ -1375,7 +1375,7 @@ func TestCommitAndPush(t *testing.T) {
13751375
}
13761376
},
13771377
state: &config.LibrarianState{},
1378-
prType: "generate",
1378+
prType: pullRequestGenerate,
13791379
push: true,
13801380
},
13811381
{
@@ -1396,7 +1396,7 @@ func TestCommitAndPush(t *testing.T) {
13961396
}
13971397
},
13981398
state: &config.LibrarianState{},
1399-
prType: "release",
1399+
prType: pullRequestRelease,
14001400
push: true,
14011401
},
14021402
{
@@ -1410,7 +1410,7 @@ func TestCommitAndPush(t *testing.T) {
14101410
setupMockClient: func(t *testing.T) GitHubClient {
14111411
return nil
14121412
},
1413-
prType: "generate",
1413+
prType: pullRequestGenerate,
14141414
push: true,
14151415
state: &config.LibrarianState{},
14161416
wantErr: true,
@@ -1432,7 +1432,7 @@ func TestCommitAndPush(t *testing.T) {
14321432
setupMockClient: func(t *testing.T) GitHubClient {
14331433
return nil
14341434
},
1435-
prType: "generate",
1435+
prType: pullRequestGenerate,
14361436
push: true,
14371437
wantErr: true,
14381438
expectedErrMsg: "mock add all error",
@@ -1453,7 +1453,7 @@ func TestCommitAndPush(t *testing.T) {
14531453
setupMockClient: func(t *testing.T) GitHubClient {
14541454
return nil
14551455
},
1456-
prType: "generate",
1456+
prType: pullRequestGenerate,
14571457
push: true,
14581458
wantErr: true,
14591459
expectedErrMsg: "create branch error",
@@ -1474,7 +1474,7 @@ func TestCommitAndPush(t *testing.T) {
14741474
setupMockClient: func(t *testing.T) GitHubClient {
14751475
return nil
14761476
},
1477-
prType: "generate",
1477+
prType: pullRequestGenerate,
14781478
push: true,
14791479
wantErr: true,
14801480
expectedErrMsg: "commit error",
@@ -1495,7 +1495,7 @@ func TestCommitAndPush(t *testing.T) {
14951495
setupMockClient: func(t *testing.T) GitHubClient {
14961496
return nil
14971497
},
1498-
prType: "generate",
1498+
prType: pullRequestGenerate,
14991499
push: true,
15001500
wantErr: true,
15011501
expectedErrMsg: "push error",
@@ -1516,7 +1516,7 @@ func TestCommitAndPush(t *testing.T) {
15161516
return &mockGitHubClient{}
15171517
},
15181518
state: &config.LibrarianState{},
1519-
prType: "random",
1519+
prType: 4,
15201520
push: true,
15211521
wantErr: true,
15221522
expectedErrMsg: "failed to create pull request body",
@@ -1539,7 +1539,7 @@ func TestCommitAndPush(t *testing.T) {
15391539
}
15401540
},
15411541
state: &config.LibrarianState{},
1542-
prType: "generate",
1542+
prType: pullRequestGenerate,
15431543
push: true,
15441544
wantErr: true,
15451545
expectedErrMsg: "failed to create pull request",
@@ -1560,7 +1560,7 @@ func TestCommitAndPush(t *testing.T) {
15601560
setupMockClient: func(t *testing.T) GitHubClient {
15611561
return nil
15621562
},
1563-
prType: "generate",
1563+
prType: pullRequestGenerate,
15641564
push: true,
15651565
},
15661566
{
@@ -1582,7 +1582,7 @@ func TestCommitAndPush(t *testing.T) {
15821582
}
15831583
},
15841584
state: &config.LibrarianState{},
1585-
prType: "generate",
1585+
prType: pullRequestGenerate,
15861586
failedGenerations: 1,
15871587
push: true,
15881588
wantErr: true,
@@ -1651,7 +1651,7 @@ func TestWritePRBody(t *testing.T) {
16511651
},
16521652
},
16531653
},
1654-
prType: "release",
1654+
prType: pullRequestRelease,
16551655
state: &config.LibrarianState{},
16561656
workRoot: t.TempDir(),
16571657
},
@@ -1669,7 +1669,7 @@ func TestWritePRBody(t *testing.T) {
16691669
},
16701670
},
16711671
},
1672-
prType: "release",
1672+
prType: pullRequestRelease,
16731673
workRoot: t.TempDir(),
16741674
},
16751675
},
@@ -1685,7 +1685,7 @@ func TestWritePRBody(t *testing.T) {
16851685
},
16861686
},
16871687
},
1688-
prType: "invalid-pr-type",
1688+
prType: 4,
16891689
workRoot: t.TempDir(),
16901690
},
16911691
},
@@ -1701,7 +1701,7 @@ func TestWritePRBody(t *testing.T) {
17011701
},
17021702
},
17031703
},
1704-
prType: "release",
1704+
prType: pullRequestRelease,
17051705
state: &config.LibrarianState{},
17061706
workRoot: filepath.Join(t.TempDir(), "missing-directory"),
17071707
},

0 commit comments

Comments
 (0)