@preventive/triage 1.0.0-alpha.6 → 1.0.0-alpha.61

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 (409) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/README.md +16 -0
  3. package/api/reap.ts +17 -0
  4. package/cli.js +6 -0
  5. package/client/dependency-paths.d.ts +3 -0
  6. package/client/dependency-paths.js +36 -0
  7. package/client/finding-link.js +305 -0
  8. package/client/linked-findings.d.ts +1 -0
  9. package/client/linked-findings.js +115 -0
  10. package/common/bundle-commit.d.ts +1 -0
  11. package/common/bundle-commit.js +4 -0
  12. package/common/bundle-compare-diff.d.ts +3 -0
  13. package/common/bundle-compare-diff.js +376 -0
  14. package/common/bundle-compare-inputs.d.ts +3 -0
  15. package/common/bundle-compare-inputs.js +141 -0
  16. package/common/bundle-compare-versions.js +172 -0
  17. package/common/bundle-comparison.d.ts +8 -0
  18. package/common/bundle-comparison.js +69 -0
  19. package/common/bundle-metadata.d.ts +15 -0
  20. package/common/bundle-metadata.js +226 -0
  21. package/common/bundle-pkg-of.d.ts +1 -0
  22. package/common/bundle-pkg-of.js +90 -0
  23. package/common/bundle-reasons.d.ts +2 -0
  24. package/common/bundle-reasons.js +21 -0
  25. package/common/bundle-source-links.d.ts +6 -0
  26. package/common/bundle-source-links.js +216 -0
  27. package/common/bundle-sourcemap.d.ts +10 -0
  28. package/common/bundle-sourcemap.js +100 -0
  29. package/common/bundle-sources.d.ts +7 -0
  30. package/common/bundle-sources.js +333 -0
  31. package/common/bundle-stats.js +41 -0
  32. package/common/bundle-tabs.js +1 -0
  33. package/common/code-language.js +65 -0
  34. package/common/default-scan-models.ts +36 -0
  35. package/common/dependency-reviews.d.ts +18 -0
  36. package/common/dependency-reviews.js +64 -0
  37. package/common/finding-duplicates.js +58 -0
  38. package/common/finding-filters.js +274 -0
  39. package/common/finding-id.js +47 -0
  40. package/common/finding-repo.d.ts +5 -0
  41. package/common/finding-repo.js +67 -0
  42. package/common/github-issue-labels.d.ts +1 -0
  43. package/common/github-issue-labels.js +11 -0
  44. package/common/github-pr.ts +56 -0
  45. package/common/gzip.js +59 -0
  46. package/common/line-diff.d.ts +8 -0
  47. package/common/line-diff.js +57 -0
  48. package/common/linked-workspace-groups.js +63 -0
  49. package/common/managed/agent-tasks.ts +292 -0
  50. package/common/managed/comments.ts +43 -0
  51. package/common/managed/finding-detail.d.ts +5 -0
  52. package/common/managed/finding-detail.js +75 -0
  53. package/common/managed/finding-values.d.ts +2 -0
  54. package/common/managed/finding-values.js +44 -0
  55. package/common/managed/github-agents.ts +114 -0
  56. package/common/managed/github-logins.ts +49 -0
  57. package/common/managed/lite-name.d.ts +2 -0
  58. package/common/managed/lite-name.js +20 -0
  59. package/common/managed/npm-packages.d.ts +7 -0
  60. package/common/managed/npm-packages.js +51 -0
  61. package/common/managed/permissions.ts +31 -0
  62. package/common/managed/report-content.ts +72 -0
  63. package/common/managed/report-filter.ts +120 -0
  64. package/common/managed/repository-alias.ts +65 -0
  65. package/common/managed/roles.ts +35 -0
  66. package/common/managed/routes.d.ts +2 -0
  67. package/common/managed/routes.js +363 -0
  68. package/common/managed/scan-models.ts +6 -0
  69. package/common/managed/team-app.d.ts +15 -0
  70. package/common/managed/team-app.js +100 -0
  71. package/common/managed/team-filters.js +26 -0
  72. package/common/managed/triage.ts +83 -0
  73. package/common/managed/upload-seal.ts +126 -0
  74. package/common/pretty-print.d.ts +5 -0
  75. package/common/pretty-print.js +24 -0
  76. package/common/repo-commit.d.ts +4 -0
  77. package/common/repo-commit.js +30 -0
  78. package/common/revalidation-conflicts.js +48 -0
  79. package/common/revalidation-input-groups.js +56 -0
  80. package/common/save-error-reason.ts +20 -7
  81. package/common/scan-server.ts +13 -0
  82. package/common/security-links.d.ts +5 -0
  83. package/common/security-links.js +33 -0
  84. package/common/server-info.ts +46 -0
  85. package/common/utf8.d.ts +3 -0
  86. package/common/utf8.js +45 -0
  87. package/common/workspace-app.js +71 -0
  88. package/common/workspace-groups.js +176 -0
  89. package/out/activity.js +4 -0
  90. package/out/brotli-fallback.js +3 -3
  91. package/out/chunk-2XUEZHIP.js +28 -0
  92. package/out/chunk-4EMM6UCD.js +1 -0
  93. package/out/chunk-4ISO5F7Y.js +7 -0
  94. package/out/chunk-4XIH7YTX.js +1 -0
  95. package/out/chunk-6CCSTJWB.js +1 -0
  96. package/out/chunk-74AEKRES.js +10 -0
  97. package/out/chunk-7BKKYVWR.js +7 -0
  98. package/out/chunk-7C7DWL7I.js +1 -0
  99. package/out/chunk-B3FNMHOE.js +1 -0
  100. package/out/chunk-BNH52UHC.js +2 -0
  101. package/out/chunk-BRO77PQY.js +42 -0
  102. package/out/chunk-C4Z6ZBXV.js +10 -0
  103. package/out/chunk-DEVAORP2.js +1 -0
  104. package/out/chunk-E3LFSUDJ.js +9 -0
  105. package/out/chunk-G3LY2FV4.js +1 -0
  106. package/out/chunk-GO7FT4OD.js +1 -0
  107. package/out/chunk-GRGIZOTP.js +4 -0
  108. package/out/chunk-HAZKDK4M.js +4 -0
  109. package/out/chunk-HOB3YPS5.js +1 -0
  110. package/out/chunk-IYPY47MC.js +1 -0
  111. package/out/chunk-IZ6MYRYB.js +3 -0
  112. package/out/chunk-J2FWBXFV.js +1 -0
  113. package/out/chunk-JCIIBLKR.js +1 -0
  114. package/out/chunk-JFL7WLRV.js +1 -0
  115. package/out/chunk-JJK5OZG2.js +15 -0
  116. package/out/chunk-JMG5DXII.js +2 -0
  117. package/out/chunk-JOXLZXMI.js +1 -0
  118. package/out/chunk-M37X4LCF.js +14 -0
  119. package/out/chunk-M3Y6SLC4.js +1 -0
  120. package/out/chunk-NRPC74AM.js +1 -0
  121. package/out/chunk-NXATZJQ2.js +1 -0
  122. package/out/chunk-OFZYEWBH.js +244 -0
  123. package/out/chunk-OIERSXON.js +1 -0
  124. package/out/chunk-OOJM4CTU.js +1 -0
  125. package/out/chunk-PAD2JHZT.js +1 -0
  126. package/out/chunk-PNHBZGDD.js +1 -0
  127. package/out/chunk-PTFQKLKO.js +1 -0
  128. package/out/chunk-PWFUOLZK.js +17 -0
  129. package/out/chunk-PXK7EU4V.js +1 -0
  130. package/out/chunk-QCOGVL37.js +52 -0
  131. package/out/chunk-QMVOEBWC.js +13 -0
  132. package/out/chunk-QRKFPLA4.js +80 -0
  133. package/out/chunk-R3NZFXVR.js +1 -0
  134. package/out/chunk-R5O6FAW4.js +2 -0
  135. package/out/chunk-RRBIJL2B.js +1 -0
  136. package/out/chunk-SYXT3SFP.js +2 -0
  137. package/out/chunk-TOXMUOIW.js +5 -0
  138. package/out/chunk-TUII34K2.js +1 -0
  139. package/out/chunk-UCP3JRE2.js +1 -0
  140. package/out/chunk-VFWMQ3LG.js +2 -0
  141. package/out/chunk-VRX2APNA.js +9 -0
  142. package/out/chunk-W3QFV4SP.js +3 -0
  143. package/out/chunk-W6VOKW2N.js +12 -0
  144. package/out/chunk-WCPJDAYA.js +10 -0
  145. package/out/chunk-WMFDQS2L.js +11 -0
  146. package/out/chunk-WREU7JAG.js +1 -0
  147. package/out/chunk-Y5ZHFNBD.js +2 -0
  148. package/out/chunk-YF4M5OCI.js +1 -0
  149. package/out/chunk-Z667U2UJ.js +1 -0
  150. package/out/chunk-ZQHLECTI.js +1 -0
  151. package/out/chunk-ZQNEWVGG.js +1 -0
  152. package/out/client-managed-import.js +4 -0
  153. package/out/client-managed.js +34 -0
  154. package/out/client-sync.js +11 -17
  155. package/out/compare.js +40 -0
  156. package/out/dialogs.js +40 -0
  157. package/out/github-issue-authorized.html +3 -0
  158. package/out/github-issue-authorized.js +1 -0
  159. package/out/graph.js +19 -4
  160. package/out/index.html +106 -8
  161. package/out/prism.js +14 -10
  162. package/out/scan.js +2 -0
  163. package/out/stasis.svg +20 -0
  164. package/out/terminal.js +366 -39
  165. package/out/view.css +1 -1
  166. package/out/view.js +19 -144
  167. package/package.json +383 -63
  168. package/risk/actions/findings.js +161 -0
  169. package/risk/actions/resolve.js +250 -0
  170. package/risk/actions/scripts/commands.js +182 -0
  171. package/risk/actions/scripts/grade.js +113 -0
  172. package/risk/actions/scripts/packages.js +123 -0
  173. package/risk/actions/scripts/shell.js +169 -0
  174. package/risk/actions/uses.js +76 -0
  175. package/risk/actions/workflow.js +110 -0
  176. package/risk/files/characters.js +196 -0
  177. package/risk/files/hosts.js +205 -0
  178. package/risk/files/readability.js +177 -0
  179. package/risk/files/runs-code.js +615 -0
  180. package/risk/files/script.js +118 -0
  181. package/risk/files/stamps.js +47 -0
  182. package/risk/index.d.ts +272 -0
  183. package/risk/index.js +11 -0
  184. package/risk/npm/assess.js +285 -0
  185. package/risk/npm/combined.js +68 -0
  186. package/risk/npm/compare.js +257 -0
  187. package/risk/npm/dependencies.js +172 -0
  188. package/risk/npm/diff.js +79 -0
  189. package/risk/npm/downloads.js +84 -0
  190. package/risk/npm/files.js +445 -0
  191. package/risk/npm/held.js +63 -0
  192. package/risk/npm/install-scripts.js +34 -0
  193. package/risk/npm/malware.js +38 -0
  194. package/risk/npm/manifest.js +65 -0
  195. package/risk/npm/new-dependency.js +60 -0
  196. package/risk/npm/old-version.js +108 -0
  197. package/risk/npm/registry.js +390 -0
  198. package/risk/npm/release.js +85 -0
  199. package/risk/npm/score.js +59 -0
  200. package/risk/npm/specs.js +124 -0
  201. package/risk/npm/tarball.js +96 -0
  202. package/server-common/database-config.ts +16 -0
  203. package/server-common/initialize.ts +18 -0
  204. package/server-common/npm-advisories.ts +101 -0
  205. package/{server → server-common}/origin.ts +5 -5
  206. package/server-common/reap.ts +48 -0
  207. package/server-common/scan-config.ts +19 -0
  208. package/server-common/standalone.ts +29 -0
  209. package/server-common/storage-crypto.ts +182 -0
  210. package/server-common/storage-log.ts +38 -0
  211. package/server-common/vercel-blob.ts +103 -0
  212. package/server-e2e/app.ts +487 -0
  213. package/{server → server-e2e}/bus-receiver.ts +2 -1
  214. package/{server → server-e2e}/cli.js +9 -4
  215. package/{server → server-e2e}/config.ts +50 -42
  216. package/{server → server-e2e}/db-neon.ts +2 -2
  217. package/{server → server-e2e}/db-revision-sql.ts +1 -1
  218. package/{server → server-e2e}/db-stmt.ts +2 -2
  219. package/{server → server-e2e}/db.ts +7 -7
  220. package/{server → server-e2e}/http.ts +44 -13
  221. package/{server → server-e2e}/hub.ts +40 -9
  222. package/server-e2e/index.ts +17 -0
  223. package/server-e2e/lifecycle.ts +95 -0
  224. package/{server → server-e2e}/neon-driver.ts +2 -2
  225. package/{server → server-e2e}/npm-proxy.ts +12 -145
  226. package/{server → server-e2e}/objstore/blob-vercel.ts +10 -114
  227. package/{server → server-e2e}/objstore/blob.ts +1 -1
  228. package/{server → server-e2e}/objstore/fetch-mint-guard.ts +2 -2
  229. package/{server → server-e2e}/objstore/handlers.ts +9 -8
  230. package/{server → server-e2e}/objstore/init.ts +19 -27
  231. package/{server → server-e2e}/objstore/reaper.ts +1 -1
  232. package/{server → server-e2e}/objstore/rest-deny.ts +1 -1
  233. package/{server → server-e2e}/objstore/rest-mint.ts +3 -3
  234. package/{server → server-e2e}/objstore/rest.ts +3 -3
  235. package/{server → server-e2e}/objstore/store-neon.ts +6 -10
  236. package/{server → server-e2e}/objstore/store.ts +7 -7
  237. package/{server → server-e2e}/objstore/tokens.ts +2 -2
  238. package/{server → server-e2e}/pubsub.ts +12 -9
  239. package/{server → server-e2e}/sign.ts +4 -4
  240. package/{server → server-e2e}/sse-server.ts +60 -33
  241. package/{server → server-e2e}/static.ts +31 -21
  242. package/{server → server-e2e}/sync-handlers.ts +64 -30
  243. package/{server → server-e2e}/ws-server.ts +20 -9
  244. package/server-managed/activity.ts +238 -0
  245. package/server-managed/agent-merges.ts +109 -0
  246. package/server-managed/app-agents.ts +23 -0
  247. package/server-managed/avatar-store.ts +13 -0
  248. package/server-managed/blob-store.ts +26 -0
  249. package/server-managed/brotli.ts +10 -0
  250. package/server-managed/bundle-advisories.ts +103 -0
  251. package/server-managed/bundle-advisory-inventory.ts +77 -0
  252. package/server-managed/bundle-build-diagnostics.d.ts +10 -0
  253. package/server-managed/bundle-build-diagnostics.js +23 -0
  254. package/server-managed/bundle-build-leases.ts +41 -0
  255. package/server-managed/bundle-build-worker.js +238 -0
  256. package/server-managed/bundle-build.ts +240 -0
  257. package/server-managed/bundle-cache.ts +199 -0
  258. package/server-managed/bundle-catalog.ts +29 -0
  259. package/server-managed/bundle-commits.ts +133 -0
  260. package/server-managed/bundle-dependency-reviews.ts +68 -0
  261. package/server-managed/bundle-store.ts +24 -0
  262. package/server-managed/bundle-summary-cache.ts +139 -0
  263. package/server-managed/bundle-validation.ts +33 -0
  264. package/server-managed/bundle.ts +119 -0
  265. package/server-managed/cache-storage.ts +20 -0
  266. package/server-managed/cli.js +21 -0
  267. package/server-managed/combined.ts +43 -0
  268. package/server-managed/comments.ts +138 -0
  269. package/server-managed/commit-coauthors.ts +105 -0
  270. package/server-managed/commit-lockfiles.ts +77 -0
  271. package/server-managed/comparison.ts +50 -0
  272. package/server-managed/config.ts +163 -0
  273. package/server-managed/content-access.ts +15 -0
  274. package/server-managed/crypto.ts +25 -0
  275. package/server-managed/db-methods.ts +1877 -0
  276. package/server-managed/db-neon.ts +388 -0
  277. package/server-managed/db-schema.ts +253 -0
  278. package/server-managed/db-table-names.ts +22 -0
  279. package/server-managed/db.ts +146 -0
  280. package/server-managed/dependabot-access.ts +61 -0
  281. package/server-managed/dependabot-alerts.ts +180 -0
  282. package/server-managed/dependency-review-comments.ts +92 -0
  283. package/server-managed/dependency-reviews.ts +363 -0
  284. package/server-managed/github-accounts.ts +59 -0
  285. package/server-managed/github-actions.ts +71 -0
  286. package/server-managed/github-app.ts +347 -0
  287. package/server-managed/github-errors.ts +70 -0
  288. package/server-managed/github-graphql.ts +42 -0
  289. package/server-managed/github-issue-links.ts +264 -0
  290. package/server-managed/github-issue-oauth.ts +30 -0
  291. package/server-managed/github-issues.ts +157 -0
  292. package/server-managed/github-link-oauth.ts +56 -0
  293. package/server-managed/github-links.ts +204 -0
  294. package/server-managed/github-metadata.ts +213 -0
  295. package/server-managed/github-oauth.ts +264 -0
  296. package/server-managed/github-pulls.ts +257 -0
  297. package/server-managed/github-requests.ts +179 -0
  298. package/server-managed/github-users.ts +30 -0
  299. package/server-managed/github-values.ts +26 -0
  300. package/server-managed/history-windows.ts +172 -0
  301. package/server-managed/http-response.ts +76 -0
  302. package/server-managed/http.ts +5796 -0
  303. package/server-managed/import-triage.ts +51 -0
  304. package/server-managed/index.ts +204 -0
  305. package/server-managed/kept-briefly.ts +37 -0
  306. package/server-managed/kept.ts +28 -0
  307. package/server-managed/link-reports.ts +149 -0
  308. package/server-managed/listed-activity.ts +39 -0
  309. package/server-managed/locked-packages.ts +152 -0
  310. package/server-managed/lockfile-scope.ts +49 -0
  311. package/server-managed/lru.ts +30 -0
  312. package/server-managed/managed-issues.ts +92 -0
  313. package/server-managed/management-catalog.ts +45 -0
  314. package/server-managed/management.ts +110 -0
  315. package/server-managed/npm-delta-worker.js +23 -0
  316. package/server-managed/npm-delta.ts +159 -0
  317. package/server-managed/npm-insights.ts +455 -0
  318. package/server-managed/npm-kept.ts +234 -0
  319. package/server-managed/npm-loads.ts +259 -0
  320. package/server-managed/npm-manifest.ts +117 -0
  321. package/server-managed/npm-packages.ts +462 -0
  322. package/server-managed/npm-patch.ts +317 -0
  323. package/server-managed/npm-provenance.ts +127 -0
  324. package/server-managed/npm-risk.ts +287 -0
  325. package/server-managed/object-storage-disk.ts +147 -0
  326. package/server-managed/object-storage-vercel.ts +71 -0
  327. package/server-managed/object-storage.ts +82 -0
  328. package/server-managed/package-entry-points.ts +79 -0
  329. package/server-managed/pretty-print.ts +425 -0
  330. package/server-managed/public-workspace.ts +242 -0
  331. package/server-managed/pull-comment-reviews.ts +161 -0
  332. package/server-managed/pull-dependencies.ts +340 -0
  333. package/server-managed/pull-dependency-reviews.ts +327 -0
  334. package/server-managed/pull-reviews.ts +202 -0
  335. package/server-managed/queue.ts +53 -0
  336. package/server-managed/release-commits.ts +85 -0
  337. package/server-managed/rename-diff-worker.js +26 -0
  338. package/server-managed/rename-diff.ts +236 -0
  339. package/server-managed/repo-path.ts +21 -0
  340. package/server-managed/report-migration.ts +32 -0
  341. package/server-managed/report-query.ts +10 -0
  342. package/server-managed/report-response.ts +16 -0
  343. package/server-managed/report-sources.ts +181 -0
  344. package/server-managed/report-store.ts +77 -0
  345. package/server-managed/repository-actions.ts +220 -0
  346. package/server-managed/repository-agents.ts +422 -0
  347. package/server-managed/repository-aliases.ts +85 -0
  348. package/server-managed/repository-browser.ts +303 -0
  349. package/server-managed/repository-changes.ts +280 -0
  350. package/server-managed/repository-code.ts +215 -0
  351. package/server-managed/repository-dependency-reviews.ts +159 -0
  352. package/server-managed/repository-discovery.ts +83 -0
  353. package/server-managed/repository-history.ts +428 -0
  354. package/server-managed/repository-issues.ts +224 -0
  355. package/server-managed/repository-policy.ts +27 -0
  356. package/server-managed/repository-pulls.ts +207 -0
  357. package/server-managed/repository-pulse.ts +477 -0
  358. package/server-managed/repository-risk-assess.ts +108 -0
  359. package/server-managed/repository-risk-audit.ts +78 -0
  360. package/server-managed/repository-risk-read.ts +170 -0
  361. package/server-managed/repository-risk.ts +483 -0
  362. package/server-managed/repository-squashes.ts +160 -0
  363. package/server-managed/repository-stamp.ts +41 -0
  364. package/server-managed/request-timing.ts +63 -0
  365. package/server-managed/reviewed-dependencies.ts +149 -0
  366. package/server-managed/revisions.ts +35 -0
  367. package/server-managed/scan-downloads.ts +87 -0
  368. package/server-managed/session.ts +132 -0
  369. package/server-managed/slugs.ts +38 -0
  370. package/server-managed/solidity-entry-points.ts +71 -0
  371. package/server-managed/sql-postgres.ts +34 -0
  372. package/server-managed/sql.ts +107 -0
  373. package/server-managed/stasis-config.ts +55 -0
  374. package/server-managed/static.ts +28 -0
  375. package/server-managed/storage-command.ts +16 -0
  376. package/server-managed/storage-db.ts +183 -0
  377. package/server-managed/storage-encryption.ts +288 -0
  378. package/server-managed/storage-maintenance.ts +259 -0
  379. package/server-managed/storage-payload.ts +121 -0
  380. package/server-managed/storage-stores.ts +91 -0
  381. package/server-managed/storage.ts +82 -0
  382. package/server-managed/team-app-summaries.ts +54 -0
  383. package/server-managed/team-app.ts +154 -0
  384. package/server-managed/team-catalog.ts +7 -0
  385. package/server-managed/team-feed.ts +180 -0
  386. package/server-managed/team-npm-scopes.ts +103 -0
  387. package/server-managed/team-reports.ts +351 -0
  388. package/server-managed/triage-response.ts +16 -0
  389. package/server-managed/type-only-proof.js +108 -0
  390. package/server-managed/type-only-worker.js +31 -0
  391. package/server-managed/type-only.ts +115 -0
  392. package/server-managed/uploads.ts +106 -0
  393. package/server-managed/upstream-cache.ts +250 -0
  394. package/server-managed/view-as.ts +58 -0
  395. package/server-managed/worker-turns.ts +76 -0
  396. package/server-managed/workspace-shares.ts +170 -0
  397. package/server.ts +70 -0
  398. package/server/index.ts +0 -480
  399. package/server/lifecycle.ts +0 -208
  400. /package/{LICENSE → LICENSE-MIT} +0 -0
  401. /package/{server → server-e2e}/auth.ts +0 -0
  402. /package/{server → server-e2e}/config.example.json +0 -0
  403. /package/{server → server-e2e}/objstore/blob-fs.ts +0 -0
  404. /package/{server → server-e2e}/objstore/fs.ts +0 -0
  405. /package/{server → server-e2e}/objstore/sign.ts +0 -0
  406. /package/{server → server-e2e}/peer.ts +0 -0
  407. /package/{server → server-e2e}/sse-session.ts +0 -0
  408. /package/{server → server-e2e}/util.ts +0 -0
  409. /package/{server → server-e2e}/validation.ts +0 -0
