Skip to content

Commit 0238852

Browse files
committed
fix:method
1 parent 808b28a commit 0238852

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

internal/githubclient/client.go

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package githubclient
22

33
import (
4+
"context"
45
"crypto/rsa"
56
"crypto/x509"
6-
"encoding/json"
77
"encoding/pem"
88
"errors"
99
"fmt"
10-
"io"
1110
"net/http"
1211
"strconv"
1312
"time"
@@ -96,36 +95,44 @@ func generateAppJWT(appID string, issuedAt time.Time, privateKeyPEM []byte) (str
9695
}
9796

9897
func getInstallationAccessToken(baseURL, appJWT, installationID string) (string, error) {
99-
url := fmt.Sprintf("%s/app/installations/%s/access_tokens", baseURL, installationID)
98+
tc := &http.Client{
99+
Transport: &jwtTransport{
100+
token: appJWT,
101+
rt: http.DefaultTransport,
102+
},
103+
Timeout: 30 * time.Second,
104+
}
100105

101-
req, err := http.NewRequest("POST", url, nil)
106+
client, err := github.NewClient(tc).WithEnterpriseURLs(baseURL, baseURL)
102107
if err != nil {
103-
return "", fmt.Errorf("failed to create request: %v", err)
108+
return "", fmt.Errorf("failed to create github client: %v", err)
104109
}
105110

106-
req.Header.Set("Authorization", "Bearer "+appJWT)
107-
req.Header.Set("Accept", "application/vnd.github.v3+json")
108-
req.Header.Set("User-Agent", "terraform-provider-kw-github")
109-
110-
client := &http.Client{Timeout: 30 * time.Second}
111-
resp, err := client.Do(req)
111+
installationIDInt, err := strconv.ParseInt(installationID, 10, 64)
112112
if err != nil {
113-
return "", fmt.Errorf("failed to make request: %v", err)
113+
return "", fmt.Errorf("invalid installation ID: %v", err)
114114
}
115-
defer resp.Body.Close()
116115

117-
if resp.StatusCode != http.StatusCreated {
118-
body, _ := io.ReadAll(resp.Body)
119-
return "", fmt.Errorf("failed to get access token: %s - %s", resp.Status, string(body))
116+
token, _, err := client.Apps.CreateInstallationToken(
117+
context.Background(),
118+
installationIDInt,
119+
&github.InstallationTokenOptions{},
120+
)
121+
if err != nil {
122+
return "", fmt.Errorf("failed to create installation token: %v", err)
120123
}
121124

122-
var tokenResp struct {
123-
Token string `json:"token"`
124-
}
125+
return token.GetToken(), nil
126+
}
125127

126-
if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil {
127-
return "", fmt.Errorf("failed to decode response: %v", err)
128-
}
128+
type jwtTransport struct {
129+
token string
130+
rt http.RoundTripper
131+
}
129132

130-
return tokenResp.Token, nil
133+
func (t *jwtTransport) RoundTrip(req *http.Request) (*http.Response, error) {
134+
req.Header.Set("Authorization", "Bearer "+t.token)
135+
req.Header.Set("Accept", "application/vnd.github.v3+json")
136+
req.Header.Set("User-Agent", "terraform-provider-kw-github")
137+
return t.rt.RoundTrip(req)
131138
}

0 commit comments

Comments
 (0)