From 74aa2c9a3469d9027e17998a680184ae0d8b7acc Mon Sep 17 00:00:00 2001 From: ecrupper Date: Wed, 10 Apr 2024 10:27:48 -0500 Subject: [PATCH] remove user param from functions that only use owner creds --- api/build/compile_publish.go | 4 ++-- scm/github/repo.go | 12 ++++++------ scm/github/repo_test.go | 6 ++++-- scm/service.go | 4 ++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/api/build/compile_publish.go b/api/build/compile_publish.go index def9b5cb5..e23e6d8b3 100644 --- a/api/build/compile_publish.go +++ b/api/build/compile_publish.go @@ -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) @@ -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) diff --git a/scm/github/repo.go b/scm/github/repo.go index c6fa33bd3..7a2d973f8 100644 --- a/scm/github/repo.go +++ b/scm/github/repo.go @@ -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 { @@ -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 diff --git a/scm/github/repo_test.go b/scm/github/repo_test.go index 9c278a3b0..64d86cb21 100644 --- a/scm/github/repo_test.go +++ b/scm/github/repo_test.go @@ -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" @@ -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) } @@ -1579,6 +1580,7 @@ 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" @@ -1586,7 +1588,7 @@ func TestGithub_GetBranch(t *testing.T) { 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) } diff --git a/scm/service.go b/scm/service.go index e93d55f94..b04a8cd5e 100644 --- a/scm/service.go +++ b/scm/service.go @@ -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)