@intentius/chant 0.124.55 → 0.124.56

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 (370) hide show
  1. package/dist/ci-agent.d.ts.map +1 -1
  2. package/dist/ci-agent.js +1 -2
  3. package/dist/ci-audit/attribution.d.ts +111 -0
  4. package/dist/ci-audit/attribution.d.ts.map +1 -0
  5. package/dist/ci-audit/attribution.js +194 -0
  6. package/dist/ci-audit/audit-log.d.ts +144 -0
  7. package/dist/ci-audit/audit-log.d.ts.map +1 -0
  8. package/dist/ci-audit/audit-log.js +149 -0
  9. package/dist/ci-audit/index.d.ts +12 -0
  10. package/dist/ci-audit/index.d.ts.map +1 -0
  11. package/dist/ci-audit/index.js +12 -0
  12. package/dist/ci-audit/unrecorded.d.ts +74 -0
  13. package/dist/ci-audit/unrecorded.d.ts.map +1 -0
  14. package/dist/ci-audit/unrecorded.js +95 -0
  15. package/dist/ci-audit.d.ts +3 -0
  16. package/dist/ci-audit.d.ts.map +1 -0
  17. package/dist/ci-audit.js +2 -0
  18. package/dist/ci-control.d.ts.map +1 -1
  19. package/dist/ci-control.js +3 -4
  20. package/dist/ci-estate.d.ts +24 -0
  21. package/dist/ci-estate.d.ts.map +1 -1
  22. package/dist/ci-estate.js +1 -2
  23. package/dist/ci-identity.d.ts.map +1 -1
  24. package/dist/ci-identity.js +2 -3
  25. package/dist/ci-job-guard.d.ts +139 -0
  26. package/dist/ci-job-guard.d.ts.map +1 -0
  27. package/dist/ci-job-guard.js +287 -0
  28. package/dist/ci-jobs.d.ts +7 -0
  29. package/dist/ci-jobs.d.ts.map +1 -1
  30. package/dist/ci-jobs.js +7 -0
  31. package/dist/ci-product-words.d.ts +7 -1
  32. package/dist/ci-product-words.d.ts.map +1 -1
  33. package/dist/ci-product-words.js +38 -4
  34. package/dist/ci-product.d.ts +35 -0
  35. package/dist/ci-product.d.ts.map +1 -1
  36. package/dist/ci-product.js +38 -3
  37. package/dist/ci-respond-run.d.ts +73 -0
  38. package/dist/ci-respond-run.d.ts.map +1 -0
  39. package/dist/ci-respond-run.js +247 -0
  40. package/dist/ci-respond.d.ts +189 -0
  41. package/dist/ci-respond.d.ts.map +1 -0
  42. package/dist/ci-respond.js +285 -0
  43. package/dist/cli/handlers/audit-log.d.ts +12 -0
  44. package/dist/cli/handlers/audit-log.d.ts.map +1 -0
  45. package/dist/cli/handlers/audit-log.js +81 -0
  46. package/dist/cli/handlers/build.d.ts +7 -0
  47. package/dist/cli/handlers/build.d.ts.map +1 -1
  48. package/dist/cli/handlers/build.js +81 -0
  49. package/dist/cli/handlers/ci-check-job.d.ts +11 -0
  50. package/dist/cli/handlers/ci-check-job.d.ts.map +1 -0
  51. package/dist/cli/handlers/ci-check-job.js +27 -0
  52. package/dist/cli/handlers/ci-check.d.ts.map +1 -1
  53. package/dist/cli/handlers/ci-check.js +38 -9
  54. package/dist/cli/handlers/e2e-safe-run.d.ts +12 -0
  55. package/dist/cli/handlers/e2e-safe-run.d.ts.map +1 -0
  56. package/dist/cli/handlers/e2e-safe-run.js +23 -0
  57. package/dist/cli/handlers/pr.d.ts +5 -0
  58. package/dist/cli/handlers/pr.d.ts.map +1 -1
  59. package/dist/cli/handlers/pr.js +259 -4
  60. package/dist/cli/handlers/run-wave.js +89 -1
  61. package/dist/cli/main.d.ts.map +1 -1
  62. package/dist/cli/main.js +56 -2
  63. package/dist/cli/parse-args.d.ts.map +1 -1
  64. package/dist/cli/parse-args.js +107 -0
  65. package/dist/cli/registry.d.ts +38 -0
  66. package/dist/cli/registry.d.ts.map +1 -1
  67. package/dist/components/adoption.d.ts.map +1 -1
  68. package/dist/components/adoption.js +1 -2
  69. package/dist/components/capability.d.ts +37 -0
  70. package/dist/components/capability.d.ts.map +1 -1
  71. package/dist/components/capability.js +25 -0
  72. package/dist/components/check-hooks.d.ts +4 -1
  73. package/dist/components/check-hooks.d.ts.map +1 -1
  74. package/dist/components/check-steps.d.ts +74 -0
  75. package/dist/components/check-steps.d.ts.map +1 -0
  76. package/dist/components/check-steps.js +102 -0
  77. package/dist/components/ci-services.d.ts.map +1 -1
  78. package/dist/components/ci-services.js +1 -2
  79. package/dist/components/driver.d.ts +12 -0
  80. package/dist/components/driver.d.ts.map +1 -1
  81. package/dist/components/driver.js +35 -1
  82. package/dist/components/groups.d.ts.map +1 -1
  83. package/dist/components/groups.js +4 -3
  84. package/dist/components/pr-env-ledger.d.ts.map +1 -1
  85. package/dist/components/pr-env-ledger.js +1 -2
  86. package/dist/components/pr-environment.d.ts +1 -1
  87. package/dist/components/pr-environment.d.ts.map +1 -1
  88. package/dist/components/pr-environment.js +1 -1
  89. package/dist/components/pr-pipeline.d.ts +129 -3
  90. package/dist/components/pr-pipeline.d.ts.map +1 -1
  91. package/dist/components/pr-pipeline.js +153 -17
  92. package/dist/components/pr-run.d.ts +8 -0
  93. package/dist/components/pr-run.d.ts.map +1 -1
  94. package/dist/components/pr-run.js +9 -4
  95. package/dist/components/product-pipeline.d.ts +35 -2
  96. package/dist/components/product-pipeline.d.ts.map +1 -1
  97. package/dist/components/product-pipeline.js +24 -5
  98. package/dist/components/wave-plan.d.ts +8 -0
  99. package/dist/components/wave-plan.d.ts.map +1 -1
  100. package/dist/components/wave-plan.js +18 -2
  101. package/dist/e2e-harness/bucket.d.ts +21 -0
  102. package/dist/e2e-harness/bucket.d.ts.map +1 -0
  103. package/dist/e2e-harness/bucket.js +61 -0
  104. package/dist/e2e-harness/claim.d.ts +70 -0
  105. package/dist/e2e-harness/claim.d.ts.map +1 -0
  106. package/dist/e2e-harness/claim.js +98 -0
  107. package/dist/e2e-harness/driver.d.ts +64 -0
  108. package/dist/e2e-harness/driver.d.ts.map +1 -0
  109. package/dist/e2e-harness/driver.js +243 -0
  110. package/dist/e2e-harness/forgejo.d.ts +77 -0
  111. package/dist/e2e-harness/forgejo.d.ts.map +1 -0
  112. package/dist/e2e-harness/forgejo.js +205 -0
  113. package/dist/e2e-harness/git.d.ts +32 -0
  114. package/dist/e2e-harness/git.d.ts.map +1 -0
  115. package/dist/e2e-harness/git.js +67 -0
  116. package/dist/e2e-harness/index.d.ts +22 -0
  117. package/dist/e2e-harness/index.d.ts.map +1 -0
  118. package/dist/e2e-harness/index.js +48 -0
  119. package/dist/e2e-harness/jobs.d.ts +8 -0
  120. package/dist/e2e-harness/jobs.d.ts.map +1 -0
  121. package/dist/e2e-harness/jobs.js +34 -0
  122. package/dist/e2e-harness/pins.d.ts +45 -0
  123. package/dist/e2e-harness/pins.d.ts.map +1 -0
  124. package/dist/e2e-harness/pins.js +44 -0
  125. package/dist/e2e-harness/ports.d.ts +113 -0
  126. package/dist/e2e-harness/ports.d.ts.map +1 -0
  127. package/dist/e2e-harness/ports.js +4 -0
  128. package/dist/e2e-harness/reports.d.ts +45 -0
  129. package/dist/e2e-harness/reports.d.ts.map +1 -0
  130. package/dist/e2e-harness/reports.js +57 -0
  131. package/dist/e2e-harness/safe-run-cli.d.ts +17 -0
  132. package/dist/e2e-harness/safe-run-cli.d.ts.map +1 -0
  133. package/dist/e2e-harness/safe-run-cli.js +43 -0
  134. package/dist/e2e-harness/safe-run.d.ts +61 -0
  135. package/dist/e2e-harness/safe-run.d.ts.map +1 -0
  136. package/dist/e2e-harness/safe-run.js +172 -0
  137. package/dist/e2e-harness/scenario.d.ts +11 -0
  138. package/dist/e2e-harness/scenario.d.ts.map +1 -0
  139. package/dist/e2e-harness/scenario.js +89 -0
  140. package/dist/e2e-harness/schemas/demo-cycle.schema.json +282 -0
  141. package/dist/e2e-harness/schemas/demo-step.schema.json +245 -0
  142. package/dist/e2e-harness/schemas/e2e-claim-row.schema.json +33 -0
  143. package/dist/e2e-harness/schemas/e2e-claims.schema.json +18 -0
  144. package/dist/e2e-harness/schemas/e2e-scenario.schema.json +150 -0
  145. package/dist/e2e-harness/schemas/index.d.ts +191 -0
  146. package/dist/e2e-harness/schemas/index.d.ts.map +1 -0
  147. package/dist/e2e-harness/schemas/index.js +62 -0
  148. package/dist/e2e-harness/stack.d.ts +124 -0
  149. package/dist/e2e-harness/stack.d.ts.map +1 -0
  150. package/dist/e2e-harness/stack.js +207 -0
  151. package/dist/e2e-harness/steps.d.ts +11 -0
  152. package/dist/e2e-harness/steps.d.ts.map +1 -0
  153. package/dist/e2e-harness/steps.js +22 -0
  154. package/dist/e2e-harness.d.ts +3 -0
  155. package/dist/e2e-harness.d.ts.map +1 -0
  156. package/dist/e2e-harness.js +2 -0
  157. package/dist/exit-codes.d.ts +7 -1
  158. package/dist/exit-codes.d.ts.map +1 -1
  159. package/dist/exit-codes.js +7 -1
  160. package/dist/lexicon.d.ts +46 -0
  161. package/dist/lexicon.d.ts.map +1 -1
  162. package/dist/lifecycle/gate-ledger.d.ts.map +1 -1
  163. package/dist/lifecycle/gate-ledger.js +2 -3
  164. package/dist/lifecycle/gated-request.d.ts.map +1 -1
  165. package/dist/lifecycle/gated-request.js +1 -2
  166. package/dist/lifecycle/query.d.ts.map +1 -1
  167. package/dist/lifecycle/query.js +4 -5
  168. package/dist/lifecycle/release-catalog.d.ts.map +1 -1
  169. package/dist/lifecycle/release-catalog.js +1 -1
  170. package/dist/lifecycle/release-ledger.d.ts.map +1 -1
  171. package/dist/lifecycle/release-ledger.js +1 -2
  172. package/dist/lifecycle/release-status.d.ts +20 -0
  173. package/dist/lifecycle/release-status.d.ts.map +1 -1
  174. package/dist/lifecycle/release-status.js +1 -22
  175. package/dist/lifecycle/wave-plans.d.ts +23 -7
  176. package/dist/lifecycle/wave-plans.d.ts.map +1 -1
  177. package/dist/lifecycle/wave-plans.js +9 -2
  178. package/dist/op/approval-status.d.ts +103 -0
  179. package/dist/op/approval-status.d.ts.map +1 -0
  180. package/dist/op/approval-status.js +197 -0
  181. package/dist/op/index.d.ts +1 -1
  182. package/dist/op/index.d.ts.map +1 -1
  183. package/dist/op/index.js +1 -1
  184. package/dist/op/op-waves-capability-spec.d.ts +8 -0
  185. package/dist/op/op-waves-capability-spec.d.ts.map +1 -1
  186. package/dist/op/op-waves-capability-spec.js +21 -0
  187. package/dist/op/op-waves-release.d.ts.map +1 -1
  188. package/dist/op/op-waves-release.js +1 -2
  189. package/dist/op/op-waves-run.d.ts +7 -2
  190. package/dist/op/op-waves-run.d.ts.map +1 -1
  191. package/dist/op/op-waves-run.js +96 -4
  192. package/dist/op/op-waves-stand-down.d.ts.map +1 -1
  193. package/dist/op/op-waves-stand-down.js +1 -2
  194. package/dist/op/op-waves.d.ts +35 -0
  195. package/dist/op/op-waves.d.ts.map +1 -1
  196. package/dist/op/op-waves.js +46 -6
  197. package/dist/op/runtime.d.ts +4 -0
  198. package/dist/op/runtime.d.ts.map +1 -1
  199. package/dist/op/runtime.js +2 -3
  200. package/dist/otlp.d.ts.map +1 -1
  201. package/dist/otlp.js +1 -2
  202. package/dist/parallelism.d.ts +17 -0
  203. package/dist/parallelism.d.ts.map +1 -0
  204. package/dist/parallelism.js +33 -0
  205. package/dist/policy-source.d.ts.map +1 -1
  206. package/dist/policy-source.js +1 -2
  207. package/dist/pr-forge.d.ts.map +1 -1
  208. package/dist/pr-forge.js +56 -39
  209. package/dist/pr-loop.d.ts +9 -0
  210. package/dist/pr-loop.d.ts.map +1 -1
  211. package/dist/pr-loop.js +22 -3
  212. package/dist/pr-merge.d.ts +64 -0
  213. package/dist/pr-merge.d.ts.map +1 -0
  214. package/dist/pr-merge.js +78 -0
  215. package/dist/pr-post.d.ts +70 -1
  216. package/dist/pr-post.d.ts.map +1 -1
  217. package/dist/pr-post.js +116 -5
  218. package/dist/pr-report-url.d.ts.map +1 -1
  219. package/dist/pr-report-url.js +1 -2
  220. package/dist/release-floors.d.ts +1 -1
  221. package/dist/release-floors.js +1 -1
  222. package/dist/run-event.d.ts.map +1 -1
  223. package/dist/run-event.js +1 -2
  224. package/dist/scheduled-op-cloud.d.ts +32 -0
  225. package/dist/scheduled-op-cloud.d.ts.map +1 -0
  226. package/dist/scheduled-op-cloud.js +69 -0
  227. package/dist/wave-runtime.mjs +340 -327
  228. package/dist/workspace/ci-lock-grain.d.ts +5 -0
  229. package/dist/workspace/ci-lock-grain.d.ts.map +1 -1
  230. package/dist/workspace/ci-lock-grain.js +1 -7
  231. package/dist/workspace/ci-run.d.ts.map +1 -1
  232. package/dist/workspace/ci-run.js +5 -1
  233. package/dist/workspace/pr-report.schema.json +5 -0
  234. package/package.json +1 -1
  235. package/src/ci-agent.ts +1 -2
  236. package/src/ci-audit/__fixtures__/cloudtrail-lookup-bucket.json +37 -0
  237. package/src/ci-audit/__fixtures__/cloudtrail-writes-window.json +59 -0
  238. package/src/ci-audit/attribution.ts +260 -0
  239. package/src/ci-audit/audit-log.ts +220 -0
  240. package/src/ci-audit/ci-audit.test.ts +175 -0
  241. package/src/ci-audit/index.ts +15 -0
  242. package/src/ci-audit/unrecorded.ts +140 -0
  243. package/src/ci-audit.ts +2 -0
  244. package/src/ci-control.ts +3 -4
  245. package/src/ci-estate.ts +1 -2
  246. package/src/ci-identity-database.test.ts +2 -3
  247. package/src/ci-identity-targets.test.ts +1 -3
  248. package/src/ci-identity.ts +2 -3
  249. package/src/ci-job-guard.test.ts +154 -0
  250. package/src/ci-job-guard.ts +319 -0
  251. package/src/ci-jobs.ts +8 -0
  252. package/src/ci-product-comment-targets.test.ts +45 -0
  253. package/src/ci-product-words.ts +35 -6
  254. package/src/ci-product.ts +55 -3
  255. package/src/ci-respond-run.ts +291 -0
  256. package/src/ci-respond.test.ts +264 -0
  257. package/src/ci-respond.ts +412 -0
  258. package/src/cli/handlers/audit-log.ts +74 -0
  259. package/src/cli/handlers/build-pr-loop-flags.test.ts +98 -0
  260. package/src/cli/handlers/build.ts +69 -0
  261. package/src/cli/handlers/ci-check-job.test.ts +114 -0
  262. package/src/cli/handlers/ci-check-job.ts +33 -0
  263. package/src/cli/handlers/ci-check.ts +38 -8
  264. package/src/cli/handlers/e2e-safe-run.ts +27 -0
  265. package/src/cli/handlers/pr-plan-comment-targets.test.ts +166 -0
  266. package/src/cli/handlers/pr-plan-flags.test.ts +161 -0
  267. package/src/cli/handlers/pr.ts +228 -4
  268. package/src/cli/handlers/run-wave.ts +79 -1
  269. package/src/cli/main.test.ts +1 -1
  270. package/src/cli/main.ts +56 -2
  271. package/src/cli/parse-args.ts +77 -0
  272. package/src/cli/registry.ts +38 -0
  273. package/src/components/adoption.ts +1 -2
  274. package/src/components/capability.ts +50 -0
  275. package/src/components/check-hooks.ts +4 -1
  276. package/src/components/check-steps.test.ts +70 -0
  277. package/src/components/check-steps.ts +158 -0
  278. package/src/components/ci-services.ts +1 -2
  279. package/src/components/driver.ts +35 -1
  280. package/src/components/groups.ts +4 -2
  281. package/src/components/plan-flags.test.ts +46 -0
  282. package/src/components/pr-comment-preview-targets.test.ts +34 -0
  283. package/src/components/pr-env-ledger.ts +1 -2
  284. package/src/components/pr-env-run-bundle.test.ts +0 -20
  285. package/src/components/pr-environment.ts +1 -1
  286. package/src/components/pr-pipeline-runners.test.ts +80 -0
  287. package/src/components/pr-pipeline.ts +232 -17
  288. package/src/components/pr-run.ts +17 -6
  289. package/src/components/pr-stale-job.test.ts +34 -0
  290. package/src/components/product-pipeline.ts +50 -8
  291. package/src/components/wave-plan.ts +18 -2
  292. package/src/e2e-harness/__project__/chant.config.ts +4 -0
  293. package/src/e2e-harness/__project__/src/compose.ts +33 -0
  294. package/src/e2e-harness/bucket.ts +69 -0
  295. package/src/e2e-harness/claim.test.ts +150 -0
  296. package/src/e2e-harness/claim.ts +140 -0
  297. package/src/e2e-harness/driver.test.ts +304 -0
  298. package/src/e2e-harness/driver.ts +294 -0
  299. package/src/e2e-harness/forgejo.ts +267 -0
  300. package/src/e2e-harness/git.ts +96 -0
  301. package/src/e2e-harness/index.ts +50 -0
  302. package/src/e2e-harness/jobs.ts +35 -0
  303. package/src/e2e-harness/pins.ts +52 -0
  304. package/src/e2e-harness/ports.ts +130 -0
  305. package/src/e2e-harness/reports.ts +79 -0
  306. package/src/e2e-harness/safe-run-cli.test.ts +102 -0
  307. package/src/e2e-harness/safe-run-cli.ts +54 -0
  308. package/src/e2e-harness/safe-run.ts +198 -0
  309. package/src/e2e-harness/scenario.ts +89 -0
  310. package/src/e2e-harness/schemas/README.md +62 -0
  311. package/src/e2e-harness/schemas/demo-cycle.schema.json +282 -0
  312. package/src/e2e-harness/schemas/demo-step.schema.json +245 -0
  313. package/src/e2e-harness/schemas/e2e-claim-row.schema.json +33 -0
  314. package/src/e2e-harness/schemas/e2e-claims.schema.json +18 -0
  315. package/src/e2e-harness/schemas/e2e-scenario.schema.json +150 -0
  316. package/src/e2e-harness/schemas/index.ts +220 -0
  317. package/src/e2e-harness/schemas.test.ts +145 -0
  318. package/src/e2e-harness/stack.test.ts +124 -0
  319. package/src/e2e-harness/stack.ts +306 -0
  320. package/src/e2e-harness/steps.ts +30 -0
  321. package/src/e2e-harness.ts +2 -0
  322. package/src/exit-codes.test.ts +3 -2
  323. package/src/exit-codes.ts +7 -1
  324. package/src/gate-decision-bundle.test.ts +0 -2
  325. package/src/lexicon.ts +48 -0
  326. package/src/lifecycle/control-entries-ledger.test.ts +2 -3
  327. package/src/lifecycle/gate-ledger.ts +2 -3
  328. package/src/lifecycle/gated-request.ts +1 -2
  329. package/src/lifecycle/query.ts +4 -5
  330. package/src/lifecycle/release-catalog.test.ts +3 -4
  331. package/src/lifecycle/release-catalog.ts +1 -1
  332. package/src/lifecycle/release-ledger.ts +1 -2
  333. package/src/lifecycle/release-status.test.ts +3 -4
  334. package/src/lifecycle/release-status.ts +1 -2
  335. package/src/lifecycle/wave-plans.ts +33 -5
  336. package/src/op/approval-status.test.ts +134 -0
  337. package/src/op/approval-status.ts +235 -0
  338. package/src/op/gate-review.test.ts +2 -1
  339. package/src/op/index.ts +2 -0
  340. package/src/op/op-waves-after-plan.test.ts +12 -0
  341. package/src/op/op-waves-capability-spec.ts +20 -0
  342. package/src/op/op-waves-parallelism.test.ts +89 -0
  343. package/src/op/op-waves-pull-request.test.ts +78 -1
  344. package/src/op/op-waves-release.ts +1 -2
  345. package/src/op/op-waves-run.ts +94 -4
  346. package/src/op/op-waves-stand-down.ts +1 -2
  347. package/src/op/op-waves.ts +63 -6
  348. package/src/op/runtime.ts +6 -3
  349. package/src/otlp.ts +1 -2
  350. package/src/parallelism.test.ts +38 -0
  351. package/src/parallelism.ts +33 -0
  352. package/src/policy-source.ts +1 -2
  353. package/src/pr-forge-bundle.test.ts +1 -1
  354. package/src/pr-forge.ts +54 -35
  355. package/src/pr-loop-bundle.test.ts +1 -1
  356. package/src/pr-loop-narrowed.test.ts +63 -0
  357. package/src/pr-loop.ts +30 -3
  358. package/src/pr-merge.test.ts +100 -0
  359. package/src/pr-merge.ts +105 -0
  360. package/src/pr-post-stale.test.ts +163 -0
  361. package/src/pr-post.ts +159 -5
  362. package/src/pr-report-url.ts +1 -2
  363. package/src/product-subpaths-plain-node.e2e.test.ts +1 -0
  364. package/src/release-floors.ts +1 -1
  365. package/src/run-event.ts +1 -2
  366. package/src/scheduled-op-cloud.ts +68 -0
  367. package/src/stage-hooks-bundle.test.ts +0 -2
  368. package/src/workspace/ci-lock-grain.ts +1 -2
  369. package/src/workspace/ci-run.ts +5 -1
  370. package/src/workspace/pr-report.schema.json +5 -0
