Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

5.7 KB
Raw Blame
package engine

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"regexp"
	"strings"

	"agentflow/internal/domain"
	"agentflow/internal/gitwork"
	"agentflow/internal/repo"
	"agentflow/internal/runtime"
)

const gitTokenEnv = gitwork.TokenEnv

type gitIdentityCtxKey struct{}

type gitIdentity struct {
	name  string
	email string
}

func withRepositoryGit(ctx context.Context, repository domain.RepositoryContext) context.Context {
	if repository.Provider == domain.RepositoryProviderSSH || gitwork.IsSSHReference(repository.CloneURL) {
		return gitwork.WithSSHRemote(ctx, repository.CloneURL)
	}
	return gitwork.WithHTTPRemote(ctx, repository.CloneURL)
}

func withRunGitCredentials(ctx context.Context, credentials runtime.RunCredentials) context.Context {
	hosts := map[string]gitwork.HTTPCredential{}
	for host, credential := range credentials.GitHTTPCredentials {
		hosts[host] = gitwork.HTTPCredential{Username: credential.GitUsername(), Token: credential.Token}
	}
	return gitwork.WithHTTPCredentials(ctx, hosts)
}

func sshRepositoryURLs(repositories []domain.RepositoryContext) []string {
	var urls []string
	for _, repository := range repositories {
		if repository.Provider == domain.RepositoryProviderSSH || gitwork.IsSSHReference(repository.CloneURL) {
			urls = append(urls, repository.CloneURL)
		}
	}
	return urls
}

func cloneRemoteForGit(ctx context.Context, remote string) string {
	// The command-scoped credential helper authenticates HTTPS. Keep tokens
	// out of clone arguments and the origin URL written by Git.
	return remote
}

func withGitIdentity(ctx context.Context, name, email string) context.Context {
	identity := gitIdentity{name: strings.TrimSpace(name), email: strings.TrimSpace(email)}
	if identity.name == "" || identity.email == "" {
		return ctx
	}
	return context.WithValue(ctx, gitIdentityCtxKey{}, identity)
}

func gitIdentityFrom(ctx context.Context) gitIdentity {
	if ctx != nil {
		if identity, ok := ctx.Value(gitIdentityCtxKey{}).(gitIdentity); ok {
			return identity
		}
	}
	return gitIdentity{}
}

func resolveGitIdentity(s *repo.Store, userID int64) gitIdentity {
	if s == nil || userID <= 0 {
		return gitIdentity{}
	}
	user, ok := s.User(userID)
	if !ok {
		return gitIdentity{}
	}
	login := strings.TrimSpace(user.LoginName)
	if login == "" {
		return gitIdentity{}
	}
	// Local platform accounts do not establish corporate or GitHub identity.
	// Use a non-deliverable address rather than inventing an employer email.
	return gitIdentity{name: login, email: login + "@users.noreply.loopforge.invalid"}
}

func applyWorkspaceGitIdentity(ctx context.Context, workspace string) error {
	identity := gitIdentityFrom(ctx)
	if identity.name == "" || identity.email == "" {
		return nil
	}
	for _, item := range []struct {
		key, value string
	}{
		{key: "user.name", value: identity.name},
		{key: "user.email", value: identity.email},
	} {
		if _, err := gitC(ctx, workspace, "config", "--local", item.key, item.value); err != nil {
			return fmt.Errorf("configure git %s: %w", item.key, err)
		}
	}
	return nil
}

func resolveGitHubToken(s *repo.Store, userID int64) string {
	if s != nil && userID > 0 {
		if credential, ok := s.GitHTTPCredentialForHost(context.Background(), userID, "github.com"); ok {
			return credential.Token
		}
	}
	return ""
}

var (
	gitCommandFailure = regexp.MustCompile(`(?i)repository checkout|git (clone|fetch|pull|push|ls-remote)`)
	gitAuthFailure    = regexp.MustCompile(`(?i)could not read Username|Authentication failed|HTTP Basic: Access denied|requested URL returned error: 40[13]|Invalid username or password`)
)

const gitTokenFixHint = "请责任人到「个人配置 → Git 访问令牌」为仓库的准确主机配置 HTTPS 令牌,并填写该平台要求的用户名、确认仓库权限,然后重试本节点"

// explainGitAuthFailure says what to fix when git rejects the clone or push.
// SOP runs use the responsible user's Git Token, which git's own
// "could not read Username" never mentions. The raw error stays appended.
func explainGitAuthFailure(err error, tokenConfigured bool) error {
	if err == nil {
		return nil
	}
	msg := err.Error()
	if strings.Contains(msg, "Git Token") || !gitCommandFailure.MatchString(msg) || !gitAuthFailure.MatchString(msg) {
		return err
	}
	if !tokenConfigured {
		return fmt.Errorf("运行责任人未配置 Git Token,无法拉取仓库。%s。原始错误:%w", gitTokenFixHint, err)
	}
	return fmt.Errorf("运行责任人的 Git Token 无效、已过期或权限不足,无法拉取仓库。%s;并确认责任人对该仓库有权限。原始错误:%w", gitTokenFixHint, err)
}

const gitAuthIncludeFile = "agentflow.auth"
const gitAskPassFile = "agentflow-askpass"
const gitAskPassSecretFile = "agentflow-askpass-secret"

// applyWorkspaceGitAuth removes credentials persisted by older AgentFlow
// versions. Git authentication is now supplied only to the relevant child
// process, so user tokens never become part of the run workspace.
func applyWorkspaceGitAuth(ctx context.Context, workspace string) error {
	gitDir := filepath.Join(workspace, ".git")
	info, err := os.Stat(gitDir)
	if err != nil || !info.IsDir() {
		return nil
	}
	for _, name := range []string{gitAuthIncludeFile, gitAskPassFile, gitAskPassSecretFile} {
		if err := os.Remove(filepath.Join(gitDir, name)); err != nil && !os.IsNotExist(err) {
			return err
		}
	}
	configured, _ := gitC(ctx, workspace, "config", "--local", "--get-all", "include.path")
	_, _ = gitC(ctx, workspace, "config", "--local", "--unset-all", "include.path")
	for _, include := range strings.Split(configured, "\n") {
		include = strings.TrimSpace(include)
		if include == "" || include == gitAuthIncludeFile {
			continue
		}
		if _, err := gitC(ctx, workspace, "config", "--local", "--add", "include.path", include); err != nil {
			return err
		}
	}
	return nil
}