Skip to content

Commit c21399b

Browse files
authored
fix(internal/postprocessor): add scopes to all appropriate commit lines (#10192)
1 parent c1cffb6 commit c21399b

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

internal/postprocessor/main.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ const (
5151
var (
5252
// hashFromLinePattern grabs the hash from the end of a github commit URL
5353
hashFromLinePattern = regexp.MustCompile(`.*/(?P<hash>[a-zA-Z0-9]*).*`)
54+
55+
// conventionalCommitTypes to look out for in multi-line commit blocks.
56+
// Pulled from: https://github.com/googleapis/release-please/blob/656b9a9ad1ec77853d16ae1f40e63c4da1e12f0f/src/strategies/go-yoshi.ts#L25-L37
57+
conventionalCommitTypes = map[string]bool{
58+
"feat": true,
59+
"fix": true,
60+
"perf": true,
61+
"revert": true,
62+
"docs": true,
63+
"style": true,
64+
"chore": true,
65+
"refactor": true,
66+
"test": true,
67+
"build": true,
68+
"ci": true,
69+
}
5470
)
5571

5672
var (
@@ -497,7 +513,7 @@ func (p *postProcessor) processCommit(title, body string) (string, string, error
497513
}
498514
}
499515

500-
// Add scope to each commit
516+
// Add scope to each commit and every nested commit therein.
501517
for commitIndex, commit := range commitsSlice {
502518
commitLines := strings.Split(strings.TrimSpace(commit), "\n")
503519
var currTitle string
@@ -528,13 +544,34 @@ func (p *postProcessor) processCommit(title, body string) (string, string, error
528544
scope = scopes[0]
529545
}
530546

531-
newCommitTitle := updateCommitTitle(currTitle, scope)
547+
newCommitTitle := updateCommit(currTitle, scope)
532548
if newTitle == "" {
533549
newTitle = newCommitTitle
534550
} else {
535551
newBody.WriteString(fmt.Sprintf("%v\n", newCommitTitle))
536552
}
537553

554+
for i, line := range commitLines {
555+
if !strings.Contains(line, ":") {
556+
// couldn't be a conventional commit line
557+
continue
558+
}
559+
commitType := line[:strings.Index(line, ":")]
560+
if strings.Contains(commitType, "(") {
561+
// if it has a scope, remove it - updateCommitTitle does
562+
// already, we want to force our own scope.
563+
commitType = commitType[:strings.Index(commitType, "(")]
564+
}
565+
566+
// always trim any potential bang
567+
commitType = strings.TrimSuffix(commitType, "!")
568+
569+
if _, ok := conventionalCommitTypes[commitType]; !ok {
570+
// not a known conventional commit type, ignore
571+
continue
572+
}
573+
commitLines[i] = updateCommit(line, scope)
574+
}
538575
newBody.WriteString(strings.Join(commitLines, "\n"))
539576
if commitIndex != 0 {
540577
newBody.WriteString(fmt.Sprintf("\n%v", endNestedCommitDelimiter))
@@ -607,7 +644,7 @@ func extractHashFromLine(line string) string {
607644
return hashVal
608645
}
609646

610-
func updateCommitTitle(title, titlePkg string) string {
647+
func updateCommit(title, titlePkg string) string {
611648
var breakChangeIndicator string
612649
titleParts := strings.Split(title, ":")
613650
commitPrefix := titleParts[0]
@@ -619,6 +656,8 @@ func updateCommitTitle(title, titlePkg string) string {
619656
}
620657
if strings.HasSuffix(commitPrefix, "!") {
621658
breakChangeIndicator = "!"
659+
// trim it so we don't dupe it, but put it back in the right place
660+
commitPrefix = strings.TrimSuffix(commitPrefix, "!")
622661
}
623662
if titlePkg == "" {
624663
return fmt.Sprintf("%v%v: %v", commitPrefix, breakChangeIndicator, msg)

internal/postprocessor/testdata/add-commit-delimiters.output

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
- [ ] Regenerate this pull request now.
22

33
---
4-
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
4+
docs(batch): Remove "not yet implemented" for Accelerator & Refine Volume API docs
55

66
---
7-
docs: update the job id format requirement
7+
docs(batch): update the job id format requirement
88

99
PiperOrigin-RevId: 489502315
1010

@@ -15,7 +15,7 @@ Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZj
1515
BEGIN_NESTED_COMMIT
1616
feat: Adds named reservation to InstancePolicy
1717
---
18-
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
18+
docs: Remove "not yet implemented" for Accelerator & Refine Volume API docs
1919

2020
---
2121
docs: update the job id format requirement

internal/postprocessor/testdata/nested-commits.output

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
- [ ] Regenerate this pull request now.
22

33
---
4-
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
4+
docs(batch): Remove "not yet implemented" for Accelerator & Refine Volume API docs
55

66
---
7-
docs: update the job id format requirement
7+
docs(batch): update the job id format requirement
88

99
PiperOrigin-RevId: 489502315
1010

@@ -15,7 +15,7 @@ Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZj
1515
BEGIN_NESTED_COMMIT
1616
feat: Adds named reservation to InstancePolicy
1717
---
18-
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
18+
docs: Remove "not yet implemented" for Accelerator & Refine Volume API docs
1919

2020
---
2121
docs: update the job id format requirement

internal/postprocessor/testdata/separate-multiple-commits.output

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
- [ ] Regenerate this pull request now.
22

33
---
4-
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
4+
docs(batch): Remove "not yet implemented" for Accelerator & Refine Volume API docs
55

66
---
7-
docs: update the job id format requirement
7+
docs(batch): update the job id format requirement
88

99
PiperOrigin-RevId: 489502315
1010

@@ -15,7 +15,7 @@ Copy-Tag: eyJwIjoiamF2YS1iYXRjaC8uT3dsQm90LnlhbWwiLCJoIjoiZmNjNTY0ZWYwNjRjN2RmZj
1515
BEGIN_NESTED_COMMIT
1616
feat: Adds named reservation to InstancePolicy
1717
---
18-
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
18+
docs: Remove "not yet implemented" for Accelerator & Refine Volume API docs
1919

2020
---
2121
docs: update the job id format requirement
@@ -30,7 +30,7 @@ END_NESTED_COMMIT
3030
BEGIN_NESTED_COMMIT
3131
feat: Adds named reservation to InstancePolicy
3232
---
33-
docs:Remove "not yet implemented" for Accelerator & Refine Volume API docs
33+
docs: Remove "not yet implemented" for Accelerator & Refine Volume API docs
3434

3535
---
3636
docs: update the job id format requirement

internal/postprocessor/testdata/separate-multiple-commits2.output

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
docs: Remove "not yet implemented" for Accelerator & Refine Volume API docs
2-
docs: update the job id format requirement
1+
docs(batch): Remove "not yet implemented" for Accelerator & Refine Volume API docs
2+
docs(batch): update the job id format requirement
33

44
PiperOrigin-RevId: 489502315
55

0 commit comments

Comments
 (0)