Diffinery

homeflare/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

137 lines · 5.5 KBRaw History
1import { Hono } from "hono";
2import type { Env } from "../env.js";
3import type { AppContext, AppVars } from "../auth/principal.js";
4import { HttpError } from "../lib/util.js";
5import type { CommitStatus } from "../repo/statuses.js";
6import { loadRepo } from "./common.js";
7import { baseOf, ghCheckRun, ghRepo, ghUser, isoSeconds, nodeId, paginate, repoApi } from "./gh.js";
8import { jsonBody, loadTarget, repoParams } from "./ghctx.js";
9
10/** Commit statuses, single check runs and check suites (GitHub REST). */
11export const statuses = new Hono<{ Bindings: Env; Variables: AppVars }>();
12
13const R = "/repos/:owner/:repo";
14
15function statusJson(c: AppContext, s: CommitStatus) {
16 const b = baseOf(c);
17 const { owner, repo } = repoParams(c);
18 const creator = ghUser(b, s.creator);
19 return {
20 url: `${repoApi(b, owner, repo)}/statuses/${s.sha}`,
21 avatar_url: creator?.avatar_url ?? null,
22 id: s.id,
23 node_id: nodeId("StatusContext", `${owner}/${repo}/${s.id}`),
24 state: s.state,
25 description: s.description,
26 target_url: s.targetUrl,
27 context: s.context,
28 created_at: isoSeconds(s.createdAt),
29 updated_at: isoSeconds(s.updatedAt),
30 creator,
31 };
32}
33
34statuses.post(`${R}/statuses/:sha`, async (c) => {
35 const { stub, caller } = await loadTarget(c, "write");
36 const b = await jsonBody<{ state?: string; target_url?: string | null; description?: string | null; context?: string }>(c);
37 const s = await stub.createStatus(c.req.param("sha"), { state: b.state, target_url: b.target_url, description: b.description, context: b.context }, caller);
38 return c.json(statusJson(c, s), 201);
39});
40
41async function listStatuses(c: AppContext) {
42 const { stub } = await loadTarget(c);
43 const sha = await stub.resolve(c.req.param("ref"));
44 if (!sha) throw new HttpError(404, "No commit found for the ref");
45 return c.json(paginate(c, (await stub.commitStatuses(sha)).map((s) => statusJson(c, s))));
46}
47statuses.get(`${R}/commits/:ref{.+}/statuses`, listStatuses);
48statuses.get(`${R}/statuses/:ref{.+}`, listStatuses);
49
50statuses.get(`${R}/commits/:ref{.+}/status`, async (c) => {
51 const { stub, repo } = await loadTarget(c);
52 const sha = await stub.resolve(c.req.param("ref"));
53 if (!sha) throw new HttpError(404, "No commit found for the ref");
54 const combined = await stub.combinedStatus(sha);
55 const api = repoApi(baseOf(c), repo.owner_login, repo.name);
56 return c.json({
57 state: combined.state,
58 statuses: paginate(c, combined.statuses.map((s) => statusJson(c, s)), 100),
59 sha,
60 total_count: combined.statuses.length,
61 repository: ghRepo(baseOf(c), repo),
62 commit_url: `${api}/commits/${sha}`,
63 url: `${api}/commits/${sha}/status`,
64 });
65});
66
67// ---------------------------------------------------------------- check runs by id
68
69statuses.get(`${R}/check-runs/:id`, async (c) => {
70 const { stub, repo } = await loadTarget(c);
71 return c.json(ghCheckRun(baseOf(c), repo.owner_login, repo.name, await stub.checkRun(c.req.param("id"))));
72});
73
74statuses.patch(`${R}/check-runs/:id`, async (c) => {
75 const { stub, caller, repo } = await loadTarget(c, "write");
76 const b = await jsonBody<Parameters<typeof stub.updateCheckRun>[1]>(c);
77 return c.json(ghCheckRun(baseOf(c), repo.owner_login, repo.name, await stub.updateCheckRun(c.req.param("id"), b, caller)));
78});
79
80statuses.get(`${R}/check-runs/:id/annotations`, async (c) => {
81 const { stub } = await loadTarget(c);
82 const check = await stub.checkRun(c.req.param("id"));
83 const list = ((check.output as { annotations?: Array<Record<string, unknown>> } | null)?.annotations ?? []).map((a) => ({
84 path: a.path ?? null,
85 start_line: a.start_line ?? null,
86 end_line: a.end_line ?? a.start_line ?? null,
87 start_column: a.start_column ?? null,
88 end_column: a.end_column ?? null,
89 annotation_level: a.annotation_level ?? "notice",
90 title: a.title ?? null,
91 message: a.message ?? "",
92 raw_details: a.raw_details ?? null,
93 blob_href: null,
94 }));
95 return c.json(paginate(c, list));
96});
97
98// ---------------------------------------------------------------- check suites (one per workflow run, or per app)
99
100type Suite = Awaited<ReturnType<Awaited<ReturnType<typeof loadRepo>>["stub"]["checkSuite"]>>;
101
102function suiteJson(c: AppContext, s: Suite) {
103 const b = baseOf(c);
104 const { owner, repo } = repoParams(c);
105 return {
106 id: s.id,
107 node_id: nodeId("CheckSuite", s.id),
108 head_branch: null,
109 head_sha: s.headSha,
110 status: s.status,
111 conclusion: s.conclusion,
112 url: `${repoApi(b, owner, repo)}/check-suites/${s.id}`,
113 app: { id: s.app, slug: s.app, name: s.app },
114 pull_requests: [],
115 latest_check_runs_count: s.runs.length,
116 check_runs_url: `${repoApi(b, owner, repo)}/check-suites/${s.id}/check-runs`,
117 };
118}
119
120statuses.get(`${R}/commits/:ref{.+}/check-suites`, async (c) => {
121 const { stub } = await loadTarget(c);
122 const sha = await stub.resolve(c.req.param("ref"));
123 if (!sha) throw new HttpError(404, "ref not found");
124 const suites = await stub.checkSuites(sha);
125 return c.json({ total_count: suites.length, check_suites: paginate(c, suites).map((s) => suiteJson(c, s)) });
126});
127
128statuses.get(`${R}/check-suites/:id`, async (c) => {
129 const { stub } = await loadTarget(c);
130 return c.json(suiteJson(c, await stub.checkSuite(c.req.param("id"))));
131});
132
133statuses.get(`${R}/check-suites/:id/check-runs`, async (c) => {
134 const { stub, repo } = await loadTarget(c);
135 const suite = await stub.checkSuite(c.req.param("id"));
136 return c.json({ total_count: suite.runs.length, check_runs: paginate(c, suite.runs).map((r) => ghCheckRun(baseOf(c), repo.owner_login, repo.name, r)) });
137});