-
Notifications
You must be signed in to change notification settings - Fork 289
/
github_code_access_token.go
63 lines (49 loc) · 1.29 KB
/
github_code_access_token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package api
import (
"context"
"fmt"
"net/http"
"time"
"github.com/buildkite/roko"
)
type GithubCodeAccessTokenRequest struct {
RepoURL string `json:"repo_url,omitempty"`
}
type GithubCodeAccessTokenResponse struct {
Token string `json:"token,omitempty"`
}
func (c *Client) GenerateGithubCodeAccessToken(ctx context.Context, repoURL, jobID string) (string, *Response, error) {
u := fmt.Sprintf("jobs/%s/github_code_access_token", railsPathEscape(jobID))
req, err := c.newRequest(ctx, http.MethodPost, u, GithubCodeAccessTokenRequest{RepoURL: repoURL})
if err != nil {
return "", nil, err
}
r := roko.NewRetrier(
roko.WithMaxAttempts(3),
roko.WithStrategy(roko.Constant(5*time.Second)),
)
var g GithubCodeAccessTokenResponse
resp, err := roko.DoFunc(ctx, r, func(r *roko.Retrier) (*Response, error) {
resp, err := c.doRequest(req, &g)
if err == nil {
return resp, nil
}
if resp != nil {
if !IsRetryableStatus(resp) {
r.Break()
return resp, err
}
if resp.Header.Get("Retry-After") != "" {
retryAfter, errParseDuration := time.ParseDuration(resp.Header.Get("Retry-After") + "s")
if errParseDuration == nil {
r.SetNextInterval(retryAfter)
}
}
}
return resp, err
})
if err != nil {
return "", resp, err
}
return g.Token, resp, nil
}