@@ -20,56 +20,63 @@ type RESTClientInterface interface {
20
20
Patch (endpoint string , body io.Reader , response interface {}) error
21
21
}
22
22
23
+ // CombineOpts holds options for combining PRs
24
+ // Use this struct to pass options to CombinePRsWithStats and related functions
25
+ // This makes the code more maintainable and clear
26
+ type CombineOpts struct {
27
+ Noop bool
28
+ Command string
29
+ Repo github.Repo
30
+ Pulls github.Pulls
31
+ }
32
+
23
33
// 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 , dryRun bool ) (combined []string , mergeConflicts []string , combinedPRLink string , err error ) {
25
- // Move variable definitions outside the conditional blocks
34
+ func CombinePRsWithStats (ctx context.Context , graphQlClient * api.GraphQLClient , restClient RESTClientInterface , opts CombineOpts ) (combined []string , mergeConflicts []string , combinedPRLink string , err error ) {
26
35
workingBranchName := combineBranchName + workingBranchSuffix
27
36
28
- repoDefaultBranch , err := getDefaultBranch (ctx , restClient , repo )
37
+ repoDefaultBranch , err := getDefaultBranch (ctx , restClient , opts . Repo )
29
38
if err != nil {
30
39
return nil , nil , "" , fmt .Errorf ("failed to get default branch: %w" , err )
31
40
}
32
41
33
- baseBranchSHA , err := getBranchSHA (ctx , restClient , repo , repoDefaultBranch )
42
+ baseBranchSHA , err := getBranchSHA (ctx , restClient , opts . Repo , repoDefaultBranch )
34
43
if err != nil {
35
44
return nil , nil , "" , fmt .Errorf ("failed to get SHA of main branch: %w" , err )
36
45
}
37
46
38
- if dryRun {
47
+ if opts . Noop {
39
48
Logger .Debug ("Dry-run mode enabled. No changes will be made." )
40
49
Logger .Debug ("Simulating branch operations" , "workingBranch" , workingBranchName , "defaultBranch" , repoDefaultBranch )
41
50
}
42
51
43
- // Skip branch creation and deletion if dry-run is enabled
44
- if ! dryRun {
45
- err = deleteBranch (ctx , restClient , repo , workingBranchName )
52
+ if ! opts .Noop {
53
+ err = deleteBranch (ctx , restClient , opts .Repo , workingBranchName )
46
54
if err != nil {
47
55
Logger .Debug ("Working branch not found, continuing" , "branch" , workingBranchName )
48
56
}
49
57
50
- err = deleteBranch (ctx , restClient , repo , combineBranchName )
58
+ err = deleteBranch (ctx , restClient , opts . Repo , combineBranchName )
51
59
if err != nil {
52
60
Logger .Debug ("Combined branch not found, continuing" , "branch" , combineBranchName )
53
61
}
54
62
55
- err = createBranch (ctx , restClient , repo , combineBranchName , baseBranchSHA )
63
+ err = createBranch (ctx , restClient , opts . Repo , combineBranchName , baseBranchSHA )
56
64
if err != nil {
57
65
return nil , nil , "" , fmt .Errorf ("failed to create combined branch: %w" , err )
58
66
}
59
67
60
- err = createBranch (ctx , restClient , repo , workingBranchName , baseBranchSHA )
68
+ err = createBranch (ctx , restClient , opts . Repo , workingBranchName , baseBranchSHA )
61
69
if err != nil {
62
70
return nil , nil , "" , fmt .Errorf ("failed to create working branch: %w" , err )
63
71
}
64
72
}
65
73
66
- // Simulate merging PRs
67
- for _ , pr := range pulls {
68
- if dryRun {
74
+ for _ , pr := range opts .Pulls {
75
+ if opts .Noop {
69
76
Logger .Debug ("Simulating merge of branch" , "branch" , pr .Head .Ref )
70
77
combined = append (combined , fmt .Sprintf ("#%d - %s" , pr .Number , pr .Title ))
71
78
} else {
72
- err := mergeBranch (ctx , restClient , repo , workingBranchName , pr .Head .Ref )
79
+ err := mergeBranch (ctx , restClient , opts . Repo , workingBranchName , pr .Head .Ref )
73
80
if err != nil {
74
81
if isMergeConflictError (err ) {
75
82
Logger .Debug ("Merge conflict" , "branch" , pr .Head .Ref , "error" , err )
@@ -84,25 +91,25 @@ func CombinePRsWithStats(ctx context.Context, graphQlClient *api.GraphQLClient,
84
91
}
85
92
}
86
93
87
- if ! dryRun {
88
- err = updateRef (ctx , restClient , repo , combineBranchName , workingBranchName )
94
+ if ! opts . Noop {
95
+ err = updateRef (ctx , restClient , opts . Repo , combineBranchName , workingBranchName )
89
96
if err != nil {
90
97
return combined , mergeConflicts , "" , fmt .Errorf ("failed to update combined branch: %w" , err )
91
98
}
92
99
93
- err = deleteBranch (ctx , restClient , repo , workingBranchName )
100
+ err = deleteBranch (ctx , restClient , opts . Repo , workingBranchName )
94
101
if err != nil {
95
102
Logger .Warn ("Failed to delete working branch" , "branch" , workingBranchName , "error" , err )
96
103
}
97
104
98
- prBody := generatePRBody (combined , mergeConflicts , command )
105
+ prBody := generatePRBody (combined , mergeConflicts , opts . Command )
99
106
prTitle := "Combined PRs"
100
- prNumber , prErr := createPullRequestWithNumber (ctx , restClient , repo , prTitle , combineBranchName , repoDefaultBranch , prBody , addLabels , addAssignees )
107
+ prNumber , prErr := createPullRequestWithNumber (ctx , restClient , opts . Repo , prTitle , combineBranchName , repoDefaultBranch , prBody , addLabels , addAssignees )
101
108
if prErr != nil {
102
109
return combined , mergeConflicts , "" , fmt .Errorf ("failed to create combined PR: %w" , prErr )
103
110
}
104
111
if prNumber > 0 {
105
- combinedPRLink = fmt .Sprintf ("https://github.com/%s/%s/pull/%d" , repo . Owner , repo .Repo , prNumber )
112
+ combinedPRLink = fmt .Sprintf ("https://github.com/%s/%s/pull/%d" , opts . Repo . Owner , opts . Repo .Repo , prNumber )
106
113
}
107
114
}
108
115
0 commit comments