Skip to content

Commit 0f6df51

Browse files
committed
Additional func to retrieve default branch of an org/repo
1 parent 0e909e3 commit 0f6df51

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

cmd/generic-autobumper/bumper/bumper.go

+25
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,31 @@ func getLastBumpCommit(gerritAuthor, commitTag string) (string, error) {
681681
return outBuf.String(), nil
682682
}
683683

684+
// getDefaultBranch retrieves the default branch name of a given repository
685+
// using the provided GitHub client. It fetches the repository details and
686+
// extracts the default branch. It returns a string representing the default
687+
// branch name and error if the repository details cannot be retrieved.
688+
func getDefaultBranch(gc github.Client, org, repo string) (string, error) {
689+
repository, err := gc.GetRepo(org, repo)
690+
if err != nil {
691+
return "", fmt.Errorf("failed to get repository details for %s/%s: %w", org, repo, err)
692+
}
693+
return repository.DefaultBranch, nil
694+
}
695+
696+
// createOrUpdatePROnDefaultBranch retrieves the default branch for the repository
697+
// and creates/updates a PR on that branch without requiring the caller to specify the branch.
698+
func createOrUpdatePROnDefaultBranch(gc github.Client, opts *Options, prTitle, prBody string) error {
699+
// Fetch the default branch using existing repo details from opts
700+
defaultBranch, err := getDefaultBranch(gc, opts.GitHubOrg, opts.GitHubRepo)
701+
if err != nil {
702+
return fmt.Errorf("failed to get default branch for %s/%s: %w", opts.GitHubOrg, opts.GitHubRepo, err)
703+
}
704+
705+
// Use the existing updatePR function to create/update the PR
706+
return UpdatePR(gc, opts.GitHubOrg, opts.GitHubRepo, "", opts.GitHubLogin, defaultBranch, opts.HeadBranchName, true, prTitle, prBody)
707+
}
708+
684709
// getChangeId generates a change ID for the gerrit PR that is deterministic
685710
// rather than being random as is normally preferable.
686711
// In particular this chooses a change ID by hashing the last commit by the

cmd/generic-autobumper/bumper/bumper_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ limitations under the License.
1717
package bumper
1818

1919
import (
20+
"context"
21+
"errors"
2022
"os"
2123
"path"
2224
"path/filepath"
2325
"strings"
2426
"testing"
2527

2628
"sigs.k8s.io/prow/pkg/config/secret"
29+
"sigs.k8s.io/prow/pkg/github"
2730
)
2831

2932
func TestValidateOptions(t *testing.T) {
@@ -337,3 +340,58 @@ func TestCDToRootDir(t *testing.T) {
337340
})
338341
}
339342
}
343+
344+
type fakedGithubClient struct {
345+
repo *github.Repo
346+
err error
347+
}
348+
349+
func (f *fakedGithubClient) GetRepo(ctx context.Context, org, repo string) (*github.Repo, error) {
350+
if f.err != nil {
351+
return nil, f.err
352+
}
353+
return f.repo, nil
354+
}
355+
356+
func TestGetDefaultBranch(t *testing.T) {
357+
testCases := []struct {
358+
name string
359+
client GithubClient
360+
org string
361+
repo string
362+
expectedBranch string
363+
expectedError bool
364+
}{
365+
{
366+
name: "Successful retrieval",
367+
client: &fakedGithubClient{
368+
repo: &github.Repo{DefaultBranch: "main"},
369+
},
370+
org: "test-org",
371+
repo: "test-repo",
372+
expectedBranch: "main",
373+
expectedError: false,
374+
},
375+
{
376+
name: "Github API failure",
377+
client: &fakedGithubClient{
378+
err: errors.New("Github API error"),
379+
},
380+
org: "test-org",
381+
repo: "test-repo",
382+
expectedError: true,
383+
},
384+
}
385+
for _, tc := range testCases {
386+
t.Run(tc.name, func(t *testing.T) {
387+
branch, err := getDefaultBranch(tc.client, tc.org, tc.repo)
388+
if tc.expectedError && err == nil {
389+
t.Errorf("Expected to get an error but the result is nil")
390+
} else if !tc.expectedError && err != nil {
391+
t.Errorf("Expected to not get an error but got one: %v", err)
392+
} else if branch != tc.expectedBranch {
393+
t.Errorf("Expected branch %q, but got %q", tc.expectedBranch, branch)
394+
}
395+
})
396+
}
397+
}

0 commit comments

Comments
 (0)