feat(forge/github): authenticate as a GitHub App and mint per-repository git credentials (archie #1817)
publish / publish (push) Failing after 1s
publish / publish (push) Failing after 1s
This commit is contained in:
8 files changed
+545
-110
No files matched your search
+47
-6
@@ -33,21 +33,47 @@ type GitHubClient struct {
|
||||
|
||||
loginMu sync.Mutex
|
||||
login string
|
||||
|
||||
// app is set when the connection authenticates as a GitHub App.
|
||||
app *app
|
||||
}
|
||||
|
||||
// NewGitHub creates a GitHub-backed Forge implementation. Non-github.com
|
||||
// hosts are configured using GitHub Enterprise's API and upload URL conventions.
|
||||
func newGitHub(token, host string, log *slog.Logger) (forge.Forge, error) {
|
||||
client := github.NewClient(github_ratelimit.NewClient(nil,
|
||||
github_secondary_ratelimit.WithSingleSleepLimit(maxRateLimitWait, nil)))
|
||||
if strings.TrimRight(host, "/") != "https://github.com" {
|
||||
var err error
|
||||
client, err = client.WithEnterpriseURLs(host, host)
|
||||
host = strings.TrimRight(host, "/")
|
||||
base := github_ratelimit.NewClient(nil, github_secondary_ratelimit.WithSingleSleepLimit(maxRateLimitWait, nil)).Transport
|
||||
enterprise := func(c *github.Client) (*github.Client, error) {
|
||||
if host == "https://github.com" {
|
||||
return c, nil
|
||||
}
|
||||
c, err := c.WithEnterpriseURLs(host, host)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("configure GitHub host %q: %w", host, err)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
return &GitHubClient{gh: client.WithAuthToken(token), log: log}, nil
|
||||
cred, isApp := parseAppCredential(token)
|
||||
if !isApp {
|
||||
client, err := enterprise(github.NewClient(&http.Client{Transport: base}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &GitHubClient{gh: client.WithAuthToken(token), log: log}, nil
|
||||
}
|
||||
apiBase := "https://api.github.com"
|
||||
if host != "https://github.com" {
|
||||
apiBase = host + "/api/v3"
|
||||
}
|
||||
a, err := newApp(base, cred, apiBase, enterprise)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client, err := enterprise(github.NewClient(&http.Client{Transport: a}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &GitHubClient{gh: client, log: log, app: a}, nil
|
||||
}
|
||||
|
||||
// AcceptInvitations accepts pending repository invitations, but only to the
|
||||
@@ -55,6 +81,9 @@ func newGitHub(token, host string, log *slog.Logger) (forge.Forge, error) {
|
||||
// as a collaborator to a configured repository is all a human has to do. An
|
||||
// invitation to anything else is left pending.
|
||||
func (c *GitHubClient) AcceptInvitations(ctx context.Context, repos []forge.RepoRef) error {
|
||||
if c.app != nil {
|
||||
return nil // an app reaches repositories by installation, not invitation
|
||||
}
|
||||
invites, _, err := c.gh.Users.ListInvitations(ctx, nil)
|
||||
if err != nil {
|
||||
return apiError("list invitations", err)
|
||||
@@ -410,6 +439,11 @@ func (c *GitHubClient) selfLogin(ctx context.Context) (string, error) {
|
||||
if c.login != "" {
|
||||
return c.login, nil
|
||||
}
|
||||
if c.app != nil {
|
||||
login, _, err := c.app.identity(ctx)
|
||||
c.login = login
|
||||
return login, err
|
||||
}
|
||||
user, _, err := c.gh.Users.Get(ctx, "")
|
||||
if err != nil {
|
||||
return "", apiError("read the token's user", err)
|
||||
@@ -553,6 +587,13 @@ func (c *GitHubClient) SetStateLabel(ctx context.Context, owner, repo string, nu
|
||||
// VerifyPush confirms the token can push to the repo (permission check
|
||||
// at startup so misconfiguration surfaces before any work is claimed).
|
||||
func (c *GitHubClient) VerifyPush(ctx context.Context, owner, repo string) error {
|
||||
if c.app != nil {
|
||||
ok, err := c.app.canPush(ctx, owner, repo)
|
||||
if err == nil && !ok {
|
||||
err = fmt.Errorf("the GitHub App needs Contents: write on %s/%s", owner, repo)
|
||||
}
|
||||
return err
|
||||
}
|
||||
r, _, err := c.gh.Repositories.Get(ctx, owner, repo)
|
||||
if err != nil {
|
||||
return apiError(fmt.Sprintf("get repository %s/%s", owner, repo), err)
|
||||
|
||||
Reference in new issue
Block a user