Skip to content

Commit 6ea32b4

Browse files
committed
refactor(github): extract inlined-struct into the github package
This will make the code easier to understand and will give opportunity for further refactoring.
1 parent c31db8d commit 6ea32b4

File tree

3 files changed

+33
-50
lines changed

3 files changed

+33
-50
lines changed

internal/cmd/combine_prs.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@ import (
99
"strings"
1010

1111
"github.com/cli/go-gh/v2/pkg/api"
12+
"github.com/github/gh-combine/internal/github"
1213
)
1314

14-
func CombinePRs(ctx context.Context, graphQlClient *api.GraphQLClient, restClient *api.RESTClient, owner, repo string, matchedPRs []struct {
15-
Number int
16-
Title string
17-
Branch string
18-
Base string
19-
BaseSHA string
20-
},
21-
) error {
15+
func CombinePRs(ctx context.Context, graphQlClient *api.GraphQLClient, restClient *api.RESTClient, owner, repo string, matchedPRs github.Pulls) error {
2216
// Define the combined branch name
2317
workingBranchName := combineBranchName + workingBranchSuffix
2418

@@ -61,19 +55,19 @@ func CombinePRs(ctx context.Context, graphQlClient *api.GraphQLClient, restClien
6155
var combinedPRs []string
6256
var mergeFailedPRs []string
6357
for _, pr := range matchedPRs {
64-
err := mergeBranch(ctx, restClient, owner, repo, workingBranchName, pr.Branch)
58+
err := mergeBranch(ctx, restClient, owner, repo, workingBranchName, pr.Head.Ref)
6559
if err != nil {
6660
// Check if the error is a 409 merge conflict
6761
if isMergeConflictError(err) {
6862
// Log merge conflicts at DEBUG level
69-
Logger.Debug("Merge conflict", "branch", pr.Branch, "error", err)
63+
Logger.Debug("Merge conflict", "branch", pr.Head.Ref, "error", err)
7064
} else {
7165
// Log other errors at WARN level
72-
Logger.Warn("Failed to merge branch", "branch", pr.Branch, "error", err)
66+
Logger.Warn("Failed to merge branch", "branch", pr.Head.Ref, "error", err)
7367
}
7468
mergeFailedPRs = append(mergeFailedPRs, fmt.Sprintf("#%d", pr.Number))
7569
} else {
76-
Logger.Debug("Merged branch", "branch", pr.Branch)
70+
Logger.Debug("Merged branch", "branch", pr.Head.Ref)
7771
combinedPRs = append(combinedPRs, fmt.Sprintf("#%d - %s", pr.Number, pr.Title))
7872
}
7973
}

internal/cmd/root.go

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/cli/go-gh/v2/pkg/api"
1010
"github.com/spf13/cobra"
1111

12+
"github.com/github/gh-combine/internal/github"
1213
"github.com/github/gh-combine/internal/version"
1314
)
1415

@@ -233,20 +234,7 @@ func processRepository(ctx context.Context, client *api.RESTClient, graphQlClien
233234
}
234235

235236
// Get open PRs for the repository
236-
var pulls []struct {
237-
Number int
238-
Title string
239-
Head struct {
240-
Ref string
241-
}
242-
Base struct {
243-
Ref string
244-
SHA string
245-
}
246-
Labels []struct {
247-
Name string
248-
}
249-
}
237+
var pulls github.Pulls
250238

251239
endpoint := fmt.Sprintf("repos/%s/%s/pulls?state=open", owner, repoName)
252240
if err := client.Get(endpoint, &pulls); err != nil {
@@ -262,17 +250,8 @@ func processRepository(ctx context.Context, client *api.RESTClient, graphQlClien
262250
}
263251

264252
// Filter PRs based on criteria
265-
var matchedPRs []struct {
266-
Number int
267-
Title string
268-
Branch string
269-
Base string
270-
BaseSHA string
271-
}
272-
253+
var matchedPRs github.Pulls
273254
for _, pull := range pulls {
274-
branch := pull.Head.Ref
275-
276255
// Temporary workaround because passing structures is useless in this
277256
// context.
278257
// Eventually the []Labels should have better support.
@@ -282,7 +261,7 @@ func processRepository(ctx context.Context, client *api.RESTClient, graphQlClien
282261
}
283262

284263
// Check if PR matches all filtering criteria
285-
if !PrMatchesCriteria(branch, labels) {
264+
if !PrMatchesCriteria(pull.Head.Ref, labels) {
286265
continue
287266
}
288267

@@ -298,19 +277,7 @@ func processRepository(ctx context.Context, client *api.RESTClient, graphQlClien
298277
continue
299278
}
300279

301-
matchedPRs = append(matchedPRs, struct {
302-
Number int
303-
Title string
304-
Branch string
305-
Base string
306-
BaseSHA string
307-
}{
308-
Number: pull.Number,
309-
Title: pull.Title,
310-
Branch: branch,
311-
Base: pull.Base.Ref,
312-
BaseSHA: pull.Base.SHA,
313-
})
280+
matchedPRs = append(matchedPRs, pull)
314281
}
315282

316283
// Check if we have enough PRs to combine

internal/github/github.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package github
2+
3+
type Ref struct {
4+
Ref string `json:"ref"`
5+
SHA string `json:"sha"`
6+
}
7+
8+
type Label struct {
9+
Name string `json:"name"`
10+
}
11+
12+
type Labels []Label
13+
14+
type Pull struct {
15+
Number int `json:"number"`
16+
Title string `json:"title"`
17+
Head Ref `json:"head"`
18+
Base Ref `json:"base"`
19+
Labels Labels `json:"labels"`
20+
}
21+
22+
type Pulls []Pull

0 commit comments

Comments
 (0)