From 81016154c0afe0c90d4d52ff0c165e7cccb3c41c Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Wed, 10 Mar 2021 09:37:15 +0900 Subject: [PATCH] GITHUB_APP_PRIVATE_KEY can now be the content of the key (#383) Resolves #382 --- github/github.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/github/github.go b/github/github.go index 5ab3f4af..418a382b 100644 --- a/github/github.go +++ b/github/github.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "net/url" + "os" "strings" "sync" "time" @@ -39,10 +40,20 @@ func (c *Config) NewClient() (*Client, error) { if len(c.Token) > 0 { transport = oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.Token})).Transport } else { - tr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, c.AppID, c.AppInstallationID, c.AppPrivateKey) - if err != nil { - return nil, fmt.Errorf("authentication failed: %v", err) + var tr *ghinstallation.Transport + + if _, err := os.Stat(c.AppPrivateKey); err == nil { + tr, err = ghinstallation.NewKeyFromFile(http.DefaultTransport, c.AppID, c.AppInstallationID, c.AppPrivateKey) + if err != nil { + return nil, fmt.Errorf("authentication failed: using private key at %s: %v", c.AppPrivateKey, err) + } + } else { + tr, err = ghinstallation.New(http.DefaultTransport, c.AppID, c.AppInstallationID, []byte(c.AppPrivateKey)) + if err != nil { + return nil, fmt.Errorf("authentication failed: using private key of size %d (%s...): %v", len(c.AppPrivateKey), strings.Split(c.AppPrivateKey, "\n")[0], err) + } } + if len(c.EnterpriseURL) > 0 { githubAPIURL, err := getEnterpriseApiUrl(c.EnterpriseURL) if err != nil {