Skip to content

Commit

Permalink
fix(internal/postprocessor): add scopes to all appropriate commit lin…
Browse files Browse the repository at this point in the history
…es (#10192)
  • Loading branch information
noahdietz committed May 16, 2024
1 parent c1cffb6 commit c21399b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
45 changes: 42 additions & 3 deletions internal/postprocessor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ const (
var (
// hashFromLinePattern grabs the hash from the end of a github commit URL
hashFromLinePattern = regexp.MustCompile(`.*/(?P<hash>[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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/postprocessor/testdata/add-commit-delimiters.output
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions internal/postprocessor/testdata/nested-commits.output
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit c21399b

Please sign in to comment.