@intentius/chant 0.124.76 → 0.124.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/dist/__fixtures__/agent-guard/plywood.d.ts +8 -0
  2. package/dist/__fixtures__/agent-guard/plywood.d.ts.map +1 -0
  3. package/dist/__fixtures__/agent-guard/plywood.js +19 -0
  4. package/dist/__fixtures__/agent-guard/terragucci.d.ts +8 -0
  5. package/dist/__fixtures__/agent-guard/terragucci.d.ts.map +1 -0
  6. package/dist/__fixtures__/agent-guard/terragucci.js +30 -0
  7. package/dist/__fixtures__/gated-ledger.d.ts +10 -0
  8. package/dist/__fixtures__/gated-ledger.d.ts.map +1 -0
  9. package/dist/__fixtures__/gated-ledger.js +51 -0
  10. package/dist/__fixtures__/notify-messages/events.d.ts +9 -0
  11. package/dist/__fixtures__/notify-messages/events.d.ts.map +1 -0
  12. package/dist/__fixtures__/notify-messages/events.js +21 -0
  13. package/dist/__fixtures__/product-words.d.ts +14 -0
  14. package/dist/__fixtures__/product-words.d.ts.map +1 -1
  15. package/dist/__fixtures__/product-words.js +11 -0
  16. package/dist/__fixtures__/terragucci-notify.d.ts +66 -0
  17. package/dist/__fixtures__/terragucci-notify.d.ts.map +1 -0
  18. package/dist/__fixtures__/terragucci-notify.js +133 -0
  19. package/dist/action-pins.d.ts +8 -0
  20. package/dist/action-pins.d.ts.map +1 -1
  21. package/dist/action-pins.js +9 -0
  22. package/dist/agent-guard.d.ts +143 -0
  23. package/dist/agent-guard.d.ts.map +1 -0
  24. package/dist/agent-guard.js +455 -0
  25. package/dist/ci-commit.d.ts +120 -5
  26. package/dist/ci-commit.d.ts.map +1 -1
  27. package/dist/ci-commit.js +214 -23
  28. package/dist/ci-own-jobs.d.ts +81 -0
  29. package/dist/ci-own-jobs.d.ts.map +1 -0
  30. package/dist/ci-own-jobs.js +233 -0
  31. package/dist/ci-product-words.d.ts +18 -4
  32. package/dist/ci-product-words.d.ts.map +1 -1
  33. package/dist/ci-product-words.js +26 -6
  34. package/dist/ci-product.d.ts +40 -1
  35. package/dist/ci-product.d.ts.map +1 -1
  36. package/dist/ci-product.js +40 -1
  37. package/dist/cli/handlers/approve.d.ts +5 -1
  38. package/dist/cli/handlers/approve.d.ts.map +1 -1
  39. package/dist/cli/handlers/approve.js +17 -11
  40. package/dist/cli/handlers/build.d.ts +1 -1
  41. package/dist/cli/handlers/build.d.ts.map +1 -1
  42. package/dist/cli/handlers/build.js +88 -2
  43. package/dist/cli/handlers/commit.d.ts.map +1 -1
  44. package/dist/cli/handlers/commit.js +3 -1
  45. package/dist/cli/handlers/notify-relay.d.ts +18 -1
  46. package/dist/cli/handlers/notify-relay.d.ts.map +1 -1
  47. package/dist/cli/handlers/notify-relay.js +90 -14
  48. package/dist/cli/handlers/pr.d.ts.map +1 -1
  49. package/dist/cli/handlers/pr.js +3 -1
  50. package/dist/cli/handlers/release.d.ts +5 -1
  51. package/dist/cli/handlers/release.d.ts.map +1 -1
  52. package/dist/cli/handlers/release.js +21 -9
  53. package/dist/cli/handlers/run-generate.d.ts.map +1 -1
  54. package/dist/cli/handlers/run-generate.js +2 -1
  55. package/dist/cli/main.d.ts.map +1 -1
  56. package/dist/cli/main.js +21 -1
  57. package/dist/cli/parse-args.d.ts.map +1 -1
  58. package/dist/cli/parse-args.js +60 -4
  59. package/dist/cli/registry.d.ts +16 -0
  60. package/dist/cli/registry.d.ts.map +1 -1
  61. package/dist/components/pr-env-components.d.ts +65 -0
  62. package/dist/components/pr-env-components.d.ts.map +1 -0
  63. package/dist/components/pr-env-components.js +162 -0
  64. package/dist/components/pr-policy.d.ts +15 -0
  65. package/dist/components/pr-policy.d.ts.map +1 -1
  66. package/dist/components/pr-policy.js +24 -1
  67. package/dist/components/release-compile.d.ts +15 -3
  68. package/dist/components/release-compile.d.ts.map +1 -1
  69. package/dist/components/release-compile.js +66 -15
  70. package/dist/config.d.ts +14 -8
  71. package/dist/config.d.ts.map +1 -1
  72. package/dist/config.js +16 -1
  73. package/dist/gate-policy.d.ts +14 -1
  74. package/dist/gate-policy.d.ts.map +1 -1
  75. package/dist/gate-policy.js +16 -6
  76. package/dist/lexicon.d.ts +25 -1
  77. package/dist/lexicon.d.ts.map +1 -1
  78. package/dist/lexicon.js +10 -0
  79. package/dist/lifecycle/gated-op.d.ts +95 -0
  80. package/dist/lifecycle/gated-op.d.ts.map +1 -0
  81. package/dist/lifecycle/gated-op.js +96 -0
  82. package/dist/lifecycle/git.d.ts.map +1 -1
  83. package/dist/lifecycle/git.js +83 -2
  84. package/dist/lifecycle/release-images.d.ts +77 -3
  85. package/dist/lifecycle/release-images.d.ts.map +1 -1
  86. package/dist/lifecycle/release-images.js +132 -6
  87. package/dist/notify-product.d.ts +11 -2
  88. package/dist/notify-product.d.ts.map +1 -1
  89. package/dist/notify-product.js +16 -4
  90. package/dist/notify-relay-repo.d.ts +131 -0
  91. package/dist/notify-relay-repo.d.ts.map +1 -0
  92. package/dist/notify-relay-repo.js +202 -0
  93. package/dist/notify-relay.d.ts +25 -1
  94. package/dist/notify-relay.d.ts.map +1 -1
  95. package/dist/notify-relay.js +27 -2
  96. package/dist/notify.d.ts +36 -4
  97. package/dist/notify.d.ts.map +1 -1
  98. package/dist/notify.js +31 -21
  99. package/dist/notify.schema.json +1 -0
  100. package/dist/op/activities/review-product.d.ts +111 -0
  101. package/dist/op/activities/review-product.d.ts.map +1 -0
  102. package/dist/op/activities/review-product.js +116 -0
  103. package/dist/op/op-waves-capability.js +2 -2
  104. package/dist/policy-engine.d.ts +95 -0
  105. package/dist/policy-engine.d.ts.map +1 -0
  106. package/dist/policy-engine.js +157 -0
  107. package/dist/pr-forge-reads.d.ts +124 -0
  108. package/dist/pr-forge-reads.d.ts.map +1 -0
  109. package/dist/pr-forge-reads.js +322 -0
  110. package/dist/pr-forge.d.ts +14 -0
  111. package/dist/pr-forge.d.ts.map +1 -1
  112. package/dist/pr-forge.js +21 -13
  113. package/dist/pr-plan-note.d.ts +293 -0
  114. package/dist/pr-plan-note.d.ts.map +1 -0
  115. package/dist/pr-plan-note.js +362 -0
  116. package/dist/release-floors.d.ts +9 -1
  117. package/dist/release-floors.d.ts.map +1 -1
  118. package/dist/release-floors.js +9 -1
  119. package/dist/state-export.d.ts +126 -0
  120. package/dist/state-export.d.ts.map +1 -0
  121. package/dist/state-export.js +156 -0
  122. package/dist/state-migration.d.ts +148 -0
  123. package/dist/state-migration.d.ts.map +1 -0
  124. package/dist/state-migration.js +146 -0
  125. package/dist/state-unlock.d.ts +167 -0
  126. package/dist/state-unlock.d.ts.map +1 -0
  127. package/dist/state-unlock.js +196 -0
  128. package/dist/wave-runtime.mjs +332 -332
  129. package/dist/workspace/declaration.schema.json +1 -1
  130. package/dist/workspace/release-workflow.d.ts +10 -0
  131. package/dist/workspace/release-workflow.d.ts.map +1 -1
  132. package/dist/workspace/release-workflow.js +10 -4
  133. package/package.json +1 -1
  134. package/src/__fixtures__/agent-guard/plywood.ts +27 -0
  135. package/src/__fixtures__/agent-guard/terragucci.ts +39 -0
  136. package/src/__fixtures__/gated-ledger.ts +58 -0
  137. package/src/__fixtures__/notify-messages/events.ts +23 -0
  138. package/src/__fixtures__/notify-messages/messages.json +50 -0
  139. package/src/__fixtures__/pr-loop/terragucci-plan.note.golden.md +53 -0
  140. package/src/__fixtures__/pr-loop/terragucci-plan.note.json +83 -0
  141. package/src/__fixtures__/product-words.ts +12 -0
  142. package/src/__fixtures__/review-agent/terragucci-review.json +94 -0
  143. package/src/__fixtures__/terragucci-notify.ts +170 -0
  144. package/src/action-pins.test.ts +12 -1
  145. package/src/action-pins.ts +11 -0
  146. package/src/agent-guard.test.ts +319 -0
  147. package/src/agent-guard.ts +547 -0
  148. package/src/ci-commit.test.ts +213 -2
  149. package/src/ci-commit.ts +258 -27
  150. package/src/ci-own-jobs.test.ts +108 -0
  151. package/src/ci-own-jobs.ts +252 -0
  152. package/src/ci-product-words.ts +35 -7
  153. package/src/ci-product.ts +81 -1
  154. package/src/cli/handlers/approve-cwd.test.ts +112 -0
  155. package/src/cli/handlers/approve.ts +22 -11
  156. package/src/cli/handlers/build-own-jobs.test.ts +80 -0
  157. package/src/cli/handlers/build-pr-loop-control.test.ts +87 -0
  158. package/src/cli/handlers/build-run-group.test.ts +59 -0
  159. package/src/cli/handlers/build.ts +83 -3
  160. package/src/cli/handlers/commit.ts +3 -0
  161. package/src/cli/handlers/notify-relay.ts +99 -17
  162. package/src/cli/handlers/pr.ts +3 -1
  163. package/src/cli/handlers/release.ts +20 -10
  164. package/src/cli/handlers/run-generate.ts +2 -1
  165. package/src/cli/main.ts +21 -1
  166. package/src/cli/parse-args.ts +45 -3
  167. package/src/cli/registry.ts +16 -0
  168. package/src/components/pr-env-components.test.ts +137 -0
  169. package/src/components/pr-env-components.ts +213 -0
  170. package/src/components/pr-policy.test.ts +41 -1
  171. package/src/components/pr-policy.ts +25 -1
  172. package/src/components/release-compile.test.ts +47 -0
  173. package/src/components/release-compile.ts +70 -13
  174. package/src/config.ts +23 -3
  175. package/src/gate-policy-bundle.test.ts +14 -0
  176. package/src/gate-policy.test.ts +7 -0
  177. package/src/gate-policy.ts +22 -7
  178. package/src/lexicon.ts +28 -1
  179. package/src/lifecycle/gated-op.ts +158 -0
  180. package/src/lifecycle/git.test.ts +92 -0
  181. package/src/lifecycle/git.ts +71 -2
  182. package/src/lifecycle/release-images-dockerfile.test.ts +221 -0
  183. package/src/lifecycle/release-images.ts +167 -8
  184. package/src/notify-message-words.test.ts +186 -0
  185. package/src/notify-product.ts +17 -4
  186. package/src/notify-relay-repo.test.ts +284 -0
  187. package/src/notify-relay-repo.ts +234 -0
  188. package/src/notify-relay.test.ts +12 -0
  189. package/src/notify-relay.ts +29 -3
  190. package/src/notify.schema.json +1 -0
  191. package/src/notify.ts +59 -19
  192. package/src/op/activities/review-product.test.ts +116 -0
  193. package/src/op/activities/review-product.ts +200 -0
  194. package/src/op/op-waves-capability.ts +2 -2
  195. package/src/op/schedule-overdue.test.ts +26 -0
  196. package/src/policy-engine.test.ts +162 -0
  197. package/src/policy-engine.ts +200 -0
  198. package/src/pr-forge-reads.test.ts +285 -0
  199. package/src/pr-forge-reads.ts +424 -0
  200. package/src/pr-forge.ts +30 -16
  201. package/src/pr-plan-note.test.ts +105 -0
  202. package/src/pr-plan-note.ts +521 -0
  203. package/src/release-floors.ts +13 -1
  204. package/src/state-export.test.ts +124 -0
  205. package/src/state-export.ts +227 -0
  206. package/src/state-migration.test.ts +133 -0
  207. package/src/state-migration.ts +236 -0
  208. package/src/state-unlock.test.ts +165 -0
  209. package/src/state-unlock.ts +293 -0
  210. package/src/workspace/declaration.schema.json +1 -1
  211. package/src/workspace/release-workflow.test.ts +34 -0
  212. package/src/workspace/release-workflow.ts +19 -4
