Skip to content

Commit

Permalink
adds option to show pr titles in stack description
Browse files Browse the repository at this point in the history
  • Loading branch information
georgiemathews authored and ejoffe committed Dec 11, 2023
1 parent f1139b3 commit 66dd2f0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type RepoConfig struct {
MergeCheck string `yaml:"mergeCheck,omitempty"`

ForceFetchTags bool `default:"false" yaml:"forceFetchTags"`

ShowPrTitlesInStack bool `default:"false" yaml:"showPrTitlesInStack"`
}

type UserConfig struct {
Expand Down
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestDefaultConfig(t *testing.T) {
PRTemplatePath: "",
PRTemplateInsertStart: "",
PRTemplateInsertEnd: "",
ShowPrTitlesInStack: false,
},
User: &UserConfig{
ShowPRLink: true,
Expand Down
21 changes: 14 additions & 7 deletions github/githubclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func (c *client) CreatePullRequest(ctx context.Context, gitcmd git.GitInterface,
Str("FromBranch", headRefName).Str("ToBranch", baseRefName).
Msg("CreatePullRequest")

body := formatBody(commit, info.PullRequests)
body := formatBody(commit, info.PullRequests, c.config.Repo.ShowPrTitlesInStack)
if c.config.Repo.PRTemplatePath != "" {
pullRequestTemplate, err := readPRTemplate(gitcmd, c.config.Repo.PRTemplatePath)
if err != nil {
Expand Down Expand Up @@ -425,7 +425,7 @@ func (c *client) CreatePullRequest(ctx context.Context, gitcmd git.GitInterface,
return pr
}

func formatStackMarkdown(commit git.Commit, stack []*github.PullRequest) string {
func formatStackMarkdown(commit git.Commit, stack []*github.PullRequest, showPrTitlesInStack bool) string {
var buf bytes.Buffer
for i := len(stack) - 1; i >= 0; i-- {
isCurrent := stack[i].Commit == commit
Expand All @@ -435,25 +435,32 @@ func formatStackMarkdown(commit git.Commit, stack []*github.PullRequest) string
} else {
suffix = ""
}
buf.WriteString(fmt.Sprintf("- #%d%s\n", stack[i].Number, suffix))
var prTitle string
if showPrTitlesInStack {
prTitle = fmt.Sprintf("%s ", stack[i].Title)
} else {
prTitle = ""
}

buf.WriteString(fmt.Sprintf("- %s#%d%s\n", prTitle, stack[i].Number, suffix))
}

return buf.String()
}

func formatBody(commit git.Commit, stack []*github.PullRequest) string {
func formatBody(commit git.Commit, stack []*github.PullRequest, showPrTitlesInStack bool) string {
if len(stack) <= 1 {
return strings.TrimSpace(commit.Body)
}

if commit.Body == "" {
return fmt.Sprintf("**Stack**:\n%s",
addManualMergeNotice(formatStackMarkdown(commit, stack)))
addManualMergeNotice(formatStackMarkdown(commit, stack, showPrTitlesInStack)))
}

return fmt.Sprintf("%s\n\n---\n\n**Stack**:\n%s",
commit.Body,
addManualMergeNotice(formatStackMarkdown(commit, stack)))
addManualMergeNotice(formatStackMarkdown(commit, stack, showPrTitlesInStack)))
}

// Reads the specified PR template file and returns it as a string
Expand Down Expand Up @@ -539,7 +546,7 @@ func (c *client) UpdatePullRequest(ctx context.Context, gitcmd git.GitInterface,
Str("FromBranch", pr.FromBranch).Str("ToBranch", baseRefName).
Interface("PR", pr).Msg("UpdatePullRequest")

body := formatBody(commit, pullRequests)
body := formatBody(commit, pullRequests, c.config.Repo.ShowPrTitlesInStack)
if c.config.Repo.PRTemplatePath != "" {
pullRequestTemplate, err := readPRTemplate(gitcmd, c.config.Repo.PRTemplatePath)
if err != nil {
Expand Down
59 changes: 58 additions & 1 deletion github/githubclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,64 @@ It even includes some **markdown** formatting.
}

for _, tc := range tests {
body := formatBody(tc.commit, tc.stack)
body := formatBody(tc.commit, tc.stack, false)
if body != tc.description {
t.Fatalf("expected: '%v', actual: '%v'", tc.description, body)
}
}
}

func TestFormatPullRequestBody_ShowPrTitle(t *testing.T) {
simpleCommit := git.Commit{
CommitID: "abc123",
CommitHash: "abcdef123456",
}
descriptiveCommit := git.Commit{
CommitID: "def456",
CommitHash: "ghijkl7890",
Body: `This body describes my nice PR.
It even includes some **markdown** formatting.`}

tests := []struct {
description string
commit git.Commit
stack []*github.PullRequest
}{
{
description: "",
commit: git.Commit{},
stack: []*github.PullRequest{},
},
{
description: `This body describes my nice PR.
It even includes some **markdown** formatting.`,
commit: descriptiveCommit,
stack: []*github.PullRequest{
{Number: 2, Commit: descriptiveCommit},
},
},
{
description: `This body describes my nice PR.
It even includes some **markdown** formatting.
---
**Stack**:
- Title B #2 ⬅
- Title A #1
⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.*`,
commit: descriptiveCommit,
stack: []*github.PullRequest{
{Number: 1, Commit: simpleCommit, Title: "Title A"},
{Number: 2, Commit: descriptiveCommit, Title: "Title B"},
},
},
}

for _, tc := range tests {
body := formatBody(tc.commit, tc.stack, true)
if body != tc.description {
t.Fatalf("expected: '%v', actual: '%v'", tc.description, body)
}
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ User specific configuration is saved to .spr.yml in the user home directory.
| mergeCheck | str | | enforce a pre-merge check using 'git spr check' |
| forceFetchTags | bool | false | also fetch tags when running 'git spr update' |
| branchNameIncludeTarget | bool | false | include target branch name in pull request branch name |
| showPrTitlesInStack | bool | false | show PR titles in stack description within pull request body |


| User Config | Type | Default | Description |
Expand Down

0 comments on commit 66dd2f0

Please sign in to comment.