From 66dd2f0dab7369ab227b01aa76eed237ab3d3b60 Mon Sep 17 00:00:00 2001 From: Georgie Mathews <13368542+georgiemathews@users.noreply.github.com> Date: Fri, 8 Dec 2023 12:00:35 -0800 Subject: [PATCH] adds option to show pr titles in stack description --- config/config.go | 2 + config/config_test.go | 1 + github/githubclient/client.go | 21 +++++++---- github/githubclient/client_test.go | 59 +++++++++++++++++++++++++++++- readme.md | 1 + 5 files changed, 76 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index e6dd20b..dbb9799 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { diff --git a/config/config_test.go b/config/config_test.go index eb72225..62553dd 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -33,6 +33,7 @@ func TestDefaultConfig(t *testing.T) { PRTemplatePath: "", PRTemplateInsertStart: "", PRTemplateInsertEnd: "", + ShowPrTitlesInStack: false, }, User: &UserConfig{ ShowPRLink: true, diff --git a/github/githubclient/client.go b/github/githubclient/client.go index 5389b83..166efc6 100644 --- a/github/githubclient/client.go +++ b/github/githubclient/client.go @@ -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 { @@ -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 @@ -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 @@ -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 { diff --git a/github/githubclient/client_test.go b/github/githubclient/client_test.go index 5511683..ed8f4e5 100644 --- a/github/githubclient/client_test.go +++ b/github/githubclient/client_test.go @@ -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) } diff --git a/readme.md b/readme.md index 3380bbe..316bd49 100644 --- a/readme.md +++ b/readme.md @@ -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 |