Skip to content

Commit b764fd6

Browse files
committed
refactor(repo): use repo struct whenever possible
1 parent 587b103 commit b764fd6

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

internal/cmd/combine_prs.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,41 @@ import (
1212
"github.com/github/gh-combine/internal/github"
1313
)
1414

15-
func CombinePRs(ctx context.Context, graphQlClient *api.GraphQLClient, restClient *api.RESTClient, owner, repo string, pulls github.Pulls) error {
15+
func CombinePRs(ctx context.Context, graphQlClient *api.GraphQLClient, restClient *api.RESTClient, repo github.Repo, pulls github.Pulls) error {
1616
// Define the combined branch name
1717
workingBranchName := combineBranchName + workingBranchSuffix
1818

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

25-
baseBranchSHA, err := getBranchSHA(ctx, restClient, owner, repo, repoDefaultBranch)
25+
baseBranchSHA, err := getBranchSHA(ctx, restClient, repo.Owner, repo.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, owner, repo, workingBranchName)
31+
err = deleteBranch(ctx, restClient, repo.Owner, repo.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, owner, repo, combineBranchName)
37+
err = deleteBranch(ctx, restClient, repo.Owner, repo.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, owner, repo, combineBranchName, baseBranchSHA)
43+
err = createBranch(ctx, restClient, repo.Owner, repo.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, owner, repo, workingBranchName, baseBranchSHA)
49+
err = createBranch(ctx, restClient, repo.Owner, repo.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, owner, repo, workingBranchName, pr.Head.Ref)
58+
err := mergeBranch(ctx, restClient, repo.Owner, repo.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, owner, repo, combineBranchName, workingBranchName)
76+
err = updateRef(ctx, restClient, repo.Owner, repo.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, owner, repo, workingBranchName)
82+
err = deleteBranch(ctx, restClient, repo.Owner, repo.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, owner, repo, prTitle, combineBranchName, repoDefaultBranch, prBody)
90+
err = createPullRequest(ctx, restClient, repo.Owner, repo.Repo, prTitle, combineBranchName, repoDefaultBranch, prBody)
9191
if err != nil {
9292
return fmt.Errorf("failed to create combined PR: %w", err)
9393
}

internal/cmd/root.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"strings"
87

98
"github.com/cli/go-gh/v2/pkg/api"
109
"github.com/spf13/cobra"
@@ -187,7 +186,7 @@ func executeCombineCommand(ctx context.Context, spinner *Spinner, repos []string
187186
return fmt.Errorf("failed to create REST client: %w", err)
188187
}
189188

190-
for _, repo := range repos {
189+
for _, repoString := range repos {
191190
// Check if context was cancelled (CTRL+C pressed)
192191
select {
193192
case <-ctx.Done():
@@ -196,7 +195,14 @@ func executeCombineCommand(ctx context.Context, spinner *Spinner, repos []string
196195
// Continue processing
197196
}
198197

199-
spinner.UpdateMessage("Processing " + repo)
198+
spinner.UpdateMessage("Parsing " + repoString)
199+
200+
repo, err := github.ParseRepo(repoString)
201+
if err != nil {
202+
return fmt.Errorf("failed to parse repo: %w", err)
203+
}
204+
205+
spinner.UpdateMessage("Processing " + repo.String())
200206
Logger.Debug("Processing repository", "repo", repo)
201207

202208
// Process the repository
@@ -215,16 +221,7 @@ func executeCombineCommand(ctx context.Context, spinner *Spinner, repos []string
215221
}
216222

217223
// processRepository handles a single repository's PRs
218-
func processRepository(ctx context.Context, client *api.RESTClient, graphQlClient *api.GraphQLClient, spinner *Spinner, repo string) error {
219-
// Parse owner and repo name
220-
parts := strings.Split(repo, "/")
221-
if len(parts) != 2 {
222-
return fmt.Errorf("invalid repository format: %s", repo)
223-
}
224-
225-
owner := parts[0]
226-
repoName := parts[1]
227-
224+
func processRepository(ctx context.Context, client *api.RESTClient, graphQlClient *api.GraphQLClient, spinner *Spinner, repo github.Repo) error {
228225
// Check for cancellation
229226
select {
230227
case <-ctx.Done():
@@ -236,8 +233,7 @@ func processRepository(ctx context.Context, client *api.RESTClient, graphQlClien
236233
// Get open PRs for the repository
237234
var pulls github.Pulls
238235

239-
endpoint := fmt.Sprintf("repos/%s/%s/pulls?state=open", owner, repoName)
240-
if err := client.Get(endpoint, &pulls); err != nil {
236+
if err := client.Get(repo.PullsEndpoint(), &pulls); err != nil {
241237
return err
242238
}
243239

@@ -266,7 +262,7 @@ func processRepository(ctx context.Context, client *api.RESTClient, graphQlClien
266262
}
267263

268264
// Check if PR meets additional requirements (CI, approval)
269-
meetsRequirements, err := PrMeetsRequirements(ctx, graphQlClient, owner, repoName, pull.Number)
265+
meetsRequirements, err := PrMeetsRequirements(ctx, graphQlClient, repo.Owner, repo.Repo, pull.Number)
270266
if err != nil {
271267
Logger.Warn("Failed to check PR requirements", "repo", repo, "pr", pull.Number, "error", err)
272268
continue
@@ -288,10 +284,8 @@ func processRepository(ctx context.Context, client *api.RESTClient, graphQlClien
288284

289285
Logger.Debug("Matched PRs", "repo", repo, "count", len(matchedPRs))
290286

291-
// If we get here, we have enough PRs to combine
292-
293287
// Combine the PRs
294-
err := CombinePRs(ctx, graphQlClient, client, owner, repoName, matchedPRs)
288+
err := CombinePRs(ctx, graphQlClient, client, repo, matchedPRs)
295289
if err != nil {
296290
return fmt.Errorf("failed to combine PRs: %w", err)
297291
}

0 commit comments

Comments
 (0)