@@ -11,13 +11,8 @@ import (
11
11
12
12
// NewGitHubClient returns a new github.Client configured with an access token
13
13
// and a http client, if available.
14
- func NewGitHubClient (ctx context.Context , httpClient * http.Client ) * github.Client {
15
- for _ , key := range []string {
16
- "CHEZMOI_GITHUB_ACCESS_TOKEN" ,
17
- "CHEZMOI_GITHUB_TOKEN" ,
18
- "GITHUB_ACCESS_TOKEN" ,
19
- "GITHUB_TOKEN" ,
20
- } {
14
+ func NewGitHubClient (ctx context.Context , httpClient * http.Client , host string ) (* github.Client , error ) {
15
+ for _ , key := range accessTokenEnvKeys (host ) {
21
16
if accessToken := os .Getenv (key ); accessToken != "" {
22
17
httpClient = oauth2 .NewClient (
23
18
context .WithValue (ctx , oauth2 .HTTPClient , httpClient ),
@@ -27,5 +22,43 @@ func NewGitHubClient(ctx context.Context, httpClient *http.Client) *github.Clien
27
22
break
28
23
}
29
24
}
30
- return github .NewClient (httpClient )
25
+ gitHubClient := github .NewClient (httpClient )
26
+ if host == "github.com" {
27
+ return gitHubClient , nil
28
+ }
29
+ return gitHubClient .WithEnterpriseURLs (
30
+ "https://" + host + "/api/v3/" ,
31
+ "https://" + host + "/api/uploads/" ,
32
+ )
33
+ }
34
+
35
+ func accessTokenEnvKeys (host string ) []string {
36
+ if host == "github.com" {
37
+ return []string {
38
+ "CHEZMOI_GITHUB_ACCESS_TOKEN" ,
39
+ "CHEZMOI_GITHUB_TOKEN" ,
40
+ "GITHUB_ACCESS_TOKEN" ,
41
+ "GITHUB_TOKEN" ,
42
+ }
43
+ }
44
+ hostKey := makeHostKey (host )
45
+ return []string {
46
+ "CHEZMOI_" + hostKey + "_ACCESS_TOKEN" ,
47
+ hostKey + "_ACCESS_TOKEN" ,
48
+ }
49
+ }
50
+
51
+ func makeHostKey (host string ) string {
52
+ hostKey := make ([]byte , 0 , len (host ))
53
+ for _ , b := range []byte (host ) {
54
+ switch {
55
+ case 'A' <= b && b <= 'Z' :
56
+ hostKey = append (hostKey , b )
57
+ case 'a' <= b && b <= 'z' :
58
+ hostKey = append (hostKey , b - 'a' + 'A' )
59
+ default :
60
+ hostKey = append (hostKey , '_' )
61
+ }
62
+ }
63
+ return string (hostKey )
31
64
}
0 commit comments