Skip to content

Commit 647d13d

Browse files
authored
Merge pull request #26 from github/cmd-used
feat: display the command used on the generated combined PR
2 parents 357d2d7 + e8508a8 commit 647d13d

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

internal/cmd/combine_prs.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type RESTClientInterface interface {
2121
}
2222

2323
// CombinePRsWithStats combines PRs and returns stats for summary output
24-
func CombinePRsWithStats(ctx context.Context, graphQlClient *api.GraphQLClient, restClient RESTClientInterface, repo github.Repo, pulls github.Pulls) (combined []string, mergeConflicts []string, combinedPRLink string, err error) {
24+
func CombinePRsWithStats(ctx context.Context, graphQlClient *api.GraphQLClient, restClient RESTClientInterface, repo github.Repo, pulls github.Pulls, command string) (combined []string, mergeConflicts []string, combinedPRLink string, err error) {
2525
workingBranchName := combineBranchName + workingBranchSuffix
2626

2727
repoDefaultBranch, err := getDefaultBranch(ctx, restClient, repo)
@@ -82,7 +82,7 @@ func CombinePRsWithStats(ctx context.Context, graphQlClient *api.GraphQLClient,
8282
Logger.Warn("Failed to delete working branch", "branch", workingBranchName, "error", err)
8383
}
8484

85-
prBody := generatePRBody(combined, mergeConflicts)
85+
prBody := generatePRBody(combined, mergeConflicts, command)
8686
prTitle := "Combined PRs"
8787
prNumber, prErr := createPullRequestWithNumber(ctx, restClient, repo, prTitle, combineBranchName, repoDefaultBranch, prBody, addLabels, addAssignees)
8888
if prErr != nil {
@@ -179,8 +179,8 @@ func getBranchSHA(ctx context.Context, client RESTClientInterface, repo github.R
179179
return ref.Object.SHA, nil
180180
}
181181

182-
// generatePRBody generates the body for the combined PR
183-
func generatePRBody(combinedPRs, mergeFailedPRs []string) string {
182+
// Updated generatePRBody to include the command used
183+
func generatePRBody(combinedPRs, mergeFailedPRs []string, command string) string {
184184
body := "✅ The following pull requests have been successfully combined:\n"
185185
for _, pr := range combinedPRs {
186186
body += "- " + pr + "\n"
@@ -192,7 +192,8 @@ func generatePRBody(combinedPRs, mergeFailedPRs []string) string {
192192
}
193193
}
194194

195-
body += "\n> Generated with [gh-combine](https://github.com/github/gh-combine)"
195+
body += "\n> Generated with [gh-combine](https://github.com/github/gh-combine)\n"
196+
body += fmt.Sprintf("\nCommand used:\n\n```bash\n%s\n```", command)
196197

197198
return body
198199
}

internal/cmd/root.go

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"strings"
78
"time"
89

910
"github.com/cli/go-gh/v2/pkg/api"
@@ -350,7 +351,8 @@ func processRepository(ctx context.Context, client *api.RESTClient, graphQlClien
350351
}{client}
351352

352353
// Combine the PRs and collect stats
353-
combined, mergeConflicts, combinedPRLink, err := CombinePRsWithStats(ctx, graphQlClient, restClientWrapper, repo, matchedPRs)
354+
commandString := buildCommandString([]string{repo.String()})
355+
combined, mergeConflicts, combinedPRLink, err := CombinePRsWithStats(ctx, graphQlClient, restClientWrapper, repo, matchedPRs, commandString)
354356
if err != nil {
355357
return fmt.Errorf("failed to combine PRs: %w", err)
356358
}
@@ -420,3 +422,78 @@ func displayStatsSummary(stats *StatsCollector, outputFormat string) {
420422
displayPlainStats(stats)
421423
}
422424
}
425+
426+
// buildCommandString reconstructs the CLI command with all set flags and arguments
427+
func buildCommandString(args []string) string {
428+
cmd := []string{"gh combine"}
429+
cmd = append(cmd, args...)
430+
431+
if branchPrefix != "" {
432+
cmd = append(cmd, "--branch-prefix", branchPrefix)
433+
}
434+
if branchSuffix != "" {
435+
cmd = append(cmd, "--branch-suffix", branchSuffix)
436+
}
437+
if branchRegex != "" {
438+
cmd = append(cmd, "--branch-regex", branchRegex)
439+
}
440+
if len(selectLabels) > 0 {
441+
cmd = append(cmd, "--labels", strings.Join(selectLabels, ","))
442+
}
443+
if len(ignoreLabels) > 0 {
444+
cmd = append(cmd, "--ignore-labels", strings.Join(ignoreLabels, ","))
445+
}
446+
if len(addLabels) > 0 {
447+
cmd = append(cmd, "--add-labels", strings.Join(addLabels, ","))
448+
}
449+
if len(addAssignees) > 0 {
450+
cmd = append(cmd, "--add-assignees", strings.Join(addAssignees, ","))
451+
}
452+
if requireCI {
453+
cmd = append(cmd, "--require-ci")
454+
}
455+
if dependabot {
456+
cmd = append(cmd, "--dependabot")
457+
}
458+
if mustBeApproved {
459+
cmd = append(cmd, "--require-approved")
460+
}
461+
if autoclose {
462+
cmd = append(cmd, "--autoclose")
463+
}
464+
if updateBranch {
465+
cmd = append(cmd, "--update-branch")
466+
}
467+
if baseBranch != "main" && baseBranch != "" {
468+
cmd = append(cmd, "--base-branch", baseBranch)
469+
}
470+
if combineBranchName != "combined-prs" && combineBranchName != "" {
471+
cmd = append(cmd, "--combine-branch-name", combineBranchName)
472+
}
473+
if workingBranchSuffix != "-working" && workingBranchSuffix != "" {
474+
cmd = append(cmd, "--working-branch-suffix", workingBranchSuffix)
475+
}
476+
if reposFile != "" {
477+
cmd = append(cmd, "--file", reposFile)
478+
}
479+
if minimum != 2 {
480+
cmd = append(cmd, "--minimum", fmt.Sprintf("%d", minimum))
481+
}
482+
if defaultOwner != "" {
483+
cmd = append(cmd, "--owner", defaultOwner)
484+
}
485+
if caseSensitiveLabels {
486+
cmd = append(cmd, "--case-sensitive-labels")
487+
}
488+
if noColor {
489+
cmd = append(cmd, "--no-color")
490+
}
491+
if noStats {
492+
cmd = append(cmd, "--no-stats")
493+
}
494+
if outputFormat != "table" && outputFormat != "" {
495+
cmd = append(cmd, "--output", outputFormat)
496+
}
497+
498+
return strings.Join(cmd, " ")
499+
}

0 commit comments

Comments
 (0)