Skip to content

Commit 6e63298

Browse files
committed
Add GitHub Enterprise support to GithubServiceProviderBasic
1 parent 5f9dc60 commit 6e63298

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

libs/ci/github/github.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,46 @@ func (_ GithubServiceProviderBasic) NewService(ghToken string, repoName string,
3030
client = client.WithAuthToken(ghToken)
3131
}
3232

33+
client, err := configureEnterpriseClient(client)
34+
if err != nil {
35+
return GithubService{}, err
36+
}
37+
3338
return GithubService{
3439
Client: client,
3540
RepoName: repoName,
3641
Owner: owner,
3742
}, nil
3843
}
3944

45+
// configureEnterpriseClient configures the GitHub client for Enterprise if
46+
// DIGGER_GITHUB_HOSTNAME or GITHUB_API_URL environment variables are set.
47+
func configureEnterpriseClient(client *github.Client) (*github.Client, error) {
48+
// Check for DIGGER_GITHUB_HOSTNAME first (explicit configuration)
49+
if hostname := os.Getenv("DIGGER_GITHUB_HOSTNAME"); hostname != "" {
50+
baseURL := fmt.Sprintf("https://%s/api/v3/", hostname)
51+
uploadURL := fmt.Sprintf("https://%s/api/uploads/", hostname)
52+
slog.Info("configuring GitHub Enterprise client", "hostname", hostname)
53+
return client.WithEnterpriseURLs(baseURL, uploadURL)
54+
}
55+
56+
// Fall back to GITHUB_API_URL (set automatically in GitHub Actions)
57+
if apiURL := os.Getenv("GITHUB_API_URL"); apiURL != "" && apiURL != "https://api.github.com" {
58+
// Derive upload URL from API URL by replacing /api/v3 with /api/uploads
59+
uploadURL := strings.Replace(apiURL, "/api/v3", "/api/uploads", 1)
60+
if !strings.HasSuffix(apiURL, "/") {
61+
apiURL += "/"
62+
}
63+
if !strings.HasSuffix(uploadURL, "/") {
64+
uploadURL += "/"
65+
}
66+
slog.Info("configuring GitHub Enterprise client from GITHUB_API_URL", "apiUrl", apiURL, "uploadUrl", uploadURL)
67+
return client.WithEnterpriseURLs(apiURL, uploadURL)
68+
}
69+
70+
return client, nil
71+
}
72+
4073
type GithubService struct {
4174
Client *github.Client
4275
RepoName string

libs/ci/github/github_test.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package github
22

33
import (
4-
"github.com/diggerhq/digger/libs/ci/generic"
4+
"os"
55
"testing"
66

7+
"github.com/diggerhq/digger/libs/ci/generic"
78
"github.com/diggerhq/digger/libs/digger_config"
89
"github.com/stretchr/testify/assert"
910
)
@@ -121,3 +122,66 @@ func TestFindAllChangedFilesOfPR(t *testing.T) {
121122
// 45 changed files including 1 renamed file so the previous filename is included
122123
assert.Equal(t, 46, len(files))
123124
}
125+
126+
func TestConfigureEnterpriseClient_WithDiggerHostname(t *testing.T) {
127+
// Set up
128+
os.Setenv("DIGGER_GITHUB_HOSTNAME", "github.example.com")
129+
defer os.Unsetenv("DIGGER_GITHUB_HOSTNAME")
130+
131+
svc, err := GithubServiceProviderBasic{}.NewService("test-token", "repo", "owner")
132+
133+
assert.NoError(t, err)
134+
assert.NotNil(t, svc.Client)
135+
assert.Equal(t, "https://github.example.com/api/v3/", svc.Client.BaseURL.String())
136+
assert.Equal(t, "https://github.example.com/api/uploads/", svc.Client.UploadURL.String())
137+
}
138+
139+
func TestConfigureEnterpriseClient_WithGitHubApiUrl(t *testing.T) {
140+
// Set up - this simulates GitHub Actions environment on Enterprise
141+
os.Setenv("GITHUB_API_URL", "https://github.example.com/api/v3")
142+
defer os.Unsetenv("GITHUB_API_URL")
143+
144+
svc, err := GithubServiceProviderBasic{}.NewService("test-token", "repo", "owner")
145+
146+
assert.NoError(t, err)
147+
assert.NotNil(t, svc.Client)
148+
assert.Equal(t, "https://github.example.com/api/v3/", svc.Client.BaseURL.String())
149+
assert.Equal(t, "https://github.example.com/api/uploads/", svc.Client.UploadURL.String())
150+
}
151+
152+
func TestConfigureEnterpriseClient_PublicGitHub(t *testing.T) {
153+
// Ensure no enterprise env vars are set
154+
os.Unsetenv("DIGGER_GITHUB_HOSTNAME")
155+
os.Unsetenv("GITHUB_API_URL")
156+
157+
svc, err := GithubServiceProviderBasic{}.NewService("test-token", "repo", "owner")
158+
159+
assert.NoError(t, err)
160+
assert.NotNil(t, svc.Client)
161+
assert.Equal(t, "https://api.github.com/", svc.Client.BaseURL.String())
162+
}
163+
164+
func TestConfigureEnterpriseClient_PublicGitHubApiUrl(t *testing.T) {
165+
// GITHUB_API_URL set to public GitHub should not trigger enterprise config
166+
os.Setenv("GITHUB_API_URL", "https://api.github.com")
167+
defer os.Unsetenv("GITHUB_API_URL")
168+
169+
svc, err := GithubServiceProviderBasic{}.NewService("test-token", "repo", "owner")
170+
171+
assert.NoError(t, err)
172+
assert.NotNil(t, svc.Client)
173+
assert.Equal(t, "https://api.github.com/", svc.Client.BaseURL.String())
174+
}
175+
176+
func TestConfigureEnterpriseClient_DiggerHostnameTakesPrecedence(t *testing.T) {
177+
// Both set - DIGGER_GITHUB_HOSTNAME should take precedence
178+
os.Setenv("DIGGER_GITHUB_HOSTNAME", "digger-enterprise.example.com")
179+
os.Setenv("GITHUB_API_URL", "https://actions-enterprise.example.com/api/v3")
180+
defer os.Unsetenv("DIGGER_GITHUB_HOSTNAME")
181+
defer os.Unsetenv("GITHUB_API_URL")
182+
183+
svc, err := GithubServiceProviderBasic{}.NewService("test-token", "repo", "owner")
184+
185+
assert.NoError(t, err)
186+
assert.Equal(t, "https://digger-enterprise.example.com/api/v3/", svc.Client.BaseURL.String())
187+
}

0 commit comments

Comments
 (0)