package/src/pr-forge.ts CHANGED
@@ -675,6 +675,11 @@ export class ForgeApiError extends Error {
675
675
  }
676
676
  }
677
677
 
678
+ /** The forge answered 404. */
679
+ const notFound = (err: unknown): boolean => err instanceof ForgeApiError && err.status === 404;
680
+
681
+ const byNumber = (a: { number: number }, b: { number: number }): number => a.number - b.number;
682
+
678
683
  const DEFAULT_HOST: Record<ForgeKind, string | null> = { github: "github.com", gitlab: "gitlab.com", forgejo: null };
679
684
 
680
685
  /** `<forge>:<login>` on the forge's default host, `<forge>@<host>:<login>` elsewhere, lower-cased (#3163's form). */
@@ -900,6 +905,22 @@ function githubShaped(kind: "github" | "forgejo", opts: GithubForgeOptions): Git
900
905
  const url = str(v.html_url);
901
906
  return { number, title: str(v.title) ?? "", body: str(v.body) ?? "", head: head ?? "", ...(base ? { base } : {}), ...(url ? { url } : {}) };
902
907
  };
908
+ /**
909
+ * The open pull requests; GitHub takes `filter` server-side, Forgejo's list
910
+ * takes none and is matched by the caller. Forgejo answers the list with
911
+ * 404 while it still counts the repository as empty (just after the first
912
+ * push). That is no pull request; a repository that is really missing 404s
913
+ * on its own read too.
914
+ */
915
+ const openPulls = async (filter: string): Promise<unknown[]> => {
916
+ try {
917
+ return await api.list(`${repo}/pulls?state=open${kind === "github" ? filter : ""}`, pageParams, pageSize);
918
+ } catch (err) {
919
+ if (kind !== "forgejo" || !notFound(err)) throw err;
920
+ await api.call("GET", repo);
921
+ return [];
922
+ }
923
+ };
903
924
  /** A pull request whose head branch lives in this repository, not a fork. */
