Skip to content

Commit

Permalink
fix(internal/postprocessor): add scopes without OwlBot api-name featu…
Browse files Browse the repository at this point in the history
…re (#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`
  • Loading branch information
adrianajg committed Feb 11, 2023
1 parent 124cad4 commit f7fe4f6
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 61 deletions.
4 changes: 0 additions & 4 deletions .github/.OwlBot.yaml
Expand Up @@ -522,7 +522,3 @@ deep-copy-regex:
dest: /workflows/apiv1
- source: /google/cloud/workflows/v1beta/cloud.google.com/go/workflows/apiv1beta
dest: /workflows/apiv1beta

# api-name inserts a [REPLACEME] in OwlBot generated commit messages that is used by
# postprocessor to identify commit titles that need scope added
api-name: REPLACEME
114 changes: 75 additions & 39 deletions internal/postprocessor/main.go
Expand Up @@ -41,17 +41,15 @@ import (
)

const (
owlBotBranchPrefix = "owl-bot-copy"
apiNameOwlBotScope = "[REPLACEME]"
owlBotBranchPrefix = "owl-bot-copy"
beginNestedCommitDelimiter = "BEGIN_NESTED_COMMIT"
endNestedCommitDelimiter = "END_NESTED_COMMIT"
copyTagSubstring = "Copy-Tag:"
)

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

var (
Expand Down Expand Up @@ -534,50 +532,87 @@ func (c *config) SetScopesAndPRInfo(ctx context.Context) error {
return nil
}

func contains(s []string, str string) bool {
for _, elem := range s {
if elem == str {
return true
}
}
return false
}

func (c *config) processCommit(title, body string) (string, string, error) {
var newPRTitle string
var commitTitle string
var commitTitleIndex int
var modules []string
var newPRBodySlice []string
var commitsSlice []string
startCommitIndex := 0

bodySlice := strings.Split(body, "\n")

// Split body into separate commits, stripping nested commit delimiters
for index, line := range bodySlice {
if strings.Contains(line, apiNameOwlBotScope) {
commitTitle = line
commitTitleIndex = index
continue
}
// When OwlBot generates the commit body, after commit titles it provides 'Source-Link's.
// The source-link pointing to the googleapis/googleapis repo commit allows us to extract
// hash and find files changed in order to identify the commit's scope.
if !strings.Contains(line, "googleapis/googleapis/") {
continue
if strings.Contains(line, beginNestedCommitDelimiter) || strings.Contains(line, endNestedCommitDelimiter) {
startCommitIndex = index + 1
}
hash := extractHashFromLine(line)
scopes, err := c.getScopesFromGoogleapisCommitHash(hash)
modules = append(modules, scopes...)
var scope string
if len(scopes) == 1 {
scope = scopes[0]
if strings.Contains(line, copyTagSubstring) {
thisCommit := strings.Join(bodySlice[startCommitIndex:index+1], "\n")
commitsSlice = append(commitsSlice, thisCommit)
startCommitIndex = index + 1
}
if err != nil {
return "", "", err
}

// Add scope to each commit
for commitIndex, commit := range commitsSlice {
commitLines := strings.Split(strings.TrimSpace(commit), "\n")
var currTitle string
if commitIndex == 0 {
currTitle = title
} else {
currTitle = commitLines[0]
commitLines = commitLines[1:]
newPRBodySlice = append(newPRBodySlice, "")
newPRBodySlice = append(newPRBodySlice, beginNestedCommitDelimiter)
}
if newPRTitle == "" {
newPRTitle = updateCommitTitle(title, scope)
continue
for _, line := range commitLines {
// When OwlBot generates the commit body, after commit titles it provides 'Source-Link's.
// The source-link pointing to the googleapis/googleapis repo commit allows us to extract
// hash and find files changed in order to identify the commit's scope.
if strings.Contains(line, "googleapis/googleapis/") {
hash := extractHashFromLine(line)
scopes, err := c.getScopesFromGoogleapisCommitHash(hash)
for _, scope := range scopes {
if !contains(c.modules, scope) {
c.modules = append(c.modules, scope)
}
}
var scope string
if len(scopes) == 1 {
scope = scopes[0]
}
if err != nil {
return "", "", err
}

newCommitTitle := updateCommitTitle(currTitle, scope)
if newPRTitle == "" {
newPRTitle = newCommitTitle
} else {
newPRBodySlice = append(newPRBodySlice, newCommitTitle)
}

newPRBodySlice = append(newPRBodySlice, commitLines...)
if commitIndex != 0 {
newPRBodySlice = append(newPRBodySlice, endNestedCommitDelimiter)
}
}
}
newCommitTitle := updateCommitTitle(commitTitle, scope)
bodySlice[commitTitleIndex] = newCommitTitle
}
body = strings.Join(bodySlice, "\n")
if c.branchOverride != "" {
c.modules = []string{}
c.modules = append(c.modules, moduleConfigs...)
} else {
c.modules = append(c.modules, modules...)
}
return newPRTitle, body, nil
newPRBody := strings.Join(newPRBodySlice, "\n")
return newPRTitle, newPRBody, nil
}

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

func updateCommitTitle(title, titlePkg string) string {
var newTitle string
var breakChangeIndicator string

firstTitlePart := firstPartTitlePattern.ReplaceAllString(title, "$titleFirstPart")
secondTitlePart := secondPartTitlePattern.ReplaceAllString(title, "$titleSecondPart")
titleSlice := strings.Split(title, ":")
firstTitlePart := titleSlice[0]
secondTitlePart := strings.TrimSpace(titleSlice[1])

var breakChangeIndicator string
if strings.HasSuffix(firstTitlePart, "!") {
breakChangeIndicator = "!"
}
Expand Down
190 changes: 172 additions & 18 deletions internal/postprocessor/main_test.go
Expand Up @@ -60,7 +60,7 @@ func TestProcessCommit(t *testing.T) {
}{
{
name: "test nested commits",
title: "feat: [REPLACEME] Adds named reservation to InstancePolicy",
title: "feat: Adds named reservation to InstancePolicy",
body: `- [ ] Regenerate this pull request now.
---
Expand All @@ -77,7 +77,7 @@ Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/fcc564ef064c7
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZjMxZDc5NzBlMTIzMThhZDA4NDcwM2FjNiJ9
BEGIN_NESTED_COMMIT
feat: [REPLACEME] Adds named reservation to InstancePolicy
feat: Adds named reservation to InstancePolicy
---
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
Expand Down Expand Up @@ -125,24 +125,181 @@ END_NESTED_COMMIT`,
},
{
name: "test nested client scope",
title: "feat: [REPLACEME] added JSON_PACKAGE field to ExportAgentRequest",
title: "feat: added JSON_PACKAGE field to ExportAgentRequest",
body: `- [ ] Regenerate this pull request now.
PiperOrigin-RevId: 504031208
PiperOrigin-RevId: 504031208
Source-Link: https://github.com/googleapis/googleapis/commit/c6af392b613b435757358fac555628d84e443abd
Source-Link: https://github.com/googleapis/googleapis/commit/c6af392b613b435757358fac555628d84e443abd
Source-Link: googleapis/googleapis-gen@7849764
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNzg0OTc2NDc3NzYyZDk4YTljMzA4MzRkYzQ1ODVkODE1YWYyZmJmYiJ9`,
Source-Link: googleapis/googleapis-gen@7849764
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNzg0OTc2NDc3NzYyZDk4YTljMzA4MzRkYzQ1ODVkODE1YWYyZmJmYiJ9`,
want: "feat(dialogflow/cx): added JSON_PACKAGE field to ExportAgentRequest",
want1: `- [ ] Regenerate this pull request now.
PiperOrigin-RevId: 504031208
PiperOrigin-RevId: 504031208
Source-Link: https://github.com/googleapis/googleapis/commit/c6af392b613b435757358fac555628d84e443abd
Source-Link: googleapis/googleapis-gen@7849764
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNzg0OTc2NDc3NzYyZDk4YTljMzA4MzRkYzQ1ODVkODE1YWYyZmJmYiJ9`,
},
{
name: "test add commit delimiters",
title: "feat: Adds named reservation to InstancePolicy",
body: `- [ ] Regenerate this pull request now.
---
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
---
docs: update the job id format requirement
PiperOrigin-RevId: 489502315
Source-Link: https://togithub.com/googleapis/googleapis/commit/db1cc1139fe0def1e87ead1fffbc5bedbeccb887
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/fcc564ef064c7dff31d7970e12318ad084703ac6
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZjMxZDc5NzBlMTIzMThhZDA4NDcwM2FjNiJ9
feat: Adds named reservation to InstancePolicy
---
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
---
docs: update the job id format requirement
PiperOrigin-RevId: 489501779
Source-Link: https://togithub.com/googleapis/googleapis/commit/488a4bdeebf9c7f505f48bed23f0b95fcbbec0bb
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/5b3d3a550015e9367ad13ee5f9febe0c3f84cf33
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiNWIzZDNhNTUwMDE1ZTkzNjdhZDEzZWU1ZjlmZWJlMGMzZjg0Y2YzMyJ9`,
want: "feat(batch): Adds named reservation to InstancePolicy",
want1: `- [ ] Regenerate this pull request now.
---
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
---
docs: update the job id format requirement
PiperOrigin-RevId: 489502315
Source-Link: https://togithub.com/googleapis/googleapis/commit/db1cc1139fe0def1e87ead1fffbc5bedbeccb887
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/fcc564ef064c7dff31d7970e12318ad084703ac6
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZjMxZDc5NzBlMTIzMThhZDA4NDcwM2FjNiJ9
BEGIN_NESTED_COMMIT
feat: Adds named reservation to InstancePolicy
---
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
---
docs: update the job id format requirement
PiperOrigin-RevId: 489501779
Source-Link: https://togithub.com/googleapis/googleapis/commit/488a4bdeebf9c7f505f48bed23f0b95fcbbec0bb
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/5b3d3a550015e9367ad13ee5f9febe0c3f84cf33
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiNWIzZDNhNTUwMDE1ZTkzNjdhZDEzZWU1ZjlmZWJlMGMzZjg0Y2YzMyJ9
END_NESTED_COMMIT`,
},
{
name: "test separate multiple commits in delimiters",
title: "feat: Adds named reservation to InstancePolicy",
body: `- [ ] Regenerate this pull request now.
---
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
---
docs: update the job id format requirement
PiperOrigin-RevId: 489502315
Source-Link: https://togithub.com/googleapis/googleapis/commit/db1cc1139fe0def1e87ead1fffbc5bedbeccb887
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/fcc564ef064c7dff31d7970e12318ad084703ac6
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZjMxZDc5NzBlMTIzMThhZDA4NDcwM2FjNiJ9
BEGIN_NESTED_COMMIT
feat: Adds named reservation to InstancePolicy
---
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
---
docs: update the job id format requirement
PiperOrigin-RevId: 489501779
Source-Link: https://togithub.com/googleapis/googleapis/commit/488a4bdeebf9c7f505f48bed23f0b95fcbbec0bb
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/5b3d3a550015e9367ad13ee5f9febe0c3f84cf33
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiNWIzZDNhNTUwMDE1ZTkzNjdhZDEzZWU1ZjlmZWJlMGMzZjg0Y2YzMyJ9
feat: Adds named reservation to InstancePolicy
---
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
---
docs: update the job id format requirement
PiperOrigin-RevId: 489501779
Source-Link: https://togithub.com/googleapis/googleapis/commit/488a4bdeebf9c7f505f48bed23f0b95fcbbec0bb
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/5b3d3a550015e9367ad13ee5f9febe0c3f84cf33
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiNWIzZDNhNTUwMDE1ZTkzNjdhZDEzZWU1ZjlmZWJlMGMzZjg0Y2YzMyJ9
END_NESTED_COMMIT`,
want: "feat(batch): Adds named reservation to InstancePolicy",
want1: `- [ ] Regenerate this pull request now.
---
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
---
docs: update the job id format requirement
PiperOrigin-RevId: 489502315
Source-Link: https://togithub.com/googleapis/googleapis/commit/db1cc1139fe0def1e87ead1fffbc5bedbeccb887
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/fcc564ef064c7dff31d7970e12318ad084703ac6
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZjMxZDc5NzBlMTIzMThhZDA4NDcwM2FjNiJ9
BEGIN_NESTED_COMMIT
feat: Adds named reservation to InstancePolicy
---
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
---
docs: update the job id format requirement
PiperOrigin-RevId: 489501779
Source-Link: https://github.com/googleapis/googleapis/commit/c6af392b613b435757358fac555628d84e443abd
Source-Link: googleapis/googleapis-gen@7849764
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNzg0OTc2NDc3NzYyZDk4YTljMzA4MzRkYzQ1ODVkODE1YWYyZmJmYiJ9`,
Source-Link: https://togithub.com/googleapis/googleapis/commit/488a4bdeebf9c7f505f48bed23f0b95fcbbec0bb
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/5b3d3a550015e9367ad13ee5f9febe0c3f84cf33
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiNWIzZDNhNTUwMDE1ZTkzNjdhZDEzZWU1ZjlmZWJlMGMzZjg0Y2YzMyJ9
END_NESTED_COMMIT
BEGIN_NESTED_COMMIT
feat: Adds named reservation to InstancePolicy
---
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
---
docs: update the job id format requirement
PiperOrigin-RevId: 489501779
Source-Link: https://togithub.com/googleapis/googleapis/commit/488a4bdeebf9c7f505f48bed23f0b95fcbbec0bb
Source-Link: https://togithub.com/googleapis/googleapis-gen/commit/5b3d3a550015e9367ad13ee5f9febe0c3f84cf33
Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiNWIzZDNhNTUwMDE1ZTkzNjdhZDEzZWU1ZjlmZWJlMGMzZjg0Y2YzMyJ9
END_NESTED_COMMIT`,
},
}
for _, tt := range tests {
Expand All @@ -156,11 +313,8 @@ END_NESTED_COMMIT`,
t.Errorf("processCommit() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("processCommit() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("processCommit() got1 = %v, want %v", got1, tt.want1)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("processCommit() mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(tt.want1, got1); diff != "" {
t.Errorf("processCommit() mismatch (-want +got):\n%s", diff)
Expand Down

0 comments on commit f7fe4f6

Please sign in to comment.