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.
448 lines
14 KiB
Go
448 lines
14 KiB
Go
package forge
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
func writeJSON(t *testing.T, w http.ResponseWriter, v any) {
|
|
t.Helper()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestAssignedIssuesExcludesPRs(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("GET /repos/o/r/issues", func(w http.ResponseWriter, r *http.Request) {
|
|
if got := r.URL.Query().Get("assignee"); got != "archie-bot" {
|
|
t.Errorf("assignee = %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 TestIssuesWithLabelExcludesPRs(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("GET /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 TestComment(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("POST /repos/o/r/issues/5/comments", func(w http.ResponseWriter, r *http.Request) {
|
|
var body map[string]string
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if body["body"] != "hello" {
|
|
t.Errorf("comment body = %q", body["body"])
|
|
}
|
|
writeJSON(t, w, map[string]any{"id": 42})
|
|
})
|
|
|
|
id, err := c.Comment(t.Context(), "o", "r", 5, "hello")
|
|
if err != nil || id != 42 {
|
|
t.Fatalf("Comment = (%d, %v)", id, err)
|
|
}
|
|
}
|
|
|
|
func TestCreatePR(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("POST /repos/o/r/pulls", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, map[string]any{"number": 9})
|
|
})
|
|
|
|
n, err := c.CreatePR(t.Context(), "o", "r", "title", "head", "base", "body")
|
|
if err != nil || n != 9 {
|
|
t.Fatalf("CreatePR = (%d, %v)", n, err)
|
|
}
|
|
}
|
|
|
|
func TestPRState(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
json map[string]any
|
|
want string
|
|
}{
|
|
{"merged", map[string]any{"merged": true, "state": "closed"}, "merged"},
|
|
{"open", map[string]any{"merged": false, "state": "open"}, "open"},
|
|
{"closed", map[string]any{"merged": false, "state": "closed"}, "closed"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("GET /repos/o/r/pulls/7", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, tt.json)
|
|
})
|
|
got, err := c.PRState(t.Context(), "o", "r", 7)
|
|
if err != nil || got != tt.want {
|
|
t.Fatalf("PRState = (%q, %v), want %q", got, err, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetPullRequest(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
json map[string]any
|
|
want PullRequest
|
|
}{
|
|
{
|
|
name: "open",
|
|
json: map[string]any{
|
|
"number": 7, "title": "Add widget", "body": "Why this matters",
|
|
"state": "open", "merged": false,
|
|
"head": map[string]any{"ref": "feature/widget", "sha": "headsha"},
|
|
"base": map[string]any{"ref": "main", "sha": "basesha"},
|
|
},
|
|
want: PullRequest{Number: 7, Title: "Add widget", Body: "Why this matters", HeadRef: "feature/widget", BaseRef: "main", HeadSHA: "headsha", BaseSHA: "basesha", State: "open"},
|
|
},
|
|
{
|
|
name: "merged",
|
|
json: map[string]any{
|
|
"number": 7, "title": "Add widget", "body": "",
|
|
"state": "closed", "merged": true,
|
|
"head": map[string]any{"ref": "feature/widget", "sha": "headsha"},
|
|
"base": map[string]any{"ref": "main", "sha": "basesha"},
|
|
},
|
|
want: PullRequest{Number: 7, Title: "Add widget", HeadRef: "feature/widget", BaseRef: "main", HeadSHA: "headsha", BaseSHA: "basesha", State: "merged"},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("GET /repos/o/r/pulls/7", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, tt.json)
|
|
})
|
|
got, err := c.GetPullRequest(t.Context(), "o", "r", 7)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != tt.want {
|
|
t.Fatalf("GetPullRequest = %+v, want %+v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCloseIssueWithComment(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
var commented, edited bool
|
|
mux.HandleFunc("POST /repos/o/r/issues/5/comments", func(w http.ResponseWriter, r *http.Request) {
|
|
commented = true
|
|
writeJSON(t, w, map[string]any{"id": 1})
|
|
})
|
|
mux.HandleFunc("PATCH /repos/o/r/issues/5", func(w http.ResponseWriter, r *http.Request) {
|
|
edited = true
|
|
var body map[string]string
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if body["state"] != "closed" {
|
|
t.Errorf("state = %q", body["state"])
|
|
}
|
|
writeJSON(t, w, map[string]any{"number": 5, "state": "closed"})
|
|
})
|
|
|
|
if err := c.CloseIssue(t.Context(), "o", "r", 5, "won't do"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !commented || !edited {
|
|
t.Fatalf("commented=%v edited=%v", commented, edited)
|
|
}
|
|
}
|
|
|
|
func TestCloseIssueWithoutComment(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("POST /repos/o/r/issues/5/comments", func(w http.ResponseWriter, r *http.Request) {
|
|
t.Fatal("should not comment when comment is empty")
|
|
})
|
|
mux.HandleFunc("PATCH /repos/o/r/issues/5", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, map[string]any{"number": 5, "state": "closed"})
|
|
})
|
|
|
|
if err := c.CloseIssue(t.Context(), "o", "r", 5, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestReact(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("POST /repos/o/r/issues/5/reactions", func(w http.ResponseWriter, r *http.Request) {
|
|
var body map[string]string
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if body["content"] != "+1" {
|
|
t.Errorf("content = %q", body["content"])
|
|
}
|
|
writeJSON(t, w, map[string]any{"id": 1, "content": "+1"})
|
|
})
|
|
|
|
if err := c.React(t.Context(), "o", "r", 5, "+1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestSetStateLabelSwapsLabels(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
var removed, added, created []string
|
|
|
|
mux.HandleFunc("GET /repos/o/r/issues/5", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, map[string]any{
|
|
"number": 5,
|
|
"labels": []map[string]any{{"name": "archie:queued"}, {"name": "bug"}},
|
|
})
|
|
})
|
|
mux.HandleFunc("DELETE /repos/o/r/issues/5/labels/archie:queued", func(w http.ResponseWriter, r *http.Request) {
|
|
removed = append(removed, "archie:queued")
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
mux.HandleFunc("POST /repos/o/r/labels", func(w http.ResponseWriter, r *http.Request) {
|
|
var body map[string]string
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
created = append(created, body["name"])
|
|
writeJSON(t, w, map[string]any{"name": body["name"], "color": body["color"]})
|
|
})
|
|
mux.HandleFunc("POST /repos/o/r/issues/5/labels", func(w http.ResponseWriter, r *http.Request) {
|
|
var body []string
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
added = append(added, body...)
|
|
writeJSON(t, w, []map[string]any{})
|
|
})
|
|
|
|
known := []string{"archie:queued", "archie:working", "archie:waiting", "archie:pr", "archie:parked"}
|
|
c.SetStateLabel(t.Context(), "o", "r", 5, "archie:working", known)
|
|
|
|
if len(removed) != 1 || removed[0] != "archie:queued" {
|
|
t.Fatalf("removed = %v", removed)
|
|
}
|
|
if len(created) != 1 || created[0] != "archie:working" {
|
|
t.Fatalf("created = %v", created)
|
|
}
|
|
if len(added) != 1 || added[0] != "archie:working" {
|
|
t.Fatalf("added = %v", added)
|
|
}
|
|
|
|
// Second call for the same label reuses the ensured-labels cache and
|
|
// skips label creation.
|
|
mux.HandleFunc("GET /repos/o/r/issues/6", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, map[string]any{"number": 6, "labels": []map[string]any{}})
|
|
})
|
|
mux.HandleFunc("POST /repos/o/r/issues/6/labels", func(w http.ResponseWriter, r *http.Request) {
|
|
var body []string
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
added = append(added, body...)
|
|
writeJSON(t, w, []map[string]any{})
|
|
})
|
|
c.SetStateLabel(t.Context(), "o", "r", 6, "archie:working", known)
|
|
if len(created) != 1 {
|
|
t.Fatalf("expected label creation to be cached, created = %v", created)
|
|
}
|
|
}
|
|
|
|
func TestSetStateLabelAlreadyPresentIsNoop(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("GET /repos/o/r/issues/5", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, map[string]any{
|
|
"number": 5,
|
|
"labels": []map[string]any{{"name": "archie:working"}},
|
|
})
|
|
})
|
|
mux.HandleFunc("POST /repos/o/r/issues/5/labels", func(w http.ResponseWriter, r *http.Request) {
|
|
t.Fatal("should not add a label that's already present")
|
|
})
|
|
mux.HandleFunc("POST /repos/o/r/labels", func(w http.ResponseWriter, r *http.Request) {
|
|
t.Fatal("should not create a label that's already present")
|
|
})
|
|
|
|
c.SetStateLabel(t.Context(), "o", "r", 5, "archie:working", []string{"archie:working"})
|
|
}
|
|
|
|
func TestSetStateLabelEmptyClearsAll(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
var removed []string
|
|
mux.HandleFunc("GET /repos/o/r/issues/5", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, map[string]any{
|
|
"number": 5,
|
|
"labels": []map[string]any{{"name": "archie:working"}},
|
|
})
|
|
})
|
|
mux.HandleFunc("DELETE /repos/o/r/issues/5/labels/archie:working", func(w http.ResponseWriter, r *http.Request) {
|
|
removed = append(removed, "archie:working")
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
|
|
c.SetStateLabel(t.Context(), "o", "r", 5, "", []string{"archie:working"})
|
|
if len(removed) != 1 {
|
|
t.Fatalf("removed = %v", removed)
|
|
}
|
|
}
|
|
|
|
func TestVerifyPush(t *testing.T) {
|
|
t.Run("has push", func(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("GET /repos/o/r", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, map[string]any{"permissions": map[string]bool{"push": true}})
|
|
})
|
|
if err := c.VerifyPush(t.Context(), "o", "r"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
|
|
t.Run("no push", func(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("GET /repos/o/r", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, map[string]any{"permissions": map[string]bool{"push": false}})
|
|
})
|
|
if err := c.VerifyPush(t.Context(), "o", "r"); err == nil {
|
|
t.Fatal("expected error for missing push permission")
|
|
}
|
|
})
|
|
|
|
t.Run("api error", func(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("GET /repos/o/r", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
})
|
|
if err := c.VerifyPush(t.Context(), "o", "r"); err == nil {
|
|
t.Fatal("expected error for 404")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestAcceptInvitations(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
var accepted []string
|
|
mux.HandleFunc("GET /user/repository_invitations", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, []map[string]any{
|
|
{"id": 1, "repository": map[string]any{"full_name": "o/ok"}},
|
|
{"id": 2, "repository": map[string]any{"full_name": "o/fails"}},
|
|
})
|
|
})
|
|
mux.HandleFunc("PATCH /user/repository_invitations/1", func(w http.ResponseWriter, r *http.Request) {
|
|
accepted = append(accepted, "1")
|
|
w.WriteHeader(http.StatusNoContent)
|
|
})
|
|
mux.HandleFunc("PATCH /user/repository_invitations/2", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
})
|
|
|
|
// A single failing invitation must not stop the others from being
|
|
// processed or fail the whole call.
|
|
if err := c.AcceptInvitations(t.Context()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(accepted) != 1 || accepted[0] != "1" {
|
|
t.Fatalf("accepted = %v", accepted)
|
|
}
|
|
}
|
|
|
|
func TestAcceptInvitationsListError(t *testing.T) {
|
|
c, mux := newTestClient(t)
|
|
mux.HandleFunc("GET /user/repository_invitations", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
})
|
|
if err := c.AcceptInvitations(t.Context()); err == nil {
|
|
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")
|
|
}
|
|
}
|