Skip to content

Commit 2cab5d6

Browse files
authored
feat(internal/automation): add branch support to RepositoryConfig (#2906)
1 parent eabb33a commit 2cab5d6

File tree

5 files changed

+150
-10
lines changed

5 files changed

+150
-10
lines changed

internal/automation/cloudbuild_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type mockCloudBuildClient struct {
3030
runError error
3131
buildTriggers []*cloudbuildpb.BuildTrigger
3232
triggersRun []string
33+
substitutions []map[string]string
3334
}
3435

3536
func (c *mockCloudBuildClient) RunBuildTrigger(ctx context.Context, req *cloudbuildpb.RunBuildTriggerRequest, opts ...gax.CallOption) error {
@@ -43,6 +44,7 @@ func (c *mockCloudBuildClient) RunBuildTrigger(ctx context.Context, req *cloudbu
4344
}
4445
}
4546
c.triggersRun = append(c.triggersRun, req.TriggerId)
47+
c.substitutions = append(c.substitutions, req.GetSource().GetSubstitutions())
4648
return nil
4749
}
4850

internal/automation/repositories.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ type RepositoryConfig struct {
3939
FullName string `yaml:"full-name"`
4040
SecretName string `yaml:"github-token-secret-name"`
4141
SupportedCommands []string `yaml:"supported-commands"`
42+
43+
// Branch configures the repository branch to checkout in Cloud Build prior
44+
// to command execution. Furthermore, it dictates the value set on the
45+
// [github.com/googleapis/librarian/internal/config.Config] Branch property
46+
// via the --branch flag.
47+
//
48+
// This property is optional. Downstream usage defaults to "main".
49+
Branch string `yaml:"branch"`
4250
}
4351

4452
// RepositoriesConfig represents all the registered librarian GitHub repositories.

