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
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ for non-trivial code changes.
## Testing against a real instance

`docs/design.md` describes the validated Forgejo mechanics and a safe way to
exercise shunt end to end against a throwaway repo on a test instance.
exercise shunt end to end against a throwaway repo on a test instance. The
env-gated forge client harness is documented in
[`docs/forge-integration-tests.md`](docs/forge-integration-tests.md).
9 changes: 7 additions & 2 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ welcome.
aggregate status for staging branches before falling back to task aggregation,
so multi-job gates are not considered green while dependent jobs are still
being materialized.
- **Env-gated forge integration harness.** Live Forgejo/Gitea client checks can
now exercise PR listing, timeline auto-merge detection, status posting,
workflow-run lookup, and optional branch-protection updates without requiring
credentials in default CI.

## Current limitations

Expand All @@ -58,8 +62,9 @@ welcome.
- **Serial initial batches.** One rollup batch is seeded at a time per
`(repo, base)`. Bisection can fan out, but shunt still avoids speculative
parallel batches from fresh queue entries.
- **No automated forge-integration tests.** The bisection state machine is unit
tested with a mock; live API coverage is still manual.
- **No automated end-to-end burn-in.** The forge client now has an env-gated live
integration harness, but disposable-repo and busy-repo queue burn-in are still
manual.
- **Basic observability only.** Prometheus text metrics cover process-local queue
depth and key counters, and operators can enable sticky PR queue-status
comments. There is still no persisted metrics history, no time-in-queue
Expand Down
48 changes: 48 additions & 0 deletions docs/forge-integration-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Forge integration tests

The forge client has an env-gated live integration harness. It is skipped by
default, so `go test ./...` and CI never require forge credentials or a running
Forgejo/Gitea instance.

Run it explicitly against a test instance and disposable repository:

```sh
SHUNT_FORGE_INTEGRATION=1 \
SHUNT_FORGE_INSTANCE=https://forge.example.com \
SHUNT_FORGE_TOKEN=<bot-token> \
SHUNT_FORGE_OWNER=<owner> \
SHUNT_FORGE_REPO=<repo> \
go test ./internal/forge -run TestForgeIntegrationHarness -count=1 -v
```

Required variables:

| Variable | Description |
|----------|-------------|
| `SHUNT_FORGE_INTEGRATION` | Set to `1` to enable the live harness. |
| `SHUNT_FORGE_INSTANCE` | Forgejo/Gitea base URL. |
| `SHUNT_FORGE_TOKEN` | Bot API token with access to the test repo. |
| `SHUNT_FORGE_OWNER` | Repository owner or organization. |
| `SHUNT_FORGE_REPO` | Repository name. |

Optional flows:

| Variable | Description |
|----------|-------------|
| `SHUNT_FORGE_BASE` | Base-branch filter for open PRs; defaults to `main`. |
| `SHUNT_FORGE_PR_INDEX` | Fetch this PR and scan its issue timeline for scheduled auto-merge events. |
| `SHUNT_FORGE_STATUS_SHA` | Post a commit status to this SHA. Defaults to context `shunt-integration`, state `pending`. |
| `SHUNT_FORGE_STATUS_CONTEXT` | Override the status context used with `SHUNT_FORGE_STATUS_SHA`. |
| `SHUNT_FORGE_STATUS_STATE` | Override the posted status state. |
| `SHUNT_FORGE_STATUS_DESCRIPTION` | Override the posted status description. |
| `SHUNT_FORGE_STATUS_TARGET_URL` | Optional target URL for the posted status. |
| `SHUNT_FORGE_RUN_SHA` | Look up the latest Actions task status for this SHA. |
| `SHUNT_FORGE_RUN_BRANCH` | Optional branch filter for run-status lookup. |
| `SHUNT_FORGE_ALLOW_BRANCH_PROTECTION_WRITE` | Set to `1` to allow the branch-protection subtest to mutate repo settings. |
| `SHUNT_FORGE_BRANCH_PROTECTION_BRANCH` | Branch to pass to `EnsureBranchProtection` when writes are enabled. |
| `SHUNT_FORGE_BRANCH_PROTECTION_STATUS_CONTEXT` | Required status context for branch protection; defaults to `merge-queue`. |
| `SHUNT_FORGE_BRANCH_PROTECTION_BOT_USER` | Bot username to add to the push allow-list when writes are enabled. |

Use a disposable repo for write-enabled subtests. The harness intentionally does
not call the merge endpoint because that flow is destructive; merge request
payloads are covered by unit tests.
136 changes: 136 additions & 0 deletions internal/forge/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package forge

import (
"os"
"strconv"
"testing"
)

type forgeIntegrationConfig struct {
instance string
token string
owner string
repo string
base string
}

