Skip to content

Commit f7fe4f6

Browse files
authored
fix(internal/postprocessor): add scopes without OwlBot api-name feature (#7404)
- Remove the api-name entry in the `.github/.OwlBot.yaml` file - Update the processCommit function to use the `Copy-Tag:` text in open PRs to add scopes to each commit - Wrap each commit (except the first) with nested commit delimiters `BEGIN_NESTED_COMMIT` and `END_NESTED_COMMIT`
1 parent 124cad4 commit f7fe4f6

File tree

3 files changed

+247
-61
lines changed

3 files changed

+247
-61
lines changed

.github/.OwlBot.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,3 @@ deep-copy-regex:
522522
dest: /workflows/apiv1
523523
- source: /google/cloud/workflows/v1beta/cloud.google.com/go/workflows/apiv1beta
524524
dest: /workflows/apiv1beta
525-
526-
# api-name inserts a [REPLACEME] in OwlBot generated commit messages that is used by
527-
# postprocessor to identify commit titles that need scope added
528-
api-name: REPLACEME

internal/postprocessor/main.go

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,15 @@ import (
4141
)
4242

4343
const (
44-
owlBotBranchPrefix = "owl-bot-copy"
45-
apiNameOwlBotScope = "[REPLACEME]"
44+
owlBotBranchPrefix = "owl-bot-copy"
45+
beginNestedCommitDelimiter = "BEGIN_NESTED_COMMIT"
46+
endNestedCommitDelimiter = "END_NESTED_COMMIT"
47+
copyTagSubstring = "Copy-Tag:"
4648
)
4749

4850
var (
4951
// hashFromLinePattern grabs the hash from the end of a github commit URL
5052
hashFromLinePattern = regexp.MustCompile(`.*/(?P<hash>[a-zA-Z0-9]*).*`)
51-
// firstPartTitlePattern grabs the existing commit title before the ': [REPLACEME]'
52-
firstPartTitlePattern = regexp.MustCompile(`(?P<titleFirstPart>)(\: *\` + apiNameOwlBotScope + `)(.*)`)
53-
// secondPartTitlePattern grabs the commit title after the ': [REPLACME]'
54-
secondPartTitlePattern = regexp.MustCompile(`.*\: *\` + apiNameOwlBotScope + ` *(?P<titleSecondPart>.*)`)
5553
)
5654

5755
var (
@@ -534,50 +532,87 @@ func (c *config) SetScopesAndPRInfo(ctx context.Context) error {
534532
return nil
535533
}
536534

535+
func contains(s []string, str string) bool {
536+
for _, elem := range s {
537+
if elem == str {
538+
return true
539+
}
540+
}
541+
return false
542+
}
543+
537544
func (c *config) processCommit(title, body string) (string, string, error) {
538545
var newPRTitle string
539-
var commitTitle string
540-
var commitTitleIndex int
541-
var modules []string
546+
var newPRBodySlice []string
547+
var commitsSlice []string
548+
startCommitIndex := 0
542549

543550
bodySlice := strings.Split(body, "\n")
551+
552+
// Split body into separate commits, stripping nested commit delimiters
544553
for index, line := range bodySlice {
545-
if strings.Contains(line, apiNameOwlBotScope) {
546-
commitTitle = line
547-
commitTitleIndex = index
548-
continue
549-
}
550-
// When OwlBot generates the commit body, after commit titles it provides 'Source-Link's.
551-
// The source-link pointing to the googleapis/googleapis repo commit allows us to extract
552-
// hash and find files changed in order to identify the commit's scope.
553-
if !strings.Contains(line, "googleapis/googleapis/") {
554-
continue
554+
if strings.Contains(line, beginNestedCommitDelimiter) || strings.Contains(line, endNestedCommitDelimiter) {
555+
startCommitIndex = index + 1
555556
}
556-
hash := extractHashFromLine(line)
557-
scopes, err := c.getScopesFromGoogleapisCommitHash(hash)
558-
modules = append(modules, scopes...)
559-
var scope string
560-
if len(scopes) == 1 {
561-
scope = scopes[0]
557+
if strings.Contains(line, copyTagSubstring) {
558+
thisCommit := strings.Join(bodySlice[startCommitIndex:index+1], "\n")
559+
commitsSlice = append(commitsSlice, thisCommit)
560+
startCommitIndex = index + 1
562561
}
563-
if err != nil {
564-
return "", "", err
562+
}
563+
564+
// Add scope to each commit
565+
for commitIndex, commit := range commitsSlice {
566+
commitLines := strings.Split(strings.TrimSpace(commit), "\n")
567+
var currTitle string
568+
if commitIndex == 0 {
569+
currTitle = title
570+
} else {
571+
currTitle = commitLines[0]
572+
commitLines = commitLines[1:]
573+
newPRBodySlice = append(newPRBodySlice, "")
574+
newPRBodySlice = append(newPRBodySlice, beginNestedCommitDelimiter)
565575
}
566-
if newPRTitle == "" {
567-
newPRTitle = updateCommitTitle(title, scope)
568-
continue
576+
for _, line := range commitLines {
577+
// When OwlBot generates the commit body, after commit titles it provides 'Source-Link's.
578+
// The source-link pointing to the googleapis/googleapis repo commit allows us to extract
579+
// hash and find files changed in order to identify the commit's scope.
580+
if strings.Contains(line, "googleapis/googleapis/") {
581+
hash := extractHashFromLine(line)
582+
scopes, err := c.getScopesFromGoogleapisCommitHash(hash)
583+
for _, scope := range scopes {
584+
if !contains(c.modules, scope) {
585+
c.modules = append(c.modules, scope)
586+
}
587+
}
588+
var scope string
589+
if len(scopes) == 1 {
590+
scope = scopes[0]
591+
}
592+
if err != nil {
593+
return "", "", err
594+
}
595+
596+
newCommitTitle := updateCommitTitle(currTitle, scope)
597+
if newPRTitle == "" {
598+
newPRTitle = newCommitTitle
599+
} else {
600+
newPRBodySlice = append(newPRBodySlice, newCommitTitle)
601+
}
602+
603+
newPRBodySlice = append(newPRBodySlice, commitLines...)
604+
if commitIndex != 0 {
605+
newPRBodySlice = append(newPRBodySlice, endNestedCommitDelimiter)
606+
}
607+
}
569608
}
570-
newCommitTitle := updateCommitTitle(commitTitle, scope)
571-
bodySlice[commitTitleIndex] = newCommitTitle
572609
}
573-
body = strings.Join(bodySlice, "\n")
574610
if c.branchOverride != "" {
575611
c.modules = []string{}
576612
c.modules = append(c.modules, moduleConfigs...)
577-
} else {
578-
c.modules = append(c.modules, modules...)
579613
}
580-
return newPRTitle, body, nil
614+
newPRBody := strings.Join(newPRBodySlice, "\n")
615+
return newPRTitle, newPRBody, nil
581616
}
582617

583618
func (c *config) getPR(ctx context.Context) (*github.PullRequest, error) {
@@ -652,11 +687,12 @@ func extractHashFromLine(line string) string {
652687

653688
func updateCommitTitle(title, titlePkg string) string {
654689
var newTitle string
690+
var breakChangeIndicator string
655691

656-
firstTitlePart := firstPartTitlePattern.ReplaceAllString(title, "$titleFirstPart")
657-
secondTitlePart := secondPartTitlePattern.ReplaceAllString(title, "$titleSecondPart")
692+
titleSlice := strings.Split(title, ":")
693+
firstTitlePart := titleSlice[0]
694+
secondTitlePart := strings.TrimSpace(titleSlice[1])
658695

659-
var breakChangeIndicator string
660696
if strings.HasSuffix(firstTitlePart, "!") {
661697
breakChangeIndicator = "!"
662698
}

internal/postprocessor/main_test.go

Lines changed: 172 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestProcessCommit(t *testing.T) {
6060
}{
6161
{
6262
name: "test nested commits",
63-
title: "feat: [REPLACEME] Adds named reservation to InstancePolicy",
63+
title: "feat: Adds named reservation to InstancePolicy",
6464
body: `- [ ] Regenerate this pull request now.
6565
6666
---
@@ -77,7 +77,7 @@ Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/fcc564ef064c7
7777
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZjMxZDc5NzBlMTIzMThhZDA4NDcwM2FjNiJ9
7878
7979
BEGIN_NESTED_COMMIT
80-
feat: [REPLACEME] Adds named reservation to InstancePolicy
80+
feat: Adds named reservation to InstancePolicy
8181
---
8282
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
8383
@@ -125,24 +125,181 @@ END_NESTED_COMMIT`,
125125
},
126126
{
127127
name: "test nested client scope",
128-
title: "feat: [REPLACEME] added JSON_PACKAGE field to ExportAgentRequest",
128+
title: "feat: added JSON_PACKAGE field to ExportAgentRequest",
129129
body: `- [ ] Regenerate this pull request now.
130130
131-
PiperOrigin-RevId: 504031208
131+
PiperOrigin-RevId: 504031208
132+
133+
Source-Link: https://github.com/googleapis/googleapis/commit/c6af392b613b435757358fac555628d84e443abd
132134
133-
Source-Link: https://github.com/googleapis/googleapis/commit/c6af392b613b435757358fac555628d84e443abd
134-
135-
Source-Link: googleapis/googleapis-gen@7849764
136-
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNzg0OTc2NDc3NzYyZDk4YTljMzA4MzRkYzQ1ODVkODE1YWYyZmJmYiJ9`,
135+
Source-Link: googleapis/googleapis-gen@7849764
136+
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNzg0OTc2NDc3NzYyZDk4YTljMzA4MzRkYzQ1ODVkODE1YWYyZmJmYiJ9`,
137137
want: "feat(dialogflow/cx): added JSON_PACKAGE field to ExportAgentRequest",
138138
want1: `- [ ] Regenerate this pull request now.
139139
140-
PiperOrigin-RevId: 504031208
140+
PiperOrigin-RevId: 504031208
141+
142+
Source-Link: https://github.com/googleapis/googleapis/commit/c6af392b613b435757358fac555628d84e443abd
143+
144+
Source-Link: googleapis/googleapis-gen@7849764
145+
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNzg0OTc2NDc3NzYyZDk4YTljMzA4MzRkYzQ1ODVkODE1YWYyZmJmYiJ9`,
146+
},
147+
{
148+
name: "test add commit delimiters",
149+
title: "feat: Adds named reservation to InstancePolicy",
150+
body: `- [ ] Regenerate this pull request now.
151+
152+
---
153+
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
154+
155+
---
156+
docs: update the job id format requirement
157+
158+
PiperOrigin-RevId: 489502315
159+
160+
Source-Link: https://togithub.com/googleapis/googleapis/commit/db1cc1139fe0def1e87ead1fffbc5bedbeccb887
161+
162+
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/fcc564ef064c7dff31d7970e12318ad084703ac6
163+
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZjMxZDc5NzBlMTIzMThhZDA4NDcwM2FjNiJ9
164+
feat: Adds named reservation to InstancePolicy
165+
---
166+
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
167+
168+
---
169+
docs: update the job id format requirement
170+
171+
PiperOrigin-RevId: 489501779
172+
173+
Source-Link: https://togithub.com/googleapis/googleapis/commit/488a4bdeebf9c7f505f48bed23f0b95fcbbec0bb
174+
175+
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/5b3d3a550015e9367ad13ee5f9febe0c3f84cf33
176+
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiNWIzZDNhNTUwMDE1ZTkzNjdhZDEzZWU1ZjlmZWJlMGMzZjg0Y2YzMyJ9`,
177+
want: "feat(batch): Adds named reservation to InstancePolicy",
178+
want1: `- [ ] Regenerate this pull request now.
179+
180+
---
181+
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
182+
183+
---
184+
docs: update the job id format requirement
185+
186+
PiperOrigin-RevId: 489502315
187+
188+
Source-Link: https://togithub.com/googleapis/googleapis/commit/db1cc1139fe0def1e87ead1fffbc5bedbeccb887
189+
190+
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/fcc564ef064c7dff31d7970e12318ad084703ac6
191+
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZjMxZDc5NzBlMTIzMThhZDA4NDcwM2FjNiJ9
192+
193+
BEGIN_NESTED_COMMIT
194+
feat: Adds named reservation to InstancePolicy
195+
---
196+
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
197+
198+
---
199+
docs: update the job id format requirement
200+
201+
PiperOrigin-RevId: 489501779
202+
203+
Source-Link: https://togithub.com/googleapis/googleapis/commit/488a4bdeebf9c7f505f48bed23f0b95fcbbec0bb
204+
205+
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/5b3d3a550015e9367ad13ee5f9febe0c3f84cf33
206+
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiNWIzZDNhNTUwMDE1ZTkzNjdhZDEzZWU1ZjlmZWJlMGMzZjg0Y2YzMyJ9
207+
END_NESTED_COMMIT`,
208+
},
209+
{
210+
name: "test separate multiple commits in delimiters",
211+
title: "feat: Adds named reservation to InstancePolicy",
212+
body: `- [ ] Regenerate this pull request now.
213+
214+
---
215+
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
216+
217+
---
218+
docs: update the job id format requirement
219+
220+
PiperOrigin-RevId: 489502315
221+
222+
Source-Link: https://togithub.com/googleapis/googleapis/commit/db1cc1139fe0def1e87ead1fffbc5bedbeccb887
223+
224+
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/fcc564ef064c7dff31d7970e12318ad084703ac6
225+
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZjMxZDc5NzBlMTIzMThhZDA4NDcwM2FjNiJ9
226+
227+
BEGIN_NESTED_COMMIT
228+
feat: Adds named reservation to InstancePolicy
229+
---
230+
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
231+
232+
---
233+
docs: update the job id format requirement
234+
235+
PiperOrigin-RevId: 489501779
236+
237+
Source-Link: https://togithub.com/googleapis/googleapis/commit/488a4bdeebf9c7f505f48bed23f0b95fcbbec0bb
238+
239+
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/5b3d3a550015e9367ad13ee5f9febe0c3f84cf33
240+
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiNWIzZDNhNTUwMDE1ZTkzNjdhZDEzZWU1ZjlmZWJlMGMzZjg0Y2YzMyJ9
241+
242+
feat: Adds named reservation to InstancePolicy
243+
---
244+
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
245+
246+
---
247+
docs: update the job id format requirement
248+
249+
PiperOrigin-RevId: 489501779
250+
251+
Source-Link: https://togithub.com/googleapis/googleapis/commit/488a4bdeebf9c7f505f48bed23f0b95fcbbec0bb
252+
253+
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/5b3d3a550015e9367ad13ee5f9febe0c3f84cf33
254+
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiNWIzZDNhNTUwMDE1ZTkzNjdhZDEzZWU1ZjlmZWJlMGMzZjg0Y2YzMyJ9
255+
END_NESTED_COMMIT`,
256+
want: "feat(batch): Adds named reservation to InstancePolicy",
257+
want1: `- [ ] Regenerate this pull request now.
258+
259+
---
260+
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
261+
262+
---
263+
docs: update the job id format requirement
264+
265+
PiperOrigin-RevId: 489502315
266+
267+
Source-Link: https://togithub.com/googleapis/googleapis/commit/db1cc1139fe0def1e87ead1fffbc5bedbeccb887
268+
269+
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/fcc564ef064c7dff31d7970e12318ad084703ac6
270+
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZjMxZDc5NzBlMTIzMThhZDA4NDcwM2FjNiJ9
271+
272+
BEGIN_NESTED_COMMIT
273+
feat: Adds named reservation to InstancePolicy
274+
---
275+
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
276+
277+
---
278+
docs: update the job id format requirement
279+
280+
PiperOrigin-RevId: 489501779
141281
142-
Source-Link: https://github.com/googleapis/googleapis/commit/c6af392b613b435757358fac555628d84e443abd
143-
144-
Source-Link: googleapis/googleapis-gen@7849764
145-
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNzg0OTc2NDc3NzYyZDk4YTljMzA4MzRkYzQ1ODVkODE1YWYyZmJmYiJ9`,
282+
Source-Link: https://togithub.com/googleapis/googleapis/commit/488a4bdeebf9c7f505f48bed23f0b95fcbbec0bb
283+
284+
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/5b3d3a550015e9367ad13ee5f9febe0c3f84cf33
285+
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiNWIzZDNhNTUwMDE1ZTkzNjdhZDEzZWU1ZjlmZWJlMGMzZjg0Y2YzMyJ9
286+
END_NESTED_COMMIT
287+
288+
BEGIN_NESTED_COMMIT
289+
feat: Adds named reservation to InstancePolicy
290+
---
291+
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
292+
293+
---
294+
docs: update the job id format requirement
295+
296+
PiperOrigin-RevId: 489501779
297+
298+
Source-Link: https://togithub.com/googleapis/googleapis/commit/488a4bdeebf9c7f505f48bed23f0b95fcbbec0bb
299+
300+
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/5b3d3a550015e9367ad13ee5f9febe0c3f84cf33
301+
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiNWIzZDNhNTUwMDE1ZTkzNjdhZDEzZWU1ZjlmZWJlMGMzZjg0Y2YzMyJ9
302+
END_NESTED_COMMIT`,
146303
},
147304
}
148305
for _, tt := range tests {
@@ -156,11 +313,8 @@ END_NESTED_COMMIT`,
156313
t.Errorf("processCommit() error = %v, wantErr %v", err, tt.wantErr)
157314
return
158315
}
159-
if got != tt.want {
160-
t.Errorf("processCommit() got = %v, want %v", got, tt.want)
161-
}
162-
if got1 != tt.want1 {
163-
t.Errorf("processCommit() got1 = %v, want %v", got1, tt.want1)
316+
if diff := cmp.Diff(tt.want, got); diff != "" {
317+
t.Errorf("processCommit() mismatch (-want +got):\n%s", diff)
164318
}
165319
if diff := cmp.Diff(tt.want1, got1); diff != "" {
166320
t.Errorf("processCommit() mismatch (-want +got):\n%s", diff)

0 commit comments

Comments
 (0)