From c21399bdc362c6c646c2c0f8c2c55903898e0eab Mon Sep 17 00:00:00 2001 From: Noah Dietz Date: Thu, 16 May 2024 09:28:30 -0700 Subject: [PATCH] fix(internal/postprocessor): add scopes to all appropriate commit lines (#10192) --- internal/postprocessor/main.go | 45 +++++++++++++++++-- .../testdata/add-commit-delimiters.output | 6 +-- .../testdata/nested-commits.output | 6 +-- .../testdata/separate-multiple-commits.output | 8 ++-- .../separate-multiple-commits2.output | 4 +- 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/internal/postprocessor/main.go b/internal/postprocessor/main.go index 9d68021ef5b7..9ffba45afc15 100644 --- a/internal/postprocessor/main.go +++ b/internal/postprocessor/main.go @@ -51,6 +51,22 @@ const ( var ( // hashFromLinePattern grabs the hash from the end of a github commit URL hashFromLinePattern = regexp.MustCompile(`.*/(?P[a-zA-Z0-9]*).*`) + + // conventionalCommitTypes to look out for in multi-line commit blocks. + // Pulled from: https://github.com/googleapis/release-please/blob/656b9a9ad1ec77853d16ae1f40e63c4da1e12f0f/src/strategies/go-yoshi.ts#L25-L37 + conventionalCommitTypes = map[string]bool{ + "feat": true, + "fix": true, + "perf": true, + "revert": true, + "docs": true, + "style": true, + "chore": true, + "refactor": true, + "test": true, + "build": true, + "ci": true, + } ) var ( @@ -497,7 +513,7 @@ func (p *postProcessor) processCommit(title, body string) (string, string, error } } - // Add scope to each commit + // Add scope to each commit and every nested commit therein. for commitIndex, commit := range commitsSlice { commitLines := strings.Split(strings.TrimSpace(commit), "\n") var currTitle string @@ -528,13 +544,34 @@ func (p *postProcessor) processCommit(title, body string) (string, string, error scope = scopes[0] } - newCommitTitle := updateCommitTitle(currTitle, scope) + newCommitTitle := updateCommit(currTitle, scope) if newTitle == "" { newTitle = newCommitTitle } else { newBody.WriteString(fmt.Sprintf("%v\n", newCommitTitle)) } + for i, line := range commitLines { + if !strings.Contains(line, ":") { + // couldn't be a conventional commit line + continue + } + commitType := line[:strings.Index(line, ":")] + if strings.Contains(commitType, "(") { + // if it has a scope, remove it - updateCommitTitle does + // already, we want to force our own scope. + commitType = commitType[:strings.Index(commitType, "(")] + } + + // always trim any potential bang + commitType = strings.TrimSuffix(commitType, "!") + + if _, ok := conventionalCommitTypes[commitType]; !ok { + // not a known conventional commit type, ignore + continue + } + commitLines[i] = updateCommit(line, scope) + } newBody.WriteString(strings.Join(commitLines, "\n")) if commitIndex != 0 { newBody.WriteString(fmt.Sprintf("\n%v", endNestedCommitDelimiter)) @@ -607,7 +644,7 @@ func extractHashFromLine(line string) string { return hashVal } -func updateCommitTitle(title, titlePkg string) string { +func updateCommit(title, titlePkg string) string { var breakChangeIndicator string titleParts := strings.Split(title, ":") commitPrefix := titleParts[0] @@ -619,6 +656,8 @@ func updateCommitTitle(title, titlePkg string) string { } if strings.HasSuffix(commitPrefix, "!") { breakChangeIndicator = "!" + // trim it so we don't dupe it, but put it back in the right place + commitPrefix = strings.TrimSuffix(commitPrefix, "!") } if titlePkg == "" { return fmt.Sprintf("%v%v: %v", commitPrefix, breakChangeIndicator, msg) diff --git a/internal/postprocessor/testdata/add-commit-delimiters.output b/internal/postprocessor/testdata/add-commit-delimiters.output index 0a7506914337..9cbcc72b42c4 100644 --- a/internal/postprocessor/testdata/add-commit-delimiters.output +++ b/internal/postprocessor/testdata/add-commit-delimiters.output @@ -1,10 +1,10 @@ - [ ] Regenerate this pull request now. --- -docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs +docs(batch): Remove "not yet implemented" for Accelerator & Refine Volume API docs --- -docs: update the job id format requirement +docs(batch): update the job id format requirement PiperOrigin-RevId: 489502315 @@ -15,7 +15,7 @@ Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZj BEGIN_NESTED_COMMIT feat: Adds named reservation to InstancePolicy --- -docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs +docs: Remove "not yet implemented" for Accelerator & Refine Volume API docs --- docs: update the job id format requirement diff --git a/internal/postprocessor/testdata/nested-commits.output b/internal/postprocessor/testdata/nested-commits.output index 79a2a3b26aed..a491c9c3e98a 100644 --- a/internal/postprocessor/testdata/nested-commits.output +++ b/internal/postprocessor/testdata/nested-commits.output @@ -1,10 +1,10 @@ - [ ] Regenerate this pull request now. --- -docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs +docs(batch): Remove "not yet implemented" for Accelerator & Refine Volume API docs --- -docs: update the job id format requirement +docs(batch): update the job id format requirement PiperOrigin-RevId: 489502315 @@ -15,7 +15,7 @@ Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZj BEGIN_NESTED_COMMIT feat: Adds named reservation to InstancePolicy --- -docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs +docs: Remove "not yet implemented" for Accelerator & Refine Volume API docs --- docs: update the job id format requirement diff --git a/internal/postprocessor/testdata/separate-multiple-commits.output b/internal/postprocessor/testdata/separate-multiple-commits.output index 314da8bb6bb3..789f8aff4fe6 100644 --- a/internal/postprocessor/testdata/separate-multiple-commits.output +++ b/internal/postprocessor/testdata/separate-multiple-commits.output @@ -1,10 +1,10 @@ - [ ] Regenerate this pull request now. --- -docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs +docs(batch): Remove "not yet implemented" for Accelerator & Refine Volume API docs --- -docs: update the job id format requirement +docs(batch): update the job id format requirement PiperOrigin-RevId: 489502315 @@ -15,7 +15,7 @@ Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZj BEGIN_NESTED_COMMIT feat: Adds named reservation to InstancePolicy --- -docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs +docs: Remove "not yet implemented" for Accelerator & Refine Volume API docs --- docs: update the job id format requirement @@ -30,7 +30,7 @@ END_NESTED_COMMIT BEGIN_NESTED_COMMIT feat: Adds named reservation to InstancePolicy --- -docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs +docs: Remove "not yet implemented" for Accelerator & Refine Volume API docs --- docs: update the job id format requirement diff --git a/internal/postprocessor/testdata/separate-multiple-commits2.output b/internal/postprocessor/testdata/separate-multiple-commits2.output index 8d294513ea02..2baa1b769c6f 100644 --- a/internal/postprocessor/testdata/separate-multiple-commits2.output +++ b/internal/postprocessor/testdata/separate-multiple-commits2.output @@ -1,5 +1,5 @@ -docs: Remove "not yet implemented" for Accelerator & Refine Volume API docs -docs: update the job id format requirement +docs(batch): Remove "not yet implemented" for Accelerator & Refine Volume API docs +docs(batch): update the job id format requirement PiperOrigin-RevId: 489502315