@intentius/chant 0.124.88 → 0.124.89

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 (303) hide show
  1. package/dist/actor.d.ts +36 -1
  2. package/dist/actor.d.ts.map +1 -1
  3. package/dist/actor.js +39 -1
  4. package/dist/chant-install.d.ts.map +1 -1
  5. package/dist/chant-install.js +1 -2
  6. package/dist/ci-base.d.ts +30 -0
  7. package/dist/ci-base.d.ts.map +1 -0
  8. package/dist/ci-base.js +33 -0
  9. package/dist/ci-comment-commands.d.ts +177 -0
  10. package/dist/ci-comment-commands.d.ts.map +1 -0
  11. package/dist/ci-comment-commands.js +325 -0
  12. package/dist/ci-comment-replies.d.ts +40 -0
  13. package/dist/ci-comment-replies.d.ts.map +1 -0
  14. package/dist/ci-comment-replies.js +59 -0
  15. package/dist/ci-evidence-from-job.d.ts +25 -0
  16. package/dist/ci-evidence-from-job.d.ts.map +1 -1
  17. package/dist/ci-evidence-from-job.js +43 -2
  18. package/dist/ci-evidence-jobs.d.ts +30 -1
  19. package/dist/ci-evidence-jobs.d.ts.map +1 -1
  20. package/dist/ci-evidence-jobs.js +46 -3
  21. package/dist/ci-image.d.ts +21 -4
  22. package/dist/ci-image.d.ts.map +1 -1
  23. package/dist/ci-image.js +62 -10
  24. package/dist/ci-note-commands.d.ts +63 -3
  25. package/dist/ci-note-commands.d.ts.map +1 -1
  26. package/dist/ci-note-commands.js +69 -35
  27. package/dist/ci-product-words.d.ts +42 -0
  28. package/dist/ci-product-words.d.ts.map +1 -1
  29. package/dist/ci-product-words.js +25 -0
  30. package/dist/ci-product.d.ts +31 -0
  31. package/dist/ci-product.d.ts.map +1 -1
  32. package/dist/ci-product.js +34 -4
  33. package/dist/cli/commands/build.d.ts.map +1 -1
  34. package/dist/cli/commands/build.js +29 -2
  35. package/dist/cli/handlers/approve.d.ts.map +1 -1
  36. package/dist/cli/handlers/approve.js +2 -1
  37. package/dist/cli/handlers/ci-check.d.ts +1 -1
  38. package/dist/cli/handlers/ci-check.d.ts.map +1 -1
  39. package/dist/cli/handlers/ci-check.js +17 -7
  40. package/dist/cli/handlers/ci-evidence.d.ts.map +1 -1
  41. package/dist/cli/handlers/ci-evidence.js +33 -7
  42. package/dist/cli/handlers/components.d.ts.map +1 -1
  43. package/dist/cli/handlers/components.js +3 -1
  44. package/dist/cli/handlers/generated-check.d.ts +8 -0
  45. package/dist/cli/handlers/generated-check.d.ts.map +1 -1
  46. package/dist/cli/handlers/generated-check.js +31 -1
  47. package/dist/cli/handlers/lock.d.ts.map +1 -1
  48. package/dist/cli/handlers/lock.js +121 -4
  49. package/dist/cli/handlers/pr-comment.d.ts +21 -0
  50. package/dist/cli/handlers/pr-comment.d.ts.map +1 -0
  51. package/dist/cli/handlers/pr-comment.js +112 -0
  52. package/dist/cli/handlers/pr-env.d.ts.map +1 -1
  53. package/dist/cli/handlers/pr-env.js +6 -3
  54. package/dist/cli/handlers/pr.d.ts +23 -1
  55. package/dist/cli/handlers/pr.d.ts.map +1 -1
  56. package/dist/cli/handlers/pr.js +55 -9
  57. package/dist/cli/handlers/promote.d.ts.map +1 -1
  58. package/dist/cli/handlers/promote.js +2 -1
  59. package/dist/cli/handlers/release.d.ts.map +1 -1
  60. package/dist/cli/handlers/release.js +11 -2
  61. package/dist/cli/handlers/run-wave.d.ts.map +1 -1
  62. package/dist/cli/handlers/run-wave.js +28 -2
  63. package/dist/cli/handlers/run.d.ts.map +1 -1
  64. package/dist/cli/handlers/run.js +2 -1
  65. package/dist/cli/main.d.ts.map +1 -1
  66. package/dist/cli/main.js +24 -2
  67. package/dist/cli/parse-args.d.ts.map +1 -1
  68. package/dist/cli/parse-args.js +16 -0
  69. package/dist/cli/registry.d.ts +11 -0
  70. package/dist/cli/registry.d.ts.map +1 -1
  71. package/dist/components/auto-release.d.ts.map +1 -1
  72. package/dist/components/auto-release.js +2 -1
  73. package/dist/components/pr-checks.d.ts +70 -6
  74. package/dist/components/pr-checks.d.ts.map +1 -1
  75. package/dist/components/pr-checks.js +74 -13
  76. package/dist/components/pr-pipeline.d.ts +16 -12
  77. package/dist/components/pr-pipeline.d.ts.map +1 -1
  78. package/dist/components/pr-pipeline.js +3 -11
  79. package/dist/components/product-pipeline.d.ts +42 -1
  80. package/dist/components/product-pipeline.d.ts.map +1 -1
  81. package/dist/components/product-pipeline.js +64 -12
  82. package/dist/components/product-plan.d.ts +190 -5
  83. package/dist/components/product-plan.d.ts.map +1 -1
  84. package/dist/components/product-plan.js +220 -39
  85. package/dist/drift-record.d.ts +7 -0
  86. package/dist/drift-record.d.ts.map +1 -1
  87. package/dist/drift-record.js +19 -0
  88. package/dist/floor-table.data.d.ts.map +1 -1
  89. package/dist/floor-table.data.js +54 -31
  90. package/dist/forge-check/contract.d.ts +11 -1
  91. package/dist/forge-check/contract.d.ts.map +1 -1
  92. package/dist/forge-check/contract.js +4 -1
  93. package/dist/forge-check/index.d.ts +2 -0
  94. package/dist/forge-check/index.d.ts.map +1 -1
  95. package/dist/forge-check/index.js +2 -0
  96. package/dist/gate-resume-run.d.ts +9 -1
  97. package/dist/gate-resume-run.d.ts.map +1 -1
  98. package/dist/gate-resume-run.js +26 -3
  99. package/dist/generated-check.d.ts +18 -4
  100. package/dist/generated-check.d.ts.map +1 -1
  101. package/dist/generated-check.js +37 -5
  102. package/dist/image-pins.d.ts +26 -0
  103. package/dist/image-pins.d.ts.map +1 -0
  104. package/dist/image-pins.js +19 -0
  105. package/dist/lexicon.d.ts +11 -0
  106. package/dist/lexicon.d.ts.map +1 -1
  107. package/dist/lifecycle/git.d.ts +11 -0
  108. package/dist/lifecycle/git.d.ts.map +1 -1
  109. package/dist/lifecycle/git.js +30 -3
  110. package/dist/lifecycle/publish-npm.d.ts +11 -0
  111. package/dist/lifecycle/publish-npm.d.ts.map +1 -1
  112. package/dist/lifecycle/publish-npm.js +27 -1
  113. package/dist/lifecycle/reach-locks.d.ts +131 -0
  114. package/dist/lifecycle/reach-locks.d.ts.map +1 -0
  115. package/dist/lifecycle/reach-locks.js +118 -0
  116. package/dist/lifecycle/release-cut.d.ts +3 -1
  117. package/dist/lifecycle/release-cut.d.ts.map +1 -1
  118. package/dist/lifecycle/release-cut.js +15 -0
  119. package/dist/lifecycle/release-images.d.ts.map +1 -1
  120. package/dist/lifecycle/release-images.js +1 -2
  121. package/dist/notify-product.d.ts +12 -3
  122. package/dist/notify-product.d.ts.map +1 -1
  123. package/dist/notify-product.js +10 -6
  124. package/dist/op/activities/source-release.d.ts.map +1 -1
  125. package/dist/op/activities/source-release.js +2 -1
  126. package/dist/op/activities/source-rollback.d.ts.map +1 -1
  127. package/dist/op/activities/source-rollback.js +2 -1
  128. package/dist/op/index.d.ts +2 -0
  129. package/dist/op/index.d.ts.map +1 -1
  130. package/dist/op/index.js +1 -0
  131. package/dist/op/op-waves-evidence-jobs.d.ts +6 -1
  132. package/dist/op/op-waves-evidence-jobs.d.ts.map +1 -1
  133. package/dist/op/op-waves-evidence-jobs.js +9 -3
  134. package/dist/op/op-waves-render-spec.d.ts +100 -0
  135. package/dist/op/op-waves-render-spec.d.ts.map +1 -0
  136. package/dist/op/op-waves-render-spec.js +162 -0
  137. package/dist/op/op-waves-run.d.ts +2 -0
  138. package/dist/op/op-waves-run.d.ts.map +1 -1
  139. package/dist/op/op-waves-run.js +2 -1
  140. package/dist/op/op-waves.d.ts +4 -0
  141. package/dist/op/op-waves.d.ts.map +1 -1
  142. package/dist/op/op-waves.js +4 -1
  143. package/dist/op/schedule-overdue.d.ts.map +1 -1
  144. package/dist/op/schedule-overdue.js +59 -3
  145. package/dist/op/wave-stop.d.ts +64 -0
  146. package/dist/op/wave-stop.d.ts.map +1 -0
  147. package/dist/op/wave-stop.js +127 -0
  148. package/dist/planned-files.d.ts +76 -0
  149. package/dist/planned-files.d.ts.map +1 -0
  150. package/dist/planned-files.js +108 -0
  151. package/dist/pr-forge-job.d.ts +22 -0
  152. package/dist/pr-forge-job.d.ts.map +1 -0
  153. package/dist/pr-forge-job.js +46 -0
  154. package/dist/pr-loop.d.ts +3 -6
  155. package/dist/pr-loop.d.ts.map +1 -1
  156. package/dist/pr-loop.js +4 -81
  157. package/dist/pr-plan-note.d.ts +97 -0
  158. package/dist/pr-plan-note.d.ts.map +1 -1
  159. package/dist/pr-plan-note.js +94 -1
  160. package/dist/pr-post.d.ts +28 -3
  161. package/dist/pr-post.d.ts.map +1 -1
  162. package/dist/pr-post.js +145 -13
  163. package/dist/pr-status.d.ts +16 -0
  164. package/dist/pr-status.d.ts.map +1 -0
  165. package/dist/pr-status.js +90 -0
  166. package/dist/ref-gate-init.d.ts +16 -2
  167. package/dist/ref-gate-init.d.ts.map +1 -1
  168. package/dist/ref-gate-init.js +24 -13
  169. package/dist/release-floors.d.ts +37 -1
  170. package/dist/release-floors.d.ts.map +1 -1
  171. package/dist/release-floors.js +61 -25
  172. package/dist/render-module.d.ts +15 -0
  173. package/dist/render-module.d.ts.map +1 -0
  174. package/dist/render-module.js +73 -0
  175. package/dist/state-migration.d.ts.map +1 -1
  176. package/dist/state-migration.js +1 -2
  177. package/dist/wave-runtime.mjs +304 -301
  178. package/dist/workspace/ci-green-cli.d.ts.map +1 -1
  179. package/dist/workspace/ci-green-cli.js +3 -2
  180. package/dist/workspace/release-train.d.ts.map +1 -1
  181. package/dist/workspace/release-train.js +2 -1
  182. package/dist/workspace/release-workflow.d.ts +4 -3
  183. package/dist/workspace/release-workflow.d.ts.map +1 -1
  184. package/dist/workspace/release-workflow.js +10 -7
  185. package/dist/workspace/trust/check-evidence-report.d.ts +7 -2
  186. package/dist/workspace/trust/check-evidence-report.d.ts.map +1 -1
  187. package/dist/workspace/trust/check-evidence-report.js +15 -8
  188. package/dist/workspace/trust/ref-gate-cli.d.ts +10 -1
  189. package/dist/workspace/trust/ref-gate-cli.d.ts.map +1 -1
  190. package/dist/workspace/trust/ref-gate-cli.js +30 -10
  191. package/dist/workspace/workspace-run.d.ts.map +1 -1
  192. package/dist/workspace/workspace-run.js +2 -1
  193. package/package.json +1 -1
  194. package/src/actor.test.ts +68 -2
  195. package/src/actor.ts +61 -1
  196. package/src/chant-install.test.ts +2 -3
  197. package/src/chant-install.ts +1 -2
  198. package/src/ci-base.test.ts +41 -0
  199. package/src/ci-base.ts +43 -0
  200. package/src/ci-comment-commands.test.ts +376 -0
  201. package/src/ci-comment-commands.ts +389 -0
  202. package/src/ci-comment-replies.ts +78 -0
  203. package/src/ci-commit.test.ts +2 -3
  204. package/src/ci-evidence-from-job.test.ts +18 -0
  205. package/src/ci-evidence-from-job.ts +58 -2
  206. package/src/ci-evidence-jobs.ts +50 -5
  207. package/src/ci-image.test.ts +130 -9
  208. package/src/ci-image.ts +85 -12
  209. package/src/ci-note-commands.ts +104 -41
  210. package/src/ci-product-words.ts +59 -0
  211. package/src/ci-product.ts +40 -4
  212. package/src/cli/commands/build.test.ts +41 -0
  213. package/src/cli/commands/build.ts +27 -2
  214. package/src/cli/handlers/approve.ts +2 -1
  215. package/src/cli/handlers/ci-check.ts +19 -7
  216. package/src/cli/handlers/ci-evidence.ts +29 -8
  217. package/src/cli/handlers/components.ts +3 -1
  218. package/src/cli/handlers/generated-check.ts +31 -1
  219. package/src/cli/handlers/lock-reach.test.ts +103 -0
  220. package/src/cli/handlers/lock.ts +100 -4
  221. package/src/cli/handlers/pr-comment.ts +104 -0
  222. package/src/cli/handlers/pr-env.ts +6 -3
  223. package/src/cli/handlers/pr-record.test.ts +1 -1
  224. package/src/cli/handlers/pr.ts +50 -10
  225. package/src/cli/handlers/promote.ts +2 -1
  226. package/src/cli/handlers/release.e2e.test.ts +35 -0
  227. package/src/cli/handlers/release.ts +14 -2
  228. package/src/cli/handlers/run-wave.ts +28 -2
  229. package/src/cli/handlers/run.ts +2 -1
  230. package/src/cli/main.ts +24 -2
  231. package/src/cli/parse-args.ts +12 -0
  232. package/src/cli/registry.ts +11 -0
  233. package/src/components/auto-release.ts +2 -1
  234. package/src/components/pr-checks.ts +112 -17
  235. package/src/components/pr-pipeline.ts +17 -15
  236. package/src/components/product-pipeline.ts +93 -11
  237. package/src/components/product-plan.test.ts +40 -2
  238. package/src/components/product-plan.ts +331 -45
  239. package/src/drift-record.test.ts +20 -0
  240. package/src/drift-record.ts +16 -0
  241. package/src/floor-table.data.ts +54 -31
  242. package/src/forge-check/contract.ts +12 -1
  243. package/src/forge-check/forge-check.test.ts +17 -0
  244. package/src/forge-check/index.ts +2 -0
  245. package/src/gate-resume-run.ts +27 -3
  246. package/src/generated-check.e2e.test.ts +21 -1
  247. package/src/generated-check.test.ts +66 -0
  248. package/src/generated-check.ts +51 -10
  249. package/src/image-pins.ts +31 -0
  250. package/src/lexicon.ts +11 -0
  251. package/src/lifecycle/git.test.ts +48 -0
  252. package/src/lifecycle/git.ts +27 -4
  253. package/src/lifecycle/publish-npm.ts +31 -1
  254. package/src/lifecycle/reach-locks.test.ts +87 -0
  255. package/src/lifecycle/reach-locks.ts +169 -0
  256. package/src/lifecycle/release-cut.ts +17 -1
  257. package/src/lifecycle/release-images.test.ts +56 -0
  258. package/src/lifecycle/release-images.ts +1 -2
  259. package/src/notify-product.ts +19 -8
  260. package/src/op/activities/source-release.ts +2 -1
  261. package/src/op/activities/source-rollback.ts +2 -1
  262. package/src/op/gate-resume.test.ts +27 -0
  263. package/src/op/index.ts +10 -0
  264. package/src/op/op-waves-evidence-jobs.ts +10 -4
  265. package/src/op/op-waves-notify-words.test.ts +18 -0
  266. package/src/op/op-waves-render-spec.test.ts +160 -0
  267. package/src/op/op-waves-render-spec.ts +171 -0
  268. package/src/op/op-waves-run.ts +2 -1
  269. package/src/op/op-waves.ts +5 -2
  270. package/src/op/schedule-overdue.test.ts +43 -0
  271. package/src/op/schedule-overdue.ts +57 -3
  272. package/src/op/wave-stop.test.ts +72 -0
  273. package/src/op/wave-stop.ts +137 -0
  274. package/src/planned-files.test.ts +80 -0
  275. package/src/planned-files.ts +134 -0
  276. package/src/pr-forge-job.test.ts +53 -0
  277. package/src/pr-forge-job.ts +46 -0
  278. package/src/pr-loop-narrowed.test.ts +2 -1
  279. package/src/pr-loop-words.test.ts +2 -1
  280. package/src/pr-loop.test.ts +1 -2
  281. package/src/pr-loop.ts +4 -80
  282. package/src/pr-plan-note.ts +155 -3
  283. package/src/pr-post-replan.test.ts +144 -0
  284. package/src/pr-post-subject.test.ts +230 -0
  285. package/src/pr-post.test.ts +2 -1
  286. package/src/pr-post.ts +150 -15
  287. package/src/pr-status.ts +91 -0
  288. package/src/ref-gate-init.ts +30 -11
  289. package/src/release-floors.ts +78 -25
  290. package/src/render-module.ts +73 -0
  291. package/src/state-migration-record.test.ts +2 -3
  292. package/src/state-migration.ts +1 -2
  293. package/src/workspace/ci-green-cli.ts +3 -2
  294. package/src/workspace/ci-green-workflow.test.ts +32 -1
  295. package/src/workspace/release-train.ts +2 -1
  296. package/src/workspace/release-workflow.test.ts +36 -5
  297. package/src/workspace/release-workflow.ts +10 -7
  298. package/src/workspace/trust/check-evidence-report.ts +15 -8
  299. package/src/workspace/trust/check-evidence.test.ts +5 -0
  300. package/src/workspace/trust/ref-gate-cli.ts +30 -8
  301. package/src/workspace/trust/ref-gate-pieces.test.ts +46 -1
  302. package/src/workspace/trust/ref-gate.test.ts +10 -0
  303. package/src/workspace/workspace-run.ts +2 -1
