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
124 changes: 124 additions & 0 deletions apps/gittensory-ui/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,12 @@
"$ref": "#/components/schemas/RoleContext"
}
},
"opportunities": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ContributorOpportunity"
}
},
"repoDecisions": {
"type": "array",
"items": {
Expand Down Expand Up @@ -2053,6 +2059,7 @@
"profile",
"outcomeHistory",
"roleContexts",
"opportunities",
"repoDecisions",
"topActions",
"cleanupFirst",
Expand Down Expand Up @@ -5148,9 +5155,16 @@
"enum": [
"active",
"historical",
"completed",
"cancelled",
"stale",
"ambiguous",
"unknown"
]
},
"isActiveOpportunity": {
"type": "boolean"
},
"fundingStatus": {
"type": "string",
"enum": [
Expand All @@ -5167,6 +5181,34 @@
"high"
]
},
"linkedPrs": {
"type": "array",
"items": {
"type": "object",
"properties": {
"number": {
"type": "number"
},
"state": {
"type": "string",
"enum": [
"open",
"closed",
"merged",
"unknown"
]
},
"isActive": {
"type": "boolean"
}
},
"required": [
"number",
"state",
"isActive"
]
}
},
"findings": {
"type": "array",
"items": {
Expand All @@ -5180,11 +5222,66 @@
"issueNumber",
"status",
"lifecycle",
"isActiveOpportunity",
"fundingStatus",
"consensusRisk",
"linkedPrs",
"findings"
]
},
"BountyLifecycleEvents": {
"type": "object",
"properties": {
"bountyId": {
"type": "string"
},
"events": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"bountyId": {
"type": "string"
},
"repoFullName": {
"type": "string"
},
"issueNumber": {
"type": "number"
},
"status": {
"type": "string"
},
"payload": {
"type": "object",
"additionalProperties": {
"nullable": true
}
},
"generatedAt": {
"type": "string"
}
},
"required": [
"id",
"bountyId",
"repoFullName",
"issueNumber",
"status",
"payload",
"generatedAt"
]
}
}
},
"required": [
"bountyId",
"events"
]
},
"RepositorySettings": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -9087,6 +9184,33 @@
]
}
},
"/v1/bounties/{id}/lifecycle": {
"get": {
"responses": {
"200": {
"description": "Bounty lifecycle transition history",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/BountyLifecycleEvents"
}
}
}
},
"404": {
"description": "Bounty not found"
}
},
"security": [
{
"GittensoryBearer": []
},
{
"GittensorySessionCookie": []
}
]
}
},
"/v1/github/webhook": {
"post": {
"responses": {
Expand Down
51 changes: 41 additions & 10 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
listAllPullRequestDetailSyncStates,
listCheckSummaries,
listBounties,
listBountiesByRepo,
listBountyLifecycleEvents,
listContributorIssues,
listContributorPullRequests,
listContributorRepoStats,
Expand All @@ -64,6 +66,7 @@ import {
listRepositories,
getLatestUpstreamRulesetSnapshot,
listUpstreamDriftReports,
persistBountyLifecycleEvent,
persistScorePreview,
persistSignalSnapshot,
upsertDigestSubscription,
Expand Down Expand Up @@ -134,7 +137,7 @@ import { buildPullRequestReviewability } from "../signals/reward-risk";
import { buildLocalBranchAnalysis, findCurrentBranchPullRequest } from "../signals/local-branch";
import { buildRepoSettingsPreview } from "../signals/settings-preview";
import { fileUpstreamDriftIssues, loadUpstreamStatus, refreshUpstreamDrift } from "../upstream/ruleset";
import type { ContributorEvidenceRecord, DataQuality, InstallationHealthRecord, JobMessage, JsonValue, RegistrySnapshot, RepoSyncSegmentRecord, RepositoryRecord, ScoringModelSnapshotRecord } from "../types";
import type { BountyLifecycleEventRecord, ContributorEvidenceRecord, DataQuality, InstallationHealthRecord, JobMessage, JsonValue, RegistrySnapshot, RepoSyncSegmentRecord, RepositoryRecord, ScoringModelSnapshotRecord } from "../types";
import { errorMessage, nowIso } from "../utils/json";

type AppBindings = { Bindings: Env };
Expand Down Expand Up @@ -1173,26 +1176,28 @@ export function createApp() {
const body = await c.req.json().catch(() => null);
const parsed = preflightSchema.safeParse(body);
if (!parsed.success) return c.json({ error: "invalid_preflight_request", issues: parsed.error.issues }, 400);
const [repo, issues, pullRequests, issueQuality] = await Promise.all([
const [repo, issues, pullRequests, bounties, issueQuality] = await Promise.all([
getRepository(c.env, parsed.data.repoFullName),
listIssues(c.env, parsed.data.repoFullName),
listPullRequests(c.env, parsed.data.repoFullName),
listBountiesByRepo(c.env, parsed.data.repoFullName),
loadOrComputeIssueQualityResponse(c.env, parsed.data.repoFullName),
]);
return c.json(buildPreflightResult(parsed.data, repo, issues, pullRequests, issueQuality?.report));
return c.json(buildPreflightResult(parsed.data, repo, issues, pullRequests, bounties, issueQuality?.report));
});

app.post("/v1/preflight/local-diff", async (c) => {
const body = await c.req.json().catch(() => null);
const parsed = localDiffPreflightSchema.safeParse(body);
if (!parsed.success) return c.json({ error: "invalid_local_diff_preflight_request", issues: parsed.error.issues }, 400);
const [repo, issues, pullRequests, issueQuality] = await Promise.all([
const [repo, issues, pullRequests, bounties, issueQuality] = await Promise.all([
getRepository(c.env, parsed.data.repoFullName),
listIssues(c.env, parsed.data.repoFullName),
listPullRequests(c.env, parsed.data.repoFullName),
listBountiesByRepo(c.env, parsed.data.repoFullName),
loadOrComputeIssueQualityResponse(c.env, parsed.data.repoFullName),
]);
return c.json(buildLocalDiffPreflightResult(parsed.data, repo, issues, pullRequests, issueQuality?.report));
return c.json(buildLocalDiffPreflightResult(parsed.data, repo, issues, pullRequests, bounties, issueQuality?.report));
});

app.post("/v1/local/branch-analysis", async (c) => {
Expand All @@ -1201,12 +1206,13 @@ export function createApp() {
if (!parsed.success) return c.json({ error: "invalid_local_branch_analysis_request", issues: parsed.error.issues }, 400);
const unauthorized = await requireContributorAccess(c, parsed.data.login);
if (unauthorized) return unauthorized;
const [context, repo, issues, pullRequests, recentMergedPullRequests, snapshot, issueQuality] = await Promise.all([
const [context, repo, issues, pullRequests, recentMergedPullRequests, bounties, snapshot, issueQuality] = await Promise.all([
loadContributorFastContext(c.env, parsed.data.login),
getRepository(c.env, parsed.data.repoFullName),
listIssues(c.env, parsed.data.repoFullName),
listPullRequests(c.env, parsed.data.repoFullName),
listRecentMergedPullRequests(c.env, parsed.data.repoFullName),
listBountiesByRepo(c.env, parsed.data.repoFullName),
getOrCreateScoringModelSnapshot(c.env),
loadOrComputeIssueQualityResponse(c.env, parsed.data.repoFullName),
]);
Expand All @@ -1220,6 +1226,7 @@ export function createApp() {
pullRequests,
contributorPullRequests: context.contributorPullRequests,
recentMergedPullRequests,
bounties,
repositories: context.repositories,
checkSummaries,
profile: context.profile,
Expand Down Expand Up @@ -1308,11 +1315,19 @@ export function createApp() {
app.get("/v1/bounties/:id/advisory", async (c) => {
const bounty = await getBounty(c.env, c.req.param("id"));
if (!bounty) return c.json({ error: "bounty_not_found" }, 404);
const [repo, issue] = await Promise.all([
const [repo, issue, pullRequests] = await Promise.all([
getRepository(c.env, bounty.repoFullName),
getIssue(c.env, bounty.repoFullName, bounty.issueNumber),
listPullRequests(c.env, bounty.repoFullName),
]);
return c.json(buildBountyAdvisory(bounty, repo, issue));
return c.json(buildBountyAdvisory(bounty, repo, issue, pullRequests));
});

app.get("/v1/bounties/:id/lifecycle", async (c) => {
const id = c.req.param("id");
const bounty = await getBounty(c.env, id);
if (!bounty) return c.json({ error: "bounty_not_found" }, 404);
return c.json({ bountyId: id, events: await listBountyLifecycleEvents(c.env, id) });
});

app.post("/v1/github/webhook", handleGitHubWebhook);
Expand Down Expand Up @@ -1504,8 +1519,24 @@ export function createApp() {
app.post("/v1/internal/bounties/import", async (c) => {
const body = await c.req.json().catch(() => null);
const bounties = normalizeGittBountySnapshot(body);
await Promise.all(bounties.map((bounty) => upsertBounty(c.env, bounty)));
return c.json({ ok: true, imported: bounties.length });
const events: BountyLifecycleEventRecord[] = [];
for (const bounty of bounties) {
const existing = await getBounty(c.env, bounty.id);
await upsertBounty(c.env, bounty);
if (!existing || existing.status !== bounty.status) {
events.push({
id: crypto.randomUUID(),
bountyId: bounty.id,
repoFullName: bounty.repoFullName,
issueNumber: bounty.issueNumber,
status: bounty.status,
payload: { previousStatus: existing?.status ?? null, source: "gitt_import" },
generatedAt: nowIso(),
});
}
}
await Promise.all(events.map((event) => persistBountyLifecycleEvent(c.env, event)));
return c.json({ ok: true, imported: bounties.length, lifecycleEvents: events.length });
});

app.post("/v1/internal/repos/:owner/:repo/settings", async (c) => {
Expand Down
24 changes: 24 additions & 0 deletions src/db/repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,18 @@ export async function listBounties(env: Env): Promise<BountyRecord[]> {
return rows.map(toBountyRecord);
}

export async function listBountiesByRepo(env: Env, fullName: string): Promise<BountyRecord[]> {
const db = getDb(env.DB);
const rows = await db.select().from(bounties).where(eq(bounties.repoFullName, fullName)).orderBy(desc(bounties.updatedAt)).limit(500);
return rows.map(toBountyRecord);
}

export async function listBountyLifecycleEvents(env: Env, bountyId: string): Promise<BountyLifecycleEventRecord[]> {
const db = getDb(env.DB);
const rows = await db.select().from(bountyLifecycleEvents).where(eq(bountyLifecycleEvents.bountyId, bountyId)).orderBy(desc(bountyLifecycleEvents.generatedAt)).limit(100);
return rows.map(toBountyLifecycleEventRecord);
}

export async function getBounty(env: Env, id: string): Promise<BountyRecord | null> {
const db = getDb(env.DB);
const [row] = await db.select().from(bounties).where(eq(bounties.id, id)).limit(1);
Expand Down Expand Up @@ -2464,6 +2476,18 @@ function toBountyRecord(row: typeof bounties.$inferSelect): BountyRecord {
};
}

function toBountyLifecycleEventRecord(row: typeof bountyLifecycleEvents.$inferSelect): BountyLifecycleEventRecord {
return {
id: row.id,
bountyId: row.bountyId,
repoFullName: row.repoFullName,
issueNumber: row.issueNumber,
status: row.status,
payload: parseJson<Record<string, never>>(row.payloadJson, {}),
generatedAt: row.generatedAt,
};
}

function toCollisionEdgeRecord(row: typeof collisionEdges.$inferSelect): CollisionEdgeRecord {
return {
id: row.id,
Expand Down
Loading