904
925
  const fromHere = (v: unknown, head: string): boolean => {
905
926
  if (!isObject(v) || !isObject(v.head) || v.head.ref !== head) return false;
@@ -950,7 +971,7 @@ function githubShaped(kind: "github" | "forgejo", opts: GithubForgeOptions): Git
950
971
  try {
951
972
  b = await api.call("GET", `${repo}/branches/${branch.split("/").map(encodeURIComponent).join("/")}`);
952
973
  } catch (err) {
953
- if (err instanceof ForgeApiError && err.status === 404) return undefined;
974
+ if (notFound(err)) return undefined;
954
975
  throw err;
955
976
  }
956
977
  // GitHub names the commit `sha`, Forgejo `id`.
@@ -1084,7 +1105,7 @@ function githubShaped(kind: "github" | "forgejo", opts: GithubForgeOptions): Git
1084
1105
  const got = await api.call("GET", `${repo}/pulls/${pr}`);
1085
1106
  return isObject(got) && got.state === "open" && got.merged !== true;
1086
1107
  } catch (err) {
1087
- if (err instanceof ForgeApiError && err.status === 404) return false;
1108
+ if (notFound(err)) return false;
1088
1109
  throw err;
1089
1110
  }
1090
1111
  },
@@ -1094,7 +1115,7 @@ function githubShaped(kind: "github" | "forgejo", opts: GithubForgeOptions): Git
1094
1115
  const pr = await api.call("GET", `${repo}/commits/${sha}/pull`);
