Skip to content

Commit

Permalink
remove user param from functions that only use owner creds
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Apr 10, 2024
1 parent 71f7d71 commit 74aa2c9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions api/build/compile_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func CompileAndPublish(
return nil, nil, retErr
}

commit, branch, baseref, headref, err := scm.GetPullRequest(c, u, r, prNum)
commit, branch, baseref, headref, err := scm.GetPullRequest(c, r, prNum)
if err != nil {
retErr := fmt.Errorf("%s: failed to get pull request info for %s: %w", baseErr, r.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand All @@ -106,7 +106,7 @@ func CompileAndPublish(
// if the source is from a schedule, fetch the commit sha from schedule branch (same as build branch at this moment)
if strings.EqualFold(cfg.Source, "schedule") {
// send API call to capture the commit sha for the branch
_, commit, err := scm.GetBranch(c, u, r, b.GetBranch())
_, commit, err := scm.GetBranch(c, r, b.GetBranch())
if err != nil {
retErr := fmt.Errorf("failed to get commit for repo %s on %s branch: %w", r.GetFullName(), r.GetBranch(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand Down
12 changes: 6 additions & 6 deletions scm/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,15 +581,15 @@ func toLibraryRepo(gr github.Repository) *api.Repo {

// GetPullRequest defines a function that retrieves
// a pull request for a repo.
func (c *client) GetPullRequest(ctx context.Context, u *library.User, r *api.Repo, number int) (string, string, string, string, error) {
func (c *client) GetPullRequest(ctx context.Context, r *api.Repo, number int) (string, string, string, string, error) {
c.Logger.WithFields(logrus.Fields{
"org": r.GetOrg(),
"repo": r.GetName(),
"user": u.GetName(),
"user": r.GetOwner().GetName(),
}).Tracef("retrieving pull request %d for repo %s", number, r.GetFullName())

// create GitHub OAuth client with user's token
client := c.newClientToken(u.GetToken())
client := c.newClientToken(r.GetOwner().GetToken())

pull, _, err := client.PullRequests.Get(ctx, r.GetOrg(), r.GetName(), number)
if err != nil {
Expand Down Expand Up @@ -641,15 +641,15 @@ func (c *client) GetHTMLURL(ctx context.Context, u *library.User, org, repo, nam
}

// GetBranch defines a function that retrieves a branch for a repo.
func (c *client) GetBranch(ctx context.Context, u *library.User, r *api.Repo, branch string) (string, string, error) {
func (c *client) GetBranch(ctx context.Context, r *api.Repo, branch string) (string, string, error) {
c.Logger.WithFields(logrus.Fields{
"org": r.GetOrg(),
"repo": r.GetName(),
"user": u.GetName(),
"user": r.GetOwner().GetName(),
}).Tracef("retrieving branch %s for repo %s", branch, r.GetFullName())

// create GitHub OAuth client with user's token
client := c.newClientToken(u.GetToken())
client := c.newClientToken(r.GetOwner().GetToken())

maxRedirects := 3

Expand Down
6 changes: 4 additions & 2 deletions scm/github/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,7 @@ func TestGithub_GetPullRequest(t *testing.T) {
r := new(api.Repo)
r.SetOrg("octocat")
r.SetName("Hello-World")
r.SetOwner(u)

wantCommit := "6dcb09b5b57875f334f61aebed695e2e4193db5e"
wantBranch := "main"
Expand All @@ -1530,7 +1531,7 @@ func TestGithub_GetPullRequest(t *testing.T) {
client, _ := NewTest(s.URL)

// run test
gotCommit, gotBranch, gotBaseRef, gotHeadRef, err := client.GetPullRequest(context.TODO(), u, r, 1)
gotCommit, gotBranch, gotBaseRef, gotHeadRef, err := client.GetPullRequest(context.TODO(), r, 1)
if err != nil {
t.Errorf("Status returned err: %v", err)
}
Expand Down Expand Up @@ -1579,14 +1580,15 @@ func TestGithub_GetBranch(t *testing.T) {
r.SetName("Hello-World")
r.SetFullName("octocat/Hello-World")
r.SetBranch("main")
r.SetOwner(u)

wantBranch := "main"
wantCommit := "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d"

client, _ := NewTest(s.URL)

// run test
gotBranch, gotCommit, err := client.GetBranch(context.TODO(), u, r, "main")
gotBranch, gotCommit, err := client.GetBranch(context.TODO(), r, "main")
if err != nil {
t.Errorf("Status returned err: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions scm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ type Service interface {
ListUserRepos(context.Context, *library.User) ([]*api.Repo, error)
// GetBranch defines a function that retrieves
// a branch for a repo.
GetBranch(context.Context, *library.User, *api.Repo, string) (string, string, error)
GetBranch(context.Context, *api.Repo, string) (string, string, error)
// GetPullRequest defines a function that retrieves
// a pull request for a repo.
GetPullRequest(context.Context, *library.User, *api.Repo, int) (string, string, string, string, error)
GetPullRequest(context.Context, *api.Repo, int) (string, string, string, string, error)
// GetRepo defines a function that retrieves
// details for a repo.
GetRepo(context.Context, *library.User, *api.Repo) (*api.Repo, int, error)
Expand Down

0 comments on commit 74aa2c9

Please sign in to comment.