Skip to content

Commit 139e4d9

Browse files
AhmadshataAhmad Shatahenrymcconville
authored
fix(config): route GitHub App token refresh to enterprise API_URL (#137)
Co-authored-by: Ahmad Shata <ahmad.shata@vodafone.com> Co-authored-by: Henry McConville <henrymcconville@users.noreply.github.com>
1 parent bfe4379 commit 139e4d9

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

config/config.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,20 @@ func (c *Config) GetClient() (*github.Client, error) {
8989

9090
// Add custom transport for GitHub App authentication if enabled
9191
if c.GitHubApp {
92-
itr, err := ghinstallation.NewKeyFromFile(transport, c.GitHubAppId, c.GitHubAppInstallationId, c.GitHubAppKeyPath)
92+
itr, err := ghinstallation.NewKeyFromFile(
93+
transport,
94+
c.GitHubAppId,
95+
c.GitHubAppInstallationId,
96+
c.GitHubAppKeyPath,
97+
)
9398
if err != nil {
9499
return nil, fmt.Errorf("creating GitHub App installation transport: %v", err)
95100
}
101+
102+
if c.ApiUrl != nil && c.ApiUrl.String() != "https://api.github.com" {
103+
itr.BaseURL = c.ApiUrl.String()
104+
}
105+
96106
transport = itr
97107
}
98108

config/config_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package config
22

33
import (
4+
"crypto/rand"
5+
"crypto/rsa"
6+
"crypto/x509"
7+
"encoding/pem"
48
"errors"
59
"net/url"
10+
"os"
11+
"path/filepath"
612
"testing"
713

14+
"github.com/bradleyfalzon/ghinstallation/v2"
15+
"github.com/gofri/go-github-pagination/githubpagination"
816
"github.com/stretchr/testify/assert"
917
)
1018

@@ -228,3 +236,52 @@ func TestGetClient(t *testing.T) {
228236
})
229237
}
230238
}
239+
240+
func TestGetClientGitHubAppEnterpriseTransportBaseURL(t *testing.T) {
241+
privateKeyPath := writeTempPrivateKeyFile(t)
242+
243+
t.Setenv("GITHUB_APP", "true")
244+
t.Setenv("GITHUB_APP_KEY_PATH", privateKeyPath)
245+
t.Setenv("GITHUB_APP_ID", "1")
246+
t.Setenv("GITHUB_APP_INSTALLATION_ID", "999999")
247+
t.Setenv("API_URL", "https://github.enterprise.test/api/v3")
248+
249+
cfg, err := Init()
250+
assert.NoError(t, err)
251+
252+
client, err := cfg.GetClient()
253+
assert.NoError(t, err)
254+
255+
paginator, ok := client.Client().Transport.(*githubpagination.GitHubPagination)
256+
if !assert.True(t, ok) {
257+
return
258+
}
259+
260+
installationTransport, ok := paginator.Base.(*ghinstallation.Transport)
261+
if !assert.True(t, ok) {
262+
return
263+
}
264+
265+
assert.Equal(t, "https://github.enterprise.test/api/v3", installationTransport.BaseURL)
266+
}
267+
268+
func writeTempPrivateKeyFile(t *testing.T) string {
269+
t.Helper()
270+
271+
key, err := rsa.GenerateKey(rand.Reader, 2048)
272+
if err != nil {
273+
t.Fatalf("generating rsa key: %v", err)
274+
}
275+
276+
keyPEM := pem.EncodeToMemory(&pem.Block{
277+
Type: "RSA PRIVATE KEY",
278+
Bytes: x509.MarshalPKCS1PrivateKey(key),
279+
})
280+
281+
path := filepath.Join(t.TempDir(), "github-app-private-key.pem")
282+
if err := os.WriteFile(path, keyPEM, 0o600); err != nil {
283+
t.Fatalf("writing private key file: %v", err)
284+
}
285+
286+
return path
287+
}

0 commit comments

Comments
 (0)