Skip to content

Commit 59ffbb8

Browse files
committed
refactor(repo): whenever possible, use github.Repo
1 parent b764fd6 commit 59ffbb8

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

internal/cmd/combine_prs.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,36 @@ func CombinePRs(ctx context.Context, graphQlClient *api.GraphQLClient, restClien
1717
workingBranchName := combineBranchName + workingBranchSuffix
1818

1919
// Get the default branch of the repository
20-
repoDefaultBranch, err := getDefaultBranch(ctx, restClient, repo.Owner, repo.Repo)
20+
repoDefaultBranch, err := getDefaultBranch(ctx, restClient, repo)
2121
if err != nil {
2222
return fmt.Errorf("failed to get default branch: %w", err)
2323
}
2424

25-
baseBranchSHA, err := getBranchSHA(ctx, restClient, repo.Owner, repo.Repo, repoDefaultBranch)
25+
baseBranchSHA, err := getBranchSHA(ctx, restClient, repo, repoDefaultBranch)
2626
if err != nil {
2727
return fmt.Errorf("failed to get SHA of main branch: %w", err)
2828
}
2929

3030
// Delete any pre-existing working branch
31-
err = deleteBranch(ctx, restClient, repo.Owner, repo.Repo, workingBranchName)
31+
err = deleteBranch(ctx, restClient, repo, workingBranchName)
3232
if err != nil {
3333
Logger.Debug("Working branch not found, continuing", "branch", workingBranchName)
3434
}
3535

3636
// Delete any pre-existing combined branch
37-
err = deleteBranch(ctx, restClient, repo.Owner, repo.Repo, combineBranchName)
37+
err = deleteBranch(ctx, restClient, repo, combineBranchName)
3838
if err != nil {
3939
Logger.Debug("Combined branch not found, continuing", "branch", combineBranchName)
4040
}
4141

4242
// Create the combined branch
43-
err = createBranch(ctx, restClient, repo.Owner, repo.Repo, combineBranchName, baseBranchSHA)
43+
err = createBranch(ctx, restClient, repo, combineBranchName, baseBranchSHA)
4444
if err != nil {
4545
return fmt.Errorf("failed to create combined branch: %w", err)
4646
}
4747

4848
// Create the working branch
49-
err = createBranch(ctx, restClient, repo.Owner, repo.Repo, workingBranchName, baseBranchSHA)
49+
err = createBranch(ctx, restClient, repo, workingBranchName, baseBranchSHA)
5050
if err != nil {
5151
return fmt.Errorf("failed to create working branch: %w", err)
5252
}
@@ -55,7 +55,7 @@ func CombinePRs(ctx context.Context, graphQlClient *api.GraphQLClient, restClien
5555
var combinedPRs []string
5656
var mergeFailedPRs []string
5757
for _, pr := range pulls {
58-
err := mergeBranch(ctx, restClient, repo.Owner, repo.Repo, workingBranchName, pr.Head.Ref)
58+
err := mergeBranch(ctx, restClient, repo, workingBranchName, pr.Head.Ref)
5959
if err != nil {
6060
// Check if the error is a 409 merge conflict
6161
if isMergeConflictError(err) {
@@ -73,21 +73,21 @@ func CombinePRs(ctx context.Context, graphQlClient *api.GraphQLClient, restClien
7373
}
7474

7575
// Update the combined branch to the latest commit of the working branch
76-
err = updateRef(ctx, restClient, repo.Owner, repo.Repo, combineBranchName, workingBranchName)
76+
err = updateRef(ctx, restClient, repo, combineBranchName, workingBranchName)
7777
if err != nil {
7878
return fmt.Errorf("failed to update combined branch: %w", err)
7979
}
8080

8181
// Delete the temporary working branch
82-
err = deleteBranch(ctx, restClient, repo.Owner, repo.Repo, workingBranchName)
82+
err = deleteBranch(ctx, restClient, repo, workingBranchName)
8383
if err != nil {
8484
Logger.Warn("Failed to delete working branch", "branch", workingBranchName, "error", err)
8585
}
8686

8787
// Create the combined PR
8888
prBody := generatePRBody(combinedPRs, mergeFailedPRs)
8989
prTitle := "Combined PRs"
90-
err = createPullRequest(ctx, restClient, repo.Owner, repo.Repo, prTitle, combineBranchName, repoDefaultBranch, prBody)
90+
err = createPullRequest(ctx, restClient, repo, prTitle, combineBranchName, repoDefaultBranch, prBody)
9191
if err != nil {
9292
return fmt.Errorf("failed to create combined PR: %w", err)
9393
}
@@ -102,11 +102,11 @@ func isMergeConflictError(err error) bool {
102102
}
103103

104104
// Find the default branch of a repository
105-
func getDefaultBranch(ctx context.Context, client *api.RESTClient, owner, repo string) (string, error) {
105+
func getDefaultBranch(ctx context.Context, client *api.RESTClient, repo github.Repo) (string, error) {
106106
var repoInfo struct {
107107
DefaultBranch string `json:"default_branch"`
108108
}
109-
endpoint := fmt.Sprintf("repos/%s/%s", owner, repo)
109+
endpoint := fmt.Sprintf("repos/%s/%s", repo.Owner, repo.Repo)
110110
err := client.Get(endpoint, &repoInfo)
111111
if err != nil {
112112
return "", fmt.Errorf("failed to get default branch: %w", err)
@@ -115,13 +115,13 @@ func getDefaultBranch(ctx context.Context, client *api.RESTClient, owner, repo s
115115
}
116116

117117
// Get the SHA of a given branch
118-
func getBranchSHA(ctx context.Context, client *api.RESTClient, owner, repo, branch string) (string, error) {
118+
func getBranchSHA(ctx context.Context, client *api.RESTClient, repo github.Repo, branch string) (string, error) {
119119
var ref struct {
120120
Object struct {
121121
SHA string `json:"sha"`
122122
} `json:"object"`
123123
}
124-
endpoint := fmt.Sprintf("repos/%s/%s/git/ref/heads/%s", owner, repo, branch)
124+
endpoint := fmt.Sprintf("repos/%s/%s/git/ref/heads/%s", repo.Owner, repo.Repo, branch)
125125
err := client.Get(endpoint, &ref)
126126
if err != nil {
127127
return "", fmt.Errorf("failed to get SHA of branch %s: %w", branch, err)
@@ -148,14 +148,14 @@ func generatePRBody(combinedPRs, mergeFailedPRs []string) string {
148148
}
149149

150150
// deleteBranch deletes a branch in the repository
151-
func deleteBranch(ctx context.Context, client *api.RESTClient, owner, repo, branch string) error {
152-
endpoint := fmt.Sprintf("repos/%s/%s/git/refs/heads/%s", owner, repo, branch)
151+
func deleteBranch(ctx context.Context, client *api.RESTClient, repo github.Repo, branch string) error {
152+
endpoint := fmt.Sprintf("repos/%s/%s/git/refs/heads/%s", repo.Owner, repo.Repo, branch)
153153
return client.Delete(endpoint, nil)
154154
}
155155

156156
// createBranch creates a new branch in the repository
157-
func createBranch(ctx context.Context, client *api.RESTClient, owner, repo, branch, sha string) error {
158-
endpoint := fmt.Sprintf("repos/%s/%s/git/refs", owner, repo)
157+
func createBranch(ctx context.Context, client *api.RESTClient, repo github.Repo, branch, sha string) error {
158+
endpoint := fmt.Sprintf("repos/%s/%s/git/refs", repo.Owner, repo.Repo)
159159
payload := map[string]string{
160160
"ref": "refs/heads/" + branch,
161161
"sha": sha,
@@ -168,8 +168,8 @@ func createBranch(ctx context.Context, client *api.RESTClient, owner, repo, bran
168168
}
169169

170170
// mergeBranch merges a branch into the base branch
171-
func mergeBranch(ctx context.Context, client *api.RESTClient, owner, repo, base, head string) error {
172-
endpoint := fmt.Sprintf("repos/%s/%s/merges", owner, repo)
171+
func mergeBranch(ctx context.Context, client *api.RESTClient, repo github.Repo, base, head string) error {
172+
endpoint := fmt.Sprintf("repos/%s/%s/merges", repo.Owner, repo.Repo)
173173
payload := map[string]string{
174174
"base": base,
175175
"head": head,
@@ -182,21 +182,21 @@ func mergeBranch(ctx context.Context, client *api.RESTClient, owner, repo, base,
182182
}
183183

184184
// updateRef updates a branch to point to the latest commit of another branch
185-
func updateRef(ctx context.Context, client *api.RESTClient, owner, repo, branch, sourceBranch string) error {
185+
func updateRef(ctx context.Context, client *api.RESTClient, repo github.Repo, branch, sourceBranch string) error {
186186
// Get the SHA of the source branch
187187
var ref struct {
188188
Object struct {
189189
SHA string `json:"sha"`
190190
} `json:"object"`
191191
}
192-
endpoint := fmt.Sprintf("repos/%s/%s/git/ref/heads/%s", owner, repo, sourceBranch)
192+
endpoint := fmt.Sprintf("repos/%s/%s/git/ref/heads/%s", repo.Owner, repo.Repo, sourceBranch)
193193
err := client.Get(endpoint, &ref)
194194
if err != nil {
195195
return fmt.Errorf("failed to get SHA of source branch: %w", err)
196196
}
197197

198198
// Update the branch to point to the new SHA
199-
endpoint = fmt.Sprintf("repos/%s/%s/git/refs/heads/%s", owner, repo, branch)
199+
endpoint = fmt.Sprintf("repos/%s/%s/git/refs/heads/%s", repo.Owner, repo.Repo, branch)
200200
payload := map[string]interface{}{
201201
"sha": ref.Object.SHA,
202202
"force": true,
@@ -209,8 +209,8 @@ func updateRef(ctx context.Context, client *api.RESTClient, owner, repo, branch,
209209
}
210210

211211
// createPullRequest creates a new pull request
212-
func createPullRequest(ctx context.Context, client *api.RESTClient, owner, repo, title, head, base, body string) error {
213-
endpoint := fmt.Sprintf("repos/%s/%s/pulls", owner, repo)
212+
func createPullRequest(ctx context.Context, client *api.RESTClient, repo github.Repo, title, head, base, body string) error {
213+
endpoint := fmt.Sprintf("repos/%s/%s/pulls", repo.Owner, repo.Repo)
214214
payload := map[string]string{
215215
"title": title,
216216
"head": head,

0 commit comments

Comments
 (0)