Skip to content

Commit

Permalink
Write SPR messages to a dedicated block inside commit messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan-mehta committed Dec 28, 2024
1 parent 2c5a21f commit 88d636c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

bin:
goreleaser --snapshot --skip-publish --clean
goreleaser --snapshot --skip=publish --clean

57 changes: 54 additions & 3 deletions github/githubclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,52 @@ func addManualMergeNotice(body string) string {
"Do not merge manually using the UI - doing so may have unexpected results.*"
}

func (c *client) UpdatePullRequest(ctx context.Context, gitcmd git.GitInterface, pullRequests []*github.PullRequest, pr *github.PullRequest, commit git.Commit, prevCommit *git.Commit) {
// embedSprDescription finds or creates a section delimited by:
//
// <!-- SPR data start: please do NOT edit this section -->
// <!-- SPR data end -->
//
// Anything between those markers is overwritten by newSprContent.
// If the markers do not exist, a new section is appended.
func embedSprDescription(existingBody, newSpr string) string {
startMarker := "<!-- SPR data start: please do NOT edit this section -->"
endMarker := "<!-- SPR data end -->"

startIndex := strings.Index(existingBody, startMarker)
endIndex := strings.Index(existingBody, endMarker)

// If both markers exist, replace:
if startIndex >= 0 && endIndex > 0 {
endIndex += len(endMarker)
result := existingBody[:startIndex] +
startMarker + "\n" + newSpr + "\n" +
endMarker +
existingBody[endIndex:]
return strings.TrimRight(result, "\n")
}

// If missing, append markers
trimmed := strings.TrimRight(existingBody, "\n")
if trimmed == "" {
// empty body
result := startMarker + "\n" + newSpr + "\n" + endMarker
return strings.TrimRight(result, "\n")
}

// Non-empty body, add a blank line, then markers
result := trimmed + "\n\n" +
startMarker + "\n" + newSpr + "\n" + endMarker
return strings.TrimRight(result, "\n")
}

func (c *client) UpdatePullRequest(
ctx context.Context,
gitcmd git.GitInterface,
pullRequests []*github.PullRequest,
pr *github.PullRequest,
commit git.Commit,
prevCommit *git.Commit,
) {
if c.config.User.LogGitHubCalls {
fmt.Printf("> github update %d : %s\n", pr.Number, pr.Title)
}
Expand All @@ -548,23 +592,30 @@ func (c *client) UpdatePullRequest(ctx context.Context, gitcmd git.GitInterface,
Str("FromBranch", pr.FromBranch).Str("ToBranch", baseRefName).
Interface("PR", pr).Msg("UpdatePullRequest")

// Generate the new "spr" content that we want to embed in the PR body
body := formatBody(commit, pullRequests, c.config.Repo.ShowPrTitlesInStack)
if c.config.Repo.PRTemplatePath != "" {
pullRequestTemplate, err := readPRTemplate(gitcmd, c.config.Repo.PRTemplatePath)
if err != nil {
log.Fatal().Err(err).Msg("failed to read PR template")
}
body, err = insertBodyIntoPRTemplate(body, pullRequestTemplate, c.config.Repo, pr)
newBody, err := insertBodyIntoPRTemplate(body, pullRequestTemplate, c.config.Repo, pr)
if err != nil {
log.Fatal().Err(err).Msg("failed to insert body into PR template")
}
body = newBody
}

// Only modify the portion in the SPR markers, keep user edits outside them
existingBody := pr.Body
sprUpdatedBody := embedSprDescription(existingBody, body)

title := &commit.Subject

input := genclient.UpdatePullRequestInput{
PullRequestId: pr.ID,
Title: title,
Body: &body,
Body: &sprUpdatedBody,
}

if !pr.InQueue {
Expand Down
46 changes: 46 additions & 0 deletions github/githubclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,3 +825,49 @@ func TestInsertBodyIntoPRTemplateErrors(t *testing.T) {
})
}
}

func TestEmbedSprDescription(t *testing.T) {
existingBody := `# Some User Heading
User paragraph that should remain untouched.
<!-- SPR data start: please do NOT edit this section -->
old spr content
<!-- SPR data end -->
Another user paragraph that should remain untouched as well.
`

newSpr := `updated spr content with new data`

want := `# Some User Heading
User paragraph that should remain untouched.
<!-- SPR data start: please do NOT edit this section -->
updated spr content with new data
<!-- SPR data end -->
Another user paragraph that should remain untouched as well.`
got := embedSprDescription(existingBody, newSpr)
if got != want {
t.Fatalf("Unexpected embedSprDescription result.\nGot:\n`%s`\n\nWant:\n`%s`\n", got, want)
}

// Test if markers are missing, we append them
existingBodyNoMarkers := `# Some User Heading
No markers here.
`
wantNoMarkers := `# Some User Heading
No markers here.
<!-- SPR data start: please do NOT edit this section -->
updated spr content with new data
<!-- SPR data end -->`
gotNoMarkers := embedSprDescription(existingBodyNoMarkers, newSpr)
if gotNoMarkers != wantNoMarkers {
t.Fatalf("Unexpected embedSprDescription result when markers are missing.\nGot:\n%s\n\nWant:\n%s\n", gotNoMarkers, wantNoMarkers)
}
}

0 comments on commit 88d636c

Please sign in to comment.