Skip to content

Commit d8ff194

Browse files
committed
use the default branch of the repo by fetching it from the API
1 parent 2b3626c commit d8ff194

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

internal/cmd/combine_prs.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ func CombinePRs(ctx context.Context, graphQlClient *api.GraphQLClient, restClien
2020
// Define the combined branch name
2121
workingBranchName := combineBranchName + workingBranchSuffix
2222

23-
baseBranchSHA, err := getBranchSHA(ctx, restClient, owner, repo, baseBranch)
23+
// Get the default branch of the repository
24+
repoDefaultBranch, err := getDefaultBranch(ctx, restClient, owner, repo)
25+
if err != nil {
26+
return fmt.Errorf("failed to get default branch: %w", err)
27+
}
28+
29+
baseBranchSHA, err := getBranchSHA(ctx, restClient, owner, repo, repoDefaultBranch)
2430
if err != nil {
2531
return fmt.Errorf("failed to get SHA of main branch: %w", err)
2632
}
@@ -78,14 +84,27 @@ func CombinePRs(ctx context.Context, graphQlClient *api.GraphQLClient, restClien
7884
// Create the combined PR
7985
prBody := generatePRBody(combinedPRs, mergeFailedPRs)
8086
prTitle := "Combined PRs"
81-
err = createPullRequest(ctx, restClient, owner, repo, prTitle, combineBranchName, baseBranch, prBody)
87+
err = createPullRequest(ctx, restClient, owner, repo, prTitle, combineBranchName, repoDefaultBranch, prBody)
8288
if err != nil {
8389
return fmt.Errorf("failed to create combined PR: %w", err)
8490
}
8591

8692
return nil
8793
}
8894

95+
// Find the default branch of a repository
96+
func getDefaultBranch(ctx context.Context, client *api.RESTClient, owner, repo string) (string, error) {
97+
var repoInfo struct {
98+
DefaultBranch string `json:"default_branch"`
99+
}
100+
endpoint := fmt.Sprintf("repos/%s/%s", owner, repo)
101+
err := client.Get(endpoint, &repoInfo)
102+
if err != nil {
103+
return "", fmt.Errorf("failed to get default branch: %w", err)
104+
}
105+
return repoInfo.DefaultBranch, nil
106+
}
107+
89108
// Get the SHA of a given branch
90109
func getBranchSHA(ctx context.Context, client *api.RESTClient, owner, repo, branch string) (string, error) {
91110
var ref struct {

0 commit comments

Comments
 (0)