1095
1116
  return isObject(pr) && typeof pr.number === "number" ? pr.number : null;
1096
1117
  } catch (err) {
1097
- if (err instanceof ForgeApiError && err.status === 404) return null;
1118
+ if (notFound(err)) return null;
1098
1119
  throw err;
1099
1120
  }
1100
1121
  }
@@ -1121,33 +1142,12 @@ function githubShaped(kind: "github" | "forgejo", opts: GithubForgeOptions): Git
1121
1142
  // GitHub filters by `owner:branch` server-side; Forgejo's list takes no
1122
1143
  // head filter, so every open page is read and matched here.
1123
1144
  const owner = opts.repo.split("/")[0];
1124
- const query = kind === "github" ? `?state=open&head=${encodeURIComponent(`${owner}:${head}`)}` : "?state=open";
1125
- let listed: unknown[];
1126
- try {
1127
- listed = await api.list(`${repo}/pulls${query}`, pageParams, pageSize);
1128
- } catch (err) {
1129
- // Forgejo answers the list with 404 while it still counts the repository
1130
- // as empty (just after the first push). That is no pull request; a
1131
- // repository that is really missing 404s on its own read too.
1132
- if (kind !== "forgejo" || !(err instanceof ForgeApiError) || err.status !== 404) throw err;
1133
- await api.call("GET", repo);
1134
- return undefined;
1135
- }
1136
- const open = listed.filter((p) => fromHere(p, head));
1137
- return open.map(pull).filter((p): p is PullRequest => p !== undefined).sort((a, b) => a.number - b.number)[0];
1145
+ const open = (await openPulls(`&head=${encodeURIComponent(`${owner}:${head}`)}`)).filter((p) => fromHere(p, head));
1146
+ return open.map(pull).filter((p): p is PullRequest => p !== undefined).sort(byNumber)[0];
1138
1147
  },
1139
1148
  async openPullRequests(base) {
1140
1149
  // GitHub filters by base server-side; Forgejo's list is matched here.
1141
- const query = kind === "github" ? `?state=open&base=${encodeURIComponent(base)}` : "?state=open";
1142
- let listed: unknown[];
1143
- try {
1144
- listed = await api.list(`${repo}/pulls${query}`, pageParams, pageSize);
1145
- } catch (err) {
1146
- // Forgejo answers 404 while it counts the repository as empty, as findPullRequest notes.
1147
- if (kind !== "forgejo" || !(err instanceof ForgeApiError) || err.status !== 404) throw err;
1148
- await api.call("GET", repo);
1149
- return [];
1150
- }
1150
+ const listed = await openPulls(`&base=${encodeURIComponent(base)}`);
1151
1151
  const out: OpenPullRequest[] = [];
1152
1152
  for (const v of listed) {
1153
1153
  if (!isObject(v) || !isObject(v.head) || !isObject(v.base)) continue;
@@ -1160,7 +1160,7 @@ function githubShaped(kind: "github" | "forgejo", opts: GithubForgeOptions): Git
1160
1160
  const author = isObject(v.user) ? str(v.user.login) : undefined;
1161
1161
  out.push({ number, head, headSha, base: into, ...(baseSha ? { baseSha } : {}), ...(author ? { author } : {}), fork: !fromHere(v, head) });
1162
1162
  }
1163
- return out.sort((a, b) => a.number - b.number);
1163
+ return out.sort(byNumber);
1164
1164
  },
