Skip to content

Commit

Permalink
enhance: new scm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Oct 29, 2024
1 parent 744cb00 commit 9e0c98f
Show file tree
Hide file tree
Showing 8 changed files with 626 additions and 10 deletions.
8 changes: 8 additions & 0 deletions scm/github/app_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (c *client) newOAuthTokenClient(ctx context.Context, token string) *github.

// newGithubAppClient returns the GitHub App client for authenticating as the GitHub App itself using the RoundTripper.
func (c *client) newGithubAppClient() (*github.Client, error) {
if c.AppsTransport == nil {
return nil, errors.New("unable to create github app client: no AppsTransport configured")
}

// create a github client based off the existing GitHub App configuration
client, err := github.NewClient(
&http.Client{
Expand Down Expand Up @@ -75,6 +79,8 @@ func (c *client) newGithubAppInstallationRepoToken(ctx context.Context, r *api.R
}

// if repo has an install ID, use it to create an installation token
// todo: fix this: its fine but return the installation token object and
// update the repo when you can find a token for it...
if r.GetInstallID() != 0 {
// create installation token for the repo
t, _, err := client.Apps.CreateInstallationToken(ctx, r.GetInstallID(), opts)
Expand All @@ -96,6 +102,8 @@ func (c *client) newGithubAppInstallationRepoToken(ctx context.Context, r *api.R
for _, install := range installations {
// find the installation that matches the org for the repo
if strings.EqualFold(install.GetAccount().GetLogin(), r.GetOrg()) {
// todo: right here... what if we're using this function to generate a netrc and the
// installation doesnt have access to that particular repo
id = install.GetID()
}
}
Expand Down
16 changes: 16 additions & 0 deletions scm/github/app_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package github
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
Expand Down Expand Up @@ -330,3 +331,18 @@ func WithSigner(signer Signer) AppsTransportOption {
at.signer = signer
}
}

// NewTestAppsTransport creates a new AppsTransport for testing purposes.
func NewTestAppsTransport(baseURL string) *AppsTransport {
pk, _ := rsa.GenerateKey(rand.Reader, 2048)
return &AppsTransport{

Check failure on line 338 in scm/github/app_transport.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/app_transport.go#L338

return statements should not be cuddled if block has more than two lines (wsl)
Raw output
scm/github/app_transport.go:338:2: return statements should not be cuddled if block has more than two lines (wsl)
	return &AppsTransport{
	^
BaseURL: baseURL,
Client: &http.Client{Transport: http.DefaultTransport},
tr: http.DefaultTransport,
signer: &RSASigner{
method: jwt.SigningMethodRS256,
key: pk,
},
appID: 1,
}
}
2 changes: 1 addition & 1 deletion scm/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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)
c.Logger.Infof("configurating github app integration for app_id %d", c.config.AppID)

transport, err := c.newGitHubAppTransport(c.config.AppID, c.config.AppPrivateKey, c.config.API)
if err != nil {
Expand Down
14 changes: 5 additions & 9 deletions scm/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,11 +729,7 @@ func (c *client) GetNetrcPassword(ctx context.Context, r *api.Repo, u *api.User,
for resource, perm := range permissions {
ghPerms, err = applyGitHubInstallationPermission(ghPerms, resource, perm)
if err != nil {
l.Errorf("unable to create github app installation token with permission %s:%s: %v", resource, perm, err)

// return the legacy token along with no error for backwards compatibility
// todo: return an error based based on app installation requirements
return u.GetToken(), nil
return u.GetToken(), err
}
}

Expand All @@ -742,10 +738,10 @@ func (c *client) GetNetrcPassword(ctx context.Context, r *api.Repo, u *api.User,
// maybe take an optional list of repos and permission set that is driven by yaml
t, err := c.newGithubAppInstallationRepoToken(ctx, r, repos, ghPerms)
if err != nil {
l.Errorf("unable to create github app installation token for repos %v with permissions %v: %v", repos, permissions, err)

// return the legacy token along with no error for backwards compatibility
// todo: return an error based based on app installation requirements
l.Tracef("unable to create github app installation token for repos %v with permissions %v: %v", repos, permissions, err)

return u.GetToken(), nil
}

Expand Down Expand Up @@ -833,7 +829,7 @@ func applyGitHubInstallationPermission(perms *github.InstallationPermissions, re
case constants.AppInstallPermissionWrite:
break
default:
return perms, fmt.Errorf("invalid permission value given for %s: %s", resource, perm)
return perms, fmt.Errorf("invalid permission level given for <resource>:<level> in %s:%s", resource, perm)
}

// convert resource from yaml string
Expand All @@ -843,7 +839,7 @@ func applyGitHubInstallationPermission(perms *github.InstallationPermissions, re
case constants.AppInstallResourceChecks:
perms.Checks = github.String(perm)
default:
return perms, fmt.Errorf("invalid permission key given: %s", perm)
return perms, fmt.Errorf("invalid permission resource given for <resource>:<level> in %s:%s", resource, perm)
}

return perms, nil
Expand Down
Loading

0 comments on commit 9e0c98f

Please sign in to comment.