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.
108 lines
3.0 KiB
Go
108 lines
3.0 KiB
Go
package prsource
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
type fakeForge struct {
|
|
pr PullRequest
|
|
diff string
|
|
archive []byte
|
|
err error
|
|
}
|
|
|
|
func (f *fakeForge) GetPullRequest(context.Context, string, string, int) (PullRequest, error) {
|
|
return f.pr, f.err
|
|
}
|
|
|
|
func (f *fakeForge) GetPullRequestDiff(context.Context, string, string, int) (string, error) {
|
|
return f.diff, f.err
|
|
}
|
|
|
|
func (f *fakeForge) GetRepoArchive(context.Context, string, string, string) (io.ReadCloser, error) {
|
|
if f.err != nil {
|
|
return nil, f.err
|
|
}
|
|
return io.NopCloser(bytes.NewReader(f.archive)), nil
|
|
}
|
|
|
|
func TestMetadataMapsForgePullRequest(t *testing.T) {
|
|
forge := &fakeForge{pr: PullRequest{Title: "t", Body: "b", HeadSHA: "abc"}}
|
|
s := New(forge)
|
|
|
|
meta, err := s.Metadata(context.Background(), "acme", "widgets", 7)
|
|
if err != nil {
|
|
t.Fatalf("Metadata: %v", err)
|
|
}
|
|
if meta.Title != "t" || meta.Body != "b" {
|
|
t.Errorf("meta = %+v, want title/body from the forge PR", meta)
|
|
}
|
|
}
|
|
|
|
func TestMetadataPropagatesForgeError(t *testing.T) {
|
|
wantErr := errors.New("forge unreachable")
|
|
s := New(&fakeForge{err: wantErr})
|
|
|
|
if _, err := s.Metadata(context.Background(), "acme", "widgets", 7); !errors.Is(err, wantErr) {
|
|
t.Fatalf("err = %v, want %v", err, wantErr)
|
|
}
|
|
}
|
|
|
|
func TestDiffFetchesOverHTTPWithNoCheckout(t *testing.T) {
|
|
forge := &fakeForge{diff: "diff --git a b"}
|
|
s := New(forge)
|
|
|
|
diff, err := s.Diff(context.Background(), "acme", "widgets", 7)
|
|
if err != nil {
|
|
t.Fatalf("Diff: %v", err)
|
|
}
|
|
if diff != "diff --git a b" {
|
|
t.Errorf("diff = %q, want the forge's raw diff response", diff)
|
|
}
|
|
}
|
|
|
|
func TestDiffPropagatesForgeError(t *testing.T) {
|
|
wantErr := errors.New("diff fetch failed")
|
|
s := New(&fakeForge{err: wantErr})
|
|
|
|
if _, err := s.Diff(context.Background(), "acme", "widgets", 7); !errors.Is(err, wantErr) {
|
|
t.Fatalf("err = %v, want %v", err, wantErr)
|
|
}
|
|
}
|
|
|
|
func TestSnapshotExtractsTheArchiveAndReturnsTheHeadSHA(t *testing.T) {
|
|
archive := buildTarGz(t, "acme-widgets-deadbeef", map[string]string{"main.go": "package main\n"})
|
|
forge := &fakeForge{pr: PullRequest{HeadSHA: "deadbeef"}, archive: archive}
|
|
s := New(forge)
|
|
dest := t.TempDir()
|
|
|
|
headSHA, err := s.Snapshot(context.Background(), "acme", "widgets", 7, dest)
|
|
if err != nil {
|
|
t.Fatalf("Snapshot: %v", err)
|
|
}
|
|
if headSHA != "deadbeef" {
|
|
t.Errorf("headSHA = %q, want the forge-reported head SHA", headSHA)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dest, "main.go")); err != nil {
|
|
t.Errorf("expected the extracted file to exist: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dest, "acme-widgets-deadbeef")); err == nil {
|
|
t.Error("the wrapper directory itself should not survive extraction")
|
|
}
|
|
}
|
|
|
|
func TestSnapshotPropagatesForgeError(t *testing.T) {
|
|
wantErr := errors.New("archive fetch failed")
|
|
s := New(&fakeForge{err: wantErr})
|
|
|
|
if _, err := s.Snapshot(context.Background(), "acme", "widgets", 7, t.TempDir()); !errors.Is(err, wantErr) {
|
|
t.Fatalf("err = %v, want %v", err, wantErr)
|
|
}
|
|
}
|