internal/automation/repositories_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,27 @@ func TestParseRepositoriesConfig(t *testing.T) {
170170
},
171171
},
172172
},
173+
{
174+
name: "valid state with branch",
175+
content: `repositories:
176+
- name: google-cloud-python
177+
branch: preview
178+
github-token-secret-name: google-cloud-python-github-token
179+
supported-commands:
180+
- generate
181+
- stage-release
182+
`,
183+
want: &RepositoriesConfig{
184+
Repositories: []*RepositoryConfig{
185+
{
186+
Name: "google-cloud-python",
187+
Branch: "preview",
188+
SecretName: "google-cloud-python-github-token",
189+
SupportedCommands: []string{"generate", "stage-release"},
190+
},
191+
},
192+
},
193+
},
173194
{
174195
name: "invalid yaml",
175196
content: `repositories:

internal/automation/trigger.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ func runCommandWithConfig(ctx context.Context, client CloudBuildClient, ghClient
112112
"_GITHUB_TOKEN_SECRET_NAME": repository.SecretName,
113113
"_PUSH": fmt.Sprintf("%v", push),
114114
}
115+
if repository.Branch != "" {
116+
substitutions["_BRANCH"] = repository.Branch
117+
}
118+
115119
if command == "publish-release" {
116120
parts := strings.Split(gitUrl, "/")
117121
repositoryOwner := parts[len(parts)-2]

internal/automation/trigger_test.go

Lines changed: 115 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,30 @@ func TestRunCommandWithConfig(t *testing.T) {
199199
Name: "stage-release",
200200
Id: "stage-release-trigger-id",
201201
},
202+
{
203+
Name: "publish-release",
204+
Id: "publish-release-trigger-id",
205+
},
206+
{
207+
Name: "update-image",
208+
Id: "update-image-trigger-id",
209+
},
202210
{
203211
Name: "prepare-release",
204212
Id: "prepare-release-trigger-id",
205213
},
206214
}
207215
for _, test := range []struct {
208-
name string
209-
command string
210-
config *RepositoriesConfig
211-
want string
212-
runError error
213-
wantErr bool
214-
ghPRs []*github.PullRequest
215-
ghError error
216-
wantTriggersRun []string
216+
name string
217+
command string
218+
config *RepositoriesConfig
219+
want string
220+
runError error
221+
wantErr bool
222+
ghPRs []*github.PullRequest
223+
ghError error
224+
wantTriggersRun []string
225+
wantSubstitutions []map[string]string
217226
}{
218227
{
219228
name: "runs generate trigger with name",
@@ -223,25 +232,42 @@ func TestRunCommandWithConfig(t *testing.T) {
223232
{
224233
Name: "google-cloud-python",
225234
SupportedCommands: []string{"generate"},
235+
SecretName: "foo",
226236
},
227237
},
228238
},
229239
wantErr: false,
230240
wantTriggersRun: []string{"generate-trigger-id"},
241+
wantSubstitutions: []map[string]string{{
242+
"_REPOSITORY": "google-cloud-python",
243+
"_FULL_REPOSITORY": "https://github.com/googleapis/google-cloud-python",
244+
"_GITHUB_TOKEN_SECRET_NAME": "foo",
245+
"_PUSH": "true",
246+
"_BUILD": "true",
247+
}},
231248
},
232249
{
233250
name: "runs generate trigger with full name",
234251
command: "generate",
235252
config: &RepositoriesConfig{
236253
Repositories: []*RepositoryConfig{
237254
{
238-
Name: "https://github.com/googleapis/google-cloud-python",
255+
Name: "google-cloud-python",
256+
FullName: "https://github.com/googleapis/google-cloud-python",
239257
SupportedCommands: []string{"generate"},
258+
SecretName: "bar",
240259
},
241260
},
242261
},
243262
wantErr: false,
244263
wantTriggersRun: []string{"generate-trigger-id"},
264+
wantSubstitutions: []map[string]string{{
265+
"_REPOSITORY": "google-cloud-python",
266+
"_FULL_REPOSITORY": "https://github.com/googleapis/google-cloud-python",
267+
"_GITHUB_TOKEN_SECRET_NAME": "bar",
268+
"_PUSH": "true",
269+
"_BUILD": "true",
270+
}},
245271
},
246272
{
247273
name: "runs generate trigger without name",
@@ -264,11 +290,87 @@ func TestRunCommandWithConfig(t *testing.T) {
264290
{
265291
Name: "google-cloud-python",
266292
SupportedCommands: []string{"stage-release"},
293+
SecretName: "baz",
267294
},
268295
},
269296
},
270297
wantErr: false,
271298
wantTriggersRun: []string{"stage-release-trigger-id"},
299+
wantSubstitutions: []map[string]string{{
300+
"_REPOSITORY": "google-cloud-python",
301+
"_FULL_REPOSITORY": "https://github.com/googleapis/google-cloud-python",
302+
"_GITHUB_TOKEN_SECRET_NAME": "baz",
303+
"_PUSH": "true",
304+
}},
305+
},
306+
{
307+
name: "runs publish-release trigger",
308+
command: "publish-release",
309+
config: &RepositoriesConfig{
310+
Repositories: []*RepositoryConfig{
311+
{
312+
Name: "google-cloud-python",
313+
SupportedCommands: []string{"publish-release"},
314+
SecretName: "qux",
315+
},
316+
},
317+
},
318+
ghPRs: []*github.PullRequest{{HTMLURL: github.Ptr("https://github.com/googleapis/google-cloud-python/pull/42")}},
319+
wantErr: false,
320+
wantTriggersRun: []string{"publish-release-trigger-id"},
321+
wantSubstitutions: []map[string]string{{
322+
"_REPOSITORY": "google-cloud-python",
323+
"_FULL_REPOSITORY": "https://github.com/googleapis/google-cloud-python",
324+
"_GITHUB_TOKEN_SECRET_NAME": "qux",
325+
"_PUSH": "true",
326+
"_PR": "https://github.com/googleapis/google-cloud-python/pull/42",
327+
}},
328+
},
329+
{
330+
name: "runs update-image trigger",
331+
command: "update-image",
332+
config: &RepositoriesConfig{
333+
Repositories: []*RepositoryConfig{
334+
{
335+
Name: "google-cloud-python",
336+
SupportedCommands: []string{"update-image"},
337+
SecretName: "quux",
338+
},
339+
},
340+
},
341+
wantErr: false,
342+
wantTriggersRun: []string{"update-image-trigger-id"},
343+
wantSubstitutions: []map[string]string{{
344+
"_REPOSITORY": "google-cloud-python",
345+
"_FULL_REPOSITORY": "https://github.com/googleapis/google-cloud-python",
346+
"_GITHUB_TOKEN_SECRET_NAME": "quux",
347+
"_PUSH": "true",
348+
"_BUILD": "true",
349+
}},
350+
},
351+
{
352+
name: "runs generate trigger on branch",
353+
command: "generate",
354+
config: &RepositoriesConfig{
355+
Repositories: []*RepositoryConfig{
356+
{
357+
Name: "google-cloud-python",
358+
SupportedCommands: []string{"generate"},
359+
Branch: "preview",
360+
SecretName: "foo",
361+
},
362+
},
363+
},
364+
wantErr: false,
365+
wantTriggersRun: []string{"generate-trigger-id"},
366+
wantSubstitutions: []map[string]string{{
367+
"_REPOSITORY": "google-cloud-python",
368+
"_FULL_REPOSITORY": "https://github.com/googleapis/google-cloud-python",
369+
"_GITHUB_TOKEN_SECRET_NAME": "foo",
370+
"_PUSH": "true",
371+
"_BRANCH": "preview",
372+
"_BUILD": "true",
373+
}},
272374
},
273375
} {
274376
t.Run(test.name, func(t *testing.T) {
@@ -290,6 +392,9 @@ func TestRunCommandWithConfig(t *testing.T) {
290392
if diff := cmp.Diff(test.wantTriggersRun, client.triggersRun); diff != "" {
291393
t.Errorf("runCommandWithConfig() triggersRun diff (-want, +got):\n%s", diff)
292394
}
395+
if diff := cmp.Diff(test.wantSubstitutions, client.substitutions); diff != "" {
396+
t.Errorf("runCommandWithConfig() substitutions diff (-want, +got):\n%s", diff)
397+
}
293398
})
294399
}
295400
}

0 commit comments

Comments
 (0)