NewGithub returns a new client for connecting to Github.
(ctx context.Context, appID int64, appPrivateKey, githubManagedAcct string, logger *zap.Logger)
| 73 | |
| 74 | // NewGithub returns a new client for connecting to Github. |
| 75 | func NewGithub(ctx context.Context, appID int64, appPrivateKey, githubManagedAcct string, logger *zap.Logger) (Github, error) { |
| 76 | atr, err := ghinstallation.NewAppsTransport(retryableHTTPRoundTripper(), appID, []byte(appPrivateKey)) |
| 77 | if err != nil { |
| 78 | return nil, fmt.Errorf("failed to create github app transport: %w", err) |
| 79 | } |
| 80 | appClient := github.NewClient(&http.Client{Transport: atr}) |
| 81 | |
| 82 | lru, err := simplelru.NewLRU(100, nil) |
| 83 | if err != nil { |
| 84 | panic(err) |
| 85 | } |
| 86 | |
| 87 | g := &githubClient{ |
| 88 | appID: appID, |
| 89 | appPrivateKey: appPrivateKey, |
| 90 | appClient: appClient, |
| 91 | appTransport: atr, |
| 92 | installationCache: lru, |
| 93 | managedAcct: githubManagedAcct, |
| 94 | } |
| 95 | |
| 96 | // Set the managed org installation client |
| 97 | if githubManagedAcct == "" { |
| 98 | g.managedOrgFetchError = fmt.Errorf("managed Git repositories are not configured for this environment") |
| 99 | return g, nil |
| 100 | } |
| 101 | i, _, err := appClient.Apps.FindOrganizationInstallation(ctx, githubManagedAcct) |
| 102 | if err != nil { |
| 103 | logger.Error("failed to get managed org installation ID", zap.Error(err), observability.ZapCtx(ctx)) |
| 104 | g.managedOrgFetchError = err |
| 105 | return g, nil |
| 106 | } |
| 107 | g.managedOrgInstallationID = *i.ID |
| 108 | |
| 109 | return g, nil |
| 110 | } |
| 111 | |
| 112 | func (g *githubClient) AppClient() *github.Client { |
| 113 | return g.appClient |