Skip to content

Commit

Permalink
Merge pull request #101 from whywaita/fix/gh-enterprise
Browse files Browse the repository at this point in the history
Fix: transport needs to set GitHub Enterprise URL
  • Loading branch information
whywaita authored Sep 10, 2021
2 parents 2786ba3 + ea1aa04 commit 4f4b419
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
12 changes: 12 additions & 0 deletions pkg/gh/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func NewClient(ctx context.Context, personalToken, gheDomain string) (*github.Cl
}

// NewClientGitHubApps create a client of GitHub using Private Key from GitHub Apps
// header is "Authorization: Bearer YOUR_JWT"
// docs: https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-a-github-app
func NewClientGitHubApps(gheDomain string) (*github.Client, error) {
appID := config.Config.GitHub.AppID
Expand All @@ -62,10 +63,16 @@ func NewClientGitHubApps(gheDomain string) (*github.Client, error) {
if gheDomain == "" {
return github.NewClient(&http.Client{Transport: itr}), nil
}
apiEndpoint, err := getAPIEndpoint(gheDomain)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub API Endpoint: %w", err)
}
itr.BaseURL = apiEndpoint.String()
return github.NewEnterpriseClient(gheDomain, gheDomain, &http.Client{Transport: itr})
}

// NewClientInstallation create a client of Github using installation ID from GitHub Apps
// header is "Authorization: token YOUR_INSTALLATION_ACCESS_TOKEN"
// docs: https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-an-installation
func NewClientInstallation(gheDomain string, installationID int64) (*github.Client, error) {
appID := config.Config.GitHub.AppID
Expand All @@ -80,6 +87,11 @@ func NewClientInstallation(gheDomain string, installationID int64) (*github.Clie
if gheDomain == "" {
return github.NewClient(&http.Client{Transport: itr}), nil
}
apiEndpoint, err := getAPIEndpoint(gheDomain)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub API Endpoint: %w", err)
}
itr.BaseURL = apiEndpoint.String()
return github.NewEnterpriseClient(gheDomain, gheDomain, &http.Client{Transport: itr})
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/gh/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ var (
)

// GenerateGitHubAppsToken generate token of GitHub Apps using private key
func GenerateGitHubAppsToken(ctx context.Context, clientInstallation *github.Client, installationID int64) (string, *time.Time, error) {
token, _, err := clientInstallation.Apps.CreateInstallationToken(ctx, installationID, nil)
// clientApps needs to response of `NewClientGitHubApps()`
func GenerateGitHubAppsToken(ctx context.Context, clientApps *github.Client, installationID int64) (string, *time.Time, error) {
token, _, err := clientApps.Apps.CreateInstallationToken(ctx, installationID, nil)
if err != nil {
return "", nil, fmt.Errorf("failed to generate token from API: %w", err)
}
Expand Down Expand Up @@ -95,11 +96,11 @@ func isInstalledGitHubAppSelected(ctx context.Context, gheDomain, inputScope str
}

func listAppsInstalledRepo(ctx context.Context, gheDomain string, installationID int64) (*github.ListRepositories, error) {
clientInstallation, err := NewClientInstallation(gheDomain, installationID)
clientApps, err := NewClientGitHubApps(gheDomain)
if err != nil {
return nil, fmt.Errorf("failed to create github.Client from installationID: %w", err)
}
token, _, err := GenerateGitHubAppsToken(ctx, clientInstallation, installationID)
token, _, err := GenerateGitHubAppsToken(ctx, clientApps, installationID)
if err != nil {
return nil, fmt.Errorf("failed to generate GitHub Apps Token: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ func (m *Manager) doTargetToken(ctx context.Context) error {
logger.Logf(false, "failed to get installationID: %+v", err)
continue
}
clientInstallation, err := gh.NewClientInstallation(target.GHEDomain.String, installationID)
clientApps, err := gh.NewClientGitHubApps(target.GHEDomain.String)
if err != nil {
logger.Logf(false, "failed to create client of GitHub installation: %+v", err)
continue
}
token, expiredAt, err := gh.GenerateGitHubAppsToken(ctx, clientInstallation, installationID)
token, expiredAt, err := gh.GenerateGitHubAppsToken(ctx, clientApps, installationID)
if err != nil {
logger.Logf(false, "failed to get Apps Token: %+v", err)
continue
Expand Down

0 comments on commit 4f4b419

Please sign in to comment.