Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-7857 fix(wakeup): evaluate --until-pr against self-hosted VCS pull requests (#9039)

小七6dadfc7216d0
diff --git a/server/internal/service/issue_wakeup_condition.go b/server/internal/service/issue_wakeup_condition.go
index 7baff1ae09..94523348af 100644
--- a/server/internal/service/issue_wakeup_condition.go
+++ b/server/internal/service/issue_wakeup_condition.go
@@ -259,9 +259,10 @@ func (s *IssueWakeupService) evaluateCondition(ctx context.Context, tx pgx.Tx, q
met, fingerprint, observed := childrenDone(c, children)
return met, fingerprint, observed, nil
case "pull_request":
- if c.Event == "checks_finished" && (s.Tasks == nil || !s.Tasks.PRSnapshotsEnabled) {
- return false, "", nil, errPRSnapshotsUnavailable
- }
+ // Without the snapshot client GitHub rows are never refreshed, so their
+ // checks neither satisfy nor stall the rule. Self-hosted VCS rows carry
+ // their own commit statuses and are evaluated regardless (#9034).
+ githubChecks := c.Event != "checks_finished" || (s.Tasks != nil && s.Tasks.PRSnapshotsEnabled)
rows, err := tx.Query(ctx, `SELECT pr.pr_number,pr.state,COALESCE(pr.snapshot_head_sha,''),COALESCE(pr.checks_rollup_state,'')
FROM github_pull_request pr JOIN issue_pull_request ipr ON ipr.pull_request_id=pr.id WHERE ipr.issue_id=$1`, w.IssueID)
if err != nil {
@@ -278,7 +279,6 @@ func (s *IssueWakeupService) evaluateCondition(ctx context.Context, tx pgx.Tx, q
return false, "", nil, err
}
linked++
- missingSnapshot = missingSnapshot || head == ""
switch c.Event {
case "merged":
if state == "merged" {
@@ -286,6 +286,10 @@ func (s *IssueWakeupService) evaluateCondition(ctx context.Context, tx pgx.Tx, q
prs = append(prs, map[string]any{"number": number, "state": state})
}
case "checks_finished":
+ if !githubChecks {
+ continue
+ }
+ missingSnapshot = missingSnapshot || head == ""
if head != "" && slices.Contains([]string{"SUCCESS", "FAILURE", "ERROR"}, checks) {
keys = append(keys, fmt.Sprintf("%d@%s:%s", number, head, checks))
prs = append(prs, map[string]any{"number": number, "head_sha": head, "checks": strings.ToLower(checks)})
@@ -296,12 +300,72 @@ func (s *IssueWakeupService) evaluateCondition(ctx context.Context, tx pgx.Tx, q
if err = rows.Err(); err != nil {
return false, "", nil, err
}
- if linked == 0 {
- return false, "", nil, errPRNotLinked
+ // Self-hosted VCS pull requests (Forgejo/Gitea/GitLab) live in their own
+ // tables. Roll up each PR's commit statuses for its current head sha,
+ // grouped by PR id like ListVCSPullRequestsByIssue: any failed status =
+ // FAILURE, all passed (at least one) = SUCCESS, pending or none = not
+ // finished. PR numbers and shas can repeat across connections and
+ // repositories, so both the rollup and the fingerprint key on the PR id.
+ vcsLinked := 0
+ vcsRows, err := tx.Query(ctx, `SELECT pr.id,pr.pr_number,pr.provider,pr.state,pr.head_sha,
+ COALESCE(SUM(CASE WHEN cs.state='failed' THEN 1 ELSE 0 END),0),
+ COALESCE(SUM(CASE WHEN cs.state='pending' THEN 1 ELSE 0 END),0),
+ COUNT(cs.state)
+ FROM vcs_pull_request pr
+ JOIN issue_vcs_pull_request ipr ON ipr.pull_request_id=pr.id
+ LEFT JOIN vcs_commit_status cs ON cs.connection_id=pr.connection_id AND cs.sha=pr.head_sha AND pr.head_sha<>''
+ WHERE ipr.issue_id=$1
+ GROUP BY pr.id`, w.IssueID)
+ if err != nil {
+ return false, "", nil, err
}
- // Any matching PR satisfies the rule, even if another awaits a snapshot.
- if c.Event == "checks_finished" && len(keys) == 0 && missingSnapshot {
- return false, "", nil, errPRSnapshotPending
+ for vcsRows.Next() {
+ var id pgtype.UUID
+ var number int32
+ var provider, state, head string
+ var failed, pending, total int64
+ if err = vcsRows.Scan(&id, &number, &provider, &state, &head, &failed, &pending, &total); err != nil {
+ vcsRows.Close()
+ return false, "", nil, err
+ }
+ vcsLinked++
+ switch c.Event {
+ case "merged":
+ if state == "merged" {
+ keys = append(keys, "vcs:"+util.UUIDToString(id))
+ prs = append(prs, map[string]any{"number": number, "provider": provider, "state": state})
+ }
+ case "checks_finished":
+ checks := ""
+ switch {
+ case failed > 0:
+ checks = "FAILURE"
+ case total > 0 && pending == 0:
+ checks = "SUCCESS"
+ }
+ if head != "" && checks != "" {
+ keys = append(keys, fmt.Sprintf("vcs:%s@%s:%s", util.UUIDToString(id), head, checks))
+ prs = append(prs, map[string]any{"number": number, "provider": provider, "head_sha": head, "checks": strings.ToLower(checks)})
+ }
+ }
+ }
+ vcsRows.Close()
+ if err = vcsRows.Err(); err != nil {
+ return false, "", nil, err
+ }
+ // Any matching PR satisfies the rule; diagnostics only explain a wait.
+ if len(keys) == 0 {
+ // GitHub-only (or unlinked) checks rules cannot progress without the
+ // snapshot client; a rule with VCS PRs waits for their statuses.
+ if !githubChecks && (linked > 0 || vcsLinked == 0) {
+ return false, "", nil, errPRSnapshotsUnavailable
+ }
+ if linked == 0 && vcsLinked == 0 {
+ return false, "", nil, errPRNotLinked
+ }
+ if c.Event == "checks_finished" && missingSnapshot {
+ return false, "", nil, errPRSnapshotPending
+ }
}
sort.Strings(keys)
return len(keys) > 0, "pr:" + strings.Join(keys, ","), map[string]any{"pull_requests": prs}, nil
diff --git a/server/internal/service/issue_wakeup_condition_pr_test.go b/server/internal/service/issue_wakeup_condition_pr_test.go
index 7687989853..e84c4fbc0e 100644
--- a/server/internal/service/issue_wakeup_condition_pr_test.go
+++ b/server/internal/service/issue_wakeup_condition_pr_test.go
@@ -31,6 +31,12 @@ func (tx *prConditionTx) Query(_ context.Context, sql string, args ...any) (pgx.
if strings.Contains(sql, "UPDATE issue_wakeup_receipt") {
return &sliceRows{}, nil
}
+ // The canned GitHub rows below describe an issue with no self-hosted VCS
+ // pull requests; the VCS aggregation finds nothing to add. The VCS path
+ // itself is covered against PostgreSQL by the condition test suite.
+ if strings.Contains(sql, "FROM vcs_pull_request") {
+ return &sliceRows{}, nil
+ }
if !strings.Contains(sql, "WHERE ipr.issue_id=$1") || len(args) != 1 || args[0] != tx.issue {
return nil, fmt.Errorf("unexpected PR scope: %s %v", sql, args)
}
diff --git a/server/internal/service/issue_wakeup_condition_test.go b/server/internal/service/issue_wakeup_condition_test.go
index c1dcc7e33a..925d5ffddc 100644
--- a/server/internal/service/issue_wakeup_condition_test.go
+++ b/server/internal/service/issue_wakeup_condition_test.go
@@ -341,3 +341,216 @@ func TestWakeupConditionPRDiagnosticsRecover(t *testing.T) {
})
}
}
+
+func TestWakeupConditionPullRequestChecksCoversVCSProviders(t *testing.T) {
+ f, s, issue, agent := conditionFixture(t)
+ ctx := context.Background()
+ var pr, connection string
+ if err := f.Pool.QueryRow(ctx, `INSERT INTO vcs_pull_request(workspace_id,connection_id,provider,repo_owner,repo_name,pr_number,title,state,html_url,pr_created_at,pr_updated_at,head_sha)
+ VALUES($1,gen_random_uuid(),'forgejo','selfhosted','wakeup-test',(extract(epoch from clock_timestamp())*1000)::bigint % 100000,'PR','open','https://forgejo.example.test/pr',now(),now(),'aaa') RETURNING id, connection_id`, f.WorkspaceID).Scan(&pr, &connection); err != nil {
+ t.Fatal(err)
+ }
+ f.Cleanup(t, "DELETE FROM vcs_pull_request WHERE id=$1", pr)
+ f.Cleanup(t, "DELETE FROM issue_vcs_pull_request WHERE pull_request_id=$1", pr)
+ f.Exec(t, "INSERT INTO issue_vcs_pull_request(issue_id,pull_request_id) VALUES($1,$2)", issue, pr)
+ f.Exec(t, `INSERT INTO vcs_commit_status(connection_id,sha,context,state) VALUES($1,'aaa','ci/test','pending')`, connection)
+ w := wakeCreate(t, f, s, issue, WakeupInput{AgentID: agent, Kind: "event", Instruction: "Look at CI",
+ Condition: condition(t, map[string]any{"type": "pull_request", "event": "checks_finished"})})
+ // Pending statuses from before registration are not a finished check.
+ wakeTick(t, f, s, w.ID)
+ if n := wakeRuns(t, f, w.ID); n != 0 {
+ t.Fatalf("pending statuses started %d runs", n)
+ }
+ f.Exec(t, "UPDATE vcs_pull_request SET head_sha='bbb' WHERE id=$1", pr)
+ f.Exec(t, `INSERT INTO vcs_commit_status(connection_id,sha,context,state) VALUES($1,'bbb','ci/test','failed')`, connection)
+ got := wakeTick(t, f, s, w.ID)
+ if n := wakeRuns(t, f, w.ID); n != 1 {
+ t.Fatalf("failed statuses on the new head started %d runs, want 1", n)
+ }
+ task, err := f.q.GetAgentTask(ctx, got.LastTaskID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(task.HandoffNote.String, `"checks":"failure"`) || !strings.Contains(task.HandoffNote.String, `"head_sha":"bbb"`) {
+ t.Fatalf("VCS PR facts missing: %s", task.HandoffNote.String)
+ }
+ if !strings.Contains(task.HandoffNote.String, `"provider":"forgejo"`) {
+ t.Fatalf("provider missing from PR facts: %s", task.HandoffNote.String)
+ }
+}
+
+func TestWakeupConditionPullRequestMergedCoversVCSProviders(t *testing.T) {
+ f, s, issue, agent := conditionFixture(t)
+ ctx := context.Background()
+ var pr string
+ if err := f.Pool.QueryRow(ctx, `INSERT INTO vcs_pull_request(workspace_id,connection_id,provider,repo_owner,repo_name,pr_number,title,state,html_url,pr_created_at,pr_updated_at,head_sha)
+ VALUES($1,gen_random_uuid(),'gitea','selfhosted','wakeup-test',(extract(epoch from clock_timestamp())*1000)::bigint % 100000,'PR','open','https://gitea.example.test/pr',now(),now(),'ccc') RETURNING id`, f.WorkspaceID).Scan(&pr); err != nil {
+ t.Fatal(err)
+ }
+ f.Cleanup(t, "DELETE FROM vcs_pull_request WHERE id=$1", pr)
+ f.Cleanup(t, "DELETE FROM issue_vcs_pull_request WHERE pull_request_id=$1", pr)
+ f.Exec(t, "INSERT INTO issue_vcs_pull_request(issue_id,pull_request_id) VALUES($1,$2)", issue, pr)
+ w := wakeCreate(t, f, s, issue, WakeupInput{AgentID: agent, Kind: "event", Instruction: "Note the merge",
+ Condition: condition(t, map[string]any{"type": "pull_request", "event": "merged"})})
+ wakeTick(t, f, s, w.ID)
+ if n := wakeRuns(t, f, w.ID); n != 0 {
+ t.Fatalf("open PR started %d runs", n)
+ }
+ f.Exec(t, "UPDATE vcs_pull_request SET state='merged' WHERE id=$1", pr)
+ wakeTick(t, f, s, w.ID)
+ if wakeRuns(t, f, w.ID) != 1 {
+ t.Fatalf("merged VCS PR did not start exactly 1 run")
+ }
+}
+
+// linkVCSPR links a self-hosted VCS pull request to issue and returns its id.
+func linkVCSPR(t *testing.T, f principalFixture, issue pgtype.UUID, connection, provider string, number int, head string) string {
+ t.Helper()
+ var pr string
+ if err := f.Pool.QueryRow(context.Background(), `INSERT INTO vcs_pull_request(workspace_id,connection_id,provider,repo_owner,repo_name,pr_number,title,state,html_url,pr_created_at,pr_updated_at,head_sha)
+ VALUES($1,$2,$3,'selfhosted','wakeup-test',$4,'PR','open','https://vcs.example.test/pr',now(),now(),$5) RETURNING id`,
+ f.WorkspaceID, connection, provider, number, head).Scan(&pr); err != nil {
+ t.Fatal(err)
+ }
+ f.Cleanup(t, "DELETE FROM vcs_pull_request WHERE id=$1", pr)
+ f.Cleanup(t, "DELETE FROM issue_vcs_pull_request WHERE pull_request_id=$1", pr)
+ f.Cleanup(t, "DELETE FROM vcs_commit_status WHERE connection_id=$1", connection)
+ f.Exec(t, "INSERT INTO issue_vcs_pull_request(issue_id,pull_request_id) VALUES($1,$2)", issue, pr)
+ return pr
+}
+
+func setVCSStatus(t *testing.T, f principalFixture, connection, sha, state string) {
+ t.Helper()
+ f.Exec(t, `INSERT INTO vcs_commit_status(connection_id,sha,context,state) VALUES($1,$2,'ci/test',$3)
+ ON CONFLICT (connection_id,sha,context) DO UPDATE SET state=EXCLUDED.state`, connection, sha, state)
+}
+
+func newConnection(t *testing.T, f principalFixture) string {
+ t.Helper()
+ var id string
+ if err := f.Pool.QueryRow(context.Background(), "SELECT gen_random_uuid()::text").Scan(&id); err != nil {
+ t.Fatal(err)
+ }
+ return id
+}
+
+func TestWakeupConditionPullRequestCoversEveryVCSProvider(t *testing.T) {
+ for _, provider := range []string{"forgejo", "gitea", "gitlab"} {
+ t.Run(provider+"/checks_finished", func(t *testing.T) {
+ f, s, issue, agent := conditionFixture(t)
+ s.Tasks.PRSnapshotsEnabled = true
+ connection := newConnection(t, f)
+ pr := linkVCSPR(t, f, issue, connection, provider, 7, "old")
+ setVCSStatus(t, f, connection, "old", "passed")
+ w := wakeCreate(t, f, s, issue, WakeupInput{AgentID: agent, Kind: "event", Instruction: "Look at CI",
+ Condition: condition(t, map[string]any{"type": "pull_request", "event": "checks_finished"})})
+ // A result already finished at registration belongs to the old head.
+ wakeTick(t, f, s, w.ID)
+ // A new head with no statuses yet is not finished.
+ f.Exec(t, "UPDATE vcs_pull_request SET head_sha='new' WHERE id=$1", pr)
+ wakeTick(t, f, s, w.ID)
+ // A late result for the replaced head must not count either.
+ setVCSStatus(t, f, connection, "old", "failed")
+ wakeTick(t, f, s, w.ID)
+ setVCSStatus(t, f, connection, "new", "pending")
+ wakeTick(t, f, s, w.ID)
+ if n := wakeRuns(t, f, w.ID); n != 0 {
+ t.Fatalf("unfinished current head started %d runs", n)
+ }
+ // The same head going from pending to passed wakes once.
+ setVCSStatus(t, f, connection, "new", "passed")
+ got := wakeTick(t, f, s, w.ID)
+ if n := wakeRuns(t, f, w.ID); n != 1 || got.LastError.Valid {
+ t.Fatalf("finished checks started %d runs (error %+v), want 1", n, got.LastError)
+ }
+ })
+ t.Run(provider+"/merged", func(t *testing.T) {
+ f, s, issue, agent := conditionFixture(t)
+ pr := linkVCSPR(t, f, issue, newConnection(t, f), provider, 7, "head")
+ w := wakeCreate(t, f, s, issue, WakeupInput{AgentID: agent, Kind: "event", Instruction: "Note the merge",
+ Condition: condition(t, map[string]any{"type": "pull_request", "event": "merged"})})
+ wakeTick(t, f, s, w.ID)
+ if n := wakeRuns(t, f, w.ID); n != 0 {
+ t.Fatalf("open PR started %d runs", n)
+ }
+ f.Exec(t, "UPDATE vcs_pull_request SET state='merged' WHERE id=$1", pr)
+ wakeTick(t, f, s, w.ID)
+ if n := wakeRuns(t, f, w.ID); n != 1 {
+ t.Fatalf("merged PR started %d runs, want 1", n)
+ }
+ })
+ }
+}
+
+// A server without the GitHub snapshot client still evaluates VCS PRs linked
+// next to a GitHub PR; the GitHub diagnostic only explains the wait.
+func TestWakeupConditionVCSChecksNotBlockedByGitHubSnapshotGate(t *testing.T) {
+ f, s, issue, agent := conditionFixture(t)
+ if s.Tasks.PRSnapshotsEnabled {
+ t.Fatal("fixture must start without the snapshot client")
+ }
+ gh := f.Insert(t, "github_pull_request", testutil.Cols{
+ "workspace_id": f.WorkspaceID, "installation_id": 1,
+ "repo_owner": "fixture", "repo_name": "wakeup", "pr_number": 1,
+ "title": "PR", "state": "open", "html_url": "https://example.test/pr/1",
+ "pr_created_at": "2026-01-01T00:00:00Z", "pr_updated_at": "2026-01-01T00:00:00Z",
+ })
+ f.Cleanup(t, "DELETE FROM issue_pull_request WHERE pull_request_id=$1", gh)
+ f.Exec(t, "INSERT INTO issue_pull_request(issue_id,pull_request_id) VALUES($1,$2)", issue, gh)
+ connection := newConnection(t, f)
+ linkVCSPR(t, f, issue, connection, "forgejo", 2, "sha")
+ setVCSStatus(t, f, connection, "sha", "pending")
+ w := wakeCreate(t, f, s, issue, WakeupInput{AgentID: agent, Kind: "event", Instruction: "Look at CI",
+ Condition: condition(t, map[string]any{"type": "pull_request", "event": "checks_finished"})})
+ got := wakeTick(t, f, s, w.ID)
+ if wakeRuns(t, f, w.ID) != 0 || !got.LastError.Valid || got.LastError.String != conditionDiagnosticPrefix+errPRSnapshotsUnavailable.Error() {
+ t.Fatalf("pending VCS checks: runs=%d error=%+v", wakeRuns(t, f, w.ID), got.LastError)
+ }
+ setVCSStatus(t, f, connection, "sha", "passed")
+ got = wakeTick(t, f, s, w.ID)
+ if n := wakeRuns(t, f, w.ID); n != 1 || got.LastError.Valid {
+ t.Fatalf("passed VCS checks started %d runs (error %+v), want 1 and a cleared diagnostic", n, got.LastError)
+ }
+}
+
+// PR numbers and shas repeat across connections: each PR rolls up its own
+// statuses and has its own fingerprint.
+func TestWakeupConditionVCSChecksKeyedByPullRequest(t *testing.T) {
+ setup := func(t *testing.T) (principalFixture, *IssueWakeupService, pgtype.UUID, string, string, string) {
+ f, s, issue, agent := conditionFixture(t)
+ s.Tasks.PRSnapshotsEnabled = true
+ a, b := newConnection(t, f), newConnection(t, f)
+ linkVCSPR(t, f, issue, a, "forgejo", 5, "same")
+ linkVCSPR(t, f, issue, b, "forgejo", 5, "same")
+ return f, s, issue, agent, a, b
+ }
+ create := func(t *testing.T, f principalFixture, s *IssueWakeupService, issue pgtype.UUID, agent string) db.IssueWakeup {
+ return wakeCreate(t, f, s, issue, WakeupInput{AgentID: agent, Kind: "event", Instruction: "Look at CI",
+ Condition: condition(t, map[string]any{"type": "pull_request", "event": "checks_finished"})})
+ }
+ t.Run("rollup", func(t *testing.T) {
+ f, s, issue, agent, a, b := setup(t)
+ setVCSStatus(t, f, a, "same", "pending")
+ setVCSStatus(t, f, b, "same", "pending")
+ w := create(t, f, s, issue, agent)
+ // One PR finishing satisfies the rule while the other still runs.
+ setVCSStatus(t, f, a, "same", "passed")
+ wakeTick(t, f, s, w.ID)
+ if n := wakeRuns(t, f, w.ID); n != 1 {
+ t.Fatalf("one finished PR started %d runs, want 1", n)
+ }
+ })
+ t.Run("fingerprint", func(t *testing.T) {
+ f, s, issue, agent, a, b := setup(t)
+ setVCSStatus(t, f, a, "same", "passed")
+ setVCSStatus(t, f, b, "same", "pending")
+ w := create(t, f, s, issue, agent)
+ // A reruns while B finishes before the next evaluation: a new result.
+ setVCSStatus(t, f, a, "same", "pending")
+ setVCSStatus(t, f, b, "same", "passed")
+ wakeTick(t, f, s, w.ID)
+ if n := wakeRuns(t, f, w.ID); n != 1 {
+ t.Fatalf("a different PR finishing started %d runs, want 1", n)
+ }
+ })
+}