Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/guides/dev/behaviour-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,39 @@ For the reusable test GitHub Apps (`fullsend-test-*`) used by temporary and test

See [behaviour-drivers.md](behaviour-drivers.md) for driver configuration and [ADR 0066](../../ADRs/0066-behaviour-tests-with-gherkin-and-drivers.md) for the decision record.

## Fork PR scenarios

Fork dispatch scenarios test `pull_request_target` harness triggering from cross-fork pull requests.

### Pool-org prerequisites

Fork scenarios require the pool org to have:

- **A long-lived fork repository** of the enrolled `test-repo`. The fork is created once (idempotently) via the `Given a fork` step and persists across test runs. Do not delete the fork repo between scenarios or CI runs.
- **The same installation token** must have write access to both the base repo and the fork repo within the org, since the e2e bot commits to the fork and opens cross-fork PRs.

### Fork lifecycle

| Resource | Lifecycle | Cleanup |
|----------|-----------|---------|
| Fork repo | Long-lived (created once per pool org) | Never deleted |
| Fork branches | Per-scenario | Deleted by `CleanupScenario` |
| Fork PRs | Per-scenario | Closed by `CleanupScenario` |

Fork PRs are opened against the base repo (not the fork). `CleanupScenario` closes them via `CloseIssue` on the base repo and deletes the head branch on the fork repo.

### Background step usage

Fork scenarios share a common `Background:` block that sets up the enrolled test repository and the fork:

```gherkin
Background:
Given the enrolled test repository
And a fork "test-repo-fork" of the enrolled test repository
```

The `Given a fork` step is idempotent: if the fork already exists, it reuses it without error. Each scenario then creates its own branch and PR within the fork.

## Version pinning for `fullsend-ai/agents`

External behaviour runners import the shared libraries from this module:
Expand Down
68 changes: 68 additions & 0 deletions e2e/behaviour/features/dispatch/fork-dispatch.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Feature: Fork PR dispatch

Background:
Given the enrolled test repository
And a fork "test-repo-fork" of the enrolled test repository

Scenario: Fork PR label dispatches harness
Given a custom harness "fork-pr-ping" with:
"""
agent: agents/triage.md
role: triage
slug: fullsend-ai-fork-pr-ping
model: opus
image: ghcr.io/fullsend-ai/fullsend-sandbox:latest
trigger: |
event.entity.kind == "change_proposal"
&& event.transition.kind == "label_changed"
&& event.transition.label.name == "ready-for-fork-ping"
"""
And a dummy agent that would:
| description | op | args |
| Fork PR payload | assert_json | .fullsend/dispatch/event-payload.json,pull_request.head.repo.fork |
| Prove fork execution | write_fixture| output/dispatch-fork-ok.json, fixtures/dispatch/ok.json |
When a fork pull request is opened
And the fork pull request is labeled "ready-for-fork-ping"
Then the harness "fork-pr-ping" workflow completes successfully
And the agent will succeed to Prove fork execution
And the harness "fork-pr-ping" was dispatched exactly 1 time

Scenario: Fork PR sync + label dispatches harness
Given a custom harness "fork-pr-sync" with:
"""
agent: agents/triage.md
role: triage
slug: fullsend-ai-fork-pr-sync
model: opus
image: ghcr.io/fullsend-ai/fullsend-sandbox:latest
trigger: |
event.entity.kind == "change_proposal"
&& event.transition.kind == "label_changed"
&& event.transition.label.name == "ready-for-fork-sync"
"""
And a dummy agent that would:
| description | op | args |
| Sync PR payload | assert_json | .fullsend/dispatch/event-payload.json,pull_request.head.repo.fork |
| Prove sync execution | write_fixture| output/dispatch-sync-ok.json, fixtures/dispatch/ok.json |
When a fork pull request is opened
And a commit is pushed to the fork pull request
And the fork pull request is labeled "ready-for-fork-sync"
Then the harness "fork-pr-sync" workflow completes successfully
And the agent will succeed to Prove sync execution
And the harness "fork-pr-sync" was dispatched exactly 1 time

