Skip to content

Commit 1df300d

Browse files
authored
Revert "fix:method (#15)"
This reverts commit d3fbe53.
1 parent d8c7b79 commit 1df300d

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

internal/githubclient/client.go

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

33
import (
4-
"context"
54
"crypto/rsa"
65
"crypto/x509"
6+
"encoding/json"
77
"encoding/pem"
88
"errors"
99
"fmt"
10+
"io"
1011
"net/http"
1112
"strconv"
1213
"time"
@@ -95,44 +96,36 @@ func generateAppJWT(appID string, issuedAt time.Time, privateKeyPEM []byte) (str
9596
}
9697

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

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

111-
installationIDInt, err := strconv.ParseInt(installationID, 10, 64)
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)
112112
if err != nil {
113-
return "", fmt.Errorf("invalid installation ID: %v", err)
113+
return "", fmt.Errorf("failed to make request: %v", err)
114114
}
115+
defer resp.Body.Close()
115116

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)
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))
123120
}
124121

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

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

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)
130+
return tokenResp.Token, nil
138131
}

0 commit comments

Comments
 (0)