Files
archie/internal/forge/gitea_methods_test.go
T
sam 600a05539b 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.
2026-09-28 14:46:09 +10:00

420 lines
13 KiB
Go

package forge
import (
"io"
"net/http"
"testing"
)
func TestGiteaAssignedIssuesExcludesPRs(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("GET /api/v1/repos/o/r/issues", func(w http.ResponseWriter, r *http.Request) {
if got := r.URL.Query().Get("assigned_by"); got != "archie-bot" {
t.Errorf("assigned_by = %q", got)
}
writeJSON(t, w, []map[string]any{
{"number": 1, "title": "real issue", "body": "b", "labels": []map[string]any{{"name": "bug"}}},
{"number": 2, "title": "a pr", "pull_request": map[string]any{"url": "x"}},
})
})
issues, err := c.AssignedIssues(t.Context(), "o", "r", "archie-bot")
if err != nil {
t.Fatal(err)
}
if len(issues) != 1 || issues[0].Number != 1 || issues[0].Title != "real issue" || len(issues[0].Labels) != 1 || issues[0].Labels[0] != "bug" {
t.Fatalf("issues = %+v", issues)
}
}
func TestGiteaIssuesWithLabelExcludesPRs(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("GET /api/v1/repos/o/r/issues", func(w http.ResponseWriter, r *http.Request) {
if got := r.URL.Query().Get("labels"); got != "widget" {
t.Errorf("labels = %q", got)
}
writeJSON(t, w, []map[string]any{
{"number": 3, "title": "labelled", "body": "b"},
})
})
issues, err := c.IssuesWithLabel(t.Context(), "o", "r", "widget")
if err != nil {
t.Fatal(err)
}
if len(issues) != 1 || issues[0].Number != 3 {
t.Fatalf("issues = %+v", issues)
}
}
func TestGiteaAssignedIssuesError(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("GET /api/v1/repos/o/r/issues", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
if _, err := c.AssignedIssues(t.Context(), "o", "r", "archie-bot"); err == nil {
t.Fatal("AssignedIssues() error = nil, want error")
}
}
func TestGiteaComment(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("POST /api/v1/repos/o/r/issues/5/comments", func(w http.ResponseWriter, r *http.Request) {
writeJSON(t, w, map[string]any{"id": 42})
})
id, err := c.Comment(t.Context(), "o", "r", 5, "hello")
if err != nil {
t.Fatal(err)
}
if id != 42 {
t.Fatalf("id = %d, want 42", id)
}
}
func TestGiteaCreatePR(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("POST /api/v1/repos/o/r/pulls", func(w http.ResponseWriter, r *http.Request) {
writeJSON(t, w, map[string]any{"number": 7})
})
num, err := c.CreatePR(t.Context(), "o", "r", "title", "head", "base", "body")
if err != nil {
t.Fatal(err)
}
if num != 7 {
t.Fatalf("num = %d, want 7", num)
}
}
func TestGiteaCreatePRError(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("POST /api/v1/repos/o/r/pulls", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnprocessableEntity)
})
if _, err := c.CreatePR(t.Context(), "o", "r", "title", "head", "base", "body"); err == nil {
t.Fatal("CreatePR() error = nil, want error")
}
}
func TestGiteaPRState(t *testing.T) {
tests := []struct {
name string
status int
body map[string]any
want string
wantErr bool
}{
{name: "open", status: http.StatusOK, body: map[string]any{"number": 1, "state": "open", "merged": false}, want: "open"},
{name: "merged", status: http.StatusOK, body: map[string]any{"number": 1, "state": "closed", "merged": true}, want: "merged"},
{name: "not found treated as closed", status: http.StatusNotFound, body: map[string]any{"message": "not found"}, want: "closed"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("GET /api/v1/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tt.status)
if tt.body != nil {
writeJSON(t, w, tt.body)
}
})
state, err := c.PRState(t.Context(), "o", "r", 1)
if tt.wantErr {
if err == nil {
t.Fatal("PRState() error = nil, want error")
}
return
}
if err != nil {
t.Fatal(err)
}
if state != tt.want {
t.Fatalf("state = %q, want %q", state, tt.want)
}
})
}
}
func TestGiteaGetPullRequest(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("GET /api/v1/repos/o/r/pulls/7", func(w http.ResponseWriter, r *http.Request) {
writeJSON(t, w, map[string]any{
"number": 7, "title": "Add widget", "body": "Why",
"state": "open", "merged": false,
"head": map[string]any{"ref": "feature/widget", "sha": "headsha"},
"base": map[string]any{"ref": "main", "sha": "basesha"},
})
})
pr, err := c.GetPullRequest(t.Context(), "o", "r", 7)
if err != nil {
t.Fatal(err)
}
want := PullRequest{Number: 7, Title: "Add widget", Body: "Why", HeadRef: "feature/widget", BaseRef: "main", HeadSHA: "headsha", BaseSHA: "basesha", State: "open"}
if pr != want {
t.Fatalf("GetPullRequest = %+v, want %+v", pr, want)
}
}
func TestGiteaGetPullRequestMissingHeadDoesNotPanic(t *testing.T) {
c, mux := newTestGiteaClient(t)
// Gitea returns head: null when a PR's head branch was deleted.
mux.HandleFunc("GET /api/v1/repos/o/r/pulls/7", func(w http.ResponseWriter, r *http.Request) {
writeJSON(t, w, map[string]any{
"number": 7, "title": "Add widget", "state": "closed", "merged": true,
"head": nil, "base": nil,
})
})
if _, err := c.GetPullRequest(t.Context(), "o", "r", 7); err == nil {
t.Fatal("GetPullRequest error = nil, want a missing-head/base error rather than a panic")
}
}
func TestGiteaCloseIssueWithComment(t *testing.T) {
c, mux := newTestGiteaClient(t)
var commented, closed bool
mux.HandleFunc("POST /api/v1/repos/o/r/issues/9/comments", func(w http.ResponseWriter, r *http.Request) {
commented = true
writeJSON(t, w, map[string]any{"id": 1})
})
mux.HandleFunc("PATCH /api/v1/repos/o/r/issues/9", func(w http.ResponseWriter, r *http.Request) {
closed = true
writeJSON(t, w, map[string]any{"number": 9, "state": "closed"})
})
if err := c.CloseIssue(t.Context(), "o", "r", 9, "final comment"); err != nil {
t.Fatal(err)
}
if !commented || !closed {
t.Fatalf("commented=%t closed=%t, want both true", commented, closed)
}
}
func TestGiteaCloseIssueWithoutComment(t *testing.T) {
c, mux := newTestGiteaClient(t)
var commented bool
mux.HandleFunc("POST /api/v1/repos/o/r/issues/9/comments", func(w http.ResponseWriter, r *http.Request) {
commented = true
})
mux.HandleFunc("PATCH /api/v1/repos/o/r/issues/9", func(w http.ResponseWriter, r *http.Request) {
writeJSON(t, w, map[string]any{"number": 9, "state": "closed"})
})
if err := c.CloseIssue(t.Context(), "o", "r", 9, ""); err != nil {
t.Fatal(err)
}
if commented {
t.Fatal("commented = true, want false when comment is empty")
}
}
func TestGiteaReact(t *testing.T) {
c, mux := newTestGiteaClient(t)
var got string
mux.HandleFunc("POST /api/v1/repos/o/r/issues/1/reactions", func(w http.ResponseWriter, r *http.Request) {
got = "called"
writeJSON(t, w, map[string]any{"content": "+1"})
})
if err := c.React(t.Context(), "o", "r", 1, "+1"); err != nil {
t.Fatal(err)
}
if got != "called" {
t.Fatal("reaction endpoint was not called")
}
}
func TestGiteaAcceptInvitationsIsNoop(t *testing.T) {
c, _ := newTestGiteaClient(t)
if err := c.AcceptInvitations(t.Context()); err != nil {
t.Fatalf("AcceptInvitations() = %v, want nil", err)
}
}
func TestGiteaVerifyPush(t *testing.T) {
tests := []struct {
name string
status int
body map[string]any
wantErr bool
}{
{name: "has push", status: http.StatusOK, body: map[string]any{"permissions": map[string]any{"push": true}}},
{name: "no push", status: http.StatusOK, body: map[string]any{"permissions": map[string]any{"push": false}}, wantErr: true},
{name: "not found", status: http.StatusNotFound, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("GET /api/v1/repos/o/r", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tt.status)
if tt.body != nil {
writeJSON(t, w, tt.body)
}
})
err := c.VerifyPush(t.Context(), "o", "r")
if tt.wantErr && err == nil {
t.Fatal("VerifyPush() error = nil, want error")
}
if !tt.wantErr && err != nil {
t.Fatalf("VerifyPush() error = %v, want nil", err)
}
})
}
}
func TestGiteaLinkBranch(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("POST /api/v1/repos/o/r/issues/3/refs", func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("Authorization"); got != "token token" {
t.Errorf("Authorization = %q", got)
}
w.WriteHeader(http.StatusOK)
})
if err := c.LinkBranch(t.Context(), "o", "r", 3, "feature-branch"); err != nil {
t.Fatal(err)
}
}
func TestGiteaLinkBranchError(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("POST /api/v1/repos/o/r/issues/3/refs", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
if err := c.LinkBranch(t.Context(), "o", "r", 3, "feature-branch"); err == nil {
t.Fatal("LinkBranch() error = nil, want error")
}
}
func TestGiteaSetStateLabelSwapsLabels(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("GET /api/v1/repos/o/r/issues/4", func(w http.ResponseWriter, r *http.Request) {
writeJSON(t, w, map[string]any{
"number": 4,
"labels": []map[string]any{
{"id": 1, "name": "archie:queued"},
{"id": 2, "name": "keep-me"},
},
})
})
var deletedID int64
mux.HandleFunc("DELETE /api/v1/repos/o/r/issues/4/labels/1", func(w http.ResponseWriter, r *http.Request) {
deletedID = 1
w.WriteHeader(http.StatusOK)
})
mux.HandleFunc("GET /api/v1/repos/o/r/labels", func(w http.ResponseWriter, r *http.Request) {
writeJSON(t, w, []map[string]any{{"id": 9, "name": "archie:working"}})
})
var added []int64
mux.HandleFunc("POST /api/v1/repos/o/r/issues/4/labels", func(w http.ResponseWriter, r *http.Request) {
added = []int64{9}
writeJSON(t, w, []map[string]any{{"id": 9, "name": "archie:working"}})
})
known := []string{"archie:queued", "archie:working", "archie:waiting"}
c.SetStateLabel(t.Context(), "o", "r", 4, "archie:working", known)
if deletedID != 1 {
t.Errorf("deletedID = %d, want 1 (archie:queued removed)", deletedID)
}
if len(added) != 1 || added[0] != 9 {
t.Errorf("added = %v, want [9]", added)
}
}
func TestGiteaSetStateLabelAlreadyPresentIsNoop(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("GET /api/v1/repos/o/r/issues/4", func(w http.ResponseWriter, r *http.Request) {
writeJSON(t, w, map[string]any{
"number": 4,
"labels": []map[string]any{{"id": 9, "name": "archie:working"}},
})
})
called := false
mux.HandleFunc("POST /api/v1/repos/o/r/issues/4/labels", func(w http.ResponseWriter, r *http.Request) {
called = true
})
c.SetStateLabel(t.Context(), "o", "r", 4, "archie:working", []string{"archie:working"})
if called {
t.Fatal("AddIssueLabels was called, want no-op when label already present")
}
}
func TestGiteaSetStateLabelEmptyClearsAll(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("GET /api/v1/repos/o/r/issues/4", func(w http.ResponseWriter, r *http.Request) {
writeJSON(t, w, map[string]any{
"number": 4,
"labels": []map[string]any{{"id": 1, "name": "archie:queued"}},
})
})
deleted := false
mux.HandleFunc("DELETE /api/v1/repos/o/r/issues/4/labels/1", func(w http.ResponseWriter, r *http.Request) {
deleted = true
w.WriteHeader(http.StatusOK)
})
added := false
mux.HandleFunc("POST /api/v1/repos/o/r/issues/4/labels", func(w http.ResponseWriter, r *http.Request) {
added = true
})
c.SetStateLabel(t.Context(), "o", "r", 4, "", []string{"archie:queued"})
if !deleted {
t.Error("deleted = false, want true (archie:queued removed)")
}
if added {
t.Error("added = true, want false (empty label clears all)")
}
}
func TestLabelNamesGitea(t *testing.T) {
names := labelNamesGitea(nil)
if names != nil {
t.Fatalf("labelNamesGitea(nil) = %v, want nil", names)
}
}
func TestGiteaGetPullRequestDiff(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("GET /api/v1/repos/o/r/pulls/7.diff", func(w http.ResponseWriter, r *http.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 TestGiteaGetRepoArchive(t *testing.T) {
c, mux := newTestGiteaClient(t)
mux.HandleFunc("GET /api/v1/repos/o/r/archive/deadbeef.tar.gz", func(w http.ResponseWriter, r *http.Request) {
_, _ = 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)
}
}