package/src/pr-loop.ts CHANGED
@@ -6,8 +6,9 @@
6
6
  * `chant components pr-plan` and `chant components pr-apply` run it, and the
7
7
  * generated CI for GitHub, GitLab and Forgejo calls them
8
8
  * (`components/pr-pipeline.ts`). This module holds the parts that need no
9
- * runner: the gate's names, the report both commands write, the pull-request
10
- * note rendered from it and the status line. terragucci reads the report and
9
+ * runner: the gate's names, the report both commands write and the
10
+ * pull-request note rendered from it; the status line is in `./pr-status.ts`
11
+ * (#4966), outside the bundle. terragucci reads the report and
11
12
  * renders the note from one bundled file with no TypeScript toolchain
12
13
  * (#3421), so this imports `./change-set` and `./plan-summary` and nothing
13
14
  * else; `pr-loop-bundle.test.ts` holds that.
@@ -1244,81 +1245,4 @@ function narrowedLines(report: PrReport, words: CiProductWords): string[] {
1244
1245
  return ["", `This ${words.verbs.plan[0]} was narrowed with \`--target\`: ${each}. The approval binds the narrowed plan. On merge each ${words.noun[0]} is ${words.verbs.plan[1]} whole.`];
1245
1246
  }
1246
1247
 
1247
- /** A commit status's description, at most 140 characters (GitHub's limit). */
1248
- export function prStatusDescription(report: PrReport, words: CiProductWords = activeProductWords()): string {
1249
- const n = report.members.length;
1250
- const many = (k: number): string => `${k} ${words.noun[k === 1 ? 0 : 1]}`;
1251
- const sum = report.members.reduce(
1252
- (acc, m) => ({ change: acc.change + m.counts.create + m.counts.update + m.counts.replace, destroy: acc.destroy + m.counts.delete + m.counts.replace }),
1253
- { change: 0, destroy: 0 },
1254
- );
1255
- const digest = report.digest ? `; plan ${report.digest.replace(/^(?:jcs1-)?sha256:/, "").slice(0, 12)}` : "";
1256
- let text: string;
1257
- switch (report.status) {
1258
- case "nothing":
1259
- text = "nothing to plan";
1260
- break;
1261
- case "planned": {
1262
- const unknown = report.members.filter((m) => memberHoleReasons(report, m.member).length > 0).length;
1263
- // #4626: a narrowed plan says so in its status.
1264
- const narrowed = Object.values(report.narrowed ?? {}).filter((a) => a.length > 0).length;
1265
- text = `${many(n)}${narrowed > 0 ? ` (${narrowed} narrowed with --target)` : ""}: ${sum.change} to change, ${sum.destroy} to destroy or replace${unknown > 0 ? `, ${unknown} with holes` : ""}${digest}`;
1266
- break;
1267
- }
1268
- case "plan-failed":
1269
- text = `a ${words.noun[0]} failed to ${words.verbs.plan[0]}${digest}`;
1270
- break;
1271
- case "recorded":
1272
- text = `recorded ${many(n)}: ${report.approval.status === "approved" ? "the approved plan" : report.approval.status === "changed" ? "the plan differs from the approved one" : "no approval for this plan"}${digest}`;
1273
- break;
1274
- case "applied":
1275
- text = `${words.verbs.apply[1]} ${many(n)}${digest}`;
1276
- break;
1277
- case "wave-applied": {
1278
- const last = (report.waves ?? []).filter((w) => w.status === "applied").at(-1);
1279
- text = `applied wave ${last?.wave ?? "?"} of ${report.selection.waves.length}${last ? ` under ${last.approval === "pull-request" ? "the pull request's approval" : `${last.gate}`}` : ""}`;
1280
- break;
1281
- }
1282
- case "failed":
1283
- text = `apply failed for ${report.members.filter((m) => m.status === "failed").map((m) => m.member).join(", ")}`;
1284
- break;
1285
- case "waiting":
1286
- text = `waiting: ${report.members.filter((m) => m.status === "waiting").map((m) => m.member).join(", ")} planned again after an upstream applied, not approved yet`;
1287
- break;
1288
- case "refused": {
1289
- const waiting = report.waves?.find((w) => w.status === "waiting");
1290
- if (waiting && (report.refusal === "wave-not-approved" || report.refusal === "wave-plan-changed")) {
1291
- text = `wave ${waiting.wave} waits at ${waiting.gate}: ${report.refusal === "wave-plan-changed" ? "its plan changed after its approval" : "its plan moved, approve it"}`;
1292
- break;
1293
- }
1294
- if (report.refusal === "earlier-wave") {
1295
- text = "refused: an earlier wave has not applied";
1296
- break;
1297
- }
1298
- if (report.refusal === "requires-unmet" || report.refusal === "before-merge-off") {
1299
- const unmet = (report.beforeMerge?.requires ?? []).filter((r) => !r.passed).map((r) => r.name);
1300
- text = report.refusal === "before-merge-off" ? "refused: this workspace does not apply before merge" : `refused: ${unmet.join(", ") || "a require"} unmet for an apply before merge`;
1301
- break;
1302
- }
1303
- if (report.refusal === "policy-denied" || report.refusal === "policy-error") {
1304
- text = report.refusal === "policy-denied" ? `refused: the policy denied the plan (${(report.policy?.rules ?? []).join(", ")})` : "refused: the policy could not be checked";
1305
- break;
1306
- }
1307
- text = `refused: ${report.refusal === "plan-changed" ? "the plan changed after review" : report.refusal === "not-a-reviewer" ? "the approver has not approved the pull request" : report.refusal === "author-approved" ? "the approver wrote the change" : report.refusal === "no-pull-request" ? "no pull request merged this commit" : report.refusal === "locked" ? "another pull request holds a target lock" : "the plan is not approved"}`;
1308
- break;
1309
- }
1310
- }
1311
- return text.length > 140 ? text.slice(0, 137) + "..." : text;
1312
- }
1313
-
1314
- /** The commit status state for a report: success, failure, or pending while it waits on approval. */
1315
- export function prStatusState(report: PrReport): "success" | "failure" | "pending" {
1316
- // #4168 decision 1: a merge whose plan differs from the approved one is recorded, and its status fails.
1317
- if (report.status === "recorded") return report.approval.status === "changed" ? "failure" : "success";
1318
- // #4294: a plan the policy denied, with no override, or one it could not check, fails at the plan stage as it would at the apply.
1319
- if (report.status === "planned" && report.policy && (report.policy.result === "error" || (report.policy.result === "denied" && !report.policy.override))) return "failure";
1320
- if (report.status === "planned" || report.status === "applied" || report.status === "wave-applied" || report.status === "nothing") return "success";
1321
- if (report.status === "refused" && (report.refusal === "not-approved" || report.refusal === "wave-not-approved")) return "pending";
1322
- if (report.status === "waiting") return "pending";
1323
- return "failure";
1324
- }
1248
+ // The commit status's description and state (`prStatusDescription`, `prStatusState`) are in ./pr-status.ts (#4966).
@@ -68,6 +68,85 @@ export interface PrPlanNoteHandoff {
68
68
  * one space, at most {@link PRODUCT_MARKER_LIMIT} characters.
69
69
  */
70
70
  marker?: string;
71
+ /**
72
+ * What the plan is taken over (#4972), such as sql-yodeler's plan digest
73
+ * and its subject's parts: `pr-post` compares it with the subject it kept
74
+ * in the previous note for the same environment and member, says what
75
+ * changed when its id differs, and keeps this one in the new note.
76
+ */
77
+ subject?: PrPlanSubject;
78
+ }
79
+
80
+ /**
81
+ * A plan's subject (#4972), as a plan verb hands it to the note job, which
82
+ * compares it with the previous note's, so the plan job needs no forge
83
+ * token to read the previous note on any forge. Both are the change's code,
84
+ * so each word is checked against {@link PLAN_SUBJECT_WORD}.
85
+ */
86
+ export interface PrPlanSubject {
87
+ /** The plan as one word, such as its digest: a different id is a changed plan. */
88
+ id: string;
89
+ /**
90
+ * The parts the id is taken over, each `<group>` or `<group>/<item>` to a
91
+ * word (a checksum, a list joined by commas): `pr-post` names the parts
92
+ * added, gone and changed, group by group, in the note words'
93
+ * `subjectParts`. Absent: the line says only that the plan changed.
94
+ */
95
+ parts?: Record<string, string>;
96
+ }
97
+
98
+ /** A word of a plan subject (#4972): letters, digits and `_.,:+=-`, at most 1024 characters, nothing that ends a comment or mentions anyone. */
99
+ export const PLAN_SUBJECT_WORD = /^[A-Za-z0-9_.,:+=-]{1,1024}$/;
100
+
101
+ /** A plan subject part's key (#4972): `<group>` or `<group>/<item>`. */
102
+ export const PLAN_SUBJECT_KEY = /^[A-Za-z0-9_.:+=-]{1,128}(\/[A-Za-z0-9_.,:+=-]{1,256})?$/;
103
+
104
+ /** The most characters the note's kept subject line holds (#4972); a subject with more parts is kept by its id alone. */
105
+ export const PLAN_SUBJECT_LIMIT = 16_384;
106
+
107
+ /** Why `subject` is not a {@link PrPlanSubject} (#4972), or undefined when it is one. */
108
+ export function planSubjectProblem(subject: unknown): string | undefined {
109
+ if (!subject || typeof subject !== "object" || Array.isArray(subject)) return "is not an object";
110
+ const s = subject as Record<string, unknown>;
111
+ if (typeof s.id !== "string" || !PLAN_SUBJECT_WORD.test(s.id)) return "has no id of letters, digits and _.,:+=-";
112
+ if (s.parts === undefined) return undefined;
113
+ if (!s.parts || typeof s.parts !== "object" || Array.isArray(s.parts)) return "has parts that are not an object";
114
+ for (const [k, v] of Object.entries(s.parts)) {
115
+ if (!PLAN_SUBJECT_KEY.test(k)) return `has a part ${JSON.stringify(k.slice(0, 64))} that is not <group> or <group>/<item>`;
116
+ if (typeof v !== "string" || !PLAN_SUBJECT_WORD.test(v)) return `has a part ${k} whose value is not a word of letters, digits and _.,:+=-`;
117
+ }
118
+ return undefined;
119
+ }
120
+
121
+ /** How the note words name one group's changes (#4972), each over `{group}` and `{items}`, the group's items as `item` writes them, joined by ", ". */
122
+ export interface PrPlanSubjectPartWords {
123
+ added?: string;
124
+ gone?: string;
125
+ changed?: string;
126
+ /** One item, over `{item}`, `{before}` and `{after}`. Default `{item}`, or for a part with no item `{before} -> {after}` (changed), `{after}` (added), `{before}` (gone). */
127
+ item?: string;
128
+ }
129
+
130
+ /**
131
+ * A product's plan note hand-off whose note is the product's own text
132
+ * (#4963), in place of a {@link PrPlanNote}: sql-yodeler's tenant set note,
133
+ * which chant's note cannot carry yet. `pr-post` checks it as it checks a
134
+ * {@link PrPlanNoteHandoff}, writes every `<!--` in `body` as `&lt;!--`,
135
+ * puts chant's marker and the product's line (`marker`) above it, adds the
136
+ * overdue Ops and the note job's notices under it, and cuts it to the
137
+ * forge's limit.
138
+ */
139
+ export interface PrPlanBodyHandoff extends Omit<PrPlanNoteHandoff, "note"> {
140
+ /** The note's markdown, at most {@link PLAN_BODY_LIMIT} characters. */
141
+ body: string;
142
+ }
143
+
144
+ /** The most characters a {@link PrPlanBodyHandoff.body} holds (#4963). */
145
+ export const PLAN_BODY_LIMIT = 1_000_000;
146
+
147
+ /** The hand-off for a product's own note text (#4963), as a plan verb writes it to {@link PR_PLAN_NOTE_FILE}. */
148
+ export function planBodyHandoff(body: string, at: { pr: number; head: string; env: string; member?: string; state: PrPlanNoteHandoff["state"]; description: string; marker?: string; subject?: PrPlanSubject }): PrPlanBodyHandoff {
149
+ return { kind: "plan-note-handoff", contract: PR_PLAN_NOTE_CONTRACT, pr: at.pr, head: at.head, env: at.env, ...(at.member !== undefined ? { member: at.member } : {}), state: at.state, description: at.description, body, ...(at.marker !== undefined ? { marker: at.marker } : {}), ...(at.subject !== undefined ? { subject: at.subject } : {}) };
71
150
  }
72
151
 
73
152
  /** The most characters a hand-off's {@link PrPlanNoteHandoff.marker} holds (#4931). */
@@ -85,8 +164,8 @@ export function productMarkerLine(name: string, body: string): string {
85
164
  }
86
165
 
87
166
  /** The hand-off for `note` (#4799), as a product's plan verb writes it to {@link PR_PLAN_NOTE_FILE}. */
88
- export function planNoteHandoff(note: PrPlanNote, at: { pr: number; env: string; member?: string; state: PrPlanNoteHandoff["state"]; description: string; marker?: string }): PrPlanNoteHandoff {
89
- return { kind: "plan-note-handoff", contract: PR_PLAN_NOTE_CONTRACT, pr: at.pr, head: note.head, env: at.env, ...(at.member !== undefined ? { member: at.member } : {}), state: at.state, description: at.description, note, ...(at.marker !== undefined ? { marker: at.marker } : {}) };
167
+ export function planNoteHandoff(note: PrPlanNote, at: { pr: number; env: string; member?: string; state: PrPlanNoteHandoff["state"]; description: string; marker?: string; subject?: PrPlanSubject }): PrPlanNoteHandoff {
168
+ return { kind: "plan-note-handoff", contract: PR_PLAN_NOTE_CONTRACT, pr: at.pr, head: note.head, env: at.env, ...(at.member !== undefined ? { member: at.member } : {}), state: at.state, description: at.description, note, ...(at.marker !== undefined ? { marker: at.marker } : {}), ...(at.subject !== undefined ? { subject: at.subject } : {}) };
90
169
  }
91
170
 
92
171
  /** One change line of a group, as chant's plan summary groups them. */
@@ -258,9 +337,82 @@ export interface PrPlanNoteWords {
258
337
  * note by it. Absent: a hand-off's marker is dropped.
259
338
  */
260
339
  productMarker?: string;
340
+ /**
341
+ * The line `pr-post` adds when a hand-off's subject differs from the
342
+ * previous note's (#4972), over `{previous}` (the previous subject's id)
343
+ * and `{changes}` (what changed, part by part, joined by "; "). Default
344
+ * "Since the previous version of this note (`{previous}`): {changes}.".
345
+ */
346
+ sinceLast?: string;
347
+ /**
348
+ * The line when no part says what changed (#4972): the subject has no
349
+ * parts, or the previous note kept its id alone. Over `{previous}`.
350
+ * Default "The plan changed since the previous version of this note (`{previous}`).".
351
+ */
352
+ sinceLastOnly?: string;
353
+ /** How `{changes}` names each group of the subject's parts (#4972), by group. Default "{group}: {items} added" (gone, changed). */
354
+ subjectParts?: Record<string, PrPlanSubjectPartWords>;
355
+ }
356
+
357
+ /** The {@link PrPlanNoteWords.sinceLast} default (#4972). */
358
+ export const SINCE_LAST_LINE = "Since the previous version of this note (`{previous}`): {changes}.";
359
+ /** The {@link PrPlanNoteWords.sinceLastOnly} default (#4972). */
360
+ export const SINCE_LAST_ONLY_LINE = "The plan changed since the previous version of this note (`{previous}`).";
361
+
362
+ /**
363
+ * What changed from `before` to `after`, group by group (#4972): each
364
+ * group's added, gone and changed parts in the note words, groups in the
365
+ * order `after` names them, then those only `before` names.
366
+ */
367
+ export function subjectChanges(before: Record<string, string>, after: Record<string, string>, words: Record<string, PrPlanSubjectPartWords> = {}): string[] {
368
+ const groupOf = (k: string): [string, string] => {
369
+ const at = k.indexOf("/");
370
+ return at < 0 ? [k, ""] : [k.slice(0, at), k.slice(at + 1)];
371
+ };
372
+ const has = (o: object, k: string): boolean => Object.prototype.hasOwnProperty.call(o, k);
373
+ const groups: string[] = [];
374
+ for (const k of [...Object.keys(after), ...Object.keys(before)]) {
375
+ const [g] = groupOf(k);
376
+ if (!groups.includes(g)) groups.push(g);
377
+ }
378
+ const fill = (t: string, v: Record<string, string>): string => t.replace(/\{(group|items|item|before|after)\}/g, (_, n: string) => v[n] ?? "");
379
+ const out: string[] = [];
380
+ for (const group of groups) {
381
+ const w = has(words, group) ? words[group]! : {};
382
+ const kinds: Record<"added" | "gone" | "changed", string[]> = { added: [], gone: [], changed: [] };
383
+ const say = (kind: "added" | "gone" | "changed", item: string, b: string, a: string) => {
384
+ const dflt = item !== "" ? "{item}" : kind === "changed" ? "{before} -> {after}" : kind === "added" ? "{after}" : "{before}";
385
+ kinds[kind].push(fill(w.item ?? dflt, { item, before: b, after: a, group }));
386
+ };
387
+ for (const k of Object.keys(after)) {
388
+ const [g, item] = groupOf(k);
389
+ if (g !== group) continue;
390
+ if (!has(before, k)) say("added", item, "", after[k]!);
391
+ else if (before[k] !== after[k]) say("changed", item, before[k]!, after[k]!);
392
+ }
393
+ for (const k of Object.keys(before)) {
394
+ const [g, item] = groupOf(k);
395
+ if (g === group && !has(after, k)) say("gone", item, before[k]!, "");
396
+ }
397
+ for (const kind of ["added", "gone", "changed"] as const) {
398
+ if (kinds[kind].length > 0) out.push(fill(w[kind] ?? `{group}: {items} ${kind}`, { group, items: kinds[kind].join(", ") }));
399
+ }
400
+ }
401
+ return out;
402
+ }
403
+
404
+ /**
405
+ * The line for a plan whose subject is no longer `previous`'s (#4972), in
406
+ * the note words, or undefined when the ids are the same.
407
+ */
408
+ export function sinceLastLine(previous: PrPlanSubject, now: PrPlanSubject, words: Pick<PrPlanNoteWords, "sinceLast" | "sinceLastOnly" | "subjectParts"> = {}): string | undefined {
409
+ if (previous.id === now.id) return undefined;
410
+ const changes = previous.parts && now.parts ? subjectChanges(previous.parts, now.parts, words.subjectParts) : [];
411
+ const fill = (t: string): string => t.replace(/\{(previous|changes)\}/g, (_, n: string) => (n === "previous" ? previous.id : changes.join("; ")));
412
+ return changes.length > 0 ? fill(words.sinceLast ?? SINCE_LAST_LINE) : fill(words.sinceLastOnly ?? SINCE_LAST_ONLY_LINE);
261
413
  }
262
414
 
263
- const NOTE_WORDS: Required<Omit<PrPlanNoteWords, "marker" | "productMarker">> = {
415
+ const NOTE_WORDS: Required<Omit<PrPlanNoteWords, "marker" | "productMarker" | "sinceLast" | "sinceLastOnly" | "subjectParts">> = {
264
416
  footer: "",
265
417
  page: "report.html",
266
418
  artifact: "chant-pr",
@@ -0,0 +1,144 @@
1
+ /**
2
+ * #4963: what pr-post does for the product plan's re-plan and for
3
+ * sql-yodeler's notes. A re-plan narrowed to an argument the change does not
4
+ * reach gets a "not affected" reply in place of the note and the status, as
5
+ * terragucci's plan-note job replied; a `PrReport` carries the product's
6
+ * marker as a hand-off does; and a hand-off may carry the product's own note
7
+ * text, defused and cut, under chant's marker and the product's.
8
+ */
9
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
10
+ import { tmpdir } from "node:os";
11
+ import { join } from "node:path";
12
+ import { afterAll, describe, expect, test } from "vitest";
13
+ import { postPlanReport } from "./pr-post";
14
+ import { prNoteMarker, type PrReport } from "./pr-loop";
15
+ import { planBodyHandoff, planNoteHandoff, PR_PLAN_NOTE_FILE, type PrPlanNote } from "./pr-plan-note";
16
+ import { withProductWords } from "./ci-product-words";
17
+ import { GITHUB_COMMENT_LIMIT } from "./plan-summary";
18
+ import type { ForgeStatus, PrForge } from "./pr-forge";
19
+ import { TERRAGUCCI, TERRAGUCCI_NOTE } from "./__fixtures__/product-words";
20
+
21
+ const NOTE = JSON.parse(readFileSync(join(import.meta.dirname, "__fixtures__", "pr-loop", "terragucci-plan.note.json"), "utf-8")) as PrPlanNote;
22
+ const REPORT = JSON.parse(readFileSync(join(import.meta.dirname, "__fixtures__", "pr-loop", "pr-plan.golden.json"), "utf-8")) as PrReport;
23
+ const HEAD = NOTE.head;
24
+ const dirs: string[] = [];
25
+ afterAll(() => {
26
+ for (const d of dirs) rmSync(d, { recursive: true, force: true });
27
+ });
28
+
29
+ function dirWith(file: string, doc: unknown): string {
30
+ const dir = mkdtempSync(join(tmpdir(), "chant-pr-post-replan-"));
31
+ dirs.push(dir);
32
+ writeFileSync(join(dir, file), JSON.stringify(doc));
33
+ return dir;
34
+ }
35
+
36
+ function fakeForge(): { forge: PrForge; statuses: ForgeStatus[]; notes: Array<{ marker: string; body: string }>; comments: Array<{ pr: number; body: string }> } {
37
+ const statuses: ForgeStatus[] = [];
38
+ const notes: Array<{ marker: string; body: string }> = [];
39
+ const comments: Array<{ pr: number; body: string }> = [];
40
+ const forge = {
41
+ kind: "github",
42
+ host: "github.com",
43
+ async setStatus(_sha: string, status: ForgeStatus) {
44
+ statuses.push(status);
45
+ },
46
+ async upsertNote(_pr: number, marker: string, body: string) {
47
+ notes.push({ marker, body });
48
+ return "1";
49
+ },
50
+ async findComment() {
51
+ return undefined;
52
+ },
53
+ async addComment(pr: number, body: string) {
54
+ comments.push({ pr, body });
55
+ return { id: 1 };
56
+ },
57
+ } as unknown as PrForge;
58
+ return { forge, statuses, notes, comments };
59
+ }
60
+
61
+ const at = { pr: 7, env: "default", state: "success" as const, description: "nothing to plan" };
62
+ const markerWords = { ...TERRAGUCCI_NOTE, productMarker: "terragucci:plan" };
63
+ const nothing: PrPlanNote = { ...NOTE, nothing: true };
64
+
65
+ describe("a re-plan narrowed to an argument the change does not reach (#4963)", () => {
66
+ test("gets a not-affected reply, and the note and the status stay as they were", async () => {
67
+ const { forge, statuses, notes, comments } = fakeForge();
68
+ const r = await withProductWords(TERRAGUCCI, () => postPlanReport({ forge, dir: dirWith(PR_PLAN_NOTE_FILE, planNoteHandoff(nothing, at)), pr: 7, head: HEAD, env: "default", result: "success", notAffected: "live/db" }));
69
+ expect(comments).toEqual([{ pr: 7, body: "terragucci: live/db is not affected by this pull request, so nothing was planned." }]);
70
+ expect(notes).toEqual([]);
71
+ expect(statuses).toEqual([]);
72
+ expect(r).toMatchObject({ noted: false, warnings: [] });
73
+ });
74
+
75
+ test("a PrReport that says nothing gets the same reply", async () => {
76
+ const { forge, comments, notes } = fakeForge();
77
+ await withProductWords(TERRAGUCCI, () => postPlanReport({ forge, dir: dirWith("pr-plan.json", { ...REPORT, status: "nothing" }), pr: REPORT.pr!, head: REPORT.head, env: REPORT.env, notAffected: "live/db" }));
78
+ expect(comments).toHaveLength(1);
79
+ expect(notes).toEqual([]);
80
+ });
81
+
82
+ test("a plan that reaches the argument, an empty argument, or one that is not a plain word posts the note as before", async () => {
83
+ for (const [note, arg] of [[NOTE, "live/db"], [nothing, ""], [nothing, "a b"], [nothing, "$(x)"]] as const) {
84
+ const { forge, comments, notes } = fakeForge();
85
+ await withProductWords(TERRAGUCCI, () => postPlanReport({ forge, dir: dirWith(PR_PLAN_NOTE_FILE, planNoteHandoff(note, at)), pr: 7, head: HEAD, env: "default", notAffected: arg }));
86
+ expect(comments).toEqual([]);
87
+ expect(notes).toHaveLength(1);
88
+ }
89
+ });
90
+
91
+ test("a failed re-plan job posts its failure, not the reply", async () => {
92
+ const { forge, comments, statuses } = fakeForge();
93
+ await withProductWords(TERRAGUCCI, () => postPlanReport({ forge, dir: dirWith(PR_PLAN_NOTE_FILE, planNoteHandoff(nothing, at)), pr: 7, head: HEAD, env: "default", result: "failure", notAffected: "live/db" }));
94
+ expect(comments).toEqual([]);
95
+ expect(statuses[0]!.state).toBe("failure");
96
+ });
97
+ });
98
+
99
+ describe("the product's marker on a PrReport (#4963)", () => {
100
+ test("goes under chant's marker when the words name a productMarker, and the report's marker is plain words", async () => {
101
+ const { forge, notes } = fakeForge();
102
+ await postPlanReport({ forge, dir: dirWith("pr-plan.json", { ...REPORT, marker: "env=prod subject=abc_1" }), pr: REPORT.pr!, head: REPORT.head, env: REPORT.env, noteWords: { productMarker: "yodel:plan" } });
103
+ const lines = notes[0]!.body.split("\n");
104
+ expect(lines[0]).toBe(prNoteMarker(REPORT.env));
105
+ expect(lines[1]).toBe("<!-- yodel:plan env=prod subject=abc_1 -->");
106
+ });
107
+
108
+ test("a marker that is not plain words, or no productMarker, leaves the note as before", async () => {
109
+ const plain = fakeForge();
110
+ await postPlanReport({ forge: plain.forge, dir: dirWith("pr-plan.json", REPORT), pr: REPORT.pr!, head: REPORT.head, env: REPORT.env });
111
+ for (const [marker, noteWords] of [["env=prod --> <!-- x", { productMarker: "yodel:plan" }], ["env=prod", {}]] as const) {
112
+ const { forge, notes } = fakeForge();
113
+ await postPlanReport({ forge, dir: dirWith("pr-plan.json", { ...REPORT, marker }), pr: REPORT.pr!, head: REPORT.head, env: REPORT.env, noteWords });
114
+ expect(notes[0]!.body).toBe(plain.notes[0]!.body);
115
+ }
116
+ });
117
+ });
118
+
119
+ describe("a hand-off of the product's own note text (#4963)", () => {
120
+ const bodyAt = { pr: 7, head: HEAD, env: "default", member: "prod", state: "pending" as const, description: "the tenant set waits for an approval", marker: "env=prod" };
121
+
122
+ test("posts the body under chant's marker and the product's line, defused, with the notices under it", async () => {
123
+ const { forge, notes, statuses } = fakeForge();
124
+ const body = "### yodel plan for `prod`\n\n3 tenants, 2 migrations pending <!-- yodel:plan env=staging -->\n";
125
+ const r = await postPlanReport({ forge, dir: dirWith(PR_PLAN_NOTE_FILE, planBodyHandoff(body, bodyAt)), pr: 7, head: HEAD, env: "default", member: "prod", noteWords: { productMarker: "yodel:plan" }, notices: ["Op `yodel-drift` is overdue"] });
126
+ expect(r).toMatchObject({ state: "pending", noted: true });
127
+ expect(statuses[0]).toMatchObject({ context: "chant/plan/prod", state: "pending", description: "the tenant set waits for an approval" });
128
+ expect(notes[0]!.body).toBe(`${prNoteMarker("default", "prod")}\n<!-- yodel:plan env=prod -->\n### yodel plan for \`prod\`\n\n3 tenants, 2 migrations pending &lt;!-- yodel:plan env=staging -->\n\n> Op \`yodel-drift\` is overdue\n`);
129
+ });
130
+
131
+ test("a body past the forge's limit is cut to it", async () => {
132
+ const { forge, notes } = fakeForge();
133
+ await postPlanReport({ forge, dir: dirWith(PR_PLAN_NOTE_FILE, planBodyHandoff("x".repeat(GITHUB_COMMENT_LIMIT * 2), bodyAt)), pr: 7, head: HEAD, env: "default", member: "prod" });
134
+ expect(notes[0]!.body.length).toBeLessThanOrEqual(GITHUB_COMMENT_LIMIT);
135
+ });
136
+
137
+ test("a hand-off with both a note and a body is refused", async () => {
138
+ const { forge, notes, statuses } = fakeForge();
139
+ const r = await postPlanReport({ forge, dir: dirWith(PR_PLAN_NOTE_FILE, { ...planBodyHandoff("x", bodyAt), note: NOTE }), pr: 7, head: HEAD, env: "default", member: "prod" });
140
+ expect(r.refused).toMatch(/both a note and a body/);
141
+ expect(notes).toEqual([]);
142
+ expect(statuses[0]!.state).toBe("failure");
143
+ });
144
+ });
@@ -0,0 +1,230 @@
1
+ /**
2
+ * #4972: the note job compares the plan's subject with the previous note's.
3
+ * A plan verb hands its subject (an id and named parts) in the plan note
4
+ * hand-off; `pr-post` keeps it in a hidden line of the note, and on the
5
+ * next post reads the previous note of the same environment and member,
6
+ * and when the id differs adds a line in the note words saying what
7
+ * changed. The plan job reads no note, so this works on GitHub, Forgejo and
8
+ * GitLab (`pr-post --open`, from a pipeline of the target branch).
9
+ */
10
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
11
+ import { tmpdir } from "node:os";
12
+ import { join } from "node:path";
13
+ import { afterAll, describe, expect, test } from "vitest";
14
+ import { postOpenPlanReports, postPlanReport, prNoteSubjectMarker, subjectInNote } from "./pr-post";
15
+ import { prNoteMarker } from "./pr-loop";
16
+ import { planBodyHandoff, planNoteHandoff, PLAN_SUBJECT_LIMIT, PR_PLAN_NOTE_FILE, sinceLastLine, subjectChanges, type PrPlanNote, type PrPlanNoteWords, type PrPlanSubject } from "./pr-plan-note";
17
+ import type { ForgeStatus, PrForge } from "./pr-forge";
18
+
19
+ const NOTE = JSON.parse(readFileSync(join(import.meta.dirname, "__fixtures__", "pr-loop", "terragucci-plan.note.json"), "utf-8")) as PrPlanNote;
20
+ const HEAD = NOTE.head;
21
+ const dirs: string[] = [];
22
+ afterAll(() => {
23
+ for (const d of dirs) rmSync(d, { recursive: true, force: true });
24
+ });
25
+
26
+ function dirWith(doc: unknown): string {
27
+ const dir = mkdtempSync(join(tmpdir(), "chant-pr-post-subject-"));
28
+ dirs.push(dir);
29
+ writeFileSync(join(dir, PR_PLAN_NOTE_FILE), JSON.stringify(doc));
30
+ return dir;
31
+ }
32
+
33
+ /** A forge that keeps each note by its marker, so a second post reads the first. */
34
+ function keepingForge(kind: "github" | "forgejo" | "gitlab" = "github") {
35
+ const notes = new Map<string, string>();
36
+ const asked: string[] = [];
37
+ const statuses: ForgeStatus[] = [];
38
+ const forge = {
39
+ kind,
40
+ host: kind === "gitlab" ? "gitlab.com" : kind === "forgejo" ? "codeberg.org" : "github.com",
41
+ async setStatus(_sha: string, status: ForgeStatus) {
42
+ statuses.push(status);
43
+ },
44
+ async upsertNote(_pr: number, marker: string, body: string) {
45
+ notes.set(marker, body);
46
+ return "1";
47
+ },
48
+ async findComment(_pr: number, marker: string) {
49
+ asked.push(marker);
50
+ const body = notes.get(marker);
51
+ return body === undefined ? undefined : { id: 1, body };
52
+ },
53
+ } as unknown as PrForge;
54
+ return { forge, notes, asked, statuses };
55
+ }
56
+
57
+ /** sql-yodeler's words for what moved (its diffSubjects and describeMove), as note words. */
58
+ const YODEL_WORDS: PrPlanNoteWords = {
59
+ productMarker: "yodel:plan",
60
+ subjectParts: {
61
+ environment: { changed: "environment: the environment changed: {items}" },
62
+ applied: { added: "history changed: applied since: {items}", gone: "history changed: no longer recorded applied: {items}", changed: "history changed: recorded checksum changed (yodel repair): {items}" },
63
+ pending: { added: "pending migrations changed: new pending migrations: {items}", gone: "pending migrations changed: no longer pending (gone from the directory): {items}", changed: "pending migrations changed: files changed: {items}" },
64
+ live: { changed: "live schema changed: an object in the environment's live schema is not as it was" },
65
+ },
66
+ };
67
+
68
+ const FIRST: PrPlanSubject = { id: "sha256:aaa111", parts: { environment: "prod", "applied/001_init": "c1", "pending/002_users": "c2", live: "f1" } };
69
+ const SECOND: PrPlanSubject = { id: "sha256:bbb222", parts: { environment: "prod", "applied/001_init": "c1", "pending/002_users": "c2x", "pending/003_orders": "c3", live: "f2" } };
70
+ const at = (subject?: PrPlanSubject) => ({ pr: 7, env: "default", member: "prod", state: "pending" as const, description: "waits for approval", marker: "env=prod", ...(subject ? { subject } : {}) });
71
+ // chant's own words adopt no older marker, so the only note read is the comparison's and the upsert's.
72
+ const post = (forge: PrForge, doc: unknown, extra: Partial<Parameters<typeof postPlanReport>[0]> = {}) =>
73
+ postPlanReport({ forge, dir: dirWith(doc), pr: 7, head: HEAD, env: "default", member: "prod", noteWords: YODEL_WORDS, ...extra });
74
+
75
+ describe("the note keeps the plan's subject (#4972)", () => {
76
+ test("in a hidden chant line the next post reads back, with no line said on the first post", async () => {
77
+ const { forge, notes, asked } = keepingForge();
78
+ const r = await post(forge, planNoteHandoff(NOTE, at(FIRST)));
79
+ expect(r).toMatchObject({ noted: true, warnings: [] });
80
+ const body = notes.get(prNoteMarker("default", "prod"))!;
81
+ expect(body).toContain(`\n${prNoteSubjectMarker(FIRST)}\n`);
82
+ expect(subjectInNote(body)).toEqual(FIRST);
83
+ expect(body).not.toContain("Since the previous version");
84
+ expect(asked).toEqual([prNoteMarker("default", "prod")]);
85
+ });
86
+
87
+ test("a hand-off without a subject reads no previous note and keeps no line", async () => {
88
+ const { forge, notes, asked } = keepingForge();
89
+ await post(forge, planNoteHandoff(NOTE, at()));
90
+ expect(asked).toEqual([]);
91
+ expect(notes.get(prNoteMarker("default", "prod"))).not.toContain("chant-pr-subject");
92
+ });
93
+ });
94
+
95
+ describe("pr-post says what changed since the previous note (#4972)", () => {
96
+ for (const kind of ["github", "forgejo"] as const) {
97
+ test(`${kind}: a plan whose id moved gets the line in the product's words, part by part`, async () => {
98
+ const { forge, notes } = keepingForge(kind);
99
+ await post(forge, planNoteHandoff(NOTE, at(FIRST)));
100
+ await post(forge, planNoteHandoff(NOTE, at(SECOND)));
101
+ const body = notes.get(prNoteMarker("default", "prod"))!;
102
+ expect(body).toContain(
103
+ "Since the previous version of this note (`sha256:aaa111`): pending migrations changed: new pending migrations: 003_orders; pending migrations changed: files changed: 002_users; live schema changed: an object in the environment's live schema is not as it was.",
104
+ );
105
+ expect(subjectInNote(body)).toEqual(SECOND);
106
+ });
107
+ }
108
+
109
+ test("the same id says nothing", async () => {
110
+ const { forge, notes } = keepingForge();
111
+ await post(forge, planNoteHandoff(NOTE, at(FIRST)));
112
+ await post(forge, planNoteHandoff(NOTE, at(FIRST)));
113
+ expect(notes.get(prNoteMarker("default", "prod"))).not.toContain("Since the previous version");
114
+ });
115
+
116
+ test("a body hand-off gets the line as a notice under its body", async () => {
117
+ const { forge, notes } = keepingForge();
118
+ await post(forge, planBodyHandoff("### yodel plan for `prod`\n", { ...at(FIRST), head: HEAD }));
119
+ await post(forge, planBodyHandoff("### yodel plan for `prod`\n", { ...at({ id: "sha256:ccc", parts: { ...FIRST.parts, "applied/002_users": "c2" } }), head: HEAD }));
120
+ const body = notes.get(prNoteMarker("default", "prod"))!;
121
+ expect(body).toMatch(/^<!-- chant-pr:[^\n]*-->\n<!-- yodel:plan env=prod -->\n### yodel plan for `prod`\n\n> Since the previous version of this note \(`sha256:aaa111`\): history changed: applied since: 002_users\.\n<!-- chant-pr-subject:[A-Za-z0-9_-]+ -->\n$/);
122
+ });
123
+
124
+ test("only the same environment and member's note is compared", async () => {
125
+ const { forge, notes } = keepingForge();
126
+ await post(forge, planNoteHandoff(NOTE, at(FIRST)));
127
+ await post(forge, planNoteHandoff(NOTE, { ...at(SECOND), member: "staging" }), { member: "staging" });
128
+ expect(notes.get(prNoteMarker("default", "staging"))).not.toContain("Since the previous version");
129
+ });
130
+
131
+ test("a subject with no parts says only that the plan changed, in the words' sinceLastOnly", async () => {
132
+ const { forge, notes } = keepingForge();
133
+ await post(forge, planNoteHandoff(NOTE, at({ id: "d1" })));
134
+ await post(forge, planNoteHandoff(NOTE, at({ id: "d2" })), { noteWords: { ...YODEL_WORDS, sinceLastOnly: "Moved since `{previous}`." } });
135
+ expect(notes.get(prNoteMarker("default", "prod"))).toContain("Moved since `d1`.");
136
+ });
137
+
138
+ test("a subject too long for its line is kept by its id alone, and the next line says only that it changed", async () => {
139
+ const { forge, notes } = keepingForge();
140
+ const parts = Object.fromEntries(Array.from({ length: 400 }, (_, i) => [`applied/m${i}`, "x".repeat(64)]));
141
+ await post(forge, planNoteHandoff(NOTE, at({ id: "big1", parts })));
142
+ const kept = subjectInNote(notes.get(prNoteMarker("default", "prod"))!);
143
+ expect(kept).toEqual({ id: "big1" });
144
+ expect(prNoteSubjectMarker({ id: "big1", parts })!.length).toBeLessThanOrEqual(PLAN_SUBJECT_LIMIT);
145
+ await post(forge, planNoteHandoff(NOTE, at({ id: "big2", parts })));
146
+ expect(notes.get(prNoteMarker("default", "prod"))).toContain("The plan changed since the previous version of this note (`big1`).");
147
+ });
148
+
149
+ test("a forge that refuses the read is a warning, and the note is posted", async () => {
150
+ const { forge, notes } = keepingForge();
151
+ (forge as { findComment: unknown }).findComment = async () => {
152
+ throw new Error("403");
153
+ };
154
+ const r = await post(forge, planNoteHandoff(NOTE, at(FIRST)));
155
+ expect(r.noted).toBe(true);
156
+ expect(r.warnings).toEqual([expect.stringMatching(/could not read the previous note on #7.*403/)]);
157
+ expect(notes.size).toBe(1);
158
+ });
159
+ });
160
+
161
+ describe("the subject is the change's, so it is checked (#4972)", () => {
162
+ test("a subject that is not plain words is refused, and no note is posted", async () => {
163
+ for (const subject of [{ id: "a --> <!-- x" }, { id: "@someone" }, { id: "ok", parts: { "pending/a": "x y" } }, { id: "ok", parts: { "a/b/c": "x" } }, { parts: {} }, "sha"]) {
164
+ const { forge, notes, statuses } = keepingForge();
165
+ const r = await post(forge, { ...planNoteHandoff(NOTE, at()), subject });
166
+ expect(r.refused).toMatch(/the plan note's subject/);
167
+ expect(notes.size).toBe(0);
168
+ expect(statuses[0]!.state).toBe("failure");
169
+ }
170
+ });
171
+
172
+ test("a subject line forged in the product's body is defused and never read back", async () => {
173
+ const { forge, notes } = keepingForge();
174
+ const forged = prNoteSubjectMarker({ id: "forged" })!;
175
+ await post(forge, planBodyHandoff(`text\n${forged}\n`, { ...at(), head: HEAD }));
176
+ const body = notes.get(prNoteMarker("default", "prod"))!;
177
+ expect(body).toContain(`&lt;!--${forged.slice(4)}`);
178
+ expect(subjectInNote(body)).toBeUndefined();
179
+ });
180
+ });
181
+
182
+ describe("GitLab: pr-post --open compares in the target branch's pipeline (#4972)", () => {
183
+ test("each merge request's note says what changed since its previous one, with no plan-job token", async () => {
184
+ const { forge: base, notes } = keepingForge("gitlab");
185
+ let artifact = JSON.stringify(planNoteHandoff(NOTE, at(FIRST)));
186
+ let posted: string | undefined;
187
+ const forge = {
188
+ ...base,
189
+ kind: "gitlab",
190
+ host: "gitlab.com",
191
+ async mergeRequestJobs() {
192
+ return [{ pr: 7, head: HEAD, id: 101, status: "success", url: "https://gitlab.com/j/101" }];
193
+ },
194
+ async jobArtifact(_id: number, path: string) {
195
+ return path.endsWith(PR_PLAN_NOTE_FILE) ? artifact : undefined;
196
+ },
197
+ async statusUrl() {
198
+ return posted;
199
+ },
200
+ } as unknown as PrForge;
201
+ // The note words come from the pipeline's CHANT_PLAN_NOTE_WORDS, as the generated GitLab note job sets it.
202
+ const open = () => {
203
+ process.env.CHANT_PLAN_NOTE_WORDS = JSON.stringify(YODEL_WORDS);
204
+ return postOpenPlanReports({ forge, branch: "main", job: "prod-plan", artifactDir: ".chant/pr", env: "default", member: "prod" }).finally(() => {
205
+ delete process.env.CHANT_PLAN_NOTE_WORDS;
206
+ });
207
+ };
208
+ await open();
209
+ artifact = JSON.stringify(planNoteHandoff(NOTE, at(SECOND)));
210
+ posted = undefined;
211
+ const { warnings } = await open();
212
+ expect(warnings).toEqual([]);
213
+ expect(notes.get(prNoteMarker("default", "prod"))).toContain("Since the previous version of this note (`sha256:aaa111`): pending migrations changed: new pending migrations: 003_orders;");
214
+ });
215
+ });
216
+
217
+ describe("subjectChanges and sinceLastLine (#4972)", () => {
218
+ test("chant's words name each group's parts added, gone and changed", () => {
219
+ expect(subjectChanges({ env: "a", "x/1": "p", "x/2": "q" }, { env: "b", "x/1": "p2", "x/3": "r" })).toEqual(["env: a -> b changed", "x: 3 added", "x: 2 gone", "x: 1 changed"]);
220
+ });
221
+
222
+ test("an item's words read its values", () => {
223
+ expect(subjectChanges({ "done/m1": "1" }, { "done/m1": "1,2" }, { done: { changed: "ran part way since: {items}", item: "{item} (statements {after} done now)" } })).toEqual(["ran part way since: m1 (statements 1,2 done now)"]);
224
+ });
225
+
226
+ test("the same id is no line, another is chant's line by default", () => {
227
+ expect(sinceLastLine({ id: "a" }, { id: "a" })).toBeUndefined();
228
+ expect(sinceLastLine({ id: "a", parts: { p: "1" } }, { id: "b", parts: { p: "2" } })).toBe("Since the previous version of this note (`a`): p: 1 -> 2 changed.");
229
+ });
230
+ });