@@ -0,0 +1,285 @@
1
+ /**
2
+ * The forge reads beside the clients (#4809), driven through a recording
3
+ * `fetch`: the calls each makes on GitHub, Forgejo and GitLab, and what each
4
+ * reads back.
5
+ */
6
+
7
+ import { describe, expect, test } from "vitest";
8
+ import { ForgeApiError, type ForgeFetch } from "./pr-forge";
9
+ import { forgejoReads, forgeReadsFor, githubReads, gitlabReads } from "./pr-forge-reads";
10
+
11
+ interface Call {
12
+ method: string;
13
+ url: string;
14
+ headers: Record<string, string>;
15
+ }
16
+
17
+ /** A fake forge: `routes` maps `METHOD path-without-query` (optionally `?page=N`) to a response body, or a status. */
18
+ function fakeFetch(routes: Record<string, unknown>): { fetch: ForgeFetch; calls: Call[] } {
19
+ const calls: Call[] = [];
20
+ const fetch: ForgeFetch = async (url, init) => {
21
+ calls.push({ method: init.method, url, headers: init.headers });
22
+ const u = new URL(url);
23
+ const key = `${init.method} ${u.pathname}`;
24
+ const page = Number(u.searchParams.get("page") ?? "1");
25
+ const found = routes[`${key}?page=${page}`] ?? routes[key];
26
+ if (found === undefined) return { ok: false, status: 404, text: async () => '{"message":"Not Found"}' };
27
+ if (typeof found === "number") return { ok: false, status: found, text: async () => "" };
28
+ return { ok: true, status: 200, text: async () => JSON.stringify(found) };
29
+ };
30
+ return { fetch, calls };
31
+ }
32
+
33
+ const SHA = "a".repeat(40);
34
+ const MERGED = "m".repeat(40);
35
+ const urls = (calls: Call[]) => calls.map((c) => `${c.method} ${c.url}`);
36
+
37
+ describe("github", () => {
38
+ const opts = { apiBase: "https://api.github.com", repo: "acme/infra", token: "t0k" };
39
+
40
+ test("names the token's user", async () => {
41
+ const { fetch, calls } = fakeFetch({ "GET /user": { login: "tg-bot", id: 7 } });
42
+ expect(await githubReads({ ...opts, fetch }).tokenUser()).toEqual({ login: "tg-bot", id: 7 });
43
+ expect(urls(calls)).toEqual(["GET https://api.github.com/user"]);
44
+ expect(calls[0]!.headers.Authorization).toBe("Bearer t0k");
45
+ });
46
+
47
+ test("reads a merged pull request's author, URL and merge commit, and undefined for a missing one", async () => {
48
+ const { fetch } = fakeFetch({
49
+ "GET /repos/acme/infra/pulls/12": {
50
+ number: 12,
51
+ title: "t",
52
+ body: "b",
53
+ state: "closed",
54
+ merged: true,
55
+ merge_commit_sha: MERGED,
56
+ html_url: "https://github.com/acme/infra/pull/12",
57
+ user: { login: "alice" },
58
+ head: { ref: "feat", sha: SHA, repo: { full_name: "acme/infra" } },
59
+ base: { ref: "main" },
60
+ updated_at: "2026-11-08T00:00:00Z",
61
+ },
62
+ });
63
+ const reads = githubReads({ ...opts, fetch });
64
+ expect(await reads.pullRequest(12)).toEqual({
65
+ number: 12,
66
+ title: "t",
67
+ body: "b",
68
+ state: "merged",
69
+ head: "feat",
70
+ headSha: SHA,
71
+ base: "main",
72
+ author: "alice",
73
+ url: "https://github.com/acme/infra/pull/12",
74
+ sameRepo: true,
75
+ mergeCommit: MERGED,
76
+ updated: "2026-11-08T00:00:00Z",
77
+ });
78
+ expect(await reads.pullRequest(13)).toBeUndefined();
79
+ });
80
+
81
+ test("finds the pull requests from a branch in any state, newest first, and filters the head server-side", async () => {
82
+ const { fetch, calls } = fakeFetch({
83
+ "GET /repos/acme/infra/pulls": [
84
+ { number: 3, state: "closed", head: { ref: "wave/1" } },
85
+ { number: 9, state: "open", head: { ref: "wave/1" } },
86
+ { number: 5, state: "open", head: { ref: "wave/1", repo: { full_name: "fork/infra" } } },
87
+ ],
88
+ });
89
+ const got = await githubReads({ ...opts, fetch }).pullRequestsFrom("wave/1");
90
+ expect(got.map((p) => [p.number, p.state])).toEqual([
91
+ [9, "open"],
92
+ [3, "closed"],
93
+ ]);
94
+ expect(urls(calls)).toEqual(["GET https://api.github.com/repos/acme/infra/pulls?state=all&head=acme%3Awave%2F1&per_page=100&page=1"]);
95
+ });
96
+
97
+ test("lists the pull requests whose head starts with a prefix, newest update first, up to the limit", async () => {
98
+ const page1 = Array.from({ length: 100 }, (_, i) => ({ number: 1000 - i, state: "closed", head: { ref: i % 2 ? "other" : `rollout/${i}` } }));
99
+ const { fetch, calls } = fakeFetch({
100
+ "GET /repos/acme/infra/pulls?page=1": page1,
101
+ "GET /repos/acme/infra/pulls?page=2": [{ number: 1, state: "open", head: { ref: "rollout/late" } }],
102
+ });
103
+ const got = await githubReads({ ...opts, fetch }).pullRequestsFrom("rollout/", { prefix: true, limit: 100 });
104
+ expect(got).toHaveLength(50);
105
+ expect(got[0]!.head).toBe("rollout/0");
106
+ expect(urls(calls)).toEqual(["GET https://api.github.com/repos/acme/infra/pulls?state=all&sort=updated&direction=desc&per_page=100&page=1"]);
107
+ });
108
+
109
+ test("reads every comment with its author, across pages", async () => {
110
+ const page1 = Array.from({ length: 100 }, (_, i) => ({ id: i + 1, body: `c${i}`, user: { login: "bob", id: 2 } }));
111
+ const { fetch } = fakeFetch({
112
+ "GET /repos/acme/infra/issues/12/comments?page=1": page1,
113
+ "GET /repos/acme/infra/issues/12/comments?page=2": [{ id: 500, body: "marker", user: { login: "tg-bot", id: 7 }, created_at: "2026-11-08T00:00:00Z" }],
114
+ });
115
+ const notes = await githubReads({ ...opts, fetch }).pullRequestNotes(12);
116
+ expect(notes).toHaveLength(101);
117
+ expect(notes.at(-1)).toEqual({ id: 500, body: "marker", author: "tg-bot", authorId: 7, system: false, created: "2026-11-08T00:00:00Z" });
118
+ });
119
+
120
+ test("reads the newest check run per name", async () => {
121
+ const { fetch, calls } = fakeFetch({
122
+ [`GET /repos/acme/infra/commits/${SHA}/check-runs`]: {
123
+ total_count: 3,
124
+ check_runs: [
125
+ { id: 1, name: "apply/net", status: "completed", conclusion: "failure" },
126
+ { id: 4, name: "apply/net", status: "completed", conclusion: "success" },
127
+ { id: 2, name: "lint", status: "in_progress", conclusion: null },
128
+ ],
129
+ },
130
+ });
131
+ expect(await githubReads({ ...opts, fetch }).checkRuns(SHA)).toEqual([
132
+ { id: 4, name: "apply/net", status: "completed", conclusion: "success" },
133
+ { id: 2, name: "lint", status: "in_progress" },
134
+ ]);
135
+ expect(urls(calls)).toEqual([`GET https://api.github.com/repos/acme/infra/commits/${SHA}/check-runs?per_page=100&page=1`]);
136
+ });
137
+
138
+ test("reads a pull request's commits with their authors and committers", async () => {
139
+ const { fetch } = fakeFetch({
140
+ "GET /repos/acme/infra/pulls/12/commits": [
141
+ {
142
+ sha: SHA,
143
+ commit: { message: "fix\n\nRequested-by: carol", author: { name: "Al", email: "al@x.io" }, committer: { name: "GitHub", email: "noreply@github.com" } },
144
+ author: { login: "alice" },
145
+ committer: { login: "web-flow" },
146
+ },
147
+ ],
148
+ });
149
+ expect(await githubReads({ ...opts, fetch }).pullRequestCommits(12)).toEqual([
150
+ { sha: SHA, message: "fix\n\nRequested-by: carol", author: { name: "Al", email: "al@x.io", login: "alice" }, committer: { name: "GitHub", email: "noreply@github.com", login: "web-flow" } },
151
+ ]);
152
+ });
153
+
154
+ test("names a collaborator's permission level, and undefined for a stranger", async () => {
155
+ const { fetch } = fakeFetch({ "GET /repos/acme/infra/collaborators/alice/permission": { permission: "triage" } });
156
+ const reads = githubReads({ ...opts, fetch });
157
+ expect(await reads.permission("alice")).toBe("triage");
158
+ expect(await reads.permission("mallory")).toBeUndefined();
159
+ });
160
+
161
+ test("lists every open pull request, into any base", async () => {
162
+ const { fetch, calls } = fakeFetch({
163
+ "GET /repos/acme/infra/pulls": [
164
+ { number: 8, user: { login: "bob" }, head: { ref: "b", sha: SHA, repo: { full_name: "bob/infra" } }, base: { ref: "release", sha: MERGED } },
165
+ { number: 4, user: { login: "al" }, head: { ref: "a", sha: SHA }, base: { ref: "main" } },
166
+ ],
167
+ });
168
+ expect(await githubReads({ ...opts, fetch }).openPullRequests()).toEqual([
169
+ { number: 4, head: "a", headSha: SHA, base: "main", author: "al", fork: false },
170
+ { number: 8, head: "b", headSha: SHA, base: "release", baseSha: MERGED, author: "bob", fork: true },
171
+ ]);
172
+ expect(urls(calls)).toEqual(["GET https://api.github.com/repos/acme/infra/pulls?state=open&per_page=100&page=1"]);
173
+ });
174
+
175
+ test("a forge error other than 404 throws a ForgeApiError", async () => {
176
+ const { fetch } = fakeFetch({ "GET /repos/acme/infra/pulls/12": 500 });
177
+ await expect(githubReads({ ...opts, fetch }).pullRequest(12)).rejects.toBeInstanceOf(ForgeApiError);
178
+ });
179
+ });
180
+
181
+ describe("forgejo", () => {
182
+ const opts = { apiBase: "https://code.acme.io/api/v1", repo: "acme/infra", token: "t0k", host: "code.acme.io" };
183
+
184
+ test("matches a branch's pull requests over pages, with token auth", async () => {
185
+ const page1 = Array.from({ length: 50 }, (_, i) => ({ number: 100 + i, state: "closed", head: { ref: `x${i}` } }));
186
+ const { fetch, calls } = fakeFetch({
187
+ "GET /api/v1/repos/acme/infra/pulls?page=1": page1,
188
+ "GET /api/v1/repos/acme/infra/pulls?page=2": [{ number: 7, state: "closed", merged: true, merge_commit_sha: MERGED, head: { ref: "wave/1" } }],
189
+ });
190
+ const got = await forgejoReads({ ...opts, fetch }).pullRequestsFrom("wave/1");
191
+ expect(got.map((p) => [p.number, p.state, p.mergeCommit])).toEqual([[7, "merged", MERGED]]);
192
+ expect(urls(calls)).toEqual([
193
+ "GET https://code.acme.io/api/v1/repos/acme/infra/pulls?state=all&sort=recentupdate&limit=50&page=1",
194
+ "GET https://code.acme.io/api/v1/repos/acme/infra/pulls?state=all&sort=recentupdate&limit=50&page=2",
195
+ ]);
196
+ expect(calls[0]!.headers.Authorization).toBe("token t0k");
197
+ });
198
+
199
+ test("an empty repository's 404 list is no pull request", async () => {
200
+ const { fetch } = fakeFetch({ "GET /api/v1/repos/acme/infra": { empty: true } });
201
+ expect(await forgejoReads({ ...opts, fetch }).pullRequestsFrom("wave/", { prefix: true })).toEqual([]);
202
+ });
203
+
204
+ test("has no check runs", async () => {
205
+ const { fetch, calls } = fakeFetch({});
206
+ expect(await forgejoReads({ ...opts, fetch }).checkRuns(SHA)).toEqual([]);
207
+ expect(calls).toEqual([]);
208
+ });
209
+ });
210
+
211
+ describe("gitlab", () => {
212
+ const opts = { apiBase: "https://gitlab.com/api/v4", project: "acme/infra", token: "glpat" };
213
+ const P = "/api/v4/projects/acme%2Finfra";
214
+
215
+ test("names the token's user by username and id", async () => {
216
+ const { fetch, calls } = fakeFetch({ "GET /api/v4/user": { username: "project_1_bot", id: 41 } });
217
+ expect(await gitlabReads({ ...opts, fetch }).tokenUser()).toEqual({ login: "project_1_bot", id: 41 });
218
+ expect(calls[0]!.headers["PRIVATE-TOKEN"]).toBe("glpat");
219
+ });
220
+
221
+ test("reads a merge request, its merge commit falling back to squash and then head", async () => {
222
+ const mr = { iid: 5, title: "t", description: "d", state: "merged", sha: SHA, source_branch: "feat", target_branch: "main", web_url: "https://gitlab.com/acme/infra/-/merge_requests/5", author: { username: "alice" }, source_project_id: 1, target_project_id: 1 };
223
+ const { fetch } = fakeFetch({ [`GET ${P}/merge_requests/5`]: { ...mr, merge_commit_sha: null, squash_commit_sha: null } });
224
+ expect(await gitlabReads({ ...opts, fetch }).pullRequest(5)).toMatchObject({ number: 5, state: "merged", author: "alice", url: mr.web_url, mergeCommit: SHA, sameRepo: true });
225
+ const squashed = fakeFetch({ [`GET ${P}/merge_requests/5`]: { ...mr, squash_commit_sha: MERGED } });
226
+ expect((await gitlabReads({ ...opts, fetch: squashed.fetch }).pullRequest(5))?.mergeCommit).toBe(MERGED);
227
+ });
228
+
229
+ test("finds the merge requests from a branch in any state, a fork's left out", async () => {
230
+ const { fetch, calls } = fakeFetch({
231
+ [`GET ${P}/merge_requests`]: [
232
+ { iid: 2, state: "opened", source_branch: "wave/1", source_project_id: 1, target_project_id: 1 },
233
+ { iid: 3, state: "opened", source_branch: "wave/1", source_project_id: 9, target_project_id: 1 },
234
+ { iid: 1, state: "closed", source_branch: "wave/1" },
235
+ ],
236
+ });
237
+ expect((await gitlabReads({ ...opts, fetch }).pullRequestsFrom("wave/1")).map((m) => [m.number, m.state])).toEqual([
238
+ [2, "open"],
239
+ [1, "closed"],
240
+ ]);
241
+ expect(urls(calls)).toEqual([`GET https://gitlab.com${P}/merge_requests?state=all&source_branch=wave%2F1&order_by=updated_at&sort=desc&per_page=100&page=1`]);
242
+ });
243
+
244
+ test("reads every note, system notes marked", async () => {
245
+ const { fetch, calls } = fakeFetch({
246
+ [`GET ${P}/merge_requests/5/notes`]: [
247
+ { id: 1, body: "approved this merge request", system: true, author: { username: "project_1_bot", id: 41 } },
248
+ { id: 2, body: "<!-- marker -->", system: false, author: { username: "project_1_bot", id: 41 } },
249
+ ],
250
+ });
251
+ const notes = await gitlabReads({ ...opts, fetch }).pullRequestNotes(5);
252
+ expect(notes.map((n) => [n.id, n.system, n.authorId])).toEqual([
253
+ [1, true, 41],
254
+ [2, false, 41],
255
+ ]);
256
+ expect(urls(calls)).toEqual([`GET https://gitlab.com${P}/merge_requests/5/notes?sort=asc&order_by=created_at&per_page=100&page=1`]);
257
+ });
258
+
259
+ test("reads a merge request's commits", async () => {
260
+ const { fetch } = fakeFetch({ [`GET ${P}/merge_requests/5/commits`]: [{ id: SHA, message: "m", author_name: "Al", author_email: "al@x.io", committer_email: "c@x.io" }] });
261
+ expect(await gitlabReads({ ...opts, fetch }).pullRequestCommits(5)).toEqual([{ sha: SHA, message: "m", author: { name: "Al", email: "al@x.io" }, committer: { email: "c@x.io" } }]);
262
+ });
263
+
264
+ test("names a member's role by the access level, and undefined for a stranger", async () => {
265
+ const { fetch } = fakeFetch({ "GET /api/v4/users": [{ id: 3, username: "alice" }], [`GET ${P}/members/all/3`]: { access_level: 30 } });
266
+ expect(await gitlabReads({ ...opts, fetch }).permission("alice")).toBe("developer");
267
+ const none = fakeFetch({ "GET /api/v4/users": [] });
268
+ expect(await gitlabReads({ ...opts, fetch: none.fetch }).permission("mallory")).toBeUndefined();
269
+ });
270
+
271
+ test("lists every open merge request, into any target", async () => {
272
+ const { fetch } = fakeFetch({
273
+ [`GET ${P}/merge_requests`]: [{ iid: 4, source_branch: "a", sha: SHA, target_branch: "release", author: { username: "al" }, diff_refs: { base_sha: MERGED }, source_project_id: 1, target_project_id: 1 }],
274
+ });
275
+ expect(await gitlabReads({ ...opts, fetch }).openPullRequests()).toEqual([{ number: 4, head: "a", headSha: SHA, base: "release", baseSha: MERGED, author: "al", fork: false }]);
276
+ });
277
+ });
278
+
279
+ test("forgeReadsFor builds the reads for a target", async () => {
280
+ const { fetch, calls } = fakeFetch({ "GET /api/v4/user": { username: "bot", id: 1 } });
281
+ const reads = forgeReadsFor({ kind: "gitlab", apiBase: "https://gitlab.com/api/v4", repo: "42", host: "gitlab.com", token: "cj", tokenSource: "CI_JOB_TOKEN", tokenHeader: "JOB-TOKEN" }, fetch);
282
+ expect(reads.kind).toBe("gitlab");
283
+ expect(await reads.tokenUser()).toEqual({ login: "bot", id: 1 });
284
+ expect(calls[0]!.headers["JOB-TOKEN"]).toBe("cj");
285
+ });