Scenario: Fork PR does not trigger issue-only harness
Given a custom harness "issue-ping" with:
"""
agent: agents/triage.md
role: triage
slug: fullsend-ai-issue-ping
model: opus
image: ghcr.io/fullsend-ai/fullsend-sandbox:latest
trigger: |
event.entity.kind == "work_item"
&& event.transition.kind == "label_changed"
&& event.transition.label.name == "ready-for-ping"
"""
When a fork pull request is opened
Then the harness "issue-ping" agent did not run
33 changes: 33 additions & 0 deletions internal/forge/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ type FakeClient struct {
CreatedRepos []Repository
CreatedFiles []FileRecord
CreatedBranches []string // "owner/repo/branch"
DeletedRefs []string // "owner/repo/refPath"
CreatedProposals []ChangeProposal
DeletedRepos []string // "owner/repo"
DeletedFiles []FileRecord
Expand Down Expand Up @@ -735,6 +736,18 @@ func (f *FakeClient) CreateBranch(_ context.Context, owner, repo, branchName str
return nil
}

func (f *FakeClient) DeleteRef(_ context.Context, owner, repo, refPath string) error {
f.mu.Lock()
defer f.mu.Unlock()

if e := f.err("DeleteRef"); e != nil {
return e
}

f.DeletedRefs = append(f.DeletedRefs, owner+"/"+repo+"/"+refPath)
return nil
}

func (f *FakeClient) CreateFileOnBranch(_ context.Context, owner, repo, branch, path, message string, content []byte) error {
f.mu.Lock()
defer f.mu.Unlock()
Expand Down Expand Up @@ -798,6 +811,26 @@ func (f *FakeClient) CreateChangeProposal(_ context.Context, owner, repo, title,
return &cp, nil
}

func (f *FakeClient) CreateCrossRepoChangeProposal(_ context.Context, baseOwner, baseRepo, headOwner, headRepo, title, body, headBranch, baseBranch string) (*ChangeProposal, error) {
f.mu.Lock()
defer f.mu.Unlock()

if e := f.err("CreateCrossRepoChangeProposal"); e != nil {
return nil, e
}

f.proposalCounter++
cp := ChangeProposal{
URL: fmt.Sprintf("https://forge.example.com/%s/%s/pull/%d", baseOwner, baseRepo, f.proposalCounter),
Title: title,
Number: f.proposalCounter,
Head: headOwner + ":" + headBranch,
Base: baseBranch,
}
f.CreatedProposals = append(f.CreatedProposals, cp)
return &cp, nil
}

func (f *FakeClient) ListRepoPullRequests(_ context.Context, owner, repo string) ([]ChangeProposal, error) {
f.mu.Lock()
defer f.mu.Unlock()
Expand Down
46 changes: 46 additions & 0 deletions internal/forge/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,45 @@ func TestFakeClient_CreateBranch(t *testing.T) {
assert.Equal(t, []string{"owner/repo/feature-branch"}, fc.CreatedBranches)
}

func TestFakeClient_DeleteRef(t *testing.T) {
ctx := context.Background()
fc := &FakeClient{}

err := fc.DeleteRef(ctx, "owner", "repo", "heads/my-branch")
require.NoError(t, err)
assert.Equal(t, []string{"owner/repo/heads/my-branch"}, fc.DeletedRefs)

// Append a second ref.
err = fc.DeleteRef(ctx, "owner", "repo", "tags/v1.0")
require.NoError(t, err)
assert.Len(t, fc.DeletedRefs, 2)
}

func TestFakeClient_CreateCrossRepoChangeProposal(t *testing.T) {
ctx := context.Background()
fc := &FakeClient{}

cp, err := fc.CreateCrossRepoChangeProposal(ctx,
"base-owner", "base-repo", "head-owner", "head-repo",
"PR title", "PR body", "feature-branch", "main",
)
require.NoError(t, err)
assert.Equal(t, 1, cp.Number)
assert.Equal(t, "PR title", cp.Title)
assert.Equal(t, "head-owner:feature-branch", cp.Head)
assert.Equal(t, "main", cp.Base)
assert.Contains(t, cp.URL, "base-owner/base-repo/pull/1")

// Second proposal gets incremented number.
cp2, err := fc.CreateCrossRepoChangeProposal(ctx,
"base-owner", "base-repo", "head-owner", "head-repo",
"title2", "body2", "branch2", "main",
)
require.NoError(t, err)
assert.Equal(t, 2, cp2.Number)
assert.Len(t, fc.CreatedProposals, 2)
}

func TestFakeClient_CreateChangeProposal(t *testing.T) {
ctx := context.Background()
fc := &FakeClient{}
Expand Down Expand Up @@ -616,11 +655,16 @@ func TestFakeClient_ErrorInjection(t *testing.T) {
{"CreateOrUpdateFile", func(fc *FakeClient) error { return fc.CreateOrUpdateFile(ctx, "o", "r", "p", "m", nil) }},
{"GetFileContent", func(fc *FakeClient) error { _, err := fc.GetFileContent(ctx, "o", "r", "p"); return err }},
{"CreateBranch", func(fc *FakeClient) error { return fc.CreateBranch(ctx, "o", "r", "b") }},
{"DeleteRef", func(fc *FakeClient) error { return fc.DeleteRef(ctx, "o", "r", "heads/b") }},
{"CreateFileOnBranch", func(fc *FakeClient) error { return fc.CreateFileOnBranch(ctx, "o", "r", "b", "p", "m", nil) }},
{"CreateChangeProposal", func(fc *FakeClient) error {
_, err := fc.CreateChangeProposal(ctx, "o", "r", "t", "b", "h", "base")
return err
}},
{"CreateCrossRepoChangeProposal", func(fc *FakeClient) error {
_, err := fc.CreateCrossRepoChangeProposal(ctx, "o", "r", "ho", "hr", "t", "b", "h", "base")
return err
}},
{"ListRepoPullRequests", func(fc *FakeClient) error { _, err := fc.ListRepoPullRequests(ctx, "o", "r"); return err }},
{"GetAuthenticatedUser", func(fc *FakeClient) error { _, err := fc.GetAuthenticatedUser(ctx); return err }},
{"GetAuthenticatedUserIdentity", func(fc *FakeClient) error {
Expand Down Expand Up @@ -749,8 +793,10 @@ func TestFakeClient_ThreadSafety(t *testing.T) {
_ = fc.CreateOrUpdateFile(ctx, "o", "r", "p", "m", []byte("data"))
_, _ = fc.GetFileContent(ctx, "o", "r", "file.txt")
_ = fc.CreateBranch(ctx, "o", "r", "b")
_ = fc.DeleteRef(ctx, "o", "r", "heads/b")
_ = fc.CreateFileOnBranch(ctx, "o", "r", "b", "p", "m", []byte("data"))
_, _ = fc.CreateChangeProposal(ctx, "o", "r", "t", "b", "h", "base")
_, _ = fc.CreateCrossRepoChangeProposal(ctx, "o", "r", "ho", "hr", "t", "b", "h", "base")
_, _ = fc.ListRepoPullRequests(ctx, "o", "r")
_, _ = fc.GetAuthenticatedUser(ctx)
_, _ = fc.GetAuthenticatedUserIdentity(ctx)
Expand Down
13 changes: 13 additions & 0 deletions internal/forge/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,26 @@ type Client interface {
// Returns forge.ErrNotFound if the branch does not exist.
GetBranchRef(ctx context.Context, owner, repo, branch string) (sha string, err error)
CreateBranch(ctx context.Context, owner, repo, branchName string) error

// DeleteRef deletes a git ref (e.g., "heads/my-branch", "tags/v1.0").
// Returns forge.ErrNotFound if the ref does not exist.
DeleteRef(ctx context.Context, owner, repo, refPath string) error
CreateFileOnBranch(ctx context.Context, owner, repo, branch, path, message string, content []byte) error
// CreateOrUpdateFileOnBranch creates or updates a file on a specific branch.
// Combines SHA-aware upsert with branch targeting.
CreateOrUpdateFileOnBranch(ctx context.Context, owner, repo, branch, path, message string, content []byte) error

// Change proposals (PRs/MRs)
CreateChangeProposal(ctx context.Context, owner, repo, title, body, head, base string) (*ChangeProposal, error)

// CreateCrossRepoChangeProposal opens a pull request where the head
// branch lives in a different repository than the base. This is
// necessary for same-owner forks where the REST API's "owner:branch"
// head format is ambiguous. Implementations should use a mechanism
// that can explicitly identify the head repository (e.g., GraphQL
// createPullRequest with headRepositoryId on GitHub).
CreateCrossRepoChangeProposal(ctx context.Context, baseOwner, baseRepo, headOwner, headRepo, title, body, headBranch, baseBranch string) (*ChangeProposal, error)

ListRepoPullRequests(ctx context.Context, owner, repo string) ([]ChangeProposal, error)

// Organization metadata
Expand Down
106 changes: 106 additions & 0 deletions internal/forge/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func (e *APIError) Error() string {
for _, d := range e.Errors {
if d.Message != "" {
s += fmt.Sprintf(" (%s)", d.Message)
} else if d.Field != "" || d.Code != "" {
// Include field/code when the detail message is empty.
// GitHub 422 validation errors sometimes omit the message
// but still provide the field and error code.
s += fmt.Sprintf(" (field=%s, code=%s)", d.Field, d.Code)
}
}
return s
Expand Down Expand Up @@ -1577,6 +1582,15 @@ func (c *LiveClient) CreateBranch(ctx context.Context, owner, repo, branchName s
return nil
}

// DeleteRef deletes a git ref (e.g., "heads/my-branch", "tags/v1.0").
// Returns forge.ErrNotFound (wrapped) if the ref does not exist.
func (c *LiveClient) DeleteRef(ctx context.Context, owner, repo, refPath string) error {
if err := c.delete_(ctx, fmt.Sprintf("/repos/%s/%s/git/refs/%s", owner, repo, refPath)); err != nil {
return fmt.Errorf("delete ref %s in %s/%s: %w", refPath, owner, repo, err)
}
return nil
}

// CreateChangeProposal creates a pull request.
func (c *LiveClient) CreateChangeProposal(ctx context.Context, owner, repo, title, body, head, base string) (*forge.ChangeProposal, error) {
payload := map[string]string{
Expand Down Expand Up @@ -1607,6 +1621,98 @@ func (c *LiveClient) CreateChangeProposal(ctx context.Context, owner, repo, titl
}, nil
}

// getRepoNodeID fetches the GraphQL node ID for a repository via the REST
// API. The node ID is needed for GraphQL mutations such as createPullRequest.
func (c *LiveClient) getRepoNodeID(ctx context.Context, owner, repo string) (string, error) {
resp, err := c.get(ctx, fmt.Sprintf("/repos/%s/%s", owner, repo))
if err != nil {
return "", fmt.Errorf("get repo node ID for %s/%s: %w", owner, repo, err)
}
var r struct {
NodeID string `json:"node_id"`
}
if err := decodeJSON(resp, &r); err != nil {
return "", fmt.Errorf("decode repo node ID for %s/%s: %w", owner, repo, err)
}
if r.NodeID == "" {
return "", fmt.Errorf("empty node ID for %s/%s", owner, repo)
}
return r.NodeID, nil
}

// CreateCrossRepoChangeProposal opens a pull request where the head branch
// lives in a different repository than the base. It uses the GraphQL
// createPullRequest mutation with explicit repositoryId and
// headRepositoryId to avoid the ambiguity of the REST API's
// "owner:branch" head format (which fails for same-owner forks).
func (c *LiveClient) CreateCrossRepoChangeProposal(ctx context.Context, baseOwner, baseRepo, headOwner, headRepo, title, body, headBranch, baseBranch string) (*forge.ChangeProposal, error) {
baseNodeID, err := c.getRepoNodeID(ctx, baseOwner, baseRepo)
if err != nil {
return nil, fmt.Errorf("create cross-repo pull request: %w", err)
}
headNodeID, err := c.getRepoNodeID(ctx, headOwner, headRepo)
if err != nil {
return nil, fmt.Errorf("create cross-repo pull request: %w", err)
}

query := `mutation($input: CreatePullRequestInput!) {
createPullRequest(input: $input) {
pullRequest {
number
title
url
}
}
}`
variables := map[string]any{
"input": map[string]any{
"repositoryId": baseNodeID,
"headRepositoryId": headNodeID,
"headRefName": headBranch,
"baseRefName": baseBranch,
"title": title,
"body": body,
},
}
gqlPayload := map[string]any{
"query": query,
"variables": variables,
}

resp, err := c.post(ctx, "/graphql", gqlPayload)
if err != nil {
return nil, fmt.Errorf("create cross-repo pull request via graphql: %w", err)
}

var gqlResult struct {
Data struct {
CreatePullRequest struct {
PullRequest struct {
Number int `json:"number"`
Title string `json:"title"`
URL string `json:"url"`
} `json:"pullRequest"`
} `json:"createPullRequest"`
} `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
if err := decodeJSON(resp, &gqlResult); err != nil {
return nil, fmt.Errorf("decode cross-repo pull request response: %w", err)
}
if len(gqlResult.Errors) > 0 {
return nil, fmt.Errorf("create cross-repo pull request: graphql: %s", gqlResult.Errors[0].Message)
}

pr := gqlResult.Data.CreatePullRequest.PullRequest
return &forge.ChangeProposal{
URL: pr.URL,
Title: pr.Title,
Number: pr.Number,
}, nil
}

// ListRepoPullRequests lists open pull requests for a repository with pagination.
func (c *LiveClient) ListRepoPullRequests(ctx context.Context, owner, repo string) ([]forge.ChangeProposal, error) {
var result []forge.ChangeProposal
Expand Down
Loading
Loading