Skip to content

Commit d8eb5d6

Browse files
committed
add --dry-run flag
1 parent 9c29a67 commit d8eb5d6

File tree

2 files changed

+73
-53
lines changed

2 files changed

+73
-53
lines changed

internal/cmd/combine_prs.go

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,75 +21,90 @@ 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, command string) (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, dryRun bool) (combined []string, mergeConflicts []string, combinedPRLink string, err error) {
25+
// Move variable definitions outside the conditional blocks
2526
workingBranchName := combineBranchName + workingBranchSuffix
27+
repoDefaultBranch := "main" // Default branch assumed for simulation
2628

27-
repoDefaultBranch, err := getDefaultBranch(ctx, restClient, repo)
28-
if err != nil {
29-
return nil, nil, "", fmt.Errorf("failed to get default branch: %w", err)
29+
if dryRun {
30+
Logger.Debug("Dry-run mode enabled. No changes will be made.")
31+
Logger.Debug("Simulating branch operations", "workingBranch", workingBranchName, "defaultBranch", repoDefaultBranch)
3032
}
3133

32-
baseBranchSHA, err := getBranchSHA(ctx, restClient, repo, repoDefaultBranch)
33-
if err != nil {
34-
return nil, nil, "", fmt.Errorf("failed to get SHA of main branch: %w", err)
35-
}
36-
// Delete any pre-existing working branch
34+
// Skip branch creation and deletion if dry-run is enabled
35+
if !dryRun {
36+
repoDefaultBranch, err = getDefaultBranch(ctx, restClient, repo)
37+
if err != nil {
38+
return nil, nil, "", fmt.Errorf("failed to get default branch: %w", err)
39+
}
3740

38-
// Delete any pre-existing working branch
39-
err = deleteBranch(ctx, restClient, repo, workingBranchName)
40-
if err != nil {
41-
Logger.Debug("Working branch not found, continuing", "branch", workingBranchName)
41+
baseBranchSHA, err := getBranchSHA(ctx, restClient, repo, repoDefaultBranch)
42+
if err != nil {
43+
return nil, nil, "", fmt.Errorf("failed to get SHA of main branch: %w", err)
44+
}
4245

43-
// Delete any pre-existing combined branch
44-
}
46+
err = deleteBranch(ctx, restClient, repo, workingBranchName)
47+
if err != nil {
48+
Logger.Debug("Working branch not found, continuing", "branch", workingBranchName)
49+
}
4550

46-
// Delete any pre-existing combined branch
47-
err = deleteBranch(ctx, restClient, repo, combineBranchName)
48-
if err != nil {
49-
Logger.Debug("Combined branch not found, continuing", "branch", combineBranchName)
50-
}
51+
err = deleteBranch(ctx, restClient, repo, combineBranchName)
52+
if err != nil {
53+
Logger.Debug("Combined branch not found, continuing", "branch", combineBranchName)
54+
}
5155

52-
err = createBranch(ctx, restClient, repo, combineBranchName, baseBranchSHA)
53-
if err != nil {
54-
return nil, nil, "", fmt.Errorf("failed to create combined branch: %w", err)
55-
}
56-
err = createBranch(ctx, restClient, repo, workingBranchName, baseBranchSHA)
57-
if err != nil {
58-
return nil, nil, "", fmt.Errorf("failed to create working branch: %w", err)
56+
err = createBranch(ctx, restClient, repo, combineBranchName, baseBranchSHA)
57+
if err != nil {
58+
return nil, nil, "", fmt.Errorf("failed to create combined branch: %w", err)
59+
}
60+
61+
err = createBranch(ctx, restClient, repo, workingBranchName, baseBranchSHA)
62+
if err != nil {
63+
return nil, nil, "", fmt.Errorf("failed to create working branch: %w", err)
64+
}
5965
}
6066

67+
// Simulate merging PRs
6168
for _, pr := range pulls {
62-
err := mergeBranch(ctx, restClient, repo, workingBranchName, pr.Head.Ref)
63-
if err != nil {
64-
if isMergeConflictError(err) {
65-
Logger.Debug("Merge conflict", "branch", pr.Head.Ref, "error", err)
69+
if dryRun {
70+
Logger.Debug("Simulating merge of branch", "branch", pr.Head.Ref)
71+
combined = append(combined, fmt.Sprintf("#%d - %s", pr.Number, pr.Title))
72+
} else {
73+
err := mergeBranch(ctx, restClient, repo, workingBranchName, pr.Head.Ref)
74+
if err != nil {
75+
if isMergeConflictError(err) {
76+
Logger.Debug("Merge conflict", "branch", pr.Head.Ref, "error", err)
77+
} else {
78+
Logger.Warn("Failed to merge branch", "branch", pr.Head.Ref, "error", err)
79+
}
80+
mergeConflicts = append(mergeConflicts, fmt.Sprintf("#%d", pr.Number))
6681
} else {
67-
Logger.Warn("Failed to merge branch", "branch", pr.Head.Ref, "error", err)
82+
Logger.Debug("Merged branch", "branch", pr.Head.Ref)
83+
combined = append(combined, fmt.Sprintf("#%d - %s", pr.Number, pr.Title))
6884
}
69-
mergeConflicts = append(mergeConflicts, fmt.Sprintf("#%d", pr.Number))
70-
} else {
71-
Logger.Debug("Merged branch", "branch", pr.Head.Ref)
72-
combined = append(combined, fmt.Sprintf("#%d - %s", pr.Number, pr.Title))
7385
}
7486
}
7587

76-
err = updateRef(ctx, restClient, repo, combineBranchName, workingBranchName)
77-
if err != nil {
78-
return combined, mergeConflicts, "", fmt.Errorf("failed to update combined branch: %w", err)
79-
}
80-
err = deleteBranch(ctx, restClient, repo, workingBranchName)
81-
if err != nil {
82-
Logger.Warn("Failed to delete working branch", "branch", workingBranchName, "error", err)
83-
}
88+
if !dryRun {
89+
err = updateRef(ctx, restClient, repo, combineBranchName, workingBranchName)
90+
if err != nil {
91+
return combined, mergeConflicts, "", fmt.Errorf("failed to update combined branch: %w", err)
92+
}
8493

85-
prBody := generatePRBody(combined, mergeConflicts, command)
86-
prTitle := "Combined PRs"
87-
prNumber, prErr := createPullRequestWithNumber(ctx, restClient, repo, prTitle, combineBranchName, repoDefaultBranch, prBody, addLabels, addAssignees)
88-
if prErr != nil {
89-
return combined, mergeConflicts, "", fmt.Errorf("failed to create combined PR: %w", prErr)
90-
}
91-
if prNumber > 0 {
92-
combinedPRLink = fmt.Sprintf("https://github.com/%s/%s/pull/%d", repo.Owner, repo.Repo, prNumber)
94+
err = deleteBranch(ctx, restClient, repo, workingBranchName)
95+
if err != nil {
96+
Logger.Warn("Failed to delete working branch", "branch", workingBranchName, "error", err)
97+
}
98+
99+
prBody := generatePRBody(combined, mergeConflicts, command)
100+
prTitle := "Combined PRs"
101+
prNumber, prErr := createPullRequestWithNumber(ctx, restClient, repo, prTitle, combineBranchName, repoDefaultBranch, prBody, addLabels, addAssignees)
102+
if prErr != nil {
103+
return combined, mergeConflicts, "", fmt.Errorf("failed to create combined PR: %w", prErr)
104+
}
105+
if prNumber > 0 {
106+
combinedPRLink = fmt.Sprintf("https://github.com/%s/%s/pull/%d", repo.Owner, repo.Repo, prNumber)
107+
}
93108
}
94109

95110
return combined, mergeConflicts, combinedPRLink, nil

internal/cmd/root.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var (
3939
noColor bool
4040
noStats bool
4141
outputFormat string
42+
dryRun bool
4243
)
4344

4445
// StatsCollector tracks stats for the CLI run
@@ -154,6 +155,7 @@ func NewRootCmd() *cobra.Command {
154155
rootCmd.Flags().BoolVar(&noColor, "no-color", false, "Disable color output")
155156
rootCmd.Flags().BoolVar(&noStats, "no-stats", false, "Disable stats summary display")
156157
rootCmd.Flags().StringVar(&outputFormat, "output", "table", "Output format: table, plain, or json")
158+
rootCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Simulate the actions without making any changes")
157159

158160
// Add deprecated flags for backward compatibility
159161
// rootCmd.Flags().IntVar(&minimum, "min-combine", 2, "Minimum number of PRs to combine (deprecated, use --minimum)")
@@ -338,7 +340,7 @@ func processRepository(ctx context.Context, client *api.RESTClient, graphQlClien
338340

339341
// Combine the PRs and collect stats
340342
commandString := buildCommandString([]string{repo.String()})
341-
combined, mergeConflicts, combinedPRLink, err := CombinePRsWithStats(ctx, graphQlClient, restClientWrapper, repo, matchedPRs, commandString)
343+
combined, mergeConflicts, combinedPRLink, err := CombinePRsWithStats(ctx, graphQlClient, restClientWrapper, repo, matchedPRs, commandString, dryRun)
342344
if err != nil {
343345
return fmt.Errorf("failed to combine PRs: %w", err)
344346
}
@@ -477,6 +479,9 @@ func buildCommandString(args []string) string {
477479
if outputFormat != "table" && outputFormat != "" {
478480
cmd = append(cmd, "--output", outputFormat)
479481
}
482+
if dryRun {
483+
cmd = append(cmd, "--dry-run")
484+
}
480485

481486
return strings.Join(cmd, " ")
482487
}

0 commit comments

Comments
 (0)