|
| 1 | +package data |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/bradleyfalzon/ghinstallation/v2" |
| 12 | + "github.com/gofri/go-github-ratelimit/v2/github_ratelimit" |
| 13 | + "github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_primary_ratelimit" |
| 14 | + "github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_secondary_ratelimit" |
| 15 | + "github.com/google/go-github/v72/github" |
| 16 | +) |
| 17 | + |
| 18 | +var GithubConfig *GithubCfg |
| 19 | + |
| 20 | +type GithubCfg struct { |
| 21 | + GithubAppId int64 |
| 22 | + GithubAppPrivateKey []byte |
| 23 | + GithubAppClient *github.Client |
| 24 | + RoundTripper http.RoundTripper |
| 25 | +} |
| 26 | + |
| 27 | +func LoadGithubConfig() error { |
| 28 | + appIdStr := os.Getenv("GITHUB_APP_ID") |
| 29 | + appPrivateKeyStr := os.Getenv("GITHUB_APP_PRIVATE_KEY") |
| 30 | + |
| 31 | + if appIdStr == "" { |
| 32 | + return errors.New("GITHUB_APP_ID not set") |
| 33 | + } |
| 34 | + |
| 35 | + if appPrivateKeyStr == "" { |
| 36 | + return errors.New("GITHUB_APP_PRIVATE_KEY not set") |
| 37 | + } |
| 38 | + |
| 39 | + appId, err := strconv.ParseInt(appIdStr, 10, 64) |
| 40 | + |
| 41 | + if err != nil { |
| 42 | + return errors.New("could not parse app id string to int64") |
| 43 | + } |
| 44 | + |
| 45 | + appPrivateKey, err := base64.StdEncoding.DecodeString(appPrivateKeyStr) |
| 46 | + |
| 47 | + if err != nil { |
| 48 | + return errors.New("failed to decode base64 string") |
| 49 | + } |
| 50 | + |
| 51 | + GithubConfig = &GithubCfg{ |
| 52 | + GithubAppId: appId, |
| 53 | + GithubAppPrivateKey: appPrivateKey, |
| 54 | + GithubAppClient: nil, |
| 55 | + RoundTripper: http.DefaultTransport, |
| 56 | + } |
| 57 | + |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +func getInstallationTransport(tr http.RoundTripper, installationId int64) (http.RoundTripper, error) { |
| 62 | + itt, err := ghinstallation.New(tr, GithubConfig.GithubAppId, installationId, GithubConfig.GithubAppPrivateKey) |
| 63 | + |
| 64 | + if err != nil { |
| 65 | + return nil, fmt.Errorf("failed to create apps transport: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + return itt, nil |
| 69 | +} |
| 70 | + |
| 71 | +func getAppTransport(tr http.RoundTripper) (http.RoundTripper, error) { |
| 72 | + atr, err := ghinstallation.NewAppsTransport(tr, GithubConfig.GithubAppId, GithubConfig.GithubAppPrivateKey) |
| 73 | + |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("failed to create apps transport: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + return atr, nil |
| 79 | +} |
| 80 | + |
| 81 | +func createLimiter(tr http.RoundTripper) http.RoundTripper { |
| 82 | + return github_ratelimit.New(tr, |
| 83 | + github_primary_ratelimit.WithLimitDetectedCallback(func(ctx *github_primary_ratelimit.CallbackContext) { |
| 84 | + fmt.Printf("Primary rate limit detected: category %s, reset time: %v\n", ctx.Category, ctx.ResetTime) |
| 85 | + }), |
| 86 | + github_secondary_ratelimit.WithLimitDetectedCallback(func(ctx *github_secondary_ratelimit.CallbackContext) { |
| 87 | + fmt.Printf("Secondary rate limit detected: reset time: %v, total sleep time: %v\n", ctx.ResetTime, ctx.TotalSleepTime) |
| 88 | + }), |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +func NewInstallationClient(installationId int64) (*github.Client, error) { |
| 93 | + tr := GithubConfig.RoundTripper |
| 94 | + tr, err := getInstallationTransport(tr, installationId) |
| 95 | + |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + tr = createLimiter(tr) |
| 101 | + |
| 102 | + return github.NewClient(&http.Client{Transport: tr}), nil |
| 103 | +} |
| 104 | + |
| 105 | +func InitAppClient() error { |
| 106 | + tr := GithubConfig.RoundTripper |
| 107 | + tr, err := getAppTransport(tr) |
| 108 | + |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + tr = createLimiter(tr) |
| 114 | + |
| 115 | + GithubConfig.GithubAppClient = github.NewClient(&http.Client{Transport: tr}) |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +type NewAppClient struct { |
| 121 | + client *github.Client |
| 122 | +} |
| 123 | + |
| 124 | +func AppClient(client *github.Client) *NewAppClient { |
| 125 | + return &NewAppClient{ |
| 126 | + client, |
| 127 | + } |
| 128 | +} |
0 commit comments