From 858da17afa757c629e7a78c6df173628db789450 Mon Sep 17 00:00:00 2001 From: davidvader Date: Tue, 29 Oct 2024 12:14:05 -0500 Subject: [PATCH] enhance: constants, helper funcs, reorganize client code, apply tracing to app transports --- compiler/registry/github/github.go | 6 +- compiler/registry/github/template.go | 2 +- scm/github/access.go | 10 +- scm/github/app_client.go | 142 ++++++++------------------- scm/github/app_transport.go | 43 +++++++- scm/github/authentication.go | 2 +- scm/github/changeset.go | 4 +- scm/github/deployment.go | 8 +- scm/github/github.go | 120 ++++++++++++++-------- scm/github/github_test.go | 2 +- scm/github/org.go | 2 +- scm/github/repo.go | 26 ++--- scm/github/user.go | 2 +- scm/github/webhook.go | 2 +- scm/github/webhook_test.go | 2 +- 15 files changed, 193 insertions(+), 180 deletions(-) diff --git a/compiler/registry/github/github.go b/compiler/registry/github/github.go index 09924393c..41be1dfcb 100644 --- a/compiler/registry/github/github.go +++ b/compiler/registry/github/github.go @@ -50,7 +50,7 @@ func New(ctx context.Context, address, token string) (*client, error) { if len(token) > 0 { // create GitHub OAuth client with user's token - gitClient = c.newClientToken(ctx, token) + gitClient = c.newOAuthTokenClient(ctx, token) } // overwrite the github client @@ -59,8 +59,8 @@ func New(ctx context.Context, address, token string) (*client, error) { return c, nil } -// newClientToken is a helper function to return the GitHub oauth2 client. -func (c *client) newClientToken(ctx context.Context, token string) *github.Client { +// newOAuthTokenClient is a helper function to return the GitHub oauth2 client. +func (c *client) newOAuthTokenClient(ctx context.Context, token string) *github.Client { // create the token object for the client ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: token}, diff --git a/compiler/registry/github/template.go b/compiler/registry/github/template.go index cd3dfc75f..8e0add5cc 100644 --- a/compiler/registry/github/template.go +++ b/compiler/registry/github/template.go @@ -19,7 +19,7 @@ func (c *client) Template(ctx context.Context, u *api.User, s *registry.Source) cli := c.Github if u != nil { // create GitHub OAuth client with user's token - cli = c.newClientToken(ctx, u.GetToken()) + cli = c.newOAuthTokenClient(ctx, u.GetToken()) } // create the options to pass diff --git a/scm/github/access.go b/scm/github/access.go index a1e7f5d4d..1bd4dd2e3 100644 --- a/scm/github/access.go +++ b/scm/github/access.go @@ -31,7 +31,7 @@ func (c *client) OrgAccess(ctx context.Context, u *api.User, org string) (string } // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, *u.Token) + client := c.newOAuthTokenClient(ctx, *u.Token) // send API call to capture org access level for user membership, _, err := client.Organizations.GetOrgMembership(ctx, *u.Name, org) @@ -67,7 +67,7 @@ func (c *client) RepoAccess(ctx context.Context, name, token, org, repo string) } // create github oauth client with the given token - client := c.newClientToken(ctx, token) + client := c.newOAuthTokenClient(ctx, token) // send API call to capture repo access level for user perm, _, err := client.Repositories.GetPermissionLevel(ctx, org, repo, name) @@ -98,7 +98,7 @@ func (c *client) TeamAccess(ctx context.Context, u *api.User, org, team string) } // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, u.GetToken()) + client := c.newOAuthTokenClient(ctx, u.GetToken()) teams := []*github.Team{} // set the max per page for the options to capture the list of repos @@ -148,7 +148,7 @@ func (c *client) ListUsersTeamsForOrg(ctx context.Context, u *api.User, org stri }).Tracef("capturing %s team membership for org %s", u.GetName(), org) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, u.GetToken()) + client := c.newOAuthTokenClient(ctx, u.GetToken()) teams := []*github.Team{} // set the max per page for the options to capture the list of repos @@ -193,7 +193,7 @@ func (c *client) RepoContributor(ctx context.Context, owner *api.User, sender, o }).Tracef("capturing %s contributor status for repo %s/%s", sender, org, repo) // create GitHub OAuth client with repo owner's token - client := c.newClientToken(ctx, owner.GetToken()) + client := c.newOAuthTokenClient(ctx, owner.GetToken()) // set the max per page for the options to capture the list of repos opts := github.ListContributorsOptions{ diff --git a/scm/github/app_client.go b/scm/github/app_client.go index f91ba4605..de2243514 100644 --- a/scm/github/app_client.go +++ b/scm/github/app_client.go @@ -4,118 +4,54 @@ package github import ( "context" - "crypto/x509" - "encoding/base64" - "encoding/pem" "errors" - "fmt" "net/http" + "net/http/httptrace" + "net/url" "strings" "github.com/google/go-github/v65/github" + "go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace" + "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" + "golang.org/x/oauth2" api "github.com/go-vela/server/api/types" - "github.com/go-vela/server/constants" ) -// NewGitHubAppTransport creates a new GitHub App transport for authenticating as the GitHub App. -func NewGitHubAppTransport(appID int64, privateKey, baseURL string) (*AppsTransport, error) { - decodedPEM, err := base64.StdEncoding.DecodeString(privateKey) - if err != nil { - return nil, fmt.Errorf("error decoding base64: %w", err) - } - - block, _ := pem.Decode(decodedPEM) - if block == nil { - return nil, fmt.Errorf("failed to parse PEM block containing the key") - } - - _privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) - if err != nil { - return nil, fmt.Errorf("failed to parse RSA private key: %w", err) - } - - transport := NewAppsTransportFromPrivateKey(http.DefaultTransport, appID, _privateKey) - transport.BaseURL = baseURL - - return transport, nil -} - -// ValidateGitHubApp ensures the GitHub App configuration is valid. -func (c *client) ValidateGitHubApp(ctx context.Context) error { - client, err := c.newGithubAppClient() - if err != nil { - return fmt.Errorf("error creating github app client: %w", err) - } - - app, _, err := client.Apps.Get(ctx, "") - if err != nil { - return fmt.Errorf("error getting github app: %w", err) - } - - perms := app.GetPermissions() - - type perm struct { - resource string - requiredPermission string - actualPermission string - } - - // GitHub App installation requires the following permissions - // - contents:read - // - checks:write - requiredPermissions := []perm{ - { - resource: constants.AppInstallResourceContents, - requiredPermission: constants.AppInstallPermissionRead, - actualPermission: perms.GetContents(), - }, - { - resource: constants.AppInstallResourceChecks, - requiredPermission: constants.AppInstallPermissionWrite, - actualPermission: perms.GetChecks(), - }, - } - - for _, p := range requiredPermissions { - err := hasPermission(p.resource, p.requiredPermission, p.actualPermission) - if err != nil { - return err - } - } - - return nil -} - -// hasPermission takes a resource:perm pair and checks if the actual permission matches the expected permission or is supersceded by a higher permission. -func hasPermission(resource, requiredPerm, actualPerm string) error { - if len(actualPerm) == 0 { - return fmt.Errorf("github app missing permission %s:%s", resource, requiredPerm) - } - - permitted := false - - switch requiredPerm { - case constants.AppInstallPermissionNone: - permitted = true - case constants.AppInstallPermissionRead: - if actualPerm == constants.AppInstallPermissionRead || - actualPerm == constants.AppInstallPermissionWrite { - permitted = true - } - case constants.AppInstallPermissionWrite: - if actualPerm == constants.AppInstallPermissionWrite { - permitted = true - } - default: - return fmt.Errorf("invalid required permission type: %s", requiredPerm) - } - - if !permitted { - return fmt.Errorf("github app requires permission %s:%s, found: %s", constants.AppInstallResourceContents, constants.AppInstallPermissionRead, actualPerm) - } - - return nil +// newOAuthTokenClient returns the GitHub OAuth client. +func (c *client) newOAuthTokenClient(ctx context.Context, token string) *github.Client { + // create the token object for the client + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + + // create the OAuth client + tc := oauth2.NewClient(ctx, ts) + // if c.SkipVerify { + // tc.Transport.(*oauth2.Transport).Base = &http.Transport{ + // Proxy: http.ProxyFromEnvironment, + // TLSClientConfig: &tls.Config{ + // InsecureSkipVerify: true, + // }, + // } + // } + + if c.Tracing.Config.EnableTracing { + tc.Transport = otelhttp.NewTransport( + tc.Transport, + otelhttp.WithClientTrace(func(ctx context.Context) *httptrace.ClientTrace { + return otelhttptrace.NewClientTrace(ctx, otelhttptrace.WithoutSubSpans()) + }), + ) + } + + // create the GitHub client from the OAuth client + github := github.NewClient(tc) + + // ensure the proper URL is set in the GitHub client + github.BaseURL, _ = url.Parse(c.config.API) + + return github } // newGithubAppClient returns the GitHub App client for authenticating as the GitHub App itself using the RoundTripper. diff --git a/scm/github/app_transport.go b/scm/github/app_transport.go index 9857f81fc..c7c68a3b7 100644 --- a/scm/github/app_transport.go +++ b/scm/github/app_transport.go @@ -6,11 +6,15 @@ import ( "bytes" "context" "crypto/rsa" + "crypto/x509" + "encoding/base64" "encoding/json" + "encoding/pem" "errors" "fmt" "io" "net/http" + "net/http/httptrace" "strconv" "strings" "sync" @@ -18,6 +22,8 @@ import ( "github.com/golang-jwt/jwt/v5" "github.com/google/go-github/v65/github" + "go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace" + "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" ) const ( @@ -40,8 +46,41 @@ type AppsTransport struct { appID int64 // appID is the GitHub App's ID } -// NewAppsTransportFromPrivateKey returns an AppsTransport using a crypto/rsa.(*PrivateKey). -func NewAppsTransportFromPrivateKey(tr http.RoundTripper, appID int64, key *rsa.PrivateKey) *AppsTransport { +// newGitHubAppTransport creates a new GitHub App transport for authenticating as the GitHub App. +func (c *client) newGitHubAppTransport(appID int64, privateKey, baseURL string) (*AppsTransport, error) { + decodedPEM, err := base64.StdEncoding.DecodeString(privateKey) + if err != nil { + return nil, fmt.Errorf("error decoding base64: %w", err) + } + + block, _ := pem.Decode(decodedPEM) + if block == nil { + return nil, fmt.Errorf("failed to parse PEM block containing the key") + } + + _privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) + if err != nil { + return nil, fmt.Errorf("failed to parse RSA private key: %w", err) + } + + transport := c.newAppsTransportFromPrivateKey(http.DefaultTransport, appID, _privateKey) + transport.BaseURL = baseURL + + // apply tracing to the transport + if c.Tracing.Config.EnableTracing { + transport.tr = otelhttp.NewTransport( + transport.tr, + otelhttp.WithClientTrace(func(ctx context.Context) *httptrace.ClientTrace { + return otelhttptrace.NewClientTrace(ctx, otelhttptrace.WithoutSubSpans()) + }), + ) + } + + return transport, nil +} + +// newAppsTransportFromPrivateKey returns an AppsTransport using a crypto/rsa.(*PrivateKey). +func (c *client) newAppsTransportFromPrivateKey(tr http.RoundTripper, appID int64, key *rsa.PrivateKey) *AppsTransport { return &AppsTransport{ BaseURL: apiBaseURL, Client: &http.Client{Transport: tr}, diff --git a/scm/github/authentication.go b/scm/github/authentication.go index f7e86bb89..2d9c7f95c 100644 --- a/scm/github/authentication.go +++ b/scm/github/authentication.go @@ -21,7 +21,7 @@ func (c *client) Authorize(ctx context.Context, token string) (string, error) { c.Logger.Trace("authorizing user with token") // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, token) + client := c.newOAuthTokenClient(ctx, token) // send API call to capture the current user making the call u, _, err := client.Users.Get(ctx, "") diff --git a/scm/github/changeset.go b/scm/github/changeset.go index 2aa07a445..7a9732fc4 100644 --- a/scm/github/changeset.go +++ b/scm/github/changeset.go @@ -21,7 +21,7 @@ func (c *client) Changeset(ctx context.Context, r *api.Repo, sha string) ([]stri }).Tracef("capturing commit changeset for %s/commit/%s", r.GetFullName(), sha) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, r.GetOwner().GetToken()) + client := c.newOAuthTokenClient(ctx, r.GetOwner().GetToken()) s := []string{} // set the max per page for the options to capture the commit @@ -50,7 +50,7 @@ func (c *client) ChangesetPR(ctx context.Context, r *api.Repo, number int) ([]st }).Tracef("capturing pull request changeset for %s/pull/%d", r.GetFullName(), number) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, r.GetOwner().GetToken()) + client := c.newOAuthTokenClient(ctx, r.GetOwner().GetToken()) s := []string{} f := []*github.CommitFile{} diff --git a/scm/github/deployment.go b/scm/github/deployment.go index e7f8ed0b4..f1c32db88 100644 --- a/scm/github/deployment.go +++ b/scm/github/deployment.go @@ -22,7 +22,7 @@ func (c *client) GetDeployment(ctx context.Context, u *api.User, r *api.Repo, id }).Tracef("capturing deployment %d for repo %s", id, r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, *u.Token) + client := c.newOAuthTokenClient(ctx, *u.Token) // send API call to capture the deployment deployment, _, err := client.Repositories.GetDeployment(ctx, r.GetOrg(), r.GetName(), id) @@ -63,7 +63,7 @@ func (c *client) GetDeploymentCount(ctx context.Context, u *api.User, r *api.Rep }).Tracef("counting deployments for repo %s", r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, *u.Token) + client := c.newOAuthTokenClient(ctx, *u.Token) // create variable to track the deployments deployments := []*github.Deployment{} @@ -105,7 +105,7 @@ func (c *client) GetDeploymentList(ctx context.Context, u *api.User, r *api.Repo }).Tracef("listing deployments for repo %s", r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, *u.Token) + client := c.newOAuthTokenClient(ctx, *u.Token) // set pagination options for listing deployments opts := &github.DeploymentsListOptions{ @@ -164,7 +164,7 @@ func (c *client) CreateDeployment(ctx context.Context, u *api.User, r *api.Repo, }).Tracef("creating deployment for repo %s", r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, *u.Token) + client := c.newOAuthTokenClient(ctx, *u.Token) var payload interface{} if d.Payload == nil { diff --git a/scm/github/github.go b/scm/github/github.go index 9c7c1c048..539a539a7 100644 --- a/scm/github/github.go +++ b/scm/github/github.go @@ -5,15 +5,12 @@ package github import ( "context" "fmt" - "net/http/httptrace" - "net/url" "github.com/google/go-github/v65/github" "github.com/sirupsen/logrus" - "go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace" - "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "golang.org/x/oauth2" + "github.com/go-vela/server/constants" "github.com/go-vela/server/tracing" ) @@ -122,7 +119,7 @@ func New(ctx context.Context, opts ...ClientOpt) (*client, error) { if c.config.AppID != 0 && len(c.config.AppPrivateKey) > 0 { c.Logger.Infof("setting up GitHub App integration for App ID %d", c.config.AppID) - transport, err := NewGitHubAppTransport(c.config.AppID, c.config.AppPrivateKey, c.config.API) + transport, err := c.newGitHubAppTransport(c.config.AppID, c.config.AppPrivateKey, c.config.API) if err != nil { return nil, err } @@ -138,6 +135,83 @@ func New(ctx context.Context, opts ...ClientOpt) (*client, error) { return c, nil } +// ValidateGitHubApp ensures the GitHub App configuration is valid. +func (c *client) ValidateGitHubApp(ctx context.Context) error { + client, err := c.newGithubAppClient() + if err != nil { + return fmt.Errorf("error creating github app client: %w", err) + } + + app, _, err := client.Apps.Get(ctx, "") + if err != nil { + return fmt.Errorf("error getting github app: %w", err) + } + + perms := app.GetPermissions() + + type perm struct { + resource string + requiredPermission string + actualPermission string + } + + // GitHub App installation requires the following permissions + // - contents:read + // - checks:write + requiredPermissions := []perm{ + { + resource: constants.AppInstallResourceContents, + requiredPermission: constants.AppInstallPermissionRead, + actualPermission: perms.GetContents(), + }, + { + resource: constants.AppInstallResourceChecks, + requiredPermission: constants.AppInstallPermissionWrite, + actualPermission: perms.GetChecks(), + }, + } + + for _, p := range requiredPermissions { + err := hasPermission(p.resource, p.requiredPermission, p.actualPermission) + if err != nil { + return err + } + } + + return nil +} + +// hasPermission takes a resource:perm pair and checks if the actual permission matches the expected permission or is supersceded by a higher permission. +func hasPermission(resource, requiredPerm, actualPerm string) error { + if len(actualPerm) == 0 { + return fmt.Errorf("github app missing permission %s:%s", resource, requiredPerm) + } + + permitted := false + + switch requiredPerm { + case constants.AppInstallPermissionNone: + permitted = true + case constants.AppInstallPermissionRead: + if actualPerm == constants.AppInstallPermissionRead || + actualPerm == constants.AppInstallPermissionWrite { + permitted = true + } + case constants.AppInstallPermissionWrite: + if actualPerm == constants.AppInstallPermissionWrite { + permitted = true + } + default: + return fmt.Errorf("invalid required permission type: %s", requiredPerm) + } + + if !permitted { + return fmt.Errorf("github app requires permission %s:%s, found: %s", constants.AppInstallResourceContents, constants.AppInstallPermissionRead, actualPerm) + } + + return nil +} + // NewTest returns a SCM implementation that integrates with the provided // mock server. Only the url from the mock server is required. // @@ -165,39 +239,3 @@ func NewTest(urls ...string) (*client, error) { WithTracing(&tracing.Client{Config: tracing.Config{EnableTracing: false}}), ) } - -// newClientToken returns the GitHub OAuth client. -func (c *client) newClientToken(ctx context.Context, token string) *github.Client { - // create the token object for the client - ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: token}, - ) - - // create the OAuth client - tc := oauth2.NewClient(ctx, ts) - // if c.SkipVerify { - // tc.Transport.(*oauth2.Transport).Base = &http.Transport{ - // Proxy: http.ProxyFromEnvironment, - // TLSClientConfig: &tls.Config{ - // InsecureSkipVerify: true, - // }, - // } - // } - - if c.Tracing.Config.EnableTracing { - tc.Transport = otelhttp.NewTransport( - tc.Transport, - otelhttp.WithClientTrace(func(ctx context.Context) *httptrace.ClientTrace { - return otelhttptrace.NewClientTrace(ctx, otelhttptrace.WithoutSubSpans()) - }), - ) - } - - // create the GitHub client from the OAuth client - github := github.NewClient(tc) - - // ensure the proper URL is set in the GitHub client - github.BaseURL, _ = url.Parse(c.config.API) - - return github -} diff --git a/scm/github/github_test.go b/scm/github/github_test.go index e497b66ec..8cfae3844 100644 --- a/scm/github/github_test.go +++ b/scm/github/github_test.go @@ -72,7 +72,7 @@ func TestGithub_newClientToken(t *testing.T) { client, _ := NewTest(s.URL) // run test - got := client.newClientToken(context.Background(), "foobar") + got := client.newOAuthTokenClient(context.Background(), "foobar") //nolint:staticcheck // ignore false positive if got == nil { diff --git a/scm/github/org.go b/scm/github/org.go index 8c9e95986..ec1f8e314 100644 --- a/scm/github/org.go +++ b/scm/github/org.go @@ -19,7 +19,7 @@ func (c *client) GetOrgName(ctx context.Context, u *api.User, o string) (string, }).Tracef("retrieving org information for %s", o) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, u.GetToken()) + client := c.newOAuthTokenClient(ctx, u.GetToken()) // send an API call to get the org info orgInfo, resp, err := client.Organizations.Get(ctx, o) diff --git a/scm/github/repo.go b/scm/github/repo.go index ba10ca059..f6ad559b2 100644 --- a/scm/github/repo.go +++ b/scm/github/repo.go @@ -55,7 +55,7 @@ func (c *client) Config(ctx context.Context, u *api.User, r *api.Repo, ref strin }).Tracef("capturing configuration file for %s/commit/%s", r.GetFullName(), ref) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, *u.Token) + client := c.newOAuthTokenClient(ctx, *u.Token) // default pipeline file names files := []string{".vela.yml", ".vela.yaml"} @@ -107,7 +107,7 @@ func (c *client) DestroyWebhook(ctx context.Context, u *api.User, org, name stri }).Tracef("deleting repository webhooks for %s/%s", org, name) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, *u.Token) + client := c.newOAuthTokenClient(ctx, *u.Token) // send API call to capture the hooks for the repo hooks, _, err := client.Repositories.ListHooks(ctx, org, name, nil) @@ -167,7 +167,7 @@ func (c *client) CreateWebhook(ctx context.Context, u *api.User, r *api.Repo, h }).Tracef("creating repository webhook for %s/%s", r.GetOrg(), r.GetName()) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, *u.Token) + client := c.newOAuthTokenClient(ctx, *u.Token) // always listen to repository events in case of repo name change events := []string{eventRepository} @@ -241,7 +241,7 @@ func (c *client) Update(ctx context.Context, u *api.User, r *api.Repo, hookID in }).Tracef("updating repository webhook for %s/%s", r.GetOrg(), r.GetName()) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, *u.Token) + client := c.newOAuthTokenClient(ctx, *u.Token) // always listen to repository events in case of repo name change events := []string{eventRepository} @@ -305,7 +305,7 @@ func (c *client) Status(ctx context.Context, u *api.User, b *api.Build, org, nam } // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, *u.Token) + client := c.newOAuthTokenClient(ctx, *u.Token) context := fmt.Sprintf("%s/%s", c.config.StatusContext, b.GetEvent()) url := fmt.Sprintf("%s/%s/%s/%d", c.config.WebUIAddress, org, name, b.GetNumber()) @@ -424,7 +424,7 @@ func (c *client) StepStatus(ctx context.Context, u *api.User, b *api.Build, s *a } // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, *u.Token) + client := c.newOAuthTokenClient(ctx, *u.Token) context := fmt.Sprintf("%s/%s/%s", c.config.StatusContext, b.GetEvent(), s.GetReportAs()) url := fmt.Sprintf("%s/%s/%s/%d#%d", c.config.WebUIAddress, org, name, b.GetNumber(), s.GetNumber()) @@ -487,7 +487,7 @@ func (c *client) GetRepo(ctx context.Context, u *api.User, r *api.Repo) (*api.Re }).Tracef("retrieving repository information for %s", r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, u.GetToken()) + client := c.newOAuthTokenClient(ctx, u.GetToken()) // send an API call to get the repo info repo, resp, err := client.Repositories.Get(ctx, r.GetOrg(), r.GetName()) @@ -507,7 +507,7 @@ func (c *client) GetOrgAndRepoName(ctx context.Context, u *api.User, o string, r }).Tracef("retrieving repository information for %s/%s", o, r) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, u.GetToken()) + client := c.newOAuthTokenClient(ctx, u.GetToken()) // send an API call to get the repo info repo, _, err := client.Repositories.Get(ctx, o, r) @@ -525,7 +525,7 @@ func (c *client) ListUserRepos(ctx context.Context, u *api.User) ([]*api.Repo, e }).Tracef("listing source repositories for %s", u.GetName()) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, u.GetToken()) + client := c.newOAuthTokenClient(ctx, u.GetToken()) r := []*github.Repository{} f := []*api.Repo{} @@ -605,7 +605,7 @@ func (c *client) GetPullRequest(ctx context.Context, r *api.Repo, number int) (s }).Tracef("retrieving pull request %d for repo %s", number, r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, r.GetOwner().GetToken()) + client := c.newOAuthTokenClient(ctx, r.GetOwner().GetToken()) pull, _, err := client.PullRequests.Get(ctx, r.GetOrg(), r.GetName(), number) if err != nil { @@ -629,7 +629,7 @@ func (c *client) GetHTMLURL(ctx context.Context, u *api.User, org, repo, name, r }).Tracef("capturing html_url for %s/%s/%s@%s", org, repo, name, ref) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, *u.Token) + client := c.newOAuthTokenClient(ctx, *u.Token) // set the reference for the options to capture the repository contents opts := &github.RepositoryContentGetOptions{ @@ -665,7 +665,7 @@ func (c *client) GetBranch(ctx context.Context, r *api.Repo, branch string) (str }).Tracef("retrieving branch %s for repo %s", branch, r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, r.GetOwner().GetToken()) + client := c.newOAuthTokenClient(ctx, r.GetOwner().GetToken()) maxRedirects := 3 @@ -795,7 +795,7 @@ func (c *client) SyncRepoWithInstallation(ctx context.Context, r *api.Repo) (*ap return r, err } - client = c.newClientToken(ctx, t.GetToken()) + client = c.newOAuthTokenClient(ctx, t.GetToken()) repos, _, err := client.Apps.ListRepos(ctx, &github.ListOptions{}) if err != nil { diff --git a/scm/github/user.go b/scm/github/user.go index d014256e4..3ceaf75ac 100644 --- a/scm/github/user.go +++ b/scm/github/user.go @@ -16,7 +16,7 @@ func (c *client) GetUserID(ctx context.Context, name string, token string) (stri }).Tracef("capturing SCM user id for %s", name) // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, token) + client := c.newOAuthTokenClient(ctx, token) // send API call to capture user user, _, err := client.Users.Get(ctx, name) diff --git a/scm/github/webhook.go b/scm/github/webhook.go index 31c83fb42..51fa7638c 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -103,7 +103,7 @@ func (c *client) VerifyWebhook(ctx context.Context, request *http.Request, r *ap // RedeliverWebhook redelivers webhooks from GitHub. func (c *client) RedeliverWebhook(ctx context.Context, u *api.User, h *api.Hook) error { // create GitHub OAuth client with user's token - client := c.newClientToken(ctx, u.GetToken()) + client := c.newOAuthTokenClient(ctx, u.GetToken()) // capture the delivery ID of the hook using GitHub API deliveryID, err := c.getDeliveryID(ctx, client, h) diff --git a/scm/github/webhook_test.go b/scm/github/webhook_test.go index 1ef1685fe..c228a5491 100644 --- a/scm/github/webhook_test.go +++ b/scm/github/webhook_test.go @@ -1542,7 +1542,7 @@ func TestGithub_GetDeliveryID(t *testing.T) { client, _ := NewTest(s.URL, "https://foo.bar.com") - ghClient := client.newClientToken(context.Background(), *u.Token) + ghClient := client.newOAuthTokenClient(context.Background(), *u.Token) // run test got, err := client.getDeliveryID(context.TODO(), ghClient, _hook)