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
@@ -8,7 +8,9 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -190,6 +192,42 @@ func (c *GitHubClient) GetPullRequest(ctx context.Context, owner, repo string, n
|
||||
}, 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.
|
||||
func (c *GitHubClient) ListReviews(ctx context.Context, owner, repo string, number int, sinceID int64) ([]Review, error) {
|
||||
var out []Review
|
||||
|
||||
Reference in new issue
Block a user