feat(prsource): rebuild PRSource over the forge's HTTP API, no git checkout

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.
This commit is contained in:
sam committed 2026-09-28 14:46:09 +10:00
1 parent 148a764521
commit 7ddaed386b
9 files changed
+571 -90

No files matched your search

+63
View File
@@ -2,6 +2,7 @@ package forge
import (
"encoding/json"
"io"
"net/http"
"testing"
)
@@ -382,3 +383,65 @@ func TestAcceptInvitationsListError(t *testing.T) {
t.Fatal("expected error")
}
}
func TestGetPullRequestDiff(t *testing.T) {
c, mux := newTestClient(t)
mux.HandleFunc("GET /repos/o/r/pulls/7", func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("Accept"); got == "" {
t.Errorf("Accept header not set for a raw diff request")
}
_, _ = w.Write([]byte("diff --git a/f.go b/f.go\n+added\n"))
})
diff, err := c.GetPullRequestDiff(t.Context(), "o", "r", 7)
if err != nil {
t.Fatal(err)
}
if diff != "diff --git a/f.go b/f.go\n+added\n" {
t.Fatalf("diff = %q", diff)
}
}
func TestGetRepoArchive(t *testing.T) {
c, mux := newTestClient(t)
mux.HandleFunc("GET /repos/o/r/tarball/deadbeef", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", "http://"+r.Host+"/archive-blob/deadbeef.tar.gz")
w.WriteHeader(http.StatusFound)
})
mux.HandleFunc("GET /archive-blob/deadbeef.tar.gz", func(w http.ResponseWriter, r *http.Request) {
// Real GitHub archive links are unauthenticated storage URLs; assert
// no forge token leaks onto this second request.
if got := r.Header.Get("Authorization"); got != "" {
t.Errorf("Authorization header present on the archive blob request: %q", got)
}
_, _ = w.Write([]byte("fake-tar-bytes"))
})
rc, err := c.GetRepoArchive(t.Context(), "o", "r", "deadbeef")
if err != nil {
t.Fatal(err)
}
defer rc.Close()
body, err := io.ReadAll(rc)
if err != nil {
t.Fatal(err)
}
if string(body) != "fake-tar-bytes" {
t.Fatalf("archive body = %q", body)
}
}
func TestGetRepoArchiveNonOKStatus(t *testing.T) {
c, mux := newTestClient(t)
mux.HandleFunc("GET /repos/o/r/tarball/badref", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", "http://"+r.Host+"/archive-blob/badref.tar.gz")
w.WriteHeader(http.StatusFound)
})
mux.HandleFunc("GET /archive-blob/badref.tar.gz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
if _, err := c.GetRepoArchive(t.Context(), "o", "r", "badref"); err == nil {
t.Fatal("want an error for a non-200 archive blob response")
}
}