Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion config/config.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -89,10 +89,20 @@ func (c *Config) GetClient() (*github.Client, error) {

// Add custom transport for GitHub App authentication if enabled
if c.GitHubApp {
itr, err := ghinstallation.NewKeyFromFile(transport, c.GitHubAppId, c.GitHubAppInstallationId, c.GitHubAppKeyPath)
itr, err := ghinstallation.NewKeyFromFile(
transport,
c.GitHubAppId,
c.GitHubAppInstallationId,
c.GitHubAppKeyPath,
)
if err != nil {
return nil, fmt.Errorf("creating GitHub App installation transport: %v", err)
}

if c.ApiUrl != nil && c.ApiUrl.String() != "https://api.github.com" {
itr.BaseURL = c.ApiUrl.String()
}

transport = itr
}

Expand Down
57 changes: 57 additions & 0 deletions config/config_test.go
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
package config

import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"net/url"
"os"
"path/filepath"
"testing"

"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/gofri/go-github-pagination/githubpagination"
"github.com/stretchr/testify/assert"
)

Expand DownExpand Up@@ -228,3 +236,52 @@ func TestGetClient(t *testing.T) {
})
}
}

func TestGetClientGitHubAppEnterpriseTransportBaseURL(t *testing.T) {
privateKeyPath := writeTempPrivateKeyFile(t)

t.Setenv("GITHUB_APP", "true")
t.Setenv("GITHUB_APP_KEY_PATH", privateKeyPath)
t.Setenv("GITHUB_APP_ID", "1")
t.Setenv("GITHUB_APP_INSTALLATION_ID", "999999")
t.Setenv("API_URL", "https://github.enterprise.test/api/v3")

cfg, err := Init()
assert.NoError(t, err)

client, err := cfg.GetClient()
assert.NoError(t, err)

paginator, ok := client.Client().Transport.(*githubpagination.GitHubPagination)
if !assert.True(t, ok) {
return
}

installationTransport, ok := paginator.Base.(*ghinstallation.Transport)
if !assert.True(t, ok) {
return
}

assert.Equal(t, "https://github.enterprise.test/api/v3", installationTransport.BaseURL)
}

func writeTempPrivateKeyFile(t *testing.T) string {
t.Helper()

key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("generating rsa key: %v", err)
}

keyPEM := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
})

path := filepath.Join(t.TempDir(), "github-app-private-key.pem")
if err := os.WriteFile(path, keyPEM, 0o600); err != nil {
t.Fatalf("writing private key file: %v", err)
}

return path
}
Loading