Skip to content

Commit 0706afe

Browse files
authored
Merge pull request #30 from github/feat/autoclose
Implement Autoclose Feature
2 parents 948a4ac + 083f2e8 commit 0706afe

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ By using the `--minimum` flag you can require a minimum number of pull requests
126126
gh combine owner/repo --minimum 3
127127
```
128128

129+
### Do Not Auto-Close Linked PRs
130+
131+
By default, the source pull requests that are combined into the new pull request will be automatically closed when the combined Pr merges by adding the `closes` keyword to the new pull request. This can be disabled by using the `--no-autoclose` flag.
132+
133+
```bash
134+
gh combine owner/repo --no-autoclose
135+
```
136+
129137
### Only Combine Pull Requests that match a given Label(s)
130138

131139
```bash

internal/cmd/combine_prs.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ func CombinePRsWithStats(ctx context.Context, graphQlClient *api.GraphQLClient,
7171
}
7272
}
7373

74+
combinedPrNumbers := []string{}
7475
for _, pr := range opts.Pulls {
7576
if opts.Noop {
7677
Logger.Debug("Simulating merge of branch", "branch", pr.Head.Ref)
7778
combined = append(combined, fmt.Sprintf("#%d - %s", pr.Number, pr.Title))
79+
combinedPrNumbers = append(combinedPrNumbers, fmt.Sprintf("#%d", pr.Number))
7880
} else {
7981
err := mergeBranch(ctx, restClient, opts.Repo, workingBranchName, pr.Head.Ref)
8082
if err != nil {
@@ -87,6 +89,7 @@ func CombinePRsWithStats(ctx context.Context, graphQlClient *api.GraphQLClient,
8789
} else {
8890
Logger.Debug("Merged branch", "branch", pr.Head.Ref)
8991
combined = append(combined, fmt.Sprintf("#%d - %s", pr.Number, pr.Title))
92+
combinedPrNumbers = append(combinedPrNumbers, fmt.Sprintf("#%d", pr.Number))
9093
}
9194
}
9295
}
@@ -102,7 +105,7 @@ func CombinePRsWithStats(ctx context.Context, graphQlClient *api.GraphQLClient,
102105
Logger.Warn("Failed to delete working branch", "branch", workingBranchName, "error", err)
103106
}
104107

105-
prBody := generatePRBody(combined, mergeConflicts, opts.Command)
108+
prBody := generatePRBody(combinedPrNumbers, mergeConflicts, opts.Command)
106109
prTitle := "Combined PRs"
107110
prNumber, prErr := createPullRequestWithNumber(ctx, restClient, opts.Repo, prTitle, combineBranchName, repoDefaultBranch, prBody, addLabels, addAssignees)
108111
if prErr != nil {
@@ -200,12 +203,19 @@ func getBranchSHA(ctx context.Context, client RESTClientInterface, repo github.R
200203
return ref.Object.SHA, nil
201204
}
202205

203-
// Updated generatePRBody to include the command used
204-
func generatePRBody(combinedPRs, mergeFailedPRs []string, command string) string {
206+
// Updated generatePRBody to include the command used and handle PR autoclose logic
207+
// combinedPrNumbers looks like ["#1", "#2"]
208+
// mergeFailedPRs looks like ["#3", "#4"]
209+
func generatePRBody(combinedPrNumbers []string, mergeFailedPRs []string, command string) string {
205210
body := "✅ The following pull requests have been successfully combined:\n"
206-
for _, pr := range combinedPRs {
207-
body += "- " + pr + "\n"
211+
for _, prNumber := range combinedPrNumbers {
212+
prRef := prNumber
213+
if !noAutoclose {
214+
prRef = "closes: " + prNumber
215+
}
216+
body += "- " + prRef + "\n"
208217
}
218+
209219
if len(mergeFailedPRs) > 0 {
210220
body += "\n⚠️ The following pull requests could not be merged due to conflicts:\n"
211221
for _, pr := range mergeFailedPRs {

internal/cmd/root.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var (
2727

2828
requireCI bool
2929
mustBeApproved bool
30-
autoclose bool
30+
noAutoclose bool
3131
updateBranch bool
3232
reposFile string
3333
minimum int
@@ -116,7 +116,7 @@ func NewRootCmd() *cobra.Command {
116116
117117
# Additional options
118118
gh combine owner/repo --dry-run # Simulate the actions without making any changes
119-
gh combine owner/repo --autoclose # Close source PRs when combined PR is merged
119+
gh combine owner/repo --no-autoclose # Do not auto-close source PRs when combined PR is merged via the closes keyword
120120
gh combine owner/repo --base-branch main # Use a different base branch for the combined PR
121121
gh combine owner/repo --no-color # Disable color output
122122
gh combine owner/repo --no-stats # Disable stats summary display
@@ -145,7 +145,7 @@ func NewRootCmd() *cobra.Command {
145145
rootCmd.Flags().BoolVar(&requireCI, "require-ci", false, "Only include PRs with passing CI checks")
146146
rootCmd.Flags().BoolVar(&dependabot, "dependabot", false, "Only include PRs with the dependabot branch prefix")
147147
rootCmd.Flags().BoolVar(&mustBeApproved, "require-approved", false, "Only include PRs that have been approved")
148-
rootCmd.Flags().BoolVar(&autoclose, "autoclose", false, "Close source PRs when combined PR is merged")
148+
rootCmd.Flags().BoolVar(&noAutoclose, "no-autoclose", false, "Do not auto-close source PRs when combined PR is merged")
149149
rootCmd.Flags().BoolVar(&updateBranch, "update-branch", false, "Update the branch of the combined PR if possible")
150150
rootCmd.Flags().StringVar(&baseBranch, "base-branch", "main", "Base branch for the combined PR (default: main)")
151151
rootCmd.Flags().StringVar(&combineBranchName, "combine-branch-name", "combined-prs", "Name of the combined PR branch")
@@ -454,8 +454,8 @@ func buildCommandString(args []string) string {
454454
if mustBeApproved {
455455
cmd = append(cmd, "--require-approved")
456456
}
457-
if autoclose {
458-
cmd = append(cmd, "--autoclose")
457+
if noAutoclose {
458+
cmd = append(cmd, "--no-autoclose")
459459
}
460460
if updateBranch {
461461
cmd = append(cmd, "--update-branch")

script/test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ set -e
66
if ! command -v tparse &> /dev/null; then
77
go test -v -cover ./...
88
else
9-
set -o pipefail && go test -json ./... | tparse -all -trimpath github.com/github/
9+
set -o pipefail && go test -cover -coverprofile=coverage.out -json ./... | tparse -all -trimpath github.com/github/
1010
fi

0 commit comments

Comments
 (0)