Skip to content

Commit

Permalink
chore(internal/postprocessor): some refactoring and fixes (#7477)
Browse files Browse the repository at this point in the history
- Updates tests to read from testdata files
- Fixing some formatting issues that could arise in updated commit messages
- Fixes linked bug
- Updates lockfile to use this code

Fixes: #7474
  • Loading branch information
codyoss authored Feb 23, 2023
1 parent 91aafec commit fa6bbe5
Show file tree
Hide file tree
Showing 13 changed files with 347 additions and 273 deletions.
2 changes: 1 addition & 1 deletion .github/.OwlBot.lock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.
docker:
image: gcr.io/cloud-devrel-public-resources/owlbot-go:latest
digest: sha256:9992ddd2fa2507963c42fa000c9b7194a342cccc5130b137e0c791158b85a791
digest: sha256:6eb2183b6e08c8e73e66f5c3a409f1d2a97235bc56349355a7e9d5dd72e02b1e
47 changes: 22 additions & 25 deletions internal/postprocessor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ func contains(s []string, str string) bool {
}

func (c *config) processCommit(title, body string) (string, string, error) {
var newPRTitle string
var newPRBodySlice []string
var newTitle string
var newBody strings.Builder
var commitsSlice []string
startCommitIndex := 0

Expand All @@ -570,8 +570,7 @@ func (c *config) processCommit(title, body string) (string, string, error) {
} else {
currTitle = commitLines[0]
commitLines = commitLines[1:]
newPRBodySlice = append(newPRBodySlice, "")
newPRBodySlice = append(newPRBodySlice, beginNestedCommitDelimiter)
newBody.WriteString(fmt.Sprintf("\n%v\n", beginNestedCommitDelimiter))
}
for _, line := range commitLines {
// When OwlBot generates the commit body, after commit titles it provides 'Source-Link's.
Expand All @@ -580,6 +579,9 @@ func (c *config) processCommit(title, body string) (string, string, error) {
if strings.Contains(line, "googleapis/googleapis/") {
hash := extractHashFromLine(line)
scopes, err := c.getScopesFromGoogleapisCommitHash(hash)
if err != nil {
return "", "", err
}
for _, scope := range scopes {
if !contains(c.modules, scope) {
c.modules = append(c.modules, scope)
Expand All @@ -589,20 +591,17 @@ func (c *config) processCommit(title, body string) (string, string, error) {
if len(scopes) == 1 {
scope = scopes[0]
}
if err != nil {
return "", "", err
}

newCommitTitle := updateCommitTitle(currTitle, scope)
if newPRTitle == "" {
newPRTitle = newCommitTitle
if newTitle == "" {
newTitle = newCommitTitle
} else {
newPRBodySlice = append(newPRBodySlice, newCommitTitle)
newBody.WriteString(fmt.Sprintf("%v\n", newCommitTitle))
}

newPRBodySlice = append(newPRBodySlice, commitLines...)
newBody.WriteString(strings.Join(commitLines, "\n"))
if commitIndex != 0 {
newPRBodySlice = append(newPRBodySlice, endNestedCommitDelimiter)
newBody.WriteString(fmt.Sprintf("\n%v", endNestedCommitDelimiter))
}
}
}
Expand All @@ -611,8 +610,7 @@ func (c *config) processCommit(title, body string) (string, string, error) {
c.modules = []string{}
c.modules = append(c.modules, moduleConfigs...)
}
newPRBody := strings.Join(newPRBodySlice, "\n")
return newPRTitle, newPRBody, nil
return newTitle, newBody.String(), nil
}

func (c *config) getPR(ctx context.Context) (*github.PullRequest, error) {
Expand Down Expand Up @@ -686,23 +684,22 @@ func extractHashFromLine(line string) string {
}

func updateCommitTitle(title, titlePkg string) string {
var newTitle string
var breakChangeIndicator string
titleParts := strings.Split(title, ":")
commitPrefix := titleParts[0]
msg := strings.TrimSpace(titleParts[1])

titleSlice := strings.Split(title, ":")
firstTitlePart := titleSlice[0]
secondTitlePart := strings.TrimSpace(titleSlice[1])

if strings.HasSuffix(firstTitlePart, "!") {
// If a scope is already provided, remove it.
if i := strings.Index(commitPrefix, "("); i > 0 {
commitPrefix = commitPrefix[:i]
}
if strings.HasSuffix(commitPrefix, "!") {
breakChangeIndicator = "!"
}
if titlePkg == "" {
newTitle = fmt.Sprintf("%v%v: %v", firstTitlePart, breakChangeIndicator, secondTitlePart)
return newTitle
return fmt.Sprintf("%v%v: %v", commitPrefix, breakChangeIndicator, msg)
}
newTitle = fmt.Sprintf("%v(%v)%v: %v", firstTitlePart, titlePkg, breakChangeIndicator, secondTitlePart)

return newTitle
return fmt.Sprintf("%v(%v)%v: %v", commitPrefix, titlePkg, breakChangeIndicator, msg)
}

// WritePRInfoToFile uses OwlBot env variable specified path to write updated
Expand Down
Loading

0 comments on commit fa6bbe5

Please sign in to comment.