@@ -0,0 +1,224 @@
1
+ // The issues and pull requests the code viewer's Issues and Pull requests
2
+ // tabs show (GET /api/admin/repositories/issues): those open, or those
3
+ // closed (a pull request's merged among them), the most recently updated
4
+ // first, a page at a time, each as one tile has room for, with how many are
5
+ // open and closed. A pull request's tile names its branches, its size, its
6
+ // review and its checks, which GraphQL alone gives, so it needs a token; an
7
+ // open one's, and its popup's, whether it changes a lockfile whose new
8
+ // packages are told (pull-dependencies.ts touchesLockfile), and its reviews
9
+ // (pull-reviews.ts), each reviewer's; and an open one's base's and head's
10
+ // commits, kept with its page but never told, by which what its lockfiles
11
+ // add is read (http.ts teamRepositoryDependencies), of those carrying a
12
+ // label alone where one is named, as a team's Dependencies names the
13
+ // repository's DEPENDENCIES_LABEL where it has one. A nonpublic repository's
14
+ // are read with its GitHub App's installation, which reads pull requests only
15
+ // with their own permission (doc/code-viewer.md).
16
+ import { ACTOR, type GraphAccount, actorOf } from './github-accounts.ts'
17
+ import { GithubApiError } from './github-errors.ts'
18
+ import { type GraphqlError, githubGraphql } from './github-graphql.ts'
19
+ import { SHA, count, nameOf, timeOf } from './github-values.ts'
20
+ import { touchesLockfile } from './pull-dependencies.ts'
21
+ import { ITEM_THREADS, type ReviewNode, TILE_THREADS, pullReviews, reviewFields, reviewsRefused } from './pull-reviews.ts'
22
+ import type { Connection } from './repository-history.ts'
23
+
24
+ const ISSUES_PAGE = 50
25
+ // What a kept page or item was read as: kept ones read before differ.
26
+ export const ISSUES_READ_VERSION = 4
27
+ const MAX_LABELS = 20
28
+ const MAX_TITLE = 1000
29
+ const COLOR = /^[a-f\d]{6}$/iu
30
+ // A page's cursor as GitHub gives it, to read on from: opaque, base64 or
31
+ // base64url as it is now (`…rOrK_u7Q==`), passed back to GitHub alone, so any
32
+ // printable one is, but none with spaces or control characters.
33
+ export const ISSUES_CURSOR = /^[!-~]{1,256}$/u
34
+
35
+ type IssuesKind = 'issues' | 'pulls'
36
+ type IssuesState = 'open' | 'closed'
37
+
38
+ const shared = `number title createdAt updatedAt closedAt author { ${ACTOR} } comments { totalCount } labels(first: ${MAX_LABELS}) { totalCount nodes { name color } }`
39
+ const fields = {
40
+ issues: `${shared} state stateReason`,
41
+ pulls: `${shared} state isDraft mergedAt reviewDecision additions deletions headRefName baseRefName
42
+ commits(last: 1) { nodes { commit { statusCheckRollup { state } } } }`,
43
+ }
44
+ // The files a pull request changes, as many as LISTED_FILES, as an open one's
45
+ // tile and its popup tell whether it changes a lockfile by them.
46
+ const LISTED_FILES = 100
47
+ const files = `files(first: ${LISTED_FILES}) { totalCount nodes { path } }`
48
+ const connection = { issues: 'issues', pulls: 'pullRequests' }
49
+ const states = { issues: { open: '[OPEN]', closed: '[CLOSED]' }, pulls: { open: '[OPEN]', closed: '[CLOSED, MERGED]' } }
50
+
51
+ // A page of `kind`, those in `state`, after the page `after` ends, and how
52
+ // many of each state there are; open pull requests with their commits and
53
+ // their reviews. `labelled`, of those carrying a label of `$labels` alone,
54
+ // the counts still of all.
55
+ const pageQuery = (kind: IssuesKind, state: IssuesState, labelled = false) => `query($owner: String!, $name: String!, $after: String${labelled ? ', $labels: [String!]' : ''}) {
56
+ repository(owner: $owner, name: $name) {
57
+ open: ${connection[kind]}(states: ${states[kind].open}) { totalCount }
58
+ closed: ${connection[kind]}(states: ${states[kind].closed}) { totalCount }
59
+ page: ${connection[kind]}(states: ${states[kind][state]}, first: ${ISSUES_PAGE}, after: $after, ${labelled ? 'labels: $labels, ' : ''}orderBy: { field: UPDATED_AT, direction: DESC }) {
60
+ pageInfo { hasNextPage endCursor }
61
+ nodes { ${fields[kind]}${kind === 'pulls' && state === 'open' ? ` baseRefOid headRefOid ${files} ${reviewFields(TILE_THREADS, false)}` : ''} }
62
+ }
63
+ }
64
+ }`
65
+
66
+ type Node = ReviewNode & { number?: unknown; title?: unknown; createdAt?: unknown; updatedAt?: unknown; closedAt?: unknown; mergedAt?: unknown; author?: GraphAccount
67
+ comments?: { totalCount?: unknown }; labels?: { totalCount?: unknown; nodes?: { name?: unknown; color?: unknown }[] }; state?: unknown; stateReason?: unknown
68
+ isDraft?: unknown; reviewDecision?: unknown; additions?: unknown; deletions?: unknown; headRefName?: unknown; baseRefName?: unknown; baseRefOid?: unknown
69
+ commits?: { nodes?: { commit?: { statusCheckRollup?: { state?: unknown } | null } | null }[] }
70
+ files?: { totalCount?: unknown; nodes?: ({ path?: unknown } | null)[] } | null }
71
+ type Page = { repository?: { open?: { totalCount?: unknown }; closed?: { totalCount?: unknown }; page?: Connection<Node> } | null }
72
+
73
+ const REVIEWS: Record<string, string> = { APPROVED: 'approved', CHANGES_REQUESTED: 'changes', REVIEW_REQUIRED: 'required' }
74
+ const CHECKS: Record<string, string> = { SUCCESS: 'success', FAILURE: 'failure', ERROR: 'failure', PENDING: 'pending', EXPECTED: 'pending' }
75
+ // Why an issue was closed, as GitHub tells it: completed, not planned or a duplicate.
76
+ export type IssueReason = 'completed' | 'not-planned' | 'duplicate'
77
+ export const ISSUE_REASONS: Record<string, IssueReason> = { COMPLETED: 'completed', NOT_PLANNED: 'not-planned', DUPLICATE: 'duplicate' }
78
+
79
+ // An issue or pull request by its number, title and author, as its tile and
80
+ // the Pulse's lists (repository-pulse.ts) name it; null for one GitHub names
81
+ // no number for.
82
+ export function itemOf(node: { number?: unknown; title?: unknown; author?: GraphAccount } | null) {
83
+ if (!node || !Number.isSafeInteger(node.number) || (node.number as number) <= 0) return null
84
+ const author = actorOf(node.author)
85
+ return { number: node.number as number, title: nameOf(node.title, MAX_TITLE) ?? '', author: author.login, authorId: author.id }
86
+ }
87
+ // An issue's state as a tile tells it, and why it was closed; a pull
88
+ // request's, and whether it is a draft.
89
+ export const issueState = (state: unknown, reason: unknown) => ({ state: state === 'OPEN' ? 'open' : 'closed', reason: ISSUE_REASONS[String(reason)] ?? null })
90
+ export const pullState = (state: unknown, draft: unknown) => ({ state: state === 'MERGED' ? 'merged' : state === 'OPEN' ? 'open' : 'closed', draft: draft === true })
91
+
92
+ // An issue or pull request as a tile shows it; null for one GitHub names no
93
+ // number for.
94
+ function item(kind: IssuesKind, node: Node | null) {
95
+ const named = itemOf(node)
96
+ if (!node || !named) return null
97
+ const labels = (node.labels?.nodes ?? []).flatMap(label => {
98
+ const name = nameOf(label?.name)
99
+ return name ? [{ name, color: typeof label?.color === 'string' && COLOR.test(label.color) ? label.color.toLowerCase() : null }] : []
100
+ })
101
+ const common = { ...named, createdAt: timeOf(node.createdAt), updatedAt: timeOf(node.updatedAt),
102
+ closedAt: timeOf(node.closedAt), comments: count(node.comments?.totalCount), labels, moreLabels: Math.max(0, count(node.labels?.totalCount) - labels.length) }
103
+ if (kind === 'issues') return { ...common, ...issueState(node.state, node.stateReason) }
104
+ const checks = node.commits?.nodes?.[0]?.commit?.statusCheckRollup?.state
105
+ const listed = node.files?.nodes
106
+ const paths = Array.isArray(listed) ? listed.flatMap(file => typeof file?.path === 'string' ? [file.path] : []) : null
107
+ return { ...common, ...pullState(node.state, node.isDraft), mergedAt: timeOf(node.mergedAt),
108
+ review: REVIEWS[String(node.reviewDecision)] ?? null, checks: CHECKS[String(checks)] ?? null, additions: count(node.additions), deletions: count(node.deletions),
109
+ head: nameOf(node.headRefName), base: nameOf(node.baseRefName),
110
+ ...paths ? { touchesLockfile: touchesLockfile(paths, count(node.files?.totalCount)) } : {} }
111
+ }
112
+
113
+ // A pull request's checks, which a GitHub App reads only with the Checks and
114
+ // Commit statuses permissions, refused: its tile then shows none; or its
115
+ // files, whose lockfiles' new packages it then tells none of. Any other
116
+ // field refused, the list is, as one without Pull requests: read is.
117
+ const checksRefused = (error: GraphqlError) => error?.type === 'FORBIDDEN' && Array.isArray(error.path)
118
+ && (error.path.includes('statusCheckRollup') || error.path.includes('files'))
119
+ const partRefused = (error: GraphqlError) => checksRefused(error) || reviewsRefused(error)
120
+
121
+ // One issue or pull request, as its popup shows it (GET
122
+ // /api/admin/repositories/item): as its tile does, with its description, its
123
+ // Markdown as written (never GitHub's HTML of it); an issue's pull requests closing
124
+ // it, a pull request's issues it closes, its commits and files changed, and
125
+ // who merged it. Its state, of a different type each, named apart. A
126
+ // description longer than MAX_BODY is shortened (`bodyShortened`), and the
127
+ // linked past the first LINKED counted (`moreLinked`), the rest on GitHub.
128
+ const MAX_BODY = 20_000
129
+ const LINKED = 10
130
+ const ITEM_QUERY = `query($owner: String!, $name: String!, $number: Int!) {
131
+ repository(owner: $owner, name: $name) {
132
+ item: issueOrPullRequest(number: $number) {
133
+ __typename
134
+ ... on Issue { ${shared} issueState: state stateReason body
135
+ closedByPullRequestsReferences(first: ${LINKED}, includeClosedPrs: true) { totalCount nodes { number title pullState: state isDraft } } }
136
+ ... on PullRequest { ${shared} pullState: state isDraft mergedAt reviewDecision additions deletions headRefName baseRefName body changedFiles ${files}
137
+ commits(last: 1) { totalCount nodes { commit { statusCheckRollup { state } } } } mergedBy { ${ACTOR} }
138
+ closingIssuesReferences(first: ${LINKED}) { totalCount nodes { number title issueState: state stateReason } }
139
+ ${reviewFields(ITEM_THREADS, true)} }
140
+ }
141
+ }
142
+ }`
143
+ type Linked = { number?: unknown; title?: unknown; issueState?: unknown; pullState?: unknown; stateReason?: unknown; isDraft?: unknown } | null
144
+ type ItemNode = Node & { __typename?: unknown; issueState?: unknown; pullState?: unknown; body?: unknown; changedFiles?: unknown
145
+ commits?: Node['commits'] & { totalCount?: unknown }; mergedBy?: GraphAccount
146
+ closedByPullRequestsReferences?: Links; closingIssuesReferences?: Links }
147
+ type Links = { totalCount?: unknown; nodes?: Linked[] } | null
148
+
149
+ // The issues or pull requests an item names, each by its number, title and
150
+ // state, as `kind`'s tile names one.
151
+ const linked = (kind: IssuesKind, nodes: Linked[] | undefined) => (nodes ?? []).flatMap(node => {
152
+ const tile = item(kind, node ? { number: node.number, title: node.title, state: node.issueState ?? node.pullState, stateReason: node.stateReason, isDraft: node.isDraft } : null)
153
+ return tile ? [{ number: tile.number, title: tile.title, state: tile.state, ...'reason' in tile ? { reason: tile.reason } : { draft: tile.draft } }] : []
154
+ })
155
+
156
+ // The issue or pull request numbered `number`, its `kind` with it.
157
+ export async function repositoryItem(fullName: string, token: string, number: number, fetchImpl: typeof fetch = globalThis.fetch) {
158
+ const [owner, name] = fullName.split('/') as [string, string]
159
+ const data = await githubGraphql(ITEM_QUERY, { owner, name, number }, token, fetchImpl, partRefused) as { repository?: { item?: ItemNode | null } | null }
160
+ const node = data?.repository?.item
161
+ const kind: IssuesKind | null = node?.__typename === 'Issue' ? 'issues' : node?.__typename === 'PullRequest' ? 'pulls' : null
162
+ const tile = kind && node ? item(kind, { ...node, state: kind === 'issues' ? node.issueState : node.pullState }) : null
163
+ if (!kind || !node || !tile) throw new GithubApiError(404, 'github-not-found')
164
+ // As written, but for blank lines before it and spaces after: a first
165
+ // line's indent, an indented code block's, kept.
166
+ const written = typeof node.body === 'string' ? node.body.replace(/^(?:[ \t]*\r?\n)+/u, '').trimEnd() : ''
167
+ const links = kind === 'issues' ? node.closedByPullRequestsReferences : node.closingIssuesReferences
168
+ const named = linked(kind === 'issues' ? 'pulls' : 'issues', links?.nodes)
169
+ const described = { body: written.slice(0, MAX_BODY), bodyShortened: written.length > MAX_BODY, moreLinked: Math.max(0, count(links?.totalCount) - named.length) }
170
+ const merger = actorOf(node.mergedBy)
171
+ return kind === 'issues' ? { kind, ...tile, ...described, closedBy: named }
172
+ : { kind, ...tile, ...described, closes: named, commits: count(node.commits?.totalCount), files: count(node.changedFiles),
173
+ mergedBy: merger.login, mergedById: merger.id, reviews: pullReviews(node, tile.author, true) }
174
+ }
175
+
176
+ // An open pull request's base's and head's commits, by its number, as GitHub
177
+ // has them now: those its REST API names `base.sha` and `head.sha`
178
+ // (pull-dependencies.ts pullCommits). None where either isn't a commit's id.
179
+ const commitsOf = (number: number, { baseRefOid: base, headRefOid: head }: Node) =>
180
+ typeof base === 'string' && SHA.test(base) && typeof head === 'string' && SHA.test(head) ? [{ number, base, head }] : []
181
+
182
+ // The label the pull requests changing a repository's dependencies carry,
183
+ // as Dependabot's do: where it has one, a team's Dependencies reads its open
184
+ // pull requests carrying it alone (http.ts teamOpenPulls).
185
+ export const DEPENDENCIES_LABEL = 'dependencies'
186
+ const LABEL_QUERY = `query($owner: String!, $name: String!, $label: String!) { repository(owner: $owner, name: $name) { label(name: $label) { name } } }`
187
+ const labelRefused = (error: GraphqlError) => error?.type === 'FORBIDDEN' && Array.isArray(error.path) && error.path.includes('label')
188
+
189
+ // The repository's label named `label`, by its name as the repository has
190
+ // it, which its pull requests are read by; null where it has none, or GitHub
191
+ // refuses to tell.
192
+ export async function repositoryLabel(fullName: string, token: string, label: string, fetchImpl: typeof fetch = globalThis.fetch) {
193
+ const [owner, name] = fullName.split('/') as [string, string]
194
+ const data = await githubGraphql(LABEL_QUERY, { owner, name, label }, token, fetchImpl, labelRefused) as { repository?: { label?: { name?: unknown } | null } | null }
195
+ const named = data?.repository?.label?.name
196
+ return { label: typeof named === 'string' && named !== '' ? named : null }
197
+ }
198
+
199
+ // A page of the repository's issues or pull requests in `state`, after the
200
+ // page `after` ended (null for the first): its `items`, how many are open and
201
+ // closed (`counts`), and, where more follow, the cursor reading them
202
+ // (`after`); open pull requests' commits (`commits`, commitsOf) too, which
203
+ // the code viewer is never told (http.ts handleRepositoryIssues). With
204
+ // `label`, those carrying it alone.
205
+ export async function repositoryIssues(fullName: string, token: string, kind: IssuesKind, state: IssuesState, after: string | null, fetchImpl: typeof fetch = globalThis.fetch,
206
+ label: string | null = null) {
207
+ const [owner, name] = fullName.split('/') as [string, string]
208
+ const data = await githubGraphql(pageQuery(kind, state, label !== null), { owner, name, after, ...label === null ? {} : { labels: [label] } }, token, fetchImpl, partRefused) as Page
209
+ const repository = data?.repository
210
+ if (!repository) throw new GithubApiError(404, 'github-not-found')
211
+ const info = repository.page?.pageInfo
212
+ const next = info?.hasNextPage === true && typeof info.endCursor === 'string' && ISSUES_CURSOR.test(info.endCursor) ? info.endCursor : null
213
+ const open = kind === 'pulls' && state === 'open'
214
+ const tiles = (repository.page?.nodes ?? []).flatMap(node => {
215
+ const tile = item(kind, node)
216
+ return tile && node ? [{ node, tile }] : []
217
+ })
218
+ return {
219
+ items: tiles.map(({ node, tile }) => open ? { ...tile, reviews: pullReviews(node, tile.author) } : tile),
220
+ counts: { open: count(repository.open?.totalCount), closed: count(repository.closed?.totalCount) },
221
+ more: next !== null, after: next,
222
+ ...open ? { commits: tiles.flatMap(({ node, tile }) => commitsOf(tile.number, node)) } : {},
223
+ }
224
+ }
@@ -0,0 +1,27 @@
1
+ import type { StoredUser } from './db.ts'
2
+
3
+ const WHITEHAT = [
4
+ 291301, // ChALkeR
5
+ 259122746, // exo-nikita
6
+ 247161625, // ex0sec
7
+ ]
8
+ const whitehatIds = new Set(WHITEHAT)
9
+
10
+ // (1) Server permission to add repositories. Keep this independent of GitHub
11
+ // identity and ecosystem exceptions if other install modes broaden it later.
12
+ export function canAddRepositories(user: StoredUser): boolean {
13
+ return user.role === 'admin'
14
+ }
15
+
16
+ // (3b) Only the ecosystem safeguard for public repositories is bypassed by
17
+ // WHITEHAT. Callers must separately establish (0) server readability, (1) add
18
+ // permission, and (2) server admin or GitHub access to this repository.
19
+ export function passesPublicRepositorySafeguard(githubUserId: number | null, involved: boolean): boolean {
20
+ return involved || (githubUserId != null && whitehatIds.has(githubUserId))
21
+ }
22
+
23
+ // Arbitrary public repositories have no involvement evidence, so (2) requires
24
+ // admin even if (1) expands in the future. This capability is also the UI gate.
25
+ export function canAddAnyPublicRepository(user: StoredUser, githubUserId: number | null): boolean {
26
+ return canAddRepositories(user) && user.role === 'admin' && passesPublicRepositorySafeguard(githubUserId, false)
27
+ }
@@ -0,0 +1,207 @@
1
+ // A repository's pull requests and its issues, the most recently updated
2
+ // first, each kept once for all of those that read them so (http.ts
3
+ // keptList): the Pulse, telling whose its year's commits are by the pull
4
+ // requests merged (agent-merges.ts agentMerges) and counting those of a
5
+ // period (repository-pulse.ts listedActivity), and the Agents tab, its pages
6
+ // of pull requests (repository-agents.ts agentPulls) and its latest issues.
7
+ // Those updated since the Pulse's year's start, a few queries an answer, the
8
+ // rest read by the next, and those updated since read as they are. An issue
9
+ // kept as both show it; a pull request as the Agents tab shows it
10
+ // (repository-agents.ts pull: the marks an agent left in its texts, never
11
+ // the texts) and, one merged, what it brought the default branch, as the
12
+ // Pulse tells it: an agent that
13
+ // commits as whoever ran it (Codex's own) leaves its commits no mark, its
14
+ // pull request's branch alone naming it (app-agents.ts
15
+ // AGENT_BRANCH_PREFIXES), or a label one who ran it gave it naming the agent
16
+ // (agent-tasks.ts labelAgents), as the Agents tab tells an agent's work; or
17
+ // its author, an account agents work as (github-agents.ts AGENT_OPERATED),
18
+ // or any GitHub App's bot.
19
+ import { type AgentIssue, type AgentPull, labelAgents } from '../common/managed/agent-tasks.ts'
20
+ import { GITHUB_AGENTS, agentOperated } from '../common/managed/github-agents.ts'
21
+ import { AGENT_BRANCH_PREFIXES } from './app-agents.ts'
22
+ import { AGENT_ISSUE_FIELDS, AGENT_PULL_FIELDS, type IssueNode, type PullNode, issue, pull } from './repository-agents.ts'
23
+ import { githubGraphql } from './github-graphql.ts'
24
+ import { SHA, count, timeOf } from './github-values.ts'
25
+ import { type Connection, nextCursor } from './repository-history.ts'
26
+ import { ISSUE_REASONS, type IssueReason } from './repository-issues.ts'
27
+
28
+ // What the pull requests kept are read by, its version: bumped as what a
29
+ // page reads, or how one is told, changes, its pull requests read again
30
+ // (http.ts keptList keys them by AGENTS_READ_VERSION and the Pulse's
31
+ // PULSE_YEAR_VERSION too, as either changes how it tells an agent's); and
32
+ // the issues kept.
33
+ export const PULLS_READ_VERSION = 2
34
+ export const ISSUES_LIST_READ_VERSION = 1
35
+ // The pull requests or issues read an answer, a page a query, as many
36
+ // queries as LIST_QUERIES, the rest left for the next to ask; of each pull
37
+ // request merged, as many of its latest commits as MERGED_COMMITS. Those
38
+ // updated since the newest read, few as they mostly are, read TOP_UP_PAGE
39
+ // first, the rest LIST_PAGE a page (GitHub reading on from a page's end
40
+ // whatever its size).
41
+ const LIST_PAGE = 100
42
+ const TOP_UP_PAGE = 10
43
+ const LIST_QUERIES = 5
44
+ const MERGED_COMMITS = 100
45
+ const LABEL_AGENTS = Object.keys(GITHUB_AGENTS)
46
+
47
+
48
+ // A pull request as read: its row on the Agents tab, whether a GitHub App's
49
+ // bot opened it (`bot`, GitHub's GraphQL API naming one by its app's login
50
+ // alone), and one merged, what it brought the default branch (`merged`).
51
+ export type KeptPull = AgentPull & { bot?: true; merged?: Merged }
52
+ // What a merged pull request brought: its merge commit (`sha`), when it was
53
+ // merged (`at`) and whether it was an agent's (agentPull); an agent's merged
54
+ // with a merge commit, which a year counts none of, its own commits too
55
+ // (`own`, its latest MERGED_COMMITS); and of one of more commits whose merge
56
+ // commit has one parent, its commits' subjects, the oldest first, for those
57
+ // it was rebased as to be told by (agent-merges.ts agentCommits), those of
58
+ // its empty commits left out, as a rebase leaves them, and how many more it
59
+ // had than those read (`more`).
60
+ export interface Merged { sha: string; at: number; agent: boolean; own?: string[]; subjects?: string[]; more?: number }
61
+ // An issue as read: as the Agents tab shows it, whether a bot opened it
62
+ // (`bot`, as a pull request's), when it was closed, and why (`reason`, as
63
+ // GitHub tells it: completed, not planned or a duplicate).
64
+ export type KeptIssue = AgentIssue & { bot?: true; closedAt: number | null; reason: IssueReason | null }
65
+ // The pull requests or issues updated since a year's start (`since`) as
66
+ // read so far (readList), the most recently updated first (`rows`); when the
67
+ // newest read was last updated, those updated since read again (`through`,
68
+ // null till all are read once), and where those read reach back no further
69
+ // than was needed, when the oldest of them was updated (`reach`); whether
70
+ // every one the repository has is read (`all`); and a read not finished:
71
+ // where it goes on (`after`), when the newest it began with was updated
72
+ // (`from`), the oldest it reads to (`stop`), how many it has read (`read`)
73
+ // and when the oldest of them was updated (`to`).
74
+ export interface Listed<Row> { rows: Row[]; since: number; through: number | null; reach?: number; all?: true
75
+ next: { after: string; from: number; stop: number; read: number; to?: number } | null }
76
+ export type PullList = Listed<KeptPull>
77
+ export type IssueList = Listed<KeptIssue>
78
+
79
+ // Since when every one updated is read: all of them since the year's
80
+ // start, -∞; none yet, ∞.
81
+ export const pullsReadTo = (list: Pick<Listed<unknown>, 'next' | 'reach'>) => list.next ? list.next.to ?? Number.POSITIVE_INFINITY : list.reach ?? Number.NEGATIVE_INFINITY
82
+
83
+ const PULLS_QUERY = `query($owner: String!, $name: String!, $after: String, $first: Int!) {
84
+ repository(owner: $owner, name: $name) {
85
+ pullRequests(first: $first, after: $after, orderBy: { field: UPDATED_AT, direction: DESC }) {
86
+ pageInfo { hasNextPage endCursor }
87
+ nodes { ${AGENT_PULL_FIELDS} mergedAt mergeCommit { oid parents { totalCount } }
88
+ commits(last: ${MERGED_COMMITS}) { totalCount nodes { commit { oid messageHeadline changedFilesIfAvailable } } } }
89
+ }
90
+ }
91
+ }`
92
+ type MergedNode = PullNode & { mergedAt?: unknown; mergeCommit?: { oid?: unknown; message?: unknown; parents?: { totalCount?: unknown } | null } | null
93
+ author?: PullNode['author'] & { __typename?: unknown }
94
+ commits?: { totalCount?: unknown; nodes?: ({ commit?: { oid?: unknown; messageHeadline?: unknown; changedFilesIfAvailable?: unknown } | null } | null)[] } | null }
95
+ const ISSUES_QUERY = `query($owner: String!, $name: String!, $after: String, $first: Int!) {
96
+ repository(owner: $owner, name: $name) {
97
+ issues(first: $first, after: $after, orderBy: { field: UPDATED_AT, direction: DESC }) {
98
+ pageInfo { hasNextPage endCursor }
99
+ nodes { ${AGENT_ISSUE_FIELDS} stateReason closedAt }
100
+ }
101
+ }
102
+ }`
103
+ type ClosedNode = IssueNode & { stateReason?: unknown; closedAt?: unknown; author?: IssueNode['author'] & { __typename?: unknown } }
104
+ const botOf = (author: { __typename?: unknown } | null | undefined) => author?.__typename === 'Bot' ? { bot: true as const } : {}
105
+ type ListPage<Node> = { repository?: Record<string, Connection<Node> | null> | null }
106
+
107
+ const agentBranch = (name: unknown) => typeof name === 'string' && AGENT_BRANCH_PREFIXES.some(prefix => name.startsWith(prefix))
108
+ // Whether a pull request is an agent's: by its branch, a label naming one,
109
+ // or its author, an account agents work as or a bot (GitHub's GraphQL API
110
+ // naming a bot by its app's login alone, its type telling it).
111
+ const agentPull = ({ headRefName, labels, author }: MergedNode) => agentBranch(headRefName)
112
+ || labelAgents((labels?.nodes ?? []).map(label => label?.name), LABEL_AGENTS).length > 0 || author?.__typename === 'Bot'
113
+ || agentOperated(typeof author?.login === 'string' ? author.login : null, Number.isSafeInteger(author?.databaseId) ? author!.databaseId as number : null)
114
+
115
+ // What a pull request merged brought (Merged); null for one not merged.
116
+ function mergedOf(node: MergedNode): Merged | null {
117
+ const [at, sha] = [timeOf(node.mergedAt), node.mergeCommit?.oid]
118
+ if (at === null || typeof sha !== 'string' || !SHA.test(sha)) return null
119
+ const commits = (node.commits?.nodes ?? []).map(item => item?.commit)
120
+ const merging = count(node.mergeCommit?.parents?.totalCount) > 1
121
+ const copied = commits.filter(commit => commit?.changedFilesIfAvailable !== 0)
122
+ // Of one of more commits than its latest read, how many more it had.
123
+ const more = Math.max(0, count(node.commits?.totalCount) - commits.length)
124
+ const subjects = !merging && (copied.length > 1 || more > 0) && copied.length > 0 && copied.every(commit => typeof commit?.messageHeadline === 'string')
125
+ ? copied.map(commit => commit!.messageHeadline as string) : null
126
+ const agent = agentPull(node)
127
+ const own = agent && merging ? commits.flatMap(commit => typeof commit?.oid === 'string' && SHA.test(commit.oid) ? [commit.oid] : []) : []
128
+ return { sha, at, agent, ...own.length > 0 ? { own } : {}, ...subjects ? { subjects, ...more > 0 ? { more } : {} } : {} }
129
+ }
130
+
131
+ // What an issue closed tells beside its row (KeptIssue).
132
+ function closedOf(node: ClosedNode): KeptIssue | null {
133
+ const row = issue(node)
134
+ return row ? { ...row, ...botOf(node.author), closedAt: timeOf(node.closedAt), reason: ISSUE_REASONS[String(node.stateReason)] ?? null } : null
135
+ }
136
+
137
+ // The most recently updated first, the higher number first of those
138
+ // updated together.
139
+ const newestFirst = (a: { number: number; updatedAt: number | null }, b: { number: number; updatedAt: number | null }) =>
140
+ (b.updatedAt ?? 0) - (a.updatedAt ?? 0) || b.number - a.number
141
+
142
+ // How a list is read: the query of its pages, the repository's connection
143
+ // they are of, and what each of them is kept as.
144
+ interface ListSpec<Node, Row> { query: string; connection: 'pullRequests' | 'issues'; row(node: Node): Row | null }
145
+ const PULLS: ListSpec<MergedNode, KeptPull> = { query: PULLS_QUERY, connection: 'pullRequests', row: node => {
146
+ const row = pull(node)
147
+ const merged = mergedOf(node)
148
+ return row ? { ...row, ...botOf(node.author), ...merged ? { merged } : {} } : null
149
+ } }
150
+ const ISSUES: ListSpec<ClosedNode, KeptIssue> = { query: ISSUES_QUERY, connection: 'issues', row: closedOf }
151
+
152
+ // The pull requests updated since `since` (readList), and what each merged
153
+ // brought.
154
+ export const readPulls = (fullName: string, token: string, since: number, kept: PullList | null, fetchImpl: typeof fetch = globalThis.fetch, need = since) =>
155
+ readList(PULLS, fullName, token, since, kept, fetchImpl, need)
156
+ // The issues updated since `since` (readList), and when and why each closed was.
157
+ export const readIssues = (fullName: string, token: string, since: number, kept: IssueList | null, fetchImpl: typeof fetch = globalThis.fetch, need = since) =>
158
+ readList(ISSUES, fullName, token, since, kept, fetchImpl, need)
159
+
160
+ // Those of a list (`spec`) updated since `since`, read the most recently
161
+ // updated first (a pull request is updated when merged, or after, an issue
162
+ // when closed), on from what was `kept`: till one updated before the year,
163
+ // or before the newest read before was, as many queries an answer as
164
+ // LIST_QUERIES, a read not finished going on from where it was left by the
165
+ // next, or once those updated since `need` are all read; where those read
166
+ // reached no further back than they needed, and `need` is further, read
167
+ // again from the newest on, as far back as it needs. Each kept as last read,
168
+ // one updated before the year forgotten.
169
+ async function readList<Node extends { updatedAt?: unknown }, Row extends { number: number; updatedAt: number | null }>(spec: ListSpec<Node, Row>,
170
+ fullName: string, token: string, since: number, kept: Listed<Row> | null, fetchImpl: typeof fetch, need: number): Promise<Listed<Row>> {
171
+ const [owner, name] = fullName.split('/') as [string, string]
172
+ const rows = new Map((kept?.rows ?? []).filter(item => (item.updatedAt ?? 0) >= since).map(item => [item.number, item]))
173
+ // Every one read, as long as none was forgotten, the year begun since.
174
+ const whole = kept?.all === true && rows.size === kept.rows.length
175
+ const further = !kept?.next && kept?.reach !== undefined && kept.reach > need
176
+ const stop = kept?.next?.stop ?? (further ? since : Math.max(since, kept?.through ?? since))
177
+ const result = (through: number | null, next: Listed<Row>['next'], reach: number | undefined, all = whole): Listed<Row> => ({
178
+ rows: [...rows.values()].toSorted(newestFirst), since, through, ...reach === undefined ? {} : { reach }, ...all ? { all: true as const } : {}, next })
179
+ // A read of those updated since the newest read alone.
180
+ const topping = !kept?.next && !further && kept?.through !== null && kept?.through !== undefined
181
+ let { after = null, from = null, read = 0, to = null }: { after?: string | null; from?: number | null; read?: number; to?: number | null } = kept?.next ?? {}
182
+ for (let queries = 0; queries < LIST_QUERIES; queries++) {
183
+ const first = topping && queries === 0 ? TOP_UP_PAGE : LIST_PAGE
184
+ const page = (await githubGraphql(spec.query, { owner, name, after, first }, token, fetchImpl) as ListPage<Node> | null)?.repository?.[spec.connection]
185
+ const cursor = nextCursor(page?.pageInfo), end = cursor === null
186
+ let crossed = false
187
+ for (const node of page?.nodes ?? []) {
188
+ const updated = timeOf(node?.updatedAt)
189
+ if (!node || updated === null) continue
190
+ if (updated < stop) { crossed = true; break }
191
+ from ??= updated
192
+ to = updated
193
+ read++
194
+ const row = spec.row(node)
195
+ if (row) rows.set(row.number, row)
196
+ }
197
+ // Read as far back as it reads to: the year's start, every one since
198
+ // read; else as far as those read before reached. Read to the last
199
+ // the repository has, every one of them.
200
+ if (crossed || end) return result(from ?? stop, null, stop > since ? kept?.reach : undefined, (end && !crossed) || (whole && stop > since))
201
+ after = cursor
202
+ // Read as far back as `need`: finished, so far back, those updated
203
+ // since read by the next as ever.
204
+ if (need > since && to !== null && to < need) return result(Math.max(from ?? stop, kept?.through ?? Number.NEGATIVE_INFINITY), null, to)
205
+ }
206
+ return result(kept?.through ?? null, { after: after!, from: from ?? stop, stop, read, ...to === null ? {} : { to } }, kept?.reach)
207
+ }