func TestForgeIntegrationHarness(t *testing.T) {
if os.Getenv("SHUNT_FORGE_INTEGRATION") != "1" {
t.Skip("set SHUNT_FORGE_INTEGRATION=1 and forge test env vars to run live Forgejo/Gitea integration tests")
}

cfg := requireForgeIntegrationConfig(t)
client := New(cfg.instance, cfg.token)

t.Run("list pull requests", func(t *testing.T) {
prs, err := client.ListOpenPRs(cfg.owner, cfg.repo, cfg.base)
if err != nil {
t.Fatalf("ListOpenPRs: %v", err)
}
t.Logf("found %d open pull requests targeting %q", len(prs), cfg.base)
})

t.Run("pull request timeline automerge detection", func(t *testing.T) {
index := optionalIntEnv(t, "SHUNT_FORGE_PR_INDEX")
if index == 0 {
t.Skip("set SHUNT_FORGE_PR_INDEX to exercise PR fetch and timeline auto-merge detection")
}

pr, err := client.GetPR(cfg.owner, cfg.repo, index)
if err != nil {
t.Fatalf("GetPR(%d): %v", index, err)
}
if pr.Number != index {
t.Fatalf("PR number = %d, want %d", pr.Number, index)
}

scheduled, err := client.AutomergeScheduled(cfg.owner, cfg.repo, index)
if err != nil {
t.Fatalf("AutomergeScheduled(%d): %v", index, err)
}
t.Logf("PR #%d automerge scheduled = %v", index, scheduled)
})

t.Run("post commit status", func(t *testing.T) {
sha := os.Getenv("SHUNT_FORGE_STATUS_SHA")
if sha == "" {
t.Skip("set SHUNT_FORGE_STATUS_SHA to post a non-required integration-test commit status")
}

context := envOrDefault("SHUNT_FORGE_STATUS_CONTEXT", "shunt-integration")
state := envOrDefault("SHUNT_FORGE_STATUS_STATE", "pending")
desc := envOrDefault("SHUNT_FORGE_STATUS_DESCRIPTION", "shunt forge integration harness")
targetURL := os.Getenv("SHUNT_FORGE_STATUS_TARGET_URL")
if err := client.SetCommitStatus(cfg.owner, cfg.repo, sha, context, state, desc, targetURL); err != nil {
t.Fatalf("SetCommitStatus(%s): %v", sha, err)
}
})

t.Run("lookup workflow run status", func(t *testing.T) {
sha := os.Getenv("SHUNT_FORGE_RUN_SHA")
if sha == "" {
t.Skip("set SHUNT_FORGE_RUN_SHA to look up a workflow run status")
}

status, err := client.RunStatus(cfg.owner, cfg.repo, sha, os.Getenv("SHUNT_FORGE_RUN_BRANCH"))
if err != nil {
t.Fatalf("RunStatus(%s): %v", sha, err)
}
t.Logf("workflow run status for %s = %q", sha, status)
})

t.Run("ensure branch protection", func(t *testing.T) {
if os.Getenv("SHUNT_FORGE_ALLOW_BRANCH_PROTECTION_WRITE") != "1" {
t.Skip("set SHUNT_FORGE_ALLOW_BRANCH_PROTECTION_WRITE=1 to allow branch-protection mutation")
}

branch := requiredEnv(t, "SHUNT_FORGE_BRANCH_PROTECTION_BRANCH")
statusContext := envOrDefault("SHUNT_FORGE_BRANCH_PROTECTION_STATUS_CONTEXT", "merge-queue")
botUser := requiredEnv(t, "SHUNT_FORGE_BRANCH_PROTECTION_BOT_USER")
changed, err := client.EnsureBranchProtection(cfg.owner, cfg.repo, branch, statusContext, botUser)
if err != nil {
t.Fatalf("EnsureBranchProtection(%s): %v", branch, err)
}
t.Logf("branch protection changed = %v", changed)
})
}

func requireForgeIntegrationConfig(t *testing.T) forgeIntegrationConfig {
t.Helper()
return forgeIntegrationConfig{
instance: requiredEnv(t, "SHUNT_FORGE_INSTANCE"),
token: requiredEnv(t, "SHUNT_FORGE_TOKEN"),
owner: requiredEnv(t, "SHUNT_FORGE_OWNER"),
repo: requiredEnv(t, "SHUNT_FORGE_REPO"),
base: envOrDefault("SHUNT_FORGE_BASE", "main"),
}
}

func requiredEnv(t *testing.T, name string) string {
t.Helper()
value := os.Getenv(name)
if value == "" {
t.Fatalf("%s is required when SHUNT_FORGE_INTEGRATION=1", name)
}
return value
}

func optionalIntEnv(t *testing.T, name string) int {
t.Helper()
value := os.Getenv(name)
if value == "" {
return 0
}
parsed, err := strconv.Atoi(value)
if err != nil || parsed <= 0 {
t.Fatalf("%s must be a positive integer", name)
}
return parsed
}

func envOrDefault(name, fallback string) string {
if value := os.Getenv(name); value != "" {
return value
}
return fallback
}
Loading