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:
9 files changed
+571
-90
No files matched your search
@@ -6,6 +6,7 @@ package forge
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -96,6 +97,21 @@ type PullRequestReader interface {
|
|||||||
GetPullRequest(ctx context.Context, owner, repo string, number int) (PullRequest, error)
|
GetPullRequest(ctx context.Context, owner, repo string, number int) (PullRequest, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PullRequestDiffReader fetches a pull request's unified diff over the
|
||||||
|
// forge's HTTP API. Implementations that cannot read PRs do not implement
|
||||||
|
// it, matching PullRequestReader.
|
||||||
|
type PullRequestDiffReader interface {
|
||||||
|
GetPullRequestDiff(ctx context.Context, owner, repo string, number int) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RepoArchiveReader fetches a gzipped tar archive of a repository at a ref
|
||||||
|
// over the forge's HTTP API, with no git-level credential involved: the
|
||||||
|
// caller reads the returned stream and closes it. Implementations that
|
||||||
|
// cannot read repository contents do not implement it.
|
||||||
|
type RepoArchiveReader interface {
|
||||||
|
GetRepoArchive(ctx context.Context, owner, repo, ref string) (io.ReadCloser, error)
|
||||||
|
}
|
||||||
|
|
||||||
// RepoForge is repository-level operations.
|
// RepoForge is repository-level operations.
|
||||||
type RepoForge interface {
|
type RepoForge interface {
|
||||||
// AcceptInvitations auto-accepts pending repository invitations.
|
// AcceptInvitations auto-accepts pending repository invitations.
|
||||||
|
|||||||
@@ -171,6 +171,27 @@ func (c *GiteaClient) GetPullRequest(ctx context.Context, owner, repo string, nu
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetPullRequestDiff fetches the pull request's unified diff over the Gitea
|
||||||
|
// API, never a local git clone.
|
||||||
|
func (c *GiteaClient) GetPullRequestDiff(_ context.Context, owner, repo string, number int) (string, error) {
|
||||||
|
diff, _, err := c.cli.GetPullRequestDiff(owner, repo, int64(number), gitea.PullRequestDiffOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("get pull request diff %s/%s#%d: %w", owner, repo, number, err)
|
||||||
|
}
|
||||||
|
return string(diff), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRepoArchive fetches a gzipped tar archive of the repository at ref
|
||||||
|
// directly from Gitea's own API -- unlike GitHub, no separate unauthenticated
|
||||||
|
// fetch is needed.
|
||||||
|
func (c *GiteaClient) GetRepoArchive(_ context.Context, owner, repo, ref string) (io.ReadCloser, error) {
|
||||||
|
body, _, err := c.cli.GetArchiveReader(owner, repo, ref, gitea.TarGZArchive)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("get repo archive %s/%s@%s: %w", owner, repo, ref, err)
|
||||||
|
}
|
||||||
|
return body, nil
|
||||||
|
}
|
||||||
|
|
||||||
// ListReviews returns the reviews on a PR with ID > sinceID.
|
// ListReviews returns the reviews on a PR with ID > sinceID.
|
||||||
func (c *GiteaClient) ListReviews(ctx context.Context, owner, repo string, number int, sinceID int64) ([]Review, error) {
|
func (c *GiteaClient) ListReviews(ctx context.Context, owner, repo string, number int, sinceID int64) ([]Review, error) {
|
||||||
var out []Review
|
var out []Review
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package forge
|
package forge
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -381,3 +382,38 @@ func TestLabelNamesGitea(t *testing.T) {
|
|||||||
t.Fatalf("labelNamesGitea(nil) = %v, want nil", names)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -190,6 +192,42 @@ func (c *GitHubClient) GetPullRequest(ctx context.Context, owner, repo string, n
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetPullRequestDiff fetches the pull request's unified diff over the
|
||||||
|
// GitHub API, never a local git clone.
|
||||||
|
func (c *GitHubClient) GetPullRequestDiff(ctx context.Context, owner, repo string, number int) (string, error) {
|
||||||
|
diff, _, err := c.gh.PullRequests.GetRaw(ctx, owner, repo, number, github.RawOptions{Type: github.Diff})
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("get pull request diff %s/%s#%d: %w", owner, repo, number, err)
|
||||||
|
}
|
||||||
|
return diff, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRepoArchive fetches a gzipped tar archive of the repository at ref.
|
||||||
|
// GetArchiveLink only resolves the download's final location; the archive's
|
||||||
|
// bytes still have to be fetched with a plain, unauthenticated GET (the URL
|
||||||
|
// GitHub redirects to is a pre-signed storage link, not a GitHub API
|
||||||
|
// endpoint), so no forge token is attached to this second request.
|
||||||
|
func (c *GitHubClient) GetRepoArchive(ctx context.Context, owner, repo, ref string) (io.ReadCloser, error) {
|
||||||
|
url, _, err := c.gh.Repositories.GetArchiveLink(ctx, owner, repo, github.Tarball, &github.RepositoryContentGetOptions{Ref: ref}, 5)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("resolve archive link %s/%s@%s: %w", owner, repo, ref, err)
|
||||||
|
}
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("build archive request %s/%s@%s: %w", owner, repo, ref, err)
|
||||||
|
}
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("fetch archive %s/%s@%s: %w", owner, repo, ref, err)
|
||||||
|
}
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
body, _ := io.ReadAll(io.LimitReader(resp.Body, 2048))
|
||||||
|
_ = resp.Body.Close()
|
||||||
|
return nil, fmt.Errorf("fetch archive %s/%s@%s: status %s: %s", owner, repo, ref, resp.Status, body)
|
||||||
|
}
|
||||||
|
return resp.Body, nil
|
||||||
|
}
|
||||||
|
|
||||||
// ListReviews returns the reviews on a PR with ID > sinceID.
|
// ListReviews returns the reviews on a PR with ID > sinceID.
|
||||||
func (c *GitHubClient) ListReviews(ctx context.Context, owner, repo string, number int, sinceID int64) ([]Review, error) {
|
func (c *GitHubClient) ListReviews(ctx context.Context, owner, repo string, number int, sinceID int64) ([]Review, error) {
|
||||||
var out []Review
|
var out []Review
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package forge
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -382,3 +383,65 @@ func TestAcceptInvitationsListError(t *testing.T) {
|
|||||||
t.Fatal("expected error")
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
package prsource
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"compress/gzip"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// extractTarGz extracts a gzipped tar stream into destDir, stripping a
|
||||||
|
// single common leading path component when every entry shares one (the
|
||||||
|
// wrapper directory GitHub's and Gitea's repository archives both add,
|
||||||
|
// named after the repo and ref -- e.g. "acme-widget-deadbeef/"). A .git
|
||||||
|
// entry is never written: PRSource's isolation contract (docs/prds/
|
||||||
|
// pr-review-agent.md) requires the pipeline to read a .git-free snapshot,
|
||||||
|
// and a malicious PR's own tarball is exactly the input this cannot trust,
|
||||||
|
// so an entry escaping destDir via ".." or an absolute path is refused
|
||||||
|
// rather than silently written outside it.
|
||||||
|
func extractTarGz(r io.Reader, destDir string) error {
|
||||||
|
gz, err := gzip.NewReader(r)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("prsource: open archive: %w", err)
|
||||||
|
}
|
||||||
|
defer gz.Close()
|
||||||
|
|
||||||
|
tr := tar.NewReader(gz)
|
||||||
|
prefix, entries, err := planExtraction(tr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, e := range entries {
|
||||||
|
if err := writeEntry(destDir, prefix, e); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// tarEntry is one archive entry buffered in memory so the wrapper-prefix
|
||||||
|
// decision (which needs to see every name first) doesn't require reading
|
||||||
|
// the tar stream twice.
|
||||||
|
type tarEntry struct {
|
||||||
|
header *tar.Header
|
||||||
|
data []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// planExtraction reads every entry and returns the common leading path
|
||||||
|
// component to strip, if every entry shares one.
|
||||||
|
func planExtraction(tr *tar.Reader) (prefix string, entries []tarEntry, err error) {
|
||||||
|
var commonPrefix string
|
||||||
|
first := true
|
||||||
|
for {
|
||||||
|
hdr, err := tr.Next()
|
||||||
|
if errors.Is(err, io.EOF) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, fmt.Errorf("prsource: read archive entry: %w", err)
|
||||||
|
}
|
||||||
|
name := path.Clean(filepath.ToSlash(hdr.Name))
|
||||||
|
if name == "." {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
top, _, hasSlash := strings.Cut(name, "/")
|
||||||
|
switch {
|
||||||
|
case !hasSlash && hdr.Typeflag != tar.TypeDir:
|
||||||
|
// A bare top-level file (no directory component at all) proves
|
||||||
|
// there is no single wrapper directory containing everything. A
|
||||||
|
// bare top-level *directory* entry is not this evidence -- it is
|
||||||
|
// exactly the wrapper directory's own header, which real
|
||||||
|
// archives include alongside the files nested under it.
|
||||||
|
commonPrefix = ""
|
||||||
|
first = false
|
||||||
|
case first:
|
||||||
|
commonPrefix = top
|
||||||
|
first = false
|
||||||
|
case commonPrefix != "" && top != commonPrefix:
|
||||||
|
commonPrefix = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var data []byte
|
||||||
|
if hdr.Typeflag == tar.TypeReg {
|
||||||
|
data, err = io.ReadAll(tr)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, fmt.Errorf("prsource: read archive entry %q: %w", name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
entries = append(entries, tarEntry{header: hdr, data: data})
|
||||||
|
}
|
||||||
|
return commonPrefix, entries, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeEntry writes one archive entry under destDir, with prefix (if any)
|
||||||
|
// stripped from its name. Directories, regular files, and nothing else are
|
||||||
|
// written: a symlink or device entry from an untrusted PR archive is
|
||||||
|
// skipped rather than followed.
|
||||||
|
func writeEntry(destDir, prefix string, e tarEntry) error {
|
||||||
|
name := path.Clean(filepath.ToSlash(e.header.Name))
|
||||||
|
if name == "." || name == prefix {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if prefix != "" {
|
||||||
|
cut, ok := strings.CutPrefix(name, prefix+"/")
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
name = cut
|
||||||
|
}
|
||||||
|
if name == "" || name == "." {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if isGitPath(name) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
target := filepath.Join(destDir, filepath.FromSlash(name))
|
||||||
|
rel, err := filepath.Rel(destDir, target)
|
||||||
|
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||||
|
return fmt.Errorf("prsource: archive entry %q escapes the destination directory", e.header.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch e.header.Typeflag {
|
||||||
|
case tar.TypeDir:
|
||||||
|
return os.MkdirAll(target, 0o755)
|
||||||
|
case tar.TypeReg:
|
||||||
|
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.WriteFile(target, e.data, 0o644)
|
||||||
|
default:
|
||||||
|
// Symlinks, devices, etc: not followed, not written.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// isGitPath reports whether name is ".git" or lives under it.
|
||||||
|
func isGitPath(name string) bool {
|
||||||
|
return name == ".git" || strings.HasPrefix(name, ".git/")
|
||||||
|
}
|
||||||
@@ -0,0 +1,164 @@
|
|||||||
|
package prsource
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// buildTarGz writes files (path -> content) into a gzipped tar stream,
|
||||||
|
// optionally wrapped in a single leading directory the way GitHub's and
|
||||||
|
// Gitea's repository archives are (e.g. "acme-widget-deadbeef/").
|
||||||
|
func buildTarGz(t *testing.T, wrapper string, files map[string]string) []byte {
|
||||||
|
t.Helper()
|
||||||
|
var buf bytes.Buffer
|
||||||
|
gz := gzip.NewWriter(&buf)
|
||||||
|
tw := tar.NewWriter(gz)
|
||||||
|
for path, content := range files {
|
||||||
|
full := path
|
||||||
|
if wrapper != "" {
|
||||||
|
full = wrapper + "/" + path
|
||||||
|
}
|
||||||
|
if err := tw.WriteHeader(&tar.Header{Name: full, Mode: 0o644, Size: int64(len(content))}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := tw.Write([]byte(content)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := tw.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := gz.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return buf.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractTarGzStripsTheWrapperDirectory(t *testing.T) {
|
||||||
|
data := buildTarGz(t, "acme-widget-deadbeef", map[string]string{
|
||||||
|
"README.md": "hello\n",
|
||||||
|
"src/main.go": "package main\n",
|
||||||
|
"src/nested/a.go": "package nested\n",
|
||||||
|
})
|
||||||
|
dest := t.TempDir()
|
||||||
|
|
||||||
|
if err := extractTarGz(bytes.NewReader(data), dest); err != nil {
|
||||||
|
t.Fatalf("extractTarGz: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rel := range []string{"README.md", "src/main.go", "src/nested/a.go"} {
|
||||||
|
if _, err := os.Stat(filepath.Join(dest, rel)); err != nil {
|
||||||
|
t.Errorf("expected %s to exist: %v", rel, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(dest, "acme-widget-deadbeef")); err == nil {
|
||||||
|
t.Error("the wrapper directory itself should not survive extraction")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractTarGzWithNoWrapperDirectory(t *testing.T) {
|
||||||
|
data := buildTarGz(t, "", map[string]string{"a.go": "package a\n"})
|
||||||
|
dest := t.TempDir()
|
||||||
|
|
||||||
|
if err := extractTarGz(bytes.NewReader(data), dest); err != nil {
|
||||||
|
t.Fatalf("extractTarGz: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(dest, "a.go")); err != nil {
|
||||||
|
t.Errorf("expected a.go to exist: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractTarGzRefusesPathTraversal(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
gz := gzip.NewWriter(&buf)
|
||||||
|
tw := tar.NewWriter(gz)
|
||||||
|
evil := "wrapper/../../../etc/passwd-clone"
|
||||||
|
if err := tw.WriteHeader(&tar.Header{Name: evil, Mode: 0o644, Size: 4}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := tw.Write([]byte("evil")); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := tw.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := gz.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
dest := t.TempDir()
|
||||||
|
|
||||||
|
if err := extractTarGz(bytes.NewReader(buf.Bytes()), dest); err == nil {
|
||||||
|
t.Fatal("want an error for a path-traversal archive entry, not a silent write outside destDir")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractTarGzLeavesNoDotGit(t *testing.T) {
|
||||||
|
data := buildTarGz(t, "wrapper", map[string]string{
|
||||||
|
"a.go": "package a\n",
|
||||||
|
".git/HEAD": "ref: refs/heads/main\n",
|
||||||
|
".git/config": "[core]\n",
|
||||||
|
".github/x.yaml": "ok\n",
|
||||||
|
})
|
||||||
|
dest := t.TempDir()
|
||||||
|
|
||||||
|
if err := extractTarGz(bytes.NewReader(data), dest); err != nil {
|
||||||
|
t.Fatalf("extractTarGz: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(dest, ".git")); err == nil {
|
||||||
|
t.Error("a .git directory must never survive extraction (Isolation: the pipeline reads a .git-free snapshot)")
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(dest, ".github", "x.yaml")); err != nil {
|
||||||
|
t.Errorf("a non-.git dotfile should still extract: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractTarGzStripsWrapperWithExplicitDirectoryEntries(t *testing.T) {
|
||||||
|
// Real GitHub/Gitea archives include explicit tar directory headers for
|
||||||
|
// the wrapper and every subdirectory, not just file entries with long
|
||||||
|
// names -- a wrapper-only directory header has no "/" in its own
|
||||||
|
// (cleaned) name, which must not be mistaken for "no wrapper exists".
|
||||||
|
var buf bytes.Buffer
|
||||||
|
gz := gzip.NewWriter(&buf)
|
||||||
|
tw := tar.NewWriter(gz)
|
||||||
|
dirs := []string{"acme-widget-deadbeef/", "acme-widget-deadbeef/src/"}
|
||||||
|
for _, d := range dirs {
|
||||||
|
if err := tw.WriteHeader(&tar.Header{Name: d, Typeflag: tar.TypeDir, Mode: 0o755}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
files := map[string]string{
|
||||||
|
"acme-widget-deadbeef/README.md": "hello\n",
|
||||||
|
"acme-widget-deadbeef/src/main.go": "package main\n",
|
||||||
|
}
|
||||||
|
for name, content := range files {
|
||||||
|
if err := tw.WriteHeader(&tar.Header{Name: name, Mode: 0o644, Size: int64(len(content))}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := tw.Write([]byte(content)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := tw.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := gz.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
dest := t.TempDir()
|
||||||
|
|
||||||
|
if err := extractTarGz(bytes.NewReader(buf.Bytes()), dest); err != nil {
|
||||||
|
t.Fatalf("extractTarGz: %v", err)
|
||||||
|
}
|
||||||
|
for _, rel := range []string{"README.md", "src/main.go"} {
|
||||||
|
if _, err := os.Stat(filepath.Join(dest, rel)); err != nil {
|
||||||
|
t.Errorf("expected %s to exist: %v", rel, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(dest, "acme-widget-deadbeef")); err == nil {
|
||||||
|
t.Error("the wrapper directory itself should not survive extraction")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,69 +3,66 @@
|
|||||||
// head, for the pr-review pipeline (docs/prds/pr-review-agent.md). It is
|
// 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
|
// 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
|
// own Trees: that path only ever reaches the task's own worktree, never a
|
||||||
// third-party PR being reviewed, so this package checks one out fresh
|
// third-party PR being reviewed.
|
||||||
// through Forge and Trees instead.
|
//
|
||||||
|
// 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
|
package prsource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PullRequest is the pull request fields Metadata and Diff need: title,
|
// PullRequest is the pull request fields Metadata, Diff and Snapshot need:
|
||||||
// body, and the head/base refs Trees checks out and diffs against. It
|
// title, body, and the head commit SHA the diff's line numbers and the
|
||||||
// mirrors forge.PullRequest's shape so callers can pass that type directly
|
// snapshot are both measured against. It mirrors forge.PullRequest's shape
|
||||||
// without this package importing internal/forge.
|
// so callers can pass that type directly without this package importing
|
||||||
|
// internal/forge.
|
||||||
type PullRequest struct {
|
type PullRequest struct {
|
||||||
Title string
|
Title string
|
||||||
Body string
|
Body string
|
||||||
HeadRef string
|
|
||||||
BaseRef string
|
|
||||||
HeadSHA string
|
HeadSHA string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forge fetches an existing pull request's metadata. Narrow by design: the
|
// Forge is the narrow forge capability this package needs: reading a pull
|
||||||
// only forge capability this package needs, matching
|
// request's metadata, its diff, and an archive of its repository at a ref.
|
||||||
// forge.PullRequestReader's GetPullRequest.
|
// Matches forge.PullRequestReader, forge.PullRequestDiffReader and
|
||||||
|
// forge.RepoArchiveReader.
|
||||||
type Forge interface {
|
type Forge interface {
|
||||||
GetPullRequest(ctx context.Context, owner, repo string, number int) (PullRequest, error)
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trees checks out a pull request's head into a fresh, isolated clone,
|
// Source implements workflow.PRSource against a real forge over HTTP.
|
||||||
// diffs it against its base, and exports its tracked files with no .git
|
|
||||||
// present. Narrow by design: the subset of worktree.Manager's methods this
|
|
||||||
// package needs.
|
|
||||||
type Trees interface {
|
|
||||||
CheckoutPR(ctx context.Context, owner, repo, headRef, baseRef string) (dir string, cleanup func(), err error)
|
|
||||||
Diff(ctx context.Context, dir, base string) (string, error)
|
|
||||||
Snapshot(ctx context.Context, dir, destDir string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Source implements workflow.PRSource against a real forge and a real git
|
|
||||||
// checkout. Each call checks out its own fresh, disposable clone: Diff and
|
|
||||||
// Snapshot never share one, since the interface gives no session across
|
|
||||||
// calls to reuse -- correctness over the extra clone.
|
|
||||||
type Source struct {
|
type Source struct {
|
||||||
forge Forge
|
forge Forge
|
||||||
trees Trees
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ workflow.PRSource = (*Source)(nil)
|
var _ workflow.PRSource = (*Source)(nil)
|
||||||
|
|
||||||
// New builds a Source. Neither argument may be nil; callers that lack a
|
// New builds a Source. forge may not be nil; a caller that lacks a forge
|
||||||
// forge PR-reading capability or a worktree manager must not construct one
|
// PR-reading capability must not construct one (matching the "a capability
|
||||||
// (matching the "a capability that cannot run is not advertised" rule the
|
// that cannot run is not advertised" rule the existing operator reviewer
|
||||||
// existing operator reviewer already follows).
|
// already follows).
|
||||||
func New(forge Forge, trees Trees) *Source {
|
func New(forge Forge) *Source {
|
||||||
return &Source{forge: forge, trees: trees}
|
return &Source{forge: forge}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Metadata returns the pull request's title and body. Commit messages are
|
// Metadata returns the pull request's title and body. Commit messages are
|
||||||
// left empty: no existing Trees or Forge capability returns them, and they
|
// left empty: no existing Forge capability returns them, and they are only
|
||||||
// are only ever one of three inputs to the hallucination-check heuristic
|
// ever one of three inputs to the hallucination-check heuristic (docs/prds/
|
||||||
// (docs/prds/pr-review-agent.md, phase 1), not load-bearing on their own.
|
// 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) {
|
func (s *Source) Metadata(ctx context.Context, owner, repo string, number int) (workflow.PRMetadata, error) {
|
||||||
pr, err := s.forge.GetPullRequest(ctx, owner, repo, number)
|
pr, err := s.forge.GetPullRequest(ctx, owner, repo, number)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -74,41 +71,32 @@ func (s *Source) Metadata(ctx context.Context, owner, repo string, number int) (
|
|||||||
return workflow.PRMetadata{Title: pr.Title, Body: pr.Body}, nil
|
return workflow.PRMetadata{Title: pr.Title, Body: pr.Body}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Diff checks out the pull request's head into a fresh clone and diffs it
|
// Diff fetches the pull request's unified diff directly over the forge's
|
||||||
// against its base branch.
|
// HTTP API.
|
||||||
func (s *Source) Diff(ctx context.Context, owner, repo string, number int) (string, error) {
|
func (s *Source) Diff(ctx context.Context, owner, repo string, number int) (string, error) {
|
||||||
pr, err := s.forge.GetPullRequest(ctx, owner, repo, number)
|
diff, err := s.forge.GetPullRequestDiff(ctx, owner, repo, number)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("prsource: fetch pull request metadata: %w", err)
|
return "", fmt.Errorf("prsource: fetch pull request diff: %w", err)
|
||||||
}
|
|
||||||
dir, cleanup, err := s.trees.CheckoutPR(ctx, owner, repo, pr.HeadRef, pr.BaseRef)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("prsource: checkout pull request head: %w", err)
|
|
||||||
}
|
|
||||||
defer cleanup()
|
|
||||||
diff, err := s.trees.Diff(ctx, dir, pr.BaseRef)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("prsource: diff pull request: %w", err)
|
|
||||||
}
|
}
|
||||||
return diff, nil
|
return diff, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Snapshot checks out the pull request's head into a fresh clone and
|
// Snapshot fetches a repository archive at the pull request's head commit
|
||||||
// exports its tracked files into destDir with no .git present, returning
|
// and extracts it into destDir with no .git present, returning the head
|
||||||
// the head commit SHA the forge reported -- the same value the diff's line
|
// SHA the forge reported -- the same value the diff's line numbers were
|
||||||
// numbers were measured against.
|
// measured against.
|
||||||
func (s *Source) Snapshot(ctx context.Context, owner, repo string, number int, destDir string) (string, error) {
|
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)
|
pr, err := s.forge.GetPullRequest(ctx, owner, repo, number)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("prsource: fetch pull request metadata: %w", err)
|
return "", fmt.Errorf("prsource: fetch pull request metadata: %w", err)
|
||||||
}
|
}
|
||||||
dir, cleanup, err := s.trees.CheckoutPR(ctx, owner, repo, pr.HeadRef, pr.BaseRef)
|
archive, err := s.forge.GetRepoArchive(ctx, owner, repo, pr.HeadSHA)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("prsource: checkout pull request head: %w", err)
|
return "", fmt.Errorf("prsource: fetch repository archive at %s: %w", pr.HeadSHA, err)
|
||||||
}
|
}
|
||||||
defer cleanup()
|
defer archive.Close()
|
||||||
if err := s.trees.Snapshot(ctx, dir, destDir); err != nil {
|
if err := extractTarGz(archive, destDir); err != nil {
|
||||||
return "", fmt.Errorf("prsource: snapshot pull request head: %w", err)
|
return "", fmt.Errorf("prsource: extract repository archive: %w", err)
|
||||||
}
|
}
|
||||||
return pr.HeadSHA, nil
|
return pr.HeadSHA, nil
|
||||||
}
|
}
|
||||||
@@ -1,44 +1,40 @@
|
|||||||
package prsource
|
package prsource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fakeForge struct {
|
type fakeForge struct {
|
||||||
pr PullRequest
|
pr PullRequest
|
||||||
err error
|
diff string
|
||||||
|
archive []byte
|
||||||
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeForge) GetPullRequest(context.Context, string, string, int) (PullRequest, error) {
|
func (f *fakeForge) GetPullRequest(context.Context, string, string, int) (PullRequest, error) {
|
||||||
return f.pr, f.err
|
return f.pr, f.err
|
||||||
}
|
}
|
||||||
|
|
||||||
type fakeTrees struct {
|
func (f *fakeForge) GetPullRequestDiff(context.Context, string, string, int) (string, error) {
|
||||||
dir string
|
|
||||||
diff string
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fakeTrees) CheckoutPR(context.Context, string, string, string, string) (string, func(), error) {
|
|
||||||
if f.err != nil {
|
|
||||||
return "", func() {}, f.err
|
|
||||||
}
|
|
||||||
return f.dir, func() {}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fakeTrees) Diff(context.Context, string, string) (string, error) {
|
|
||||||
return f.diff, f.err
|
return f.diff, f.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeTrees) Snapshot(context.Context, string, string) error {
|
func (f *fakeForge) GetRepoArchive(context.Context, string, string, string) (io.ReadCloser, error) {
|
||||||
return f.err
|
if f.err != nil {
|
||||||
|
return nil, f.err
|
||||||
|
}
|
||||||
|
return io.NopCloser(bytes.NewReader(f.archive)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMetadataMapsForgePullRequest(t *testing.T) {
|
func TestMetadataMapsForgePullRequest(t *testing.T) {
|
||||||
forge := &fakeForge{pr: PullRequest{Title: "t", Body: "b", HeadSHA: "abc"}}
|
forge := &fakeForge{pr: PullRequest{Title: "t", Body: "b", HeadSHA: "abc"}}
|
||||||
s := New(forge, &fakeTrees{})
|
s := New(forge)
|
||||||
|
|
||||||
meta, err := s.Metadata(context.Background(), "acme", "widgets", 7)
|
meta, err := s.Metadata(context.Background(), "acme", "widgets", 7)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -51,44 +47,59 @@ func TestMetadataMapsForgePullRequest(t *testing.T) {
|
|||||||
|
|
||||||
func TestMetadataPropagatesForgeError(t *testing.T) {
|
func TestMetadataPropagatesForgeError(t *testing.T) {
|
||||||
wantErr := errors.New("forge unreachable")
|
wantErr := errors.New("forge unreachable")
|
||||||
s := New(&fakeForge{err: wantErr}, &fakeTrees{})
|
s := New(&fakeForge{err: wantErr})
|
||||||
|
|
||||||
if _, err := s.Metadata(context.Background(), "acme", "widgets", 7); !errors.Is(err, wantErr) {
|
if _, err := s.Metadata(context.Background(), "acme", "widgets", 7); !errors.Is(err, wantErr) {
|
||||||
t.Fatalf("err = %v, want %v", err, wantErr)
|
t.Fatalf("err = %v, want %v", err, wantErr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDiffChecksOutThePRAndDiffsAgainstBase(t *testing.T) {
|
func TestDiffFetchesOverHTTPWithNoCheckout(t *testing.T) {
|
||||||
forge := &fakeForge{pr: PullRequest{HeadRef: "feature", BaseRef: "main"}}
|
forge := &fakeForge{diff: "diff --git a b"}
|
||||||
trees := &fakeTrees{dir: "/tmp/checkout", diff: "diff --git a b"}
|
s := New(forge)
|
||||||
s := New(forge, trees)
|
|
||||||
|
|
||||||
diff, err := s.Diff(context.Background(), "acme", "widgets", 7)
|
diff, err := s.Diff(context.Background(), "acme", "widgets", 7)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Diff: %v", err)
|
t.Fatalf("Diff: %v", err)
|
||||||
}
|
}
|
||||||
if diff != "diff --git a b" {
|
if diff != "diff --git a b" {
|
||||||
t.Errorf("diff = %q, want the checked-out worktree's diff", diff)
|
t.Errorf("diff = %q, want the forge's raw diff response", diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSnapshotReturnsTheHeadSHAForgeReported(t *testing.T) {
|
func TestDiffPropagatesForgeError(t *testing.T) {
|
||||||
forge := &fakeForge{pr: PullRequest{HeadSHA: "deadbeef"}}
|
wantErr := errors.New("diff fetch failed")
|
||||||
trees := &fakeTrees{dir: "/tmp/checkout"}
|
s := New(&fakeForge{err: wantErr})
|
||||||
s := New(forge, trees)
|
|
||||||
|
|
||||||
headSHA, err := s.Snapshot(context.Background(), "acme", "widgets", 7, t.TempDir())
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("Snapshot: %v", err)
|
t.Fatalf("Snapshot: %v", err)
|
||||||
}
|
}
|
||||||
if headSHA != "deadbeef" {
|
if headSHA != "deadbeef" {
|
||||||
t.Errorf("headSHA = %q, want the forge-reported head SHA", headSHA)
|
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 TestSnapshotPropagatesCheckoutFailure(t *testing.T) {
|
func TestSnapshotPropagatesForgeError(t *testing.T) {
|
||||||
wantErr := errors.New("clone failed")
|
wantErr := errors.New("archive fetch failed")
|
||||||
s := New(&fakeForge{}, &fakeTrees{err: wantErr})
|
s := New(&fakeForge{err: wantErr})
|
||||||
|
|
||||||
if _, err := s.Snapshot(context.Background(), "acme", "widgets", 7, t.TempDir()); !errors.Is(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)
|
t.Fatalf("err = %v, want %v", err, wantErr)
|
||||||
|
|||||||
Reference in new issue
Block a user