Skip to content

Commit

Permalink
Support GitHub App token on GitHub Enterprise server (#877)
Browse files Browse the repository at this point in the history
* Support GitHub App token on GitHub Enterprise server

* Refactor
  • Loading branch information
int128 authored Feb 18, 2023
1 parent 3654822 commit c9caed0
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions pkg/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,30 @@ func NewClient(ctx context.Context) (Client, error) {
}

func newOAuth2Client(ctx context.Context) (*http.Client, error) {
token := os.Getenv("GITHUB_TOKEN")
var (
token = os.Getenv("GITHUB_TOKEN")
appID = os.Getenv("GITHUB_APP_ID")
installationID = os.Getenv("GITHUB_APP_INSTALLATION_ID")
privateKey = os.Getenv("GITHUB_APP_PRIVATE_KEY")
ghesURL = os.Getenv("GITHUB_ENTERPRISE_URL")
)
if token != "" {
return oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})), nil
}
appID, installationID, privateKey := os.Getenv("GITHUB_APP_ID"), os.Getenv("GITHUB_APP_INSTALLATION_ID"), os.Getenv("GITHUB_APP_PRIVATE_KEY")
if appID != "" && installationID != "" && privateKey != "" {
k, err := oauth2githubapp.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, fmt.Errorf("invalid GITHUB_APP_PRIVATE_KEY: %w", err)
}
cfg := oauth2githubapp.Config{
PrivateKey: k,
AppID: appID,
InstallationID: installationID,
}
return oauth2.NewClient(ctx, cfg.TokenSource(ctx)), nil
if appID == "" || installationID == "" || privateKey == "" {
return nil, fmt.Errorf("you need to set either GITHUB_TOKEN or GitHub App configuration")
}
k, err := oauth2githubapp.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, fmt.Errorf("invalid GITHUB_APP_PRIVATE_KEY: %w", err)
}
cfg := oauth2githubapp.Config{
PrivateKey: k,
AppID: appID,
InstallationID: installationID,
BaseURL: ghesURL,
}
return nil, fmt.Errorf("you need to set either GITHUB_TOKEN or GITHUB_APP_ID")
return oauth2.NewClient(ctx, cfg.TokenSource(ctx)), nil
}

func newGitHubClient(hc *http.Client) (*github.Client, error) {
Expand Down

0 comments on commit c9caed0

Please sign in to comment.