The pipeline's Stage.Run bodies execute inside the sandboxed archie-agent process, which holds neither a forge credential nor a git-level one -- confirmed the egress/CredentialCapability mechanism (grants.go, credentials.go) only wires into the Kit/harness launch path (kitrun.Launcher), not the built-in runner pr-review actually uses, so routing through it as previously planned isn't viable without a much bigger structural change. The git-shell Trees dependency needed a local clone and a git-level credential the sandbox likely doesn't have even with a bare API token. Adds two narrow forge capabilities (PullRequestDiffReader, RepoArchiveReader) implemented on both backends: - GitHub: PullRequests.GetRaw for the diff, Repositories.GetArchiveLink plus a manual unauthenticated fetch of the resolved (pre-signed, non-API) blob URL for the archive. - Gitea: GetPullRequestDiff and GetArchiveReader directly (no separate fetch needed). PRSource.Snapshot fetches the archive and extracts it with a new extractTarGz, which strips the wrapper directory both forges add (whether represented as a bare-name path prefix or an explicit tar directory header), refuses any entry that would escape destDir, and drops .git entries per the Isolation contract. Real bug caught before commit: the wrapper-stripping heuristic initially treated a lone top-level file the same as a top-level directory header, silently discarding the file entirely when no wrapper existed. Caught by TestExtractTarGzWithNoWrapperDirectory before it shipped. Not wired into any trigger yet -- watched-repositories and the operator trigger still need this Source constructed and handed to the pr-review task's TaskContext, which is where an execution-model decision (daemon-side vs sandboxed) has to land first. Left as a precise continuation on archie-core-afbk.7, not guessed past.
103 lines
4.3 KiB
Go
103 lines
4.3 KiB
Go
// Package prsource implements workflow.PRSource: fetching an arbitrary
|
|
// external pull request's metadata, diff and a read-only snapshot of its
|
|
// head, for the pr-review pipeline (docs/prds/pr-review-agent.md). It is
|
|
// the PR-under-review counterpart to worktree.Manager's use as the task's
|
|
// own Trees: that path only ever reaches the task's own worktree, never a
|
|
// third-party PR being reviewed.
|
|
//
|
|
// Every call goes over the forge's HTTP API -- never a git-shell checkout.
|
|
// The pipeline's Stage.Run bodies execute inside the sandboxed archie-agent
|
|
// process, which holds no forge credential and no git-level credential
|
|
// either; a git clone would need one. An HTTP call carries only whatever
|
|
// bearer token the forge client was constructed with, matching the PRD's
|
|
// Isolation rule ("no pipeline agent holds a forge credential") for the
|
|
// pipeline's own agent calls, while this package's construction site is the
|
|
// one place a forge-reading token is actually held.
|
|
package prsource
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
)
|
|
|
|
// PullRequest is the pull request fields Metadata, Diff and Snapshot need:
|
|
// title, body, and the head commit SHA the diff's line numbers and the
|
|
// snapshot are both measured against. It mirrors forge.PullRequest's shape
|
|
// so callers can pass that type directly without this package importing
|
|
// internal/forge.
|
|
type PullRequest struct {
|
|
Title string
|
|
Body string
|
|
HeadSHA string
|
|
}
|
|
|
|
// Forge is the narrow forge capability this package needs: reading a pull
|
|
// request's metadata, its diff, and an archive of its repository at a ref.
|
|
// Matches forge.PullRequestReader, forge.PullRequestDiffReader and
|
|
// forge.RepoArchiveReader.
|
|
type Forge interface {
|
|
GetPullRequest(ctx context.Context, owner, repo string, number int) (PullRequest, error)
|
|
GetPullRequestDiff(ctx context.Context, owner, repo string, number int) (string, error)
|
|
GetRepoArchive(ctx context.Context, owner, repo, ref string) (io.ReadCloser, error)
|
|
}
|
|
|
|
// Source implements workflow.PRSource against a real forge over HTTP.
|
|
type Source struct {
|
|
forge Forge
|
|
}
|
|
|
|
var _ workflow.PRSource = (*Source)(nil)
|
|
|
|
// New builds a Source. forge may not be nil; a caller that lacks a forge
|
|
// PR-reading capability must not construct one (matching the "a capability
|
|
// that cannot run is not advertised" rule the existing operator reviewer
|
|
// already follows).
|
|
func New(forge Forge) *Source {
|
|
return &Source{forge: forge}
|
|
}
|
|
|
|
// Metadata returns the pull request's title and body. Commit messages are
|
|
// left empty: no existing Forge capability returns them, and they are only
|
|
// ever one of three inputs to the hallucination-check heuristic (docs/prds/
|
|
// pr-review-agent.md, phase 1), not load-bearing on their own.
|
|
func (s *Source) Metadata(ctx context.Context, owner, repo string, number int) (workflow.PRMetadata, error) {
|
|
pr, err := s.forge.GetPullRequest(ctx, owner, repo, number)
|
|
if err != nil {
|
|
return workflow.PRMetadata{}, fmt.Errorf("prsource: fetch pull request metadata: %w", err)
|
|
}
|
|
return workflow.PRMetadata{Title: pr.Title, Body: pr.Body}, nil
|
|
}
|
|
|
|
// Diff fetches the pull request's unified diff directly over the forge's
|
|
// HTTP API.
|
|
func (s *Source) Diff(ctx context.Context, owner, repo string, number int) (string, error) {
|
|
diff, err := s.forge.GetPullRequestDiff(ctx, owner, repo, number)
|
|
if err != nil {
|
|
return "", fmt.Errorf("prsource: fetch pull request diff: %w", err)
|
|
}
|
|
return diff, nil
|
|
}
|
|
|
|
// Snapshot fetches a repository archive at the pull request's head commit
|
|
// and extracts it into destDir with no .git present, returning the head
|
|
// SHA the forge reported -- the same value the diff's line numbers were
|
|
// measured against.
|
|
func (s *Source) Snapshot(ctx context.Context, owner, repo string, number int, destDir string) (string, error) {
|
|
pr, err := s.forge.GetPullRequest(ctx, owner, repo, number)
|
|
if err != nil {
|
|
return "", fmt.Errorf("prsource: fetch pull request metadata: %w", err)
|
|
}
|
|
archive, err := s.forge.GetRepoArchive(ctx, owner, repo, pr.HeadSHA)
|
|
if err != nil {
|
|
return "", fmt.Errorf("prsource: fetch repository archive at %s: %w", pr.HeadSHA, err)
|
|
}
|
|
defer archive.Close()
|
|
if err := extractTarGz(archive, destDir); err != nil {
|
|
return "", fmt.Errorf("prsource: extract repository archive: %w", err)
|
|
}
|
|
return pr.HeadSHA, nil
|
|
}
|