mirror of
https://github.com/multica-ai/multica.git
synced 2026-08-03 11:10:23 +02:00
feat(github): API-snapshot PR cards — CI status + mergeability (MUL-5265)
Fetch each linked PR's CI checks and mergeability from the GitHub GraphQL API as the single source of truth (Plan C). Webhooks, page visits and a bounded TTL sweep are refresh triggers only; nothing is inferred from webhook payloads anymore. Backend (server/internal/integrations/ghsnapshot): - installation-token cache + GraphQL client (private key / tokens never logged) - one paginated pullRequest query -> normalized per-check snapshot - outbound queue: (installation,repo,PR) dedup + single in-flight per PR, bounded worker pool, Retry-After / rate-limit backoff, jitter - head-SHA-guarded atomic batch replace (a slow response for an old head can never overwrite a newer head's snapshot) - bounded chase window (30s->5m, stops on terminal/closed) + page-visit + TTL refresh; clean degradation when no App private key is configured Removes the old suite-level webhook aggregation display path (query + handlers + tests). check_suite / check_run / status are now pure triggers. Frontend: PR card shows two independent tri-state elements (CI status + mergeability). "Ready to merge" only when merge state is clean; no-checks and unknown-mergeable never assert a positive verdict; progress strip removed; four locales; stale marker. Docs: github-integration + environment-variables (four languages) — now required App private key, read-only Checks/Commit-statuses permissions, new event subscriptions, capability boundaries and troubleshooting. Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -115,67 +115,6 @@ func (q *Queries) DeletePendingGitHubInstallation(ctx context.Context, installat
|
||||
return err
|
||||
}
|
||||
|
||||
const drainPendingCheckSuitesForPR = `-- name: DrainPendingCheckSuitesForPR :many
|
||||
DELETE FROM github_pending_check_suite
|
||||
WHERE workspace_id = $1
|
||||
AND repo_owner = $2
|
||||
AND repo_name = $3
|
||||
AND pr_number = $4
|
||||
RETURNING suite_id, head_sha, app_id, conclusion, status, suite_updated_at
|
||||
`
|
||||
|
||||
type DrainPendingCheckSuitesForPRParams struct {
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
RepoOwner string `json:"repo_owner"`
|
||||
RepoName string `json:"repo_name"`
|
||||
PrNumber int32 `json:"pr_number"`
|
||||
}
|
||||
|
||||
type DrainPendingCheckSuitesForPRRow struct {
|
||||
SuiteID int64 `json:"suite_id"`
|
||||
HeadSha string `json:"head_sha"`
|
||||
AppID int64 `json:"app_id"`
|
||||
Conclusion pgtype.Text `json:"conclusion"`
|
||||
Status string `json:"status"`
|
||||
SuiteUpdatedAt pgtype.Timestamptz `json:"suite_updated_at"`
|
||||
}
|
||||
|
||||
// Atomically reads + deletes all pending suites for the given PR address.
|
||||
// Caller replays each row through UpsertPullRequestCheckSuite. RETURNING
|
||||
// gives us the payloads we need without a separate SELECT, so two parallel
|
||||
// handlers racing on the same PR can't double-apply the same row.
|
||||
func (q *Queries) DrainPendingCheckSuitesForPR(ctx context.Context, arg DrainPendingCheckSuitesForPRParams) ([]DrainPendingCheckSuitesForPRRow, error) {
|
||||
rows, err := q.db.Query(ctx, drainPendingCheckSuitesForPR,
|
||||
arg.WorkspaceID,
|
||||
arg.RepoOwner,
|
||||
arg.RepoName,
|
||||
arg.PrNumber,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []DrainPendingCheckSuitesForPRRow{}
|
||||
for rows.Next() {
|
||||
var i DrainPendingCheckSuitesForPRRow
|
||||
if err := rows.Scan(
|
||||
&i.SuiteID,
|
||||
&i.HeadSha,
|
||||
&i.AppID,
|
||||
&i.Conclusion,
|
||||
&i.Status,
|
||||
&i.SuiteUpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getGitHubInstallationByID = `-- name: GetGitHubInstallationByID :one
|
||||
SELECT id, workspace_id, installation_id, account_login, account_type, account_avatar_url, connected_by_id, created_at, updated_at FROM github_installation
|
||||
WHERE id = $1
|
||||
@@ -199,7 +138,7 @@ func (q *Queries) GetGitHubInstallationByID(ctx context.Context, id pgtype.UUID)
|
||||
}
|
||||
|
||||
const getGitHubPullRequest = `-- name: GetGitHubPullRequest :one
|
||||
SELECT id, workspace_id, installation_id, repo_owner, repo_name, pr_number, title, state, html_url, branch, author_login, author_avatar_url, merged_at, closed_at, pr_created_at, pr_updated_at, created_at, updated_at, head_sha, mergeable_state, additions, deletions, changed_files FROM github_pull_request
|
||||
SELECT id, workspace_id, installation_id, repo_owner, repo_name, pr_number, title, state, html_url, branch, author_login, author_avatar_url, merged_at, closed_at, pr_created_at, pr_updated_at, created_at, updated_at, head_sha, mergeable_state, additions, deletions, changed_files, api_mergeable, api_merge_state_status, checks_rollup_state, snapshot_head_sha, snapshot_fetched_at FROM github_pull_request
|
||||
WHERE workspace_id = $1 AND repo_owner = $2 AND repo_name = $3 AND pr_number = $4
|
||||
`
|
||||
|
||||
@@ -242,6 +181,11 @@ func (q *Queries) GetGitHubPullRequest(ctx context.Context, arg GetGitHubPullReq
|
||||
&i.Additions,
|
||||
&i.Deletions,
|
||||
&i.ChangedFiles,
|
||||
&i.ApiMergeable,
|
||||
&i.ApiMergeStateStatus,
|
||||
&i.ChecksRollupState,
|
||||
&i.SnapshotHeadSha,
|
||||
&i.SnapshotFetchedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -500,33 +444,32 @@ func (q *Queries) ListIssueIDsForPullRequest(ctx context.Context, pullRequestID
|
||||
|
||||
const listPullRequestsByIssue = `-- name: ListPullRequestsByIssue :many
|
||||
WITH issue_prs AS (
|
||||
SELECT pr.id, pr.head_sha
|
||||
SELECT pr.id, pr.snapshot_head_sha
|
||||
FROM github_pull_request pr
|
||||
JOIN issue_pull_request ipr ON ipr.pull_request_id = pr.id
|
||||
WHERE ipr.issue_id = $1 AND NOT ipr.reference_only
|
||||
),
|
||||
per_app_latest AS (
|
||||
SELECT DISTINCT ON (cs.pr_id, cs.app_id)
|
||||
cs.pr_id, cs.app_id, cs.conclusion, cs.status
|
||||
FROM github_pull_request_check_suite cs
|
||||
JOIN issue_prs ip ON ip.id = cs.pr_id
|
||||
WHERE cs.head_sha = ip.head_sha AND ip.head_sha <> ''
|
||||
ORDER BY cs.pr_id, cs.app_id, cs.updated_at DESC
|
||||
),
|
||||
checks AS (
|
||||
SELECT
|
||||
pr_id,
|
||||
cr.pr_id,
|
||||
COUNT(*)::bigint AS total,
|
||||
SUM(CASE WHEN status = 'completed' AND conclusion IN
|
||||
('failure','cancelled','timed_out','action_required','startup_failure','stale')
|
||||
SUM(CASE WHEN cr.status = 'completed' AND cr.conclusion IN
|
||||
('failure','cancelled','timed_out','action_required','startup_failure','stale','error')
|
||||
THEN 1 ELSE 0 END)::bigint AS failed,
|
||||
SUM(CASE WHEN status = 'completed' AND conclusion IN
|
||||
SUM(CASE WHEN cr.status = 'completed' AND cr.conclusion IN
|
||||
('success','neutral','skipped')
|
||||
THEN 1 ELSE 0 END)::bigint AS passed,
|
||||
SUM(CASE WHEN status <> 'completed' OR conclusion IS NULL
|
||||
THEN 1 ELSE 0 END)::bigint AS pending
|
||||
FROM per_app_latest
|
||||
GROUP BY pr_id
|
||||
SUM(CASE WHEN cr.status <> 'completed' OR cr.conclusion IS NULL
|
||||
THEN 1 ELSE 0 END)::bigint AS running,
|
||||
COALESCE(
|
||||
array_agg(cr.name) FILTER (WHERE cr.status = 'completed' AND cr.conclusion IN
|
||||
('failure','cancelled','timed_out','action_required','startup_failure','stale','error')),
|
||||
'{}'
|
||||
)::text[] AS failed_names
|
||||
FROM github_pull_request_check_run cr
|
||||
JOIN issue_prs ip ON ip.id = cr.pr_id
|
||||
WHERE cr.head_sha = ip.snapshot_head_sha AND ip.snapshot_head_sha <> ''
|
||||
GROUP BY cr.pr_id
|
||||
)
|
||||
SELECT
|
||||
pr.id, pr.workspace_id, pr.installation_id, pr.repo_owner, pr.repo_name,
|
||||
@@ -534,11 +477,14 @@ SELECT
|
||||
pr.author_avatar_url, pr.merged_at, pr.closed_at, pr.pr_created_at,
|
||||
pr.pr_updated_at, pr.head_sha, pr.mergeable_state,
|
||||
pr.additions, pr.deletions, pr.changed_files,
|
||||
pr.api_mergeable, pr.api_merge_state_status, pr.checks_rollup_state,
|
||||
pr.snapshot_head_sha, pr.snapshot_fetched_at,
|
||||
pr.created_at, pr.updated_at,
|
||||
COALESCE(c.total, 0)::bigint AS checks_total,
|
||||
COALESCE(c.passed, 0)::bigint AS checks_passed,
|
||||
COALESCE(c.failed, 0)::bigint AS checks_failed,
|
||||
COALESCE(c.pending, 0)::bigint AS checks_pending
|
||||
COALESCE(c.running, 0)::bigint AS checks_running,
|
||||
COALESCE(c.failed_names, '{}')::text[] AS failed_check_names
|
||||
FROM github_pull_request pr
|
||||
JOIN issue_pull_request ipr ON ipr.pull_request_id = pr.id
|
||||
LEFT JOIN checks c ON c.pr_id = pr.id
|
||||
@@ -547,46 +493,51 @@ ORDER BY pr.pr_created_at DESC
|
||||
`
|
||||
|
||||
type ListPullRequestsByIssueRow struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
InstallationID int64 `json:"installation_id"`
|
||||
RepoOwner string `json:"repo_owner"`
|
||||
RepoName string `json:"repo_name"`
|
||||
PrNumber int32 `json:"pr_number"`
|
||||
Title string `json:"title"`
|
||||
State string `json:"state"`
|
||||
HtmlUrl string `json:"html_url"`
|
||||
Branch pgtype.Text `json:"branch"`
|
||||
AuthorLogin pgtype.Text `json:"author_login"`
|
||||
AuthorAvatarUrl pgtype.Text `json:"author_avatar_url"`
|
||||
MergedAt pgtype.Timestamptz `json:"merged_at"`
|
||||
ClosedAt pgtype.Timestamptz `json:"closed_at"`
|
||||
PrCreatedAt pgtype.Timestamptz `json:"pr_created_at"`
|
||||
PrUpdatedAt pgtype.Timestamptz `json:"pr_updated_at"`
|
||||
HeadSha string `json:"head_sha"`
|
||||
MergeableState pgtype.Text `json:"mergeable_state"`
|
||||
Additions int32 `json:"additions"`
|
||||
Deletions int32 `json:"deletions"`
|
||||
ChangedFiles int32 `json:"changed_files"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
ChecksTotal int64 `json:"checks_total"`
|
||||
ChecksPassed int64 `json:"checks_passed"`
|
||||
ChecksFailed int64 `json:"checks_failed"`
|
||||
ChecksPending int64 `json:"checks_pending"`
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
InstallationID int64 `json:"installation_id"`
|
||||
RepoOwner string `json:"repo_owner"`
|
||||
RepoName string `json:"repo_name"`
|
||||
PrNumber int32 `json:"pr_number"`
|
||||
Title string `json:"title"`
|
||||
State string `json:"state"`
|
||||
HtmlUrl string `json:"html_url"`
|
||||
Branch pgtype.Text `json:"branch"`
|
||||
AuthorLogin pgtype.Text `json:"author_login"`
|
||||
AuthorAvatarUrl pgtype.Text `json:"author_avatar_url"`
|
||||
MergedAt pgtype.Timestamptz `json:"merged_at"`
|
||||
ClosedAt pgtype.Timestamptz `json:"closed_at"`
|
||||
PrCreatedAt pgtype.Timestamptz `json:"pr_created_at"`
|
||||
PrUpdatedAt pgtype.Timestamptz `json:"pr_updated_at"`
|
||||
HeadSha string `json:"head_sha"`
|
||||
MergeableState pgtype.Text `json:"mergeable_state"`
|
||||
Additions int32 `json:"additions"`
|
||||
Deletions int32 `json:"deletions"`
|
||||
ChangedFiles int32 `json:"changed_files"`
|
||||
ApiMergeable pgtype.Text `json:"api_mergeable"`
|
||||
ApiMergeStateStatus pgtype.Text `json:"api_merge_state_status"`
|
||||
ChecksRollupState pgtype.Text `json:"checks_rollup_state"`
|
||||
SnapshotHeadSha string `json:"snapshot_head_sha"`
|
||||
SnapshotFetchedAt pgtype.Timestamptz `json:"snapshot_fetched_at"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
ChecksTotal int64 `json:"checks_total"`
|
||||
ChecksPassed int64 `json:"checks_passed"`
|
||||
ChecksFailed int64 `json:"checks_failed"`
|
||||
ChecksRunning int64 `json:"checks_running"`
|
||||
FailedCheckNames []string `json:"failed_check_names"`
|
||||
}
|
||||
|
||||
// Returns the issue's linked PRs with the aggregated check-suite counts for
|
||||
// the PR's CURRENT head SHA. The `issue_prs` CTE narrows to this issue's PR
|
||||
// ids first so the per-app aggregation only touches suite rows for those
|
||||
// PRs — without that scoping the planner has to scan/aggregate every PR's
|
||||
// suites in the workspace before joining on issue. Per-app latest suite is
|
||||
// selected so a single app firing multiple suites on the same head doesn't
|
||||
// get counted N times. Late-arriving suites for an OLD head are stored but
|
||||
// excluded by the head_sha filter, so they can't override the new head's
|
||||
// pending view. reference_only links (a PR that merely mentions the issue
|
||||
// identifier in its body, with no closing keyword and no title/branch
|
||||
// reference) are filtered out — they are not working PRs for this issue.
|
||||
// Returns the issue's linked PRs with the GitHub API snapshot (MUL-5265): the
|
||||
// mergeability verdict, the CI rollup, and per-check counts for the PR's
|
||||
// CURRENT snapshot head SHA. Checks are aggregated from
|
||||
// github_pull_request_check_run — the run-level snapshot written by the API
|
||||
// refresh pipeline — NOT the legacy suite-level webhook aggregation, which is
|
||||
// removed. The `issue_prs` CTE narrows to this issue's PR ids first so the
|
||||
// aggregation only touches check rows for those PRs. Rows for an OLD head are
|
||||
// excluded by the snapshot_head_sha filter. reference_only links (a PR that
|
||||
// merely mentions the issue identifier in its body, with no closing keyword and
|
||||
// no title/branch reference) are filtered out — they are not working PRs.
|
||||
func (q *Queries) ListPullRequestsByIssue(ctx context.Context, issueID pgtype.UUID) ([]ListPullRequestsByIssueRow, error) {
|
||||
rows, err := q.db.Query(ctx, listPullRequestsByIssue, issueID)
|
||||
if err != nil {
|
||||
@@ -618,12 +569,18 @@ func (q *Queries) ListPullRequestsByIssue(ctx context.Context, issueID pgtype.UU
|
||||
&i.Additions,
|
||||
&i.Deletions,
|
||||
&i.ChangedFiles,
|
||||
&i.ApiMergeable,
|
||||
&i.ApiMergeStateStatus,
|
||||
&i.ChecksRollupState,
|
||||
&i.SnapshotHeadSha,
|
||||
&i.SnapshotFetchedAt,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.ChecksTotal,
|
||||
&i.ChecksPassed,
|
||||
&i.ChecksFailed,
|
||||
&i.ChecksPending,
|
||||
&i.ChecksRunning,
|
||||
&i.FailedCheckNames,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -741,7 +698,7 @@ ON CONFLICT (workspace_id, repo_owner, repo_name, pr_number) DO UPDATE SET
|
||||
deletions = EXCLUDED.deletions,
|
||||
changed_files = EXCLUDED.changed_files,
|
||||
updated_at = now()
|
||||
RETURNING id, workspace_id, installation_id, repo_owner, repo_name, pr_number, title, state, html_url, branch, author_login, author_avatar_url, merged_at, closed_at, pr_created_at, pr_updated_at, created_at, updated_at, head_sha, mergeable_state, additions, deletions, changed_files
|
||||
RETURNING id, workspace_id, installation_id, repo_owner, repo_name, pr_number, title, state, html_url, branch, author_login, author_avatar_url, merged_at, closed_at, pr_created_at, pr_updated_at, created_at, updated_at, head_sha, mergeable_state, additions, deletions, changed_files, api_mergeable, api_merge_state_status, checks_rollup_state, snapshot_head_sha, snapshot_fetched_at
|
||||
`
|
||||
|
||||
type UpsertGitHubPullRequestParams struct {
|
||||
@@ -830,71 +787,15 @@ func (q *Queries) UpsertGitHubPullRequest(ctx context.Context, arg UpsertGitHubP
|
||||
&i.Additions,
|
||||
&i.Deletions,
|
||||
&i.ChangedFiles,
|
||||
&i.ApiMergeable,
|
||||
&i.ApiMergeStateStatus,
|
||||
&i.ChecksRollupState,
|
||||
&i.SnapshotHeadSha,
|
||||
&i.SnapshotFetchedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const upsertPendingCheckSuite = `-- name: UpsertPendingCheckSuite :exec
|
||||
|
||||
INSERT INTO github_pending_check_suite (
|
||||
workspace_id, installation_id, repo_owner, repo_name, pr_number,
|
||||
suite_id, head_sha, app_id, conclusion, status, suite_updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5,
|
||||
$6, $7, $8, $11, $9, $10
|
||||
)
|
||||
ON CONFLICT (workspace_id, repo_owner, repo_name, pr_number, suite_id) DO UPDATE SET
|
||||
installation_id = EXCLUDED.installation_id,
|
||||
head_sha = EXCLUDED.head_sha,
|
||||
app_id = EXCLUDED.app_id,
|
||||
conclusion = EXCLUDED.conclusion,
|
||||
status = EXCLUDED.status,
|
||||
suite_updated_at = EXCLUDED.suite_updated_at,
|
||||
received_at = now()
|
||||
WHERE EXCLUDED.suite_updated_at >= github_pending_check_suite.suite_updated_at
|
||||
`
|
||||
|
||||
type UpsertPendingCheckSuiteParams struct {
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
InstallationID int64 `json:"installation_id"`
|
||||
RepoOwner string `json:"repo_owner"`
|
||||
RepoName string `json:"repo_name"`
|
||||
PrNumber int32 `json:"pr_number"`
|
||||
SuiteID int64 `json:"suite_id"`
|
||||
HeadSha string `json:"head_sha"`
|
||||
AppID int64 `json:"app_id"`
|
||||
Status string `json:"status"`
|
||||
SuiteUpdatedAt pgtype.Timestamptz `json:"suite_updated_at"`
|
||||
Conclusion pgtype.Text `json:"conclusion"`
|
||||
}
|
||||
|
||||
// =====================
|
||||
// GitHub pending check_suite (out-of-order arrival stash)
|
||||
// =====================
|
||||
// Stashes a check_suite event whose PR row is not yet mirrored. Replayed
|
||||
// (and deleted) by DrainPendingCheckSuitesForPR once the matching
|
||||
// `pull_request` webhook lands. ON CONFLICT keeps the newest payload
|
||||
// for the same (workspace, repo, pr_number, suite_id) — repeated
|
||||
// deliveries while the PR is still missing are idempotent. The
|
||||
// suite_updated_at guard mirrors UpsertPullRequestCheckSuite so an older
|
||||
// event arriving after a newer one cannot overwrite the newer payload.
|
||||
func (q *Queries) UpsertPendingCheckSuite(ctx context.Context, arg UpsertPendingCheckSuiteParams) error {
|
||||
_, err := q.db.Exec(ctx, upsertPendingCheckSuite,
|
||||
arg.WorkspaceID,
|
||||
arg.InstallationID,
|
||||
arg.RepoOwner,
|
||||
arg.RepoName,
|
||||
arg.PrNumber,
|
||||
arg.SuiteID,
|
||||
arg.HeadSha,
|
||||
arg.AppID,
|
||||
arg.Status,
|
||||
arg.SuiteUpdatedAt,
|
||||
arg.Conclusion,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const upsertPendingGitHubInstallation = `-- name: UpsertPendingGitHubInstallation :one
|
||||
INSERT INTO github_pending_installation (
|
||||
installation_id, account_login, account_type, account_avatar_url
|
||||
@@ -934,51 +835,3 @@ func (q *Queries) UpsertPendingGitHubInstallation(ctx context.Context, arg Upser
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const upsertPullRequestCheckSuite = `-- name: UpsertPullRequestCheckSuite :exec
|
||||
|
||||
INSERT INTO github_pull_request_check_suite (
|
||||
pr_id, suite_id, head_sha, app_id, conclusion, status, updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $7, $5, $6
|
||||
)
|
||||
ON CONFLICT (pr_id, suite_id) DO UPDATE SET
|
||||
head_sha = EXCLUDED.head_sha,
|
||||
app_id = EXCLUDED.app_id,
|
||||
conclusion = EXCLUDED.conclusion,
|
||||
status = EXCLUDED.status,
|
||||
updated_at = EXCLUDED.updated_at
|
||||
WHERE EXCLUDED.updated_at >= github_pull_request_check_suite.updated_at
|
||||
`
|
||||
|
||||
type UpsertPullRequestCheckSuiteParams struct {
|
||||
PrID pgtype.UUID `json:"pr_id"`
|
||||
SuiteID int64 `json:"suite_id"`
|
||||
HeadSha string `json:"head_sha"`
|
||||
AppID int64 `json:"app_id"`
|
||||
Status string `json:"status"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
Conclusion pgtype.Text `json:"conclusion"`
|
||||
}
|
||||
|
||||
// =====================
|
||||
// GitHub PR check suite
|
||||
// =====================
|
||||
// Upserts a single check_suite row keyed by (pr_id, suite_id). The WHERE
|
||||
// clause on the DO UPDATE branch prevents a late-arriving older event from
|
||||
// overwriting a newer one — same-PR/same-suite ordering protection. Late
|
||||
// events targeting an old head still land here (their head_sha is stored
|
||||
// on the row); the head_sha filter in ListPullRequestsByIssue keeps them
|
||||
// out of the current aggregate.
|
||||
func (q *Queries) UpsertPullRequestCheckSuite(ctx context.Context, arg UpsertPullRequestCheckSuiteParams) error {
|
||||
_, err := q.db.Exec(ctx, upsertPullRequestCheckSuite,
|
||||
arg.PrID,
|
||||
arg.SuiteID,
|
||||
arg.HeadSha,
|
||||
arg.AppID,
|
||||
arg.Status,
|
||||
arg.UpdatedAt,
|
||||
arg.Conclusion,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
292
server/pkg/db/generated/github_snapshot.sql.go
Normal file
292
server/pkg/db/generated/github_snapshot.sql.go
Normal file
@@ -0,0 +1,292 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: github_snapshot.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const deleteGitHubPRCheckRuns = `-- name: DeleteGitHubPRCheckRuns :exec
|
||||
DELETE FROM github_pull_request_check_run WHERE pr_id = $1
|
||||
`
|
||||
|
||||
// First half of the atomic per-check replace. Runs inside the same transaction
|
||||
// as UpdateGitHubPRSnapshot and the inserts below.
|
||||
func (q *Queries) DeleteGitHubPRCheckRuns(ctx context.Context, prID pgtype.UUID) error {
|
||||
_, err := q.db.Exec(ctx, deleteGitHubPRCheckRuns, prID)
|
||||
return err
|
||||
}
|
||||
|
||||
const getGitHubPullRequestByID = `-- name: GetGitHubPullRequestByID :one
|
||||
SELECT id, workspace_id, installation_id, repo_owner, repo_name, pr_number, title, state, html_url, branch, author_login, author_avatar_url, merged_at, closed_at, pr_created_at, pr_updated_at, created_at, updated_at, head_sha, mergeable_state, additions, deletions, changed_files, api_mergeable, api_merge_state_status, checks_rollup_state, snapshot_head_sha, snapshot_fetched_at FROM github_pull_request WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetGitHubPullRequestByID(ctx context.Context, id pgtype.UUID) (GithubPullRequest, error) {
|
||||
row := q.db.QueryRow(ctx, getGitHubPullRequestByID, id)
|
||||
var i GithubPullRequest
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.WorkspaceID,
|
||||
&i.InstallationID,
|
||||
&i.RepoOwner,
|
||||
&i.RepoName,
|
||||
&i.PrNumber,
|
||||
&i.Title,
|
||||
&i.State,
|
||||
&i.HtmlUrl,
|
||||
&i.Branch,
|
||||
&i.AuthorLogin,
|
||||
&i.AuthorAvatarUrl,
|
||||
&i.MergedAt,
|
||||
&i.ClosedAt,
|
||||
&i.PrCreatedAt,
|
||||
&i.PrUpdatedAt,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.HeadSha,
|
||||
&i.MergeableState,
|
||||
&i.Additions,
|
||||
&i.Deletions,
|
||||
&i.ChangedFiles,
|
||||
&i.ApiMergeable,
|
||||
&i.ApiMergeStateStatus,
|
||||
&i.ChecksRollupState,
|
||||
&i.SnapshotHeadSha,
|
||||
&i.SnapshotFetchedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertGitHubPRCheckRun = `-- name: InsertGitHubPRCheckRun :exec
|
||||
INSERT INTO github_pull_request_check_run (
|
||||
pr_id, head_sha, ordinal, name, status, conclusion, details_url, is_status_context
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $7, $8, $6
|
||||
)
|
||||
`
|
||||
|
||||
type InsertGitHubPRCheckRunParams struct {
|
||||
PrID pgtype.UUID `json:"pr_id"`
|
||||
HeadSha string `json:"head_sha"`
|
||||
Ordinal int32 `json:"ordinal"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
IsStatusContext bool `json:"is_status_context"`
|
||||
Conclusion pgtype.Text `json:"conclusion"`
|
||||
DetailsUrl pgtype.Text `json:"details_url"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertGitHubPRCheckRun(ctx context.Context, arg InsertGitHubPRCheckRunParams) error {
|
||||
_, err := q.db.Exec(ctx, insertGitHubPRCheckRun,
|
||||
arg.PrID,
|
||||
arg.HeadSha,
|
||||
arg.Ordinal,
|
||||
arg.Name,
|
||||
arg.Status,
|
||||
arg.IsStatusContext,
|
||||
arg.Conclusion,
|
||||
arg.DetailsUrl,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const listGitHubPRNumbersByHeadSHA = `-- name: ListGitHubPRNumbersByHeadSHA :many
|
||||
SELECT DISTINCT pr_number
|
||||
FROM github_pull_request
|
||||
WHERE installation_id = $1 AND repo_owner = $2 AND repo_name = $3 AND head_sha = $4
|
||||
`
|
||||
|
||||
type ListGitHubPRNumbersByHeadSHAParams struct {
|
||||
InstallationID int64 `json:"installation_id"`
|
||||
RepoOwner string `json:"repo_owner"`
|
||||
RepoName string `json:"repo_name"`
|
||||
HeadSha string `json:"head_sha"`
|
||||
}
|
||||
|
||||
// Resolves a commit SHA to the PR numbers whose head it is. `status` webhook
|
||||
// events (legacy commit statuses) carry a SHA + repo but no PR number, so we
|
||||
// map back through the mirrored head_sha to find which PR(s) to refresh.
|
||||
func (q *Queries) ListGitHubPRNumbersByHeadSHA(ctx context.Context, arg ListGitHubPRNumbersByHeadSHAParams) ([]int32, error) {
|
||||
rows, err := q.db.Query(ctx, listGitHubPRNumbersByHeadSHA,
|
||||
arg.InstallationID,
|
||||
arg.RepoOwner,
|
||||
arg.RepoName,
|
||||
arg.HeadSha,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []int32{}
|
||||
for rows.Next() {
|
||||
var pr_number int32
|
||||
if err := rows.Scan(&pr_number); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, pr_number)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listGitHubPRRowsByAddress = `-- name: ListGitHubPRRowsByAddress :many
|
||||
|
||||
SELECT id, workspace_id, head_sha, state
|
||||
FROM github_pull_request
|
||||
WHERE installation_id = $1 AND repo_owner = $2 AND repo_name = $3 AND pr_number = $4
|
||||
`
|
||||
|
||||
type ListGitHubPRRowsByAddressParams struct {
|
||||
InstallationID int64 `json:"installation_id"`
|
||||
RepoOwner string `json:"repo_owner"`
|
||||
RepoName string `json:"repo_name"`
|
||||
PrNumber int32 `json:"pr_number"`
|
||||
}
|
||||
|
||||
type ListGitHubPRRowsByAddressRow struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
HeadSha string `json:"head_sha"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
// =====================
|
||||
// GitHub API snapshot (MUL-5265, Plan C)
|
||||
//
|
||||
// These queries back the API-snapshot refresh pipeline. The GitHub GraphQL
|
||||
// response is the single source of truth; each successful fetch is written as
|
||||
// one atomic batch replace (guarded update of the PR row + full replace of the
|
||||
// per-check rows) inside a single transaction.
|
||||
// =====================
|
||||
// One (installation, owner, repo, number) address can map to several
|
||||
// github_pull_request rows — the same installation can be bound to multiple
|
||||
// workspaces (#4823/#4855), each mirroring its own row. A single API fetch is
|
||||
// applied to every matching row (each guarded by its own head_sha).
|
||||
func (q *Queries) ListGitHubPRRowsByAddress(ctx context.Context, arg ListGitHubPRRowsByAddressParams) ([]ListGitHubPRRowsByAddressRow, error) {
|
||||
rows, err := q.db.Query(ctx, listGitHubPRRowsByAddress,
|
||||
arg.InstallationID,
|
||||
arg.RepoOwner,
|
||||
arg.RepoName,
|
||||
arg.PrNumber,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListGitHubPRRowsByAddressRow{}
|
||||
for rows.Next() {
|
||||
var i ListGitHubPRRowsByAddressRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.WorkspaceID,
|
||||
&i.HeadSha,
|
||||
&i.State,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listStaleOpenGitHubPRs = `-- name: ListStaleOpenGitHubPRs :many
|
||||
SELECT DISTINCT installation_id, repo_owner, repo_name, pr_number
|
||||
FROM github_pull_request
|
||||
WHERE state IN ('open', 'draft')
|
||||
AND (snapshot_fetched_at IS NULL OR snapshot_fetched_at < $1)
|
||||
ORDER BY installation_id, repo_owner, repo_name, pr_number
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListStaleOpenGitHubPRsParams struct {
|
||||
OlderThan pgtype.Timestamptz `json:"older_than"`
|
||||
MaxRows int32 `json:"max_rows"`
|
||||
}
|
||||
|
||||
type ListStaleOpenGitHubPRsRow struct {
|
||||
InstallationID int64 `json:"installation_id"`
|
||||
RepoOwner string `json:"repo_owner"`
|
||||
RepoName string `json:"repo_name"`
|
||||
PrNumber int32 `json:"pr_number"`
|
||||
}
|
||||
|
||||
// TTL / safety-net sweep source. Returns distinct addresses of open/draft PRs
|
||||
// whose snapshot is missing or older than the TTL cutoff. Bounded by LIMIT so
|
||||
// one sweep can never fan out unbounded. Open PRs whose base branch advanced
|
||||
// (a conflict-producing event that emits NO pull_request webhook on this PR)
|
||||
// are recovered here without needing Contents:read. Merged/closed PRs are
|
||||
// excluded — a settled PR leaves the refresh set.
|
||||
func (q *Queries) ListStaleOpenGitHubPRs(ctx context.Context, arg ListStaleOpenGitHubPRsParams) ([]ListStaleOpenGitHubPRsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listStaleOpenGitHubPRs, arg.OlderThan, arg.MaxRows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListStaleOpenGitHubPRsRow{}
|
||||
for rows.Next() {
|
||||
var i ListStaleOpenGitHubPRsRow
|
||||
if err := rows.Scan(
|
||||
&i.InstallationID,
|
||||
&i.RepoOwner,
|
||||
&i.RepoName,
|
||||
&i.PrNumber,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateGitHubPRSnapshot = `-- name: UpdateGitHubPRSnapshot :execrows
|
||||
UPDATE github_pull_request
|
||||
SET api_mergeable = $1,
|
||||
api_merge_state_status = $2,
|
||||
checks_rollup_state = $3,
|
||||
snapshot_head_sha = $4,
|
||||
snapshot_fetched_at = $5,
|
||||
updated_at = now()
|
||||
WHERE id = $6 AND head_sha = $4
|
||||
`
|
||||
|
||||
type UpdateGitHubPRSnapshotParams struct {
|
||||
ApiMergeable pgtype.Text `json:"api_mergeable"`
|
||||
ApiMergeStateStatus pgtype.Text `json:"api_merge_state_status"`
|
||||
ChecksRollupState pgtype.Text `json:"checks_rollup_state"`
|
||||
HeadSha string `json:"head_sha"`
|
||||
FetchedAt pgtype.Timestamptz `json:"fetched_at"`
|
||||
PrID pgtype.UUID `json:"pr_id"`
|
||||
}
|
||||
|
||||
// Head-SHA anti-stale write (acceptance criterion 1): the snapshot is written
|
||||
// only when the row's current head_sha still equals the head the snapshot was
|
||||
// fetched for. If the head advanced (a newer push landed while this request was
|
||||
// in flight, mirrored by the pull_request webhook), 0 rows are updated and the
|
||||
// caller discards the whole response — the per-check replace is skipped too.
|
||||
func (q *Queries) UpdateGitHubPRSnapshot(ctx context.Context, arg UpdateGitHubPRSnapshotParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, updateGitHubPRSnapshot,
|
||||
arg.ApiMergeable,
|
||||
arg.ApiMergeStateStatus,
|
||||
arg.ChecksRollupState,
|
||||
arg.HeadSha,
|
||||
arg.FetchedAt,
|
||||
arg.PrID,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
@@ -521,29 +521,45 @@ type GithubPendingInstallation struct {
|
||||
}
|
||||
|
||||
type GithubPullRequest struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
InstallationID int64 `json:"installation_id"`
|
||||
RepoOwner string `json:"repo_owner"`
|
||||
RepoName string `json:"repo_name"`
|
||||
PrNumber int32 `json:"pr_number"`
|
||||
Title string `json:"title"`
|
||||
State string `json:"state"`
|
||||
HtmlUrl string `json:"html_url"`
|
||||
Branch pgtype.Text `json:"branch"`
|
||||
AuthorLogin pgtype.Text `json:"author_login"`
|
||||
AuthorAvatarUrl pgtype.Text `json:"author_avatar_url"`
|
||||
MergedAt pgtype.Timestamptz `json:"merged_at"`
|
||||
ClosedAt pgtype.Timestamptz `json:"closed_at"`
|
||||
PrCreatedAt pgtype.Timestamptz `json:"pr_created_at"`
|
||||
PrUpdatedAt pgtype.Timestamptz `json:"pr_updated_at"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
HeadSha string `json:"head_sha"`
|
||||
MergeableState pgtype.Text `json:"mergeable_state"`
|
||||
Additions int32 `json:"additions"`
|
||||
Deletions int32 `json:"deletions"`
|
||||
ChangedFiles int32 `json:"changed_files"`
|
||||
ID pgtype.UUID `json:"id"`
|
||||
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
||||
InstallationID int64 `json:"installation_id"`
|
||||
RepoOwner string `json:"repo_owner"`
|
||||
RepoName string `json:"repo_name"`
|
||||
PrNumber int32 `json:"pr_number"`
|
||||
Title string `json:"title"`
|
||||
State string `json:"state"`
|
||||
HtmlUrl string `json:"html_url"`
|
||||
Branch pgtype.Text `json:"branch"`
|
||||
AuthorLogin pgtype.Text `json:"author_login"`
|
||||
AuthorAvatarUrl pgtype.Text `json:"author_avatar_url"`
|
||||
MergedAt pgtype.Timestamptz `json:"merged_at"`
|
||||
ClosedAt pgtype.Timestamptz `json:"closed_at"`
|
||||
PrCreatedAt pgtype.Timestamptz `json:"pr_created_at"`
|
||||
PrUpdatedAt pgtype.Timestamptz `json:"pr_updated_at"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
HeadSha string `json:"head_sha"`
|
||||
MergeableState pgtype.Text `json:"mergeable_state"`
|
||||
Additions int32 `json:"additions"`
|
||||
Deletions int32 `json:"deletions"`
|
||||
ChangedFiles int32 `json:"changed_files"`
|
||||
ApiMergeable pgtype.Text `json:"api_mergeable"`
|
||||
ApiMergeStateStatus pgtype.Text `json:"api_merge_state_status"`
|
||||
ChecksRollupState pgtype.Text `json:"checks_rollup_state"`
|
||||
SnapshotHeadSha string `json:"snapshot_head_sha"`
|
||||
SnapshotFetchedAt pgtype.Timestamptz `json:"snapshot_fetched_at"`
|
||||
}
|
||||
|
||||
type GithubPullRequestCheckRun struct {
|
||||
PrID pgtype.UUID `json:"pr_id"`
|
||||
HeadSha string `json:"head_sha"`
|
||||
Ordinal int32 `json:"ordinal"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Conclusion pgtype.Text `json:"conclusion"`
|
||||
DetailsUrl pgtype.Text `json:"details_url"`
|
||||
IsStatusContext bool `json:"is_status_context"`
|
||||
}
|
||||
|
||||
type GithubPullRequestCheckSuite struct {
|
||||
|
||||
@@ -130,45 +130,43 @@ SELECT * FROM github_pull_request
|
||||
WHERE workspace_id = $1 AND repo_owner = $2 AND repo_name = $3 AND pr_number = $4;
|
||||
|
||||
-- name: ListPullRequestsByIssue :many
|
||||
-- Returns the issue's linked PRs with the aggregated check-suite counts for
|
||||
-- the PR's CURRENT head SHA. The `issue_prs` CTE narrows to this issue's PR
|
||||
-- ids first so the per-app aggregation only touches suite rows for those
|
||||
-- PRs — without that scoping the planner has to scan/aggregate every PR's
|
||||
-- suites in the workspace before joining on issue. Per-app latest suite is
|
||||
-- selected so a single app firing multiple suites on the same head doesn't
|
||||
-- get counted N times. Late-arriving suites for an OLD head are stored but
|
||||
-- excluded by the head_sha filter, so they can't override the new head's
|
||||
-- pending view. reference_only links (a PR that merely mentions the issue
|
||||
-- identifier in its body, with no closing keyword and no title/branch
|
||||
-- reference) are filtered out — they are not working PRs for this issue.
|
||||
-- Returns the issue's linked PRs with the GitHub API snapshot (MUL-5265): the
|
||||
-- mergeability verdict, the CI rollup, and per-check counts for the PR's
|
||||
-- CURRENT snapshot head SHA. Checks are aggregated from
|
||||
-- github_pull_request_check_run — the run-level snapshot written by the API
|
||||
-- refresh pipeline — NOT the legacy suite-level webhook aggregation, which is
|
||||
-- removed. The `issue_prs` CTE narrows to this issue's PR ids first so the
|
||||
-- aggregation only touches check rows for those PRs. Rows for an OLD head are
|
||||
-- excluded by the snapshot_head_sha filter. reference_only links (a PR that
|
||||
-- merely mentions the issue identifier in its body, with no closing keyword and
|
||||
-- no title/branch reference) are filtered out — they are not working PRs.
|
||||
WITH issue_prs AS (
|
||||
SELECT pr.id, pr.head_sha
|
||||
SELECT pr.id, pr.snapshot_head_sha
|
||||
FROM github_pull_request pr
|
||||
JOIN issue_pull_request ipr ON ipr.pull_request_id = pr.id
|
||||
WHERE ipr.issue_id = sqlc.arg('issue_id') AND NOT ipr.reference_only
|
||||
),
|
||||
per_app_latest AS (
|
||||
SELECT DISTINCT ON (cs.pr_id, cs.app_id)
|
||||
cs.pr_id, cs.app_id, cs.conclusion, cs.status
|
||||
FROM github_pull_request_check_suite cs
|
||||
JOIN issue_prs ip ON ip.id = cs.pr_id
|
||||
WHERE cs.head_sha = ip.head_sha AND ip.head_sha <> ''
|
||||
ORDER BY cs.pr_id, cs.app_id, cs.updated_at DESC
|
||||
),
|
||||
checks AS (
|
||||
SELECT
|
||||
pr_id,
|
||||
cr.pr_id,
|
||||
COUNT(*)::bigint AS total,
|
||||
SUM(CASE WHEN status = 'completed' AND conclusion IN
|
||||
('failure','cancelled','timed_out','action_required','startup_failure','stale')
|
||||
SUM(CASE WHEN cr.status = 'completed' AND cr.conclusion IN
|
||||
('failure','cancelled','timed_out','action_required','startup_failure','stale','error')
|
||||
THEN 1 ELSE 0 END)::bigint AS failed,
|
||||
SUM(CASE WHEN status = 'completed' AND conclusion IN
|
||||
SUM(CASE WHEN cr.status = 'completed' AND cr.conclusion IN
|
||||
('success','neutral','skipped')
|
||||
THEN 1 ELSE 0 END)::bigint AS passed,
|
||||
SUM(CASE WHEN status <> 'completed' OR conclusion IS NULL
|
||||
THEN 1 ELSE 0 END)::bigint AS pending
|
||||
FROM per_app_latest
|
||||
GROUP BY pr_id
|
||||
SUM(CASE WHEN cr.status <> 'completed' OR cr.conclusion IS NULL
|
||||
THEN 1 ELSE 0 END)::bigint AS running,
|
||||
COALESCE(
|
||||
array_agg(cr.name) FILTER (WHERE cr.status = 'completed' AND cr.conclusion IN
|
||||
('failure','cancelled','timed_out','action_required','startup_failure','stale','error')),
|
||||
'{}'
|
||||
)::text[] AS failed_names
|
||||
FROM github_pull_request_check_run cr
|
||||
JOIN issue_prs ip ON ip.id = cr.pr_id
|
||||
WHERE cr.head_sha = ip.snapshot_head_sha AND ip.snapshot_head_sha <> ''
|
||||
GROUP BY cr.pr_id
|
||||
)
|
||||
SELECT
|
||||
pr.id, pr.workspace_id, pr.installation_id, pr.repo_owner, pr.repo_name,
|
||||
@@ -176,11 +174,14 @@ SELECT
|
||||
pr.author_avatar_url, pr.merged_at, pr.closed_at, pr.pr_created_at,
|
||||
pr.pr_updated_at, pr.head_sha, pr.mergeable_state,
|
||||
pr.additions, pr.deletions, pr.changed_files,
|
||||
pr.api_mergeable, pr.api_merge_state_status, pr.checks_rollup_state,
|
||||
pr.snapshot_head_sha, pr.snapshot_fetched_at,
|
||||
pr.created_at, pr.updated_at,
|
||||
COALESCE(c.total, 0)::bigint AS checks_total,
|
||||
COALESCE(c.passed, 0)::bigint AS checks_passed,
|
||||
COALESCE(c.failed, 0)::bigint AS checks_failed,
|
||||
COALESCE(c.pending, 0)::bigint AS checks_pending
|
||||
COALESCE(c.running, 0)::bigint AS checks_running,
|
||||
COALESCE(c.failed_names, '{}')::text[] AS failed_check_names
|
||||
FROM github_pull_request pr
|
||||
JOIN issue_pull_request ipr ON ipr.pull_request_id = pr.id
|
||||
LEFT JOIN checks c ON c.pr_id = pr.id
|
||||
@@ -246,71 +247,6 @@ FROM github_pull_request pr
|
||||
JOIN issue_pull_request ipr ON ipr.pull_request_id = pr.id
|
||||
WHERE ipr.issue_id = $1 AND NOT ipr.reference_only;
|
||||
|
||||
-- =====================
|
||||
-- GitHub PR check suite
|
||||
-- =====================
|
||||
|
||||
-- name: UpsertPullRequestCheckSuite :exec
|
||||
-- Upserts a single check_suite row keyed by (pr_id, suite_id). The WHERE
|
||||
-- clause on the DO UPDATE branch prevents a late-arriving older event from
|
||||
-- overwriting a newer one — same-PR/same-suite ordering protection. Late
|
||||
-- events targeting an old head still land here (their head_sha is stored
|
||||
-- on the row); the head_sha filter in ListPullRequestsByIssue keeps them
|
||||
-- out of the current aggregate.
|
||||
INSERT INTO github_pull_request_check_suite (
|
||||
pr_id, suite_id, head_sha, app_id, conclusion, status, updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, sqlc.narg('conclusion'), $5, $6
|
||||
)
|
||||
ON CONFLICT (pr_id, suite_id) DO UPDATE SET
|
||||
head_sha = EXCLUDED.head_sha,
|
||||
app_id = EXCLUDED.app_id,
|
||||
conclusion = EXCLUDED.conclusion,
|
||||
status = EXCLUDED.status,
|
||||
updated_at = EXCLUDED.updated_at
|
||||
WHERE EXCLUDED.updated_at >= github_pull_request_check_suite.updated_at;
|
||||
|
||||
-- =====================
|
||||
-- GitHub pending check_suite (out-of-order arrival stash)
|
||||
-- =====================
|
||||
|
||||
-- name: UpsertPendingCheckSuite :exec
|
||||
-- Stashes a check_suite event whose PR row is not yet mirrored. Replayed
|
||||
-- (and deleted) by DrainPendingCheckSuitesForPR once the matching
|
||||
-- `pull_request` webhook lands. ON CONFLICT keeps the newest payload
|
||||
-- for the same (workspace, repo, pr_number, suite_id) — repeated
|
||||
-- deliveries while the PR is still missing are idempotent. The
|
||||
-- suite_updated_at guard mirrors UpsertPullRequestCheckSuite so an older
|
||||
-- event arriving after a newer one cannot overwrite the newer payload.
|
||||
INSERT INTO github_pending_check_suite (
|
||||
workspace_id, installation_id, repo_owner, repo_name, pr_number,
|
||||
suite_id, head_sha, app_id, conclusion, status, suite_updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5,
|
||||
$6, $7, $8, sqlc.narg('conclusion'), $9, $10
|
||||
)
|
||||
ON CONFLICT (workspace_id, repo_owner, repo_name, pr_number, suite_id) DO UPDATE SET
|
||||
installation_id = EXCLUDED.installation_id,
|
||||
head_sha = EXCLUDED.head_sha,
|
||||
app_id = EXCLUDED.app_id,
|
||||
conclusion = EXCLUDED.conclusion,
|
||||
status = EXCLUDED.status,
|
||||
suite_updated_at = EXCLUDED.suite_updated_at,
|
||||
received_at = now()
|
||||
WHERE EXCLUDED.suite_updated_at >= github_pending_check_suite.suite_updated_at;
|
||||
|
||||
-- name: DrainPendingCheckSuitesForPR :many
|
||||
-- Atomically reads + deletes all pending suites for the given PR address.
|
||||
-- Caller replays each row through UpsertPullRequestCheckSuite. RETURNING
|
||||
-- gives us the payloads we need without a separate SELECT, so two parallel
|
||||
-- handlers racing on the same PR can't double-apply the same row.
|
||||
DELETE FROM github_pending_check_suite
|
||||
WHERE workspace_id = $1
|
||||
AND repo_owner = $2
|
||||
AND repo_name = $3
|
||||
AND pr_number = $4
|
||||
RETURNING suite_id, head_sha, app_id, conclusion, status, suite_updated_at;
|
||||
|
||||
-- =====================
|
||||
-- Issue ↔ Pull Request link
|
||||
-- =====================
|
||||
|
||||
69
server/pkg/db/queries/github_snapshot.sql
Normal file
69
server/pkg/db/queries/github_snapshot.sql
Normal file
@@ -0,0 +1,69 @@
|
||||
-- =====================
|
||||
-- GitHub API snapshot (MUL-5265, Plan C)
|
||||
--
|
||||
-- These queries back the API-snapshot refresh pipeline. The GitHub GraphQL
|
||||
-- response is the single source of truth; each successful fetch is written as
|
||||
-- one atomic batch replace (guarded update of the PR row + full replace of the
|
||||
-- per-check rows) inside a single transaction.
|
||||
-- =====================
|
||||
|
||||
-- name: ListGitHubPRRowsByAddress :many
|
||||
-- One (installation, owner, repo, number) address can map to several
|
||||
-- github_pull_request rows — the same installation can be bound to multiple
|
||||
-- workspaces (#4823/#4855), each mirroring its own row. A single API fetch is
|
||||
-- applied to every matching row (each guarded by its own head_sha).
|
||||
SELECT id, workspace_id, head_sha, state
|
||||
FROM github_pull_request
|
||||
WHERE installation_id = $1 AND repo_owner = $2 AND repo_name = $3 AND pr_number = $4;
|
||||
|
||||
-- name: UpdateGitHubPRSnapshot :execrows
|
||||
-- Head-SHA anti-stale write (acceptance criterion 1): the snapshot is written
|
||||
-- only when the row's current head_sha still equals the head the snapshot was
|
||||
-- fetched for. If the head advanced (a newer push landed while this request was
|
||||
-- in flight, mirrored by the pull_request webhook), 0 rows are updated and the
|
||||
-- caller discards the whole response — the per-check replace is skipped too.
|
||||
UPDATE github_pull_request
|
||||
SET api_mergeable = sqlc.narg('api_mergeable'),
|
||||
api_merge_state_status = sqlc.narg('api_merge_state_status'),
|
||||
checks_rollup_state = sqlc.narg('checks_rollup_state'),
|
||||
snapshot_head_sha = sqlc.arg('head_sha'),
|
||||
snapshot_fetched_at = sqlc.arg('fetched_at'),
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg('pr_id') AND head_sha = sqlc.arg('head_sha');
|
||||
|
||||
-- name: DeleteGitHubPRCheckRuns :exec
|
||||
-- First half of the atomic per-check replace. Runs inside the same transaction
|
||||
-- as UpdateGitHubPRSnapshot and the inserts below.
|
||||
DELETE FROM github_pull_request_check_run WHERE pr_id = $1;
|
||||
|
||||
-- name: InsertGitHubPRCheckRun :exec
|
||||
INSERT INTO github_pull_request_check_run (
|
||||
pr_id, head_sha, ordinal, name, status, conclusion, details_url, is_status_context
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, sqlc.narg('conclusion'), sqlc.narg('details_url'), $6
|
||||
);
|
||||
|
||||
-- name: ListStaleOpenGitHubPRs :many
|
||||
-- TTL / safety-net sweep source. Returns distinct addresses of open/draft PRs
|
||||
-- whose snapshot is missing or older than the TTL cutoff. Bounded by LIMIT so
|
||||
-- one sweep can never fan out unbounded. Open PRs whose base branch advanced
|
||||
-- (a conflict-producing event that emits NO pull_request webhook on this PR)
|
||||
-- are recovered here without needing Contents:read. Merged/closed PRs are
|
||||
-- excluded — a settled PR leaves the refresh set.
|
||||
SELECT DISTINCT installation_id, repo_owner, repo_name, pr_number
|
||||
FROM github_pull_request
|
||||
WHERE state IN ('open', 'draft')
|
||||
AND (snapshot_fetched_at IS NULL OR snapshot_fetched_at < sqlc.arg('older_than'))
|
||||
ORDER BY installation_id, repo_owner, repo_name, pr_number
|
||||
LIMIT sqlc.arg('max_rows');
|
||||
|
||||
-- name: ListGitHubPRNumbersByHeadSHA :many
|
||||
-- Resolves a commit SHA to the PR numbers whose head it is. `status` webhook
|
||||
-- events (legacy commit statuses) carry a SHA + repo but no PR number, so we
|
||||
-- map back through the mirrored head_sha to find which PR(s) to refresh.
|
||||
SELECT DISTINCT pr_number
|
||||
FROM github_pull_request
|
||||
WHERE installation_id = $1 AND repo_owner = $2 AND repo_name = $3 AND head_sha = $4;
|
||||
|
||||
-- name: GetGitHubPullRequestByID :one
|
||||
SELECT * FROM github_pull_request WHERE id = $1;
|
||||
Reference in New Issue
Block a user