This commit is contained in:
9 files changed
+633
-114
No files matched your search
@@ -3,6 +3,7 @@ package archied
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@@ -101,27 +102,34 @@ func (b *boot) openRepositoryConnection(ctx context.Context, principal access.Pr
|
||||
if !strings.Contains(host, "://") {
|
||||
host = "https://" + host
|
||||
}
|
||||
out.Forge = b.connectionForge(ctx, connection, principal.Org, token)
|
||||
cfg := b.cfgHolder.Get()
|
||||
username := connection.Account
|
||||
gitToken := token
|
||||
var minted forge.GitCredential
|
||||
if issuer, ok := out.Forge.(forge.GitCredentialIssuer); ok {
|
||||
minted, err = issuer.GitCredential(ctx, repository.Owner, repository.Name)
|
||||
switch {
|
||||
case errors.Is(err, errors.ErrUnsupported):
|
||||
case err != nil:
|
||||
b.log.WarnContext(ctx, "git credential unavailable", "connection", connection.ID, "error", err)
|
||||
return failure("credential_unavailable", fmt.Sprintf("%s could not issue a credential for %s/%s. Check the app is installed on it.", connection.Name, repository.Owner, repository.Name))
|
||||
default:
|
||||
username, gitToken = minted.Username, minted.Token
|
||||
}
|
||||
}
|
||||
if username == "" && connection.Type == "gitea" {
|
||||
username, err = connectionAccount(ctx, connection.Type, host, token)
|
||||
if err != nil {
|
||||
return failure("account_unverified", "The forge account could not be verified. Check the credential and its permissions.")
|
||||
}
|
||||
}
|
||||
cfg := b.cfgHolder.Get()
|
||||
botUser, botEmail := commitIdentity(cfg, connection, username)
|
||||
var signer git.Signer
|
||||
if connection.SigningKey.Key != "" {
|
||||
key, err := b.secrets.Resolve(connection.SigningKey)
|
||||
if err != nil || key == "" {
|
||||
return failure("signing_key_unavailable", fmt.Sprintf("The signing key for %s is unavailable. Check its secret reference.", connection.Name))
|
||||
}
|
||||
if signer, err = worktree.NewSSHSigner([]byte(key)); err != nil {
|
||||
return failure("signing_key_invalid", fmt.Sprintf("The signing key for %s is not an OpenSSH private key.", connection.Name))
|
||||
}
|
||||
botUser, botEmail := commitIdentity(cfg, connection, minted, username)
|
||||
signer, code, message := b.connectionSigner(connection)
|
||||
if code != "" {
|
||||
return failure(code, message)
|
||||
}
|
||||
out.Trees = &worktree.Manager{WorkDir: worktree.OrgWorkDir(cfg.WorkDir, string(principal.Org)), Token: token, AuthUser: username, BaseURL: host, BotUser: botUser, BotEmail: botEmail, Signer: signer, Log: b.log}
|
||||
out.Forge = b.connectionForge(ctx, connection, principal.Org, token)
|
||||
out.Trees = &worktree.Manager{WorkDir: worktree.OrgWorkDir(cfg.WorkDir, string(principal.Org)), Token: gitToken, AuthUser: username, BaseURL: host, BotUser: botUser, BotEmail: botEmail, Signer: signer, Log: b.log}
|
||||
if operation == config.OperationPR {
|
||||
if _, disabled := out.Forge.(*forge.NoopForge); disabled {
|
||||
return failure("forge_unavailable", "Enable the forge extension for this connection.")
|
||||
@@ -131,11 +139,15 @@ func (b *boot) openRepositoryConnection(ctx context.Context, principal access.Pr
|
||||
}
|
||||
|
||||
// commitIdentity is the author of commits made through a connection: its
|
||||
// configured commit identity, else the root bot fields, else its account.
|
||||
func commitIdentity(cfg config.Config, connection config.ForgeConnection, account string) (string, string) {
|
||||
// configured commit identity, else the identity the forge minted with the
|
||||
// credential, else the root bot fields, else its account.
|
||||
func commitIdentity(cfg config.Config, connection config.ForgeConnection, minted forge.GitCredential, account string) (string, string) {
|
||||
if connection.CommitName != "" {
|
||||
return connection.CommitName, configuration.DefaultBotEmail(connection.Type, connection.CommitName, connection.CommitEmail)
|
||||
}
|
||||
if minted.CommitName != "" {
|
||||
return minted.CommitName, minted.CommitEmail
|
||||
}
|
||||
if cfg.BotUser != "" {
|
||||
return cfg.BotUser, cfg.BotEmail
|
||||
}
|
||||
@@ -145,3 +157,20 @@ func commitIdentity(cfg config.Config, connection config.ForgeConnection, accoun
|
||||
}
|
||||
return user, configuration.DefaultBotEmail(connection.Type, user, connection.CommitEmail)
|
||||
}
|
||||
|
||||
// connectionSigner resolves the connection's SSH signing key. It returns nil
|
||||
// when none is configured, or a failure code and message.
|
||||
func (b *boot) connectionSigner(connection config.ForgeConnection) (git.Signer, string, string) {
|
||||
if connection.SigningKey.Key == "" {
|
||||
return nil, "", ""
|
||||
}
|
||||
key, err := b.secrets.Resolve(connection.SigningKey)
|
||||
if err != nil || key == "" {
|
||||
return nil, "signing_key_unavailable", fmt.Sprintf("The signing key for %s is unavailable. Check its secret reference.", connection.Name)
|
||||
}
|
||||
signer, err := worktree.NewSSHSigner([]byte(key))
|
||||
if err != nil {
|
||||
return nil, "signing_key_invalid", fmt.Sprintf("The signing key for %s is not an OpenSSH private key.", connection.Name)
|
||||
}
|
||||
return signer, "", ""
|
||||
}
|
||||
Reference in new issue
Block a user