1165
1165
  branchHead: branchTip,
1166
1166
  async pullRequestHead(pr) {
@@ -1168,7 +1168,7 @@ function githubShaped(kind: "github" | "forgejo", opts: GithubForgeOptions): Git
1168
1168
  try {
1169
1169
  got = await api.call("GET", `${repo}/pulls/${pr}`);
1170
1170
  } catch (err) {
1171
- if (err instanceof ForgeApiError && err.status === 404) return undefined;
1171
+ if (notFound(err)) return undefined;
1172
1172
  throw err;
1173
1173
  }
1174
1174
  if (!isObject(got) || !isObject(got.head)) return undefined;
@@ -1336,6 +1336,8 @@ export function gitlabForge(opts: GitlabForgeOptions): PrForge {
1336
1336
  return { id, body: str(v.description) ?? "", ...(created ? { created } : {}), ...(str(v.title) !== undefined ? { title: str(v.title) } : {}), ...(author ? { author } : {}), ...(url ? { url } : {}) };
1337
1337
  };
1338
1338
  const notes = `${project}/merge_requests`;
1339
+ /** The open merge requests from (`source`) or into (`target`) `branch`. */
1340
+ const openMrs = (end: "source" | "target", branch: string): Promise<unknown[]> => api.list(`${notes}?state=opened&${end}_branch=${encodeURIComponent(branch)}`, pageParams, 100);
1339
1341
  const mergeRequest = (v: unknown): PullRequest | undefined => {
1340
1342
  if (!isObject(v)) return undefined;
1341
1343
  const number = num(v.iid);
@@ -1457,7 +1459,7 @@ export function gitlabForge(opts: GitlabForgeOptions): PrForge {
1457
1459
  try {
1458
1460
  member = await api.call("GET", `${project}/members/all/${id}`);
1459
1461
  } catch (err) {
1460
- if (err instanceof ForgeApiError && err.status === 404) return `${login} is not a member`;
1462
+ if (notFound(err)) return `${login} is not a member`;
1461
1463
  return `could not read ${login}'s access (${err instanceof Error ? err.message : String(err)})`;
1462
1464
  }
1463
1465
  const level = isObject(member) ? (num(member.access_level) ?? 0) : 0;
@@ -1510,7 +1512,7 @@ export function gitlabForge(opts: GitlabForgeOptions): PrForge {
1510
1512
  const got = await api.call("GET", `${notes}/${mr}`);
1511
1513
  return isObject(got) && got.state === "opened";
1512
1514
  } catch (err) {
1513
- if (err instanceof ForgeApiError && err.status === 404) return false;
1515
+ if (notFound(err)) return false;
1514
1516
  throw err;
1515
1517
  }
1516
1518
  },
@@ -1528,13 +1530,13 @@ export function gitlabForge(opts: GitlabForgeOptions): PrForge {
1528
1530
  return mergeRequest(made) ?? { number: 0, title, body: spec.body, head: spec.head, base };
1529
1531
  },
1530
1532
  async findPullRequest(head) {
1531
- const open = await api.list(`${notes}?state=opened&source_branch=${encodeURIComponent(head)}`, pageParams, 100);
1533
+ const open = await openMrs("source", head);
1532
1534
  return open
1533
1535
  // A fork's merge request into this project has another source project.
1534
1536
  .filter((m) => isObject(m) && m.source_branch === head && (m.source_project_id === undefined || m.source_project_id === m.project_id))
1535
1537
  .map(mergeRequest)
1536
1538
  .filter((m): m is PullRequest => m !== undefined)
1537
- .sort((a, b) => a.number - b.number)[0];
1539
+ .sort(byNumber)[0];
1538
1540
  },
1539
1541
  async scheduledRuns(q) {
1540
1542
  // The variable the gitlab lexicon's generated Op file selects a scheduled job by.
@@ -1549,9 +1551,26 @@ export function gitlabForge(opts: GitlabForgeOptions): PrForge {
1549
1551
  const last = newestTime(pipelines.map((p) => (isObject(p) ? str(p.created_at) : undefined)));
1550
1552
  return { ...(last ? { last } : {}), ...(ours.length === 0 ? { noSchedule: true } : {}) };
1551
1553
  },
1554
+ // #4628: the open merge requests into `base`, for the stale marking a push to it runs.
1555
+ async openPullRequests(base) {
1556
+ const out: OpenPullRequest[] = [];
1557
+ for (const m of await openMrs("target", base)) {
1558
+ if (!isObject(m)) continue;
1559
+ const number = num(m.iid);
1560
+ const head = str(m.source_branch);
1561
+ const headSha = str(m.sha);
1562
+ if (number === undefined || !head || !headSha || str(m.target_branch) !== base) continue;
1563
+ const refs = isObject(m.diff_refs) ? m.diff_refs : undefined;
1564
+ const baseSha = refs ? str(refs.base_sha) : undefined;
1565
+ const author = isObject(m.author) ? str(m.author.username) : undefined;
1566
+ const fork = num(m.source_project_id) !== undefined && num(m.source_project_id) !== num(m.target_project_id);
1567
+ out.push({ number, head, headSha, base, ...(baseSha ? { baseSha } : {}), ...(author ? { author } : {}), fork });
1568
+ }
1569
+ return out.sort(byNumber);
1570
+ },
1552
1571
  async mergeRequestJobs(branch, job) {
1553
1572
  const out: MergeRequestJob[] = [];
1554
- const open = await api.list(`${notes}?state=opened&target_branch=${encodeURIComponent(branch)}`, pageParams, 100);
1573
+ const open = await openMrs("target", branch);
1555
1574
  for (const m of open) {
1556
1575
  if (!isObject(m)) continue;
1557
1576
  const iid = num(m.iid);
@@ -1579,7 +1598,7 @@ export function gitlabForge(opts: GitlabForgeOptions): PrForge {
1579
1598
  try {
1580
1599
  got = await api.call("GET", `${notes}/${mr}`);
1581
1600
  } catch (err) {
1582
- if (err instanceof ForgeApiError && err.status === 404) return undefined;
1601
+ if (notFound(err)) return undefined;
1583
1602
  throw err;
1584
1603
  }
1585
1604
  if (!isObject(got)) return undefined;
@@ -45,7 +45,7 @@ describe("the pull-request loop bundle", () => {
45
45
  "packages/core/src/pr-migrations.ts",
46
46
  // The Pulumi URN rule (#4548, the may-appear rows): no imports.
47
47
  "packages/core/src/pulumi-urn.ts",
48
- // The next-release floor constant (#4480): no imports.
48
+ // The next-release floor constant (#4480, #4626's narrowed floor): no imports.
49
49
  "packages/core/src/release-floors.ts",
50
50
  ]);
51
51
  // About 89 KB today, after the hooks line (#4288), the blocked set (#4291), the scheduled-run reads (#4289) the requires checks (#4301) and the migration notes (#4407), about 97 KB; with the group lines (#4446), about 100 KB.
@@ -0,0 +1,63 @@
1
+ /**
2
+ * A narrowed plan in the note and the status (#4626): each narrowed
3
+ * member's row lists its addresses, a line says the plan on merge is of
4
+ * each member whole, and the status counts the narrowed members, in the
5
+ * product's words. Written for the ci-products post-merge pass.
6
+ */
7
+ import { describe, expect, test } from "vitest";
8
+ import { composeChangeSet } from "./change-set";
9
+ import { memberCounts, PR_REPORT_SCHEMA_ID, prStatusDescription, renderPrNote, type PrReport } from "./pr-loop";
10
+ import { assertProductWords } from "./ci-product-words";
11
+ import { PLYWOOD } from "./__fixtures__/product-words";
12
+
13
+ const hex = (n: number) => n.toString(16).padStart(64, "0");
14
+ const URN = "urn:pulumi:dev::shop::aws:s3/bucket:Bucket::site";
15
+ const doc = composeChangeSet([
16
+ {
17
+ member: { member: "shop-dev", lexicon: "pulumi", planner: "pulumi", status: "planned", planDigest: `jcs1-sha256:${hex(1)}`, holes: [] },
18
+ entries: [{ member: "shop-dev", lexicon: "pulumi", planner: "pulumi", address: URN, type: "aws:s3/bucket:Bucket", action: "create", attributes: [] }],
19
+ },
20
+ ]);
21
+ const report: PrReport = {
22
+ $schema: PR_REPORT_SCHEMA_ID,
23
+ contract: 1,
24
+ stage: "plan",
25
+ pr: 12,
26
+ env: "dev",
27
+ base: "1".repeat(40),
28
+ head: "2".repeat(40),
29
+ op: "pulumi-up",
30
+ gate: "up",
31
+ digest: doc.digest,
32
+ status: "planned",
33
+ approval: { status: "pending" },
34
+ selection: { changed: ["shop"], dependents: [], unclaimed: [], indeterminate: [], waves: [["shop"]] },
35
+ members: [{ member: "shop-dev", component: "shop", planDigest: `jcs1-sha256:${hex(1)}`, status: "planned", counts: memberCounts(doc, "shop-dev") }],
36
+ changeSet: doc,
37
+ };
38
+ const plywood = assertProductWords(PLYWOOD);
39
+
40
+ describe("a narrowed plan (#4626)", () => {
41
+ const narrowed: PrReport = { ...report, narrowed: { shop: [URN] } };
42
+
43
+ test("the member's row lists the addresses", () => {
44
+ const note = renderPrNote(narrowed, { words: plywood });
45
+ const row = note.split("\n").find((l) => l.startsWith("| `shop-dev`"))!;
46
+ expect(row).toContain(`narrowed to \`${URN}\``);
47
+ });
48
+
49
+ test("a line says so in the product's words, and that the update on merge plans each stack whole", () => {
50
+ const note = renderPrNote(narrowed, { words: plywood });
51
+ expect(note).toContain(`This preview was narrowed with \`--target\`: \`shop\` to \`${URN}\`. The approval binds the narrowed plan. On merge each stack is previewed whole.`);
52
+ });
53
+
54
+ test("the status counts the narrowed members", () => {
55
+ expect(prStatusDescription(narrowed, plywood)).toMatch(/^1 stack \(1 narrowed with --target\): /);
56
+ expect(prStatusDescription(narrowed, plywood).length).toBeLessThanOrEqual(140);
57
+ });
58
+
59
+ test("a plan that was not narrowed says nothing of it", () => {
60
+ expect(renderPrNote(report, { words: plywood })).not.toContain("narrowed");
61
+ expect(prStatusDescription(report, plywood)).not.toContain("narrowed");
62
+ });
63
+ });
package/src/pr-loop.ts CHANGED
@@ -102,10 +102,13 @@ export const PR_REPORT_NOT_STARTED_FLOOR = "0.124.54";
102
102
  export const PR_REPORT_GROUPS_FLOOR = "0.124.54";
103
103
 
104
104
  /** The first chant whose report can carry `lockTargets` (#4552). */
105
- export const PR_REPORT_LOCK_TARGETS_FLOOR = NEXT_RELEASE;
105
+ export const PR_REPORT_LOCK_TARGETS_FLOOR = "0.124.55";
106
+
107
+ /** The first chant whose report carries {@link PrReport.narrowed} (#4626). */
108
+ export const PR_REPORT_NARROWED_FLOOR = NEXT_RELEASE;
106
109
 
107
110
  /** The first chant whose report carries {@link PrReport.crossStackEdges} (#4617). */
108
- export const PR_REPORT_CROSS_STACK_FLOOR = NEXT_RELEASE;
111
+ export const PR_REPORT_CROSS_STACK_FLOOR = "0.124.55";
109
112
 
110
113
  /** The gate a pull request's apply waits on, unless the pipeline names another. */
111
114
  export const PR_APPLY_GATE = "pr-apply";
@@ -593,6 +596,13 @@ export interface PrReport {
593
596
  * {@link PR_REPORT_LOCK_TARGETS_FLOOR}.
594
597
  */
595
598
  lockTargets?: string[];
599
+ /**
600
+ * Plan stage (#4626): the addresses each component's plan was narrowed to
601
+ * with `pr-plan --target`, from the command line or the comment, by
602
+ * component. The note and the status say so. Absent when nothing was
603
+ * narrowed. Added within contract 1 by chant {@link PR_REPORT_NARROWED_FLOOR}.
604
+ */
605
+ narrowed?: Record<string, string[]>;
596
606
  /**
597
607
  * The stacks outside the workspace a member reads (#4617, a Pulumi
598
608
  * `StackReference` no step here applies, #4547), each marked stale,
@@ -1109,6 +1119,8 @@ export function renderPrNote(report: PrReport, options: { limit?: number; approv
1109
1119
  (m.groupOutcome === "partial" ? ", applied in part" : m.groupOutcome === "no-changes" ? ", no changes" : "") +
1110
1120
  (m.inputsMoved?.length ? `, inputs from ${m.inputsMoved.join(", ")} moved` : "") +
1111
1121
  (placeholders.length > 0 ? `, previewed with placeholders for ${placeholders.map(code).join(", ")}` : "") +
1122
+ // #4626: a member whose plan was narrowed says to what.
1123
+ (report.narrowed?.[m.component] ? `, narrowed to ${report.narrowed[m.component]!.map(code).join(", ")}` : "") +
1112
1124
  (holes.length > 0 ? `; ${holes.length > 2 ? `${holes[0]}; and ${holes.length - 1} more, listed under Holes` : holes.join("; ")}` : "");
1113
1125
  // A member whose plan has holes and no entries says nothing about what
1114
1126
  // it changes, so its counts are unknown, not zero (#4084).
@@ -1120,6 +1132,7 @@ export function renderPrNote(report: PrReport, options: { limit?: number; approv
1120
1132
  out.push("", `A ${noun} whose inputs moved ${words.verbs.apply[1]} the plan it was approved with. The next run plans it against the new values.`);
1121
1133
  }
1122
1134
  }
1135
+ out.push(...narrowedLines(report, words));
1123
1136
  out.push(...mayAppearLines(report, options.applyWord ?? words.stages.apply));
1124
1137
  const blocked = blockedLine(report.members);
1125
1138
  if (blocked) out.push("", blocked);
@@ -1220,6 +1233,18 @@ function memberDetailSections(report: PrReport, room: number, noun: string): str
1220
1233
  return out.length > 0 ? out.join("\n") + "\n" : "";
1221
1234
  }
1222
1235
 
1236
+ /**
1237
+ * The note's line for a narrowed plan (#4626): which of the plan's
1238
+ * components were narrowed and to how many addresses, and that the plan on
1239
+ * merge is of each whole, in the product's words.
1240
+ */
1241
+ function narrowedLines(report: PrReport, words: CiProductWords): string[] {
1242
+ const narrowed = Object.entries(report.narrowed ?? {}).filter(([, a]) => a.length > 0);
1243
+ if (narrowed.length === 0) return [];
1244
+ const each = narrowed.map(([c, a]) => `${code(c)} to ${a.map(code).join(", ")}`).join("; ");
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.`];
1246
+ }
1247
+
1223
1248
  /** A commit status's description, at most 140 characters (GitHub's limit). */
1224
1249
  export function prStatusDescription(report: PrReport, words: CiProductWords = activeProductWords()): string {
1225
1250
  const n = report.members.length;
@@ -1236,7 +1261,9 @@ export function prStatusDescription(report: PrReport, words: CiProductWords = ac
1236
1261
  break;
1237
1262
  case "planned": {
1238
1263
  const unknown = report.members.filter((m) => memberHoleReasons(report, m.member).length > 0).length;
1239
- text = `${many(n)}: ${sum.change} to change, ${sum.destroy} to destroy or replace${unknown > 0 ? `, ${unknown} with holes` : ""}${digest}`;
1264
+ // #4626: a narrowed plan says so in its status.
1265
+ const narrowed = Object.values(report.narrowed ?? {}).filter((a) => a.length > 0).length;
1266
+ 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}`;
1240
1267
  break;
1241
1268
  }
1242
1269
  case "plan-failed":
@@ -0,0 +1,100 @@
1
+ /**
2
+ * #4636: `pr-apply --before-merge --merge auto` merges the pull request once
3
+ * its update applied and the records are pushed, with the head checked
4
+ * again, on fake GitHub, GitLab and Forgejo forges. A refusal is posted and
5
+ * never applies again.
6
+ */
7
+
8
+ import { describe, expect, test } from "vitest";
9
+ import { parseArgs } from "./cli/parse-args";
10
+ import { prLoopJobs } from "./components/pr-pipeline";
11
+ import { ciLoopOptions } from "./workspace/ci-run";
12
+ import { mergeAfterApply, mergeTokenEnv, prMergeMarker } from "./pr-merge";
13
+ import type { MergeOptions, MergeResult, PullRequestHead } from "./pr-forge";
14
+
15
+ const HEAD = "a".repeat(40);
16
+
17
+ function fakeForge(kind: "github" | "gitlab" | "forgejo", o: { head?: PullRequestHead; merge?: MergeResult | Error } = {}) {
18
+ const merges: Array<{ pr: number; opts: MergeOptions }> = [];
19
+ const comments: Array<{ pr: number; marker: string; body: string }> = [];
20
+ const forge = {
21
+ kind,
22
+ async pullRequestHead() {
23
+ return o.head ?? { sha: HEAD, branch: "feature", open: true, sameRepo: true };
24
+ },
25
+ async mergePullRequest(pr: number, opts: MergeOptions) {
26
+ merges.push({ pr, opts });
27
+ if (o.merge instanceof Error) throw o.merge;
28
+ return o.merge ?? { merged: true, sha: "b".repeat(40) };
29
+ },
30
+ async upsertComment(pr: number, marker: string, body: string) {
31
+ comments.push({ pr, marker, body });
32
+ return { action: "created" as const, id: 1 };
33
+ },
34
+ };
35
+ return { forge, merges, comments };
36
+ }
37
+
38
+ const pushed = { pushed: true };
39
+
40
+ describe("the merge after an update before merge (#4636)", () => {
41
+ for (const kind of ["github", "gitlab", "forgejo"] as const) {
42
+ test(`merges only the head that applied, once the records are pushed (${kind})`, async () => {
43
+ const { forge, merges, comments } = fakeForge(kind);
44
+ expect(await mergeAfterApply({ forge, pr: 7, head: HEAD, method: "squash", recorded: pushed })).toEqual({ merged: true, sha: "b".repeat(40) });
45
+ expect(merges).toEqual([{ pr: 7, opts: { sha: HEAD, method: "squash" } }]);
46
+ expect(comments).toEqual([]);
47
+ });
48
+
49
+ test(`a refused merge posts the reason and is never retried (${kind})`, async () => {
50
+ const { forge, merges, comments } = fakeForge(kind, { merge: { merged: false, reason: 'Required status check "ci" is expected' } });
51
+ const r = await mergeAfterApply({ forge, pr: 7, head: HEAD, recorded: pushed });
52
+ expect(r).toMatchObject({ merged: false, posted: true, reason: expect.stringContaining("Required status check") });
53
+ expect(merges).toHaveLength(1);
54
+ expect(comments[0]).toMatchObject({ pr: 7, marker: prMergeMarker(7) });
55
+ expect(comments[0]!.body).toContain("stays applied and recorded; it is never applied again to merge");
56
+ });
57
+ }
58
+
59
+ test("a new push since the apply, a closed pull request, or records that did not land stop the merge before it is asked", async () => {
60
+ const moved = fakeForge("github", { head: { sha: "c".repeat(40), branch: "feature", open: true, sameRepo: true } });
61
+ expect(await mergeAfterApply({ forge: moved.forge, pr: 7, head: HEAD, recorded: pushed })).toMatchObject({ merged: false, reason: expect.stringContaining("not aaaaaaaaaaaa, the commit that applied") });
62
+ expect(moved.merges).toEqual([]);
63
+ const closed = fakeForge("forgejo", { head: { sha: HEAD, branch: "feature", open: false, sameRepo: true } });
64
+ expect((await mergeAfterApply({ forge: closed.forge, pr: 7, head: HEAD, recorded: pushed })).reason).toBe("it is no longer open");
65
+ const unpushed = fakeForge("gitlab");
66
+ const r = await mergeAfterApply({ forge: unpushed.forge, pr: 7, head: HEAD, recorded: { pushed: false, warning: "chant/lifecycle has no remote" } });
67
+ expect(r.reason).toContain("records are not on the ledger's remote (chant/lifecycle has no remote)");
68
+ expect(unpushed.merges).toEqual([]);
69
+ expect(unpushed.comments[0]!.body).toContain("Merge request !7 was not merged");
70
+ });
71
+
72
+ test("a forge error is a refusal, posted", async () => {
73
+ const { forge, comments } = fakeForge("github", { merge: new Error("500 Internal Server Error") });
74
+ expect(await mergeAfterApply({ forge, pr: 7, head: HEAD, recorded: pushed })).toMatchObject({ merged: false, posted: true });
75
+ expect(comments).toHaveLength(1);
76
+ });
77
+
78
+ test("the merge client holds the named token and no other", () => {
79
+ const env = mergeTokenEnv("PLYWOOD_MERGE_TOKEN", { GITHUB_TOKEN: "job", CHANT_FORGE_TOKEN: "plan", PLYWOOD_MERGE_TOKEN: "merge", GITHUB_REPOSITORY: "acme/infra" });
80
+ expect(env).toEqual({ CHANT_FORGE_TOKEN: "merge", PLYWOOD_MERGE_TOKEN: "merge", GITHUB_REPOSITORY: "acme/infra" });
81
+ expect(mergeTokenEnv("PLYWOOD_MERGE_TOKEN", {})).toEqual({ error: expect.stringContaining("not set") });
82
+ });
83
+ });
84
+
85
+ describe("the pipeline carries --merge auto (#4636)", () => {
86
+ test("the apply before merge job runs with the merge flags and maps the secret on GitHub", () => {
87
+ const [, apply] = prLoopJobs("github", "prod", { applyBeforeMerge: true, applyMerge: { tokenEnv: "PLYWOOD_MERGE_TOKEN", method: "squash" } });
88
+ expect(apply!.command).toContain("pr-apply --before-merge --merge auto --merge-token-env PLYWOOD_MERGE_TOKEN --merge-method squash");
89
+ expect(apply!.run.command).toContain("--apply-before-merge --merge auto --merge-token-env PLYWOOD_MERGE_TOKEN --merge-method squash");
90
+ expect(apply!.env.PLYWOOD_MERGE_TOKEN).toBe("${{ secrets.PLYWOOD_MERGE_TOKEN }}");
91
+ const [, gitlab] = prLoopJobs("gitlab", "prod", { applyBeforeMerge: true, applyMerge: { tokenEnv: "PLYWOOD_MERGE_TOKEN" } });
92
+ expect(gitlab!.env.PLYWOOD_MERGE_TOKEN).toBeUndefined();
93
+ expect(() => prLoopJobs("github", "prod", { applyMerge: { tokenEnv: "X" } })).toThrow(/apply-before-merge/);
94
+ });
95
+
96
+ test("chant ci run apply passes the flags on to pr-apply", () => {
97
+ const args = parseArgs(["ci", "run", "apply", "--apply-before-merge", "--merge", "auto", "--merge-token-env", "PLYWOOD_MERGE_TOKEN"]);
98
+ expect(ciLoopOptions(args).applyMerge).toEqual({ tokenEnv: "PLYWOOD_MERGE_TOKEN" });
99
+ });
100
+ });
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Auto merge after an update before merge (#4636): `pr-apply --before-merge
3
+ * --merge auto --merge-token-env <VAR>` merges the pull request once every
4
+ * member applied and the apply's records are on the ledger's remote.
5
+ *
6
+ * The merge is a second step after the apply, and never a reason to apply
7
+ * again:
8
+ *
9
+ * - The records come first. The apply's release records and gate facts are
10
+ * pushed to `chant/lifecycle`; a push that did not land leaves the pull
11
+ * request open with the reason posted.
12
+ * - The head is checked again. The pull request must still be open at the
13
+ * commit that applied, and the forge's merge is asked to merge only that
14
+ * commit, so a push after the apply is never merged unapplied.
15
+ * - A refusal (branch protection, a new push, a conflict, a closed pull
16
+ * request) is posted on the pull request in one comment the next attempt
17
+ * edits, and the apply stays as it was recorded.
18
+ * - The merge's forge client holds the token `--merge-token-env` names and
19
+ * no other: never the plan job's token, nor the apply job's own. On GitLab
20
+ * the variable is a protected one.
21
+ */
22
+
23
+ import { activeProductWords, type CiProductWords } from "./ci-product-words";
24
+ import type { MergeOptions, PrForge } from "./pr-forge";
25
+
26
+ /** How a pull request applied before merge gets merged. */
27
+ export type PrApplyMerge = "manual" | "auto";
28
+
29
+ export const PR_APPLY_MERGES: readonly PrApplyMerge[] = ["manual", "auto"];
30
+
31
+ export type PrMergeMethod = NonNullable<MergeOptions["method"]>;
32
+
33
+ export const PR_MERGE_METHODS: readonly PrMergeMethod[] = ["merge", "squash", "rebase"];
34
+
35
+ /** The marker of the comment a refused merge posts. */
36
+ export function prMergeMarker(pr: number): string {
37
+ return `<!-- chant:merge:${pr} -->`;
38
+ }
39
+
40
+ /** What {@link mergeAfterApply} did. */
41
+ export interface MergeAfterApply {
42
+ merged: boolean;
43
+ /** The merge commit, when the forge says. */
44
+ sha?: string;
45
+ /** Why it was not merged. */
46
+ reason?: string;
47
+ /** The refusal was posted on the pull request. */
48
+ posted?: boolean;
49
+ }
50
+
51
+ export interface MergeAfterApplyInput {
52
+ /** The client built from the merge token alone. */
53
+ forge: Pick<PrForge, "kind" | "mergePullRequest" | "pullRequestHead" | "upsertComment">;
54
+ pr: number;
55
+ /** The commit that applied. */
56
+ head: string;
57
+ method?: PrMergeMethod;
58
+ /** Whether the apply's records reached the ledger's remote, and why not. */
59
+ recorded: { pushed: boolean; warning?: string };
60
+ words?: CiProductWords;
61
+ }
62
+
63
+ const short = (sha: string): string => sha.slice(0, 12);
64
+
65
+ /** Merge the pull request after its update applied. Never throws; a refusal is posted and returned. */
66
+ export async function mergeAfterApply(i: MergeAfterApplyInput): Promise<MergeAfterApply> {
67
+ const words = i.words ?? activeProductWords();
68
+ const what = i.forge.kind === "gitlab" ? `Merge request !${i.pr}` : `Pull request ${i.pr}`;
69
+ const refuse = async (reason: string): Promise<MergeAfterApply> => {
70
+ const body = `${prMergeMarker(i.pr)}\n${what} was not merged: ${reason}. The ${words.verbs.apply[0]} of ${short(i.head)} stays applied and recorded; it is never applied again to merge. Merge it by hand once the reason is gone, or ${words.verbs.apply[0]} again after a new push.\n`;
71
+ try {
72
+ await i.forge.upsertComment(i.pr, prMergeMarker(i.pr), body);
73
+ return { merged: false, reason, posted: true };
74
+ } catch {
75
+ return { merged: false, reason, posted: false };
76
+ }
77
+ };
78
+ if (!i.recorded.pushed) return refuse(`the ${words.verbs.apply[0]}'s records are not on the ledger's remote${i.recorded.warning ? ` (${i.recorded.warning})` : ""}`);
79
+ try {
80
+ if (i.forge.pullRequestHead) {
81
+ const now = await i.forge.pullRequestHead(i.pr);
82
+ if (!now) return refuse("the forge could not say its head");
83
+ if (!now.open) return refuse("it is no longer open");
84
+ if (now.sha !== i.head) return refuse(`its head is ${short(now.sha)} now, not ${short(i.head)}, the commit that applied`);
85
+ }
86
+ const merged = await i.forge.mergePullRequest(i.pr, { sha: i.head, ...(i.method ? { method: i.method } : {}) });
87
+ if (!merged.merged) return refuse(merged.reason ?? "the forge refused the merge");
88
+ return { merged: true, ...(merged.sha ? { sha: merged.sha } : {}) };
89
+ } catch (err) {
90
+ return refuse(`the forge answered ${err instanceof Error ? err.message : String(err)}`);
91
+ }
92
+ }
93
+
94
+ /**
95
+ * The environment a merge client reads its token from: the job's, with the
96
+ * named variable as the only token and every other forge token left out.
97
+ */
98
+ export function mergeTokenEnv(tokenEnv: string, env: Record<string, string | undefined> = process.env): Record<string, string | undefined> | { error: string } {
99
+ const token = env[tokenEnv]?.trim();
100
+ if (!token) return { error: `--merge-token-env names ${tokenEnv}, which is not set in this job` };
101
+ const out: Record<string, string | undefined> = { ...env };
102
+ for (const name of ["CHANT_FORGE_TOKEN", "GITHUB_TOKEN", "GH_TOKEN", "GITLAB_TOKEN", "CI_JOB_TOKEN", "FORGEJO_TOKEN", "CHANT_GITHUB_TOKEN", "CHANT_GITLAB_TOKEN", "CHANT_FORGEJO_TOKEN"]) delete out[name];
103
+ out.CHANT_FORGE_TOKEN = token;
104
+ return out;
105
+ }