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

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 (406) 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 +35 -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 +34 -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-2SY4PYUQ.js +4 -0
  92. package/out/chunk-2XUEZHIP.js +28 -0
  93. package/out/chunk-4EMM6UCD.js +1 -0
  94. package/out/chunk-4HI2MSAV.js +17 -0
  95. package/out/chunk-4ISO5F7Y.js +7 -0
  96. package/out/chunk-4TQKM3JC.js +42 -0
  97. package/out/chunk-4XIH7YTX.js +1 -0
  98. package/out/chunk-6VDDCFHT.js +3 -0
  99. package/out/chunk-74AEKRES.js +10 -0
  100. package/out/chunk-7BKKYVWR.js +7 -0
  101. package/out/chunk-7C7DWL7I.js +1 -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-EEQHVKR3.js +1 -0
  106. package/out/chunk-G3LY2FV4.js +1 -0
  107. package/out/chunk-GO7FT4OD.js +1 -0
  108. package/out/chunk-HAZKDK4M.js +4 -0
  109. package/out/chunk-HNQNDNIE.js +12 -0
  110. package/out/chunk-HOB3YPS5.js +1 -0
  111. package/out/chunk-IYPY47MC.js +1 -0
  112. package/out/chunk-IZ6MYRYB.js +3 -0
  113. package/out/chunk-J2FWBXFV.js +1 -0
  114. package/out/chunk-JCIIBLKR.js +1 -0
  115. package/out/chunk-JFL7WLRV.js +1 -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-OFPDMZJF.js +244 -0
  123. package/out/chunk-OOJM4CTU.js +1 -0
  124. package/out/chunk-OQRSIBSC.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-QMVOEBWC.js +13 -0
  131. package/out/chunk-QRKFPLA4.js +80 -0
  132. package/out/chunk-R3NZFXVR.js +1 -0
  133. package/out/chunk-R5O6FAW4.js +2 -0
  134. package/out/chunk-RRBIJL2B.js +1 -0
  135. package/out/chunk-SYXT3SFP.js +2 -0
  136. package/out/chunk-TOXMUOIW.js +5 -0
  137. package/out/chunk-TUII34K2.js +1 -0
  138. package/out/chunk-TZS7KERJ.js +52 -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-VZJIHHTQ.js +1 -0
  143. package/out/chunk-WCPJDAYA.js +10 -0
  144. package/out/chunk-WMFDQS2L.js +11 -0
  145. package/out/chunk-WREU7JAG.js +1 -0
  146. package/out/chunk-Y5ZHFNBD.js +2 -0
  147. package/out/chunk-YF4M5OCI.js +1 -0
  148. package/out/chunk-YGRRXC2L.js +1 -0
  149. package/out/chunk-YTO6AQZM.js +2 -0
  150. package/out/chunk-Z667U2UJ.js +1 -0
  151. package/out/chunk-ZQHLECTI.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 +35 -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 +379 -62
  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 +258 -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 +17 -0
  249. package/server-managed/brotli.ts +10 -0
  250. package/server-managed/bundle-advisories.ts +102 -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 +203 -0
  256. package/server-managed/bundle-build.ts +232 -0
  257. package/server-managed/bundle-cache.ts +211 -0
  258. package/server-managed/bundle-catalog.ts +29 -0
  259. package/server-managed/bundle-commits.ts +140 -0
  260. package/server-managed/bundle-dependency-reviews.ts +74 -0
  261. package/server-managed/bundle-store.ts +24 -0
  262. package/server-managed/bundle-summary-cache.ts +130 -0
  263. package/server-managed/bundle-validation.ts +32 -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 +47 -0
  268. package/server-managed/comments.ts +151 -0
  269. package/server-managed/commit-coauthors.ts +106 -0
  270. package/server-managed/commit-lockfiles.ts +76 -0
  271. package/server-managed/comparison.ts +52 -0
  272. package/server-managed/config.ts +164 -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 +1958 -0
  276. package/server-managed/db-neon.ts +484 -0
  277. package/server-managed/db-schema.ts +238 -0
  278. package/server-managed/db-table-names.ts +22 -0
  279. package/server-managed/db.ts +152 -0
  280. package/server-managed/dependabot-access.ts +60 -0
  281. package/server-managed/dependabot-alerts.ts +185 -0
  282. package/server-managed/dependency-review-comments.ts +98 -0
  283. package/server-managed/dependency-reviews.ts +364 -0
  284. package/server-managed/github-accounts.ts +60 -0
  285. package/server-managed/github-actions.ts +71 -0
  286. package/server-managed/github-app.ts +376 -0
  287. package/server-managed/github-errors.ts +70 -0
  288. package/server-managed/github-graphql.ts +39 -0
  289. package/server-managed/github-issue-links.ts +244 -0
  290. package/server-managed/github-issue-oauth.ts +47 -0
  291. package/server-managed/github-issues.ts +155 -0
  292. package/server-managed/github-link-oauth.ts +69 -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 +238 -0
  296. package/server-managed/github-pulls.ts +246 -0
  297. package/server-managed/github-requests.ts +166 -0
  298. package/server-managed/github-users.ts +30 -0
  299. package/server-managed/history-windows.ts +172 -0
  300. package/server-managed/http-response.ts +76 -0
  301. package/server-managed/http.ts +5855 -0
  302. package/server-managed/import-triage.ts +51 -0
  303. package/server-managed/index.ts +204 -0
  304. package/server-managed/kept-briefly.ts +37 -0
  305. package/server-managed/kept.ts +28 -0
  306. package/server-managed/link-reports.ts +162 -0
  307. package/server-managed/listed-activity.ts +39 -0
  308. package/server-managed/locked-packages.ts +152 -0
  309. package/server-managed/lockfile-scope.ts +49 -0
  310. package/server-managed/lru.ts +30 -0
  311. package/server-managed/managed-issues.ts +92 -0
  312. package/server-managed/management-catalog.ts +45 -0
  313. package/server-managed/management.ts +99 -0
  314. package/server-managed/npm-delta-worker.js +23 -0
  315. package/server-managed/npm-delta.ts +194 -0
  316. package/server-managed/npm-insights.ts +450 -0
  317. package/server-managed/npm-kept.ts +233 -0
  318. package/server-managed/npm-loads.ts +264 -0
  319. package/server-managed/npm-manifest.ts +115 -0
  320. package/server-managed/npm-packages.ts +454 -0
  321. package/server-managed/npm-patch.ts +325 -0
  322. package/server-managed/npm-provenance.ts +123 -0
  323. package/server-managed/npm-risk.ts +285 -0
  324. package/server-managed/object-storage-disk.ts +148 -0
  325. package/server-managed/object-storage-vercel.ts +67 -0
  326. package/server-managed/object-storage.ts +75 -0
  327. package/server-managed/package-entry-points.ts +79 -0
  328. package/server-managed/pretty-print.ts +436 -0
  329. package/server-managed/public-workspace.ts +238 -0
  330. package/server-managed/pull-comment-reviews.ts +161 -0
  331. package/server-managed/pull-dependencies.ts +351 -0
  332. package/server-managed/pull-dependency-reviews.ts +334 -0
  333. package/server-managed/pull-reviews.ts +205 -0
  334. package/server-managed/queue.ts +39 -0
  335. package/server-managed/release-commits.ts +84 -0
  336. package/server-managed/rename-diff-worker.js +26 -0
  337. package/server-managed/rename-diff.ts +279 -0
  338. package/server-managed/repo-path.ts +21 -0
  339. package/server-managed/report-migration.ts +35 -0
  340. package/server-managed/report-query.ts +10 -0
  341. package/server-managed/report-response.ts +16 -0
  342. package/server-managed/report-sources.ts +185 -0
  343. package/server-managed/report-store.ts +85 -0
  344. package/server-managed/repository-actions.ts +221 -0
  345. package/server-managed/repository-agents.ts +448 -0
  346. package/server-managed/repository-aliases.ts +91 -0
  347. package/server-managed/repository-browser.ts +301 -0
  348. package/server-managed/repository-changes.ts +284 -0
  349. package/server-managed/repository-code.ts +215 -0
  350. package/server-managed/repository-dependency-reviews.ts +166 -0
  351. package/server-managed/repository-discovery.ts +82 -0
  352. package/server-managed/repository-history.ts +432 -0
  353. package/server-managed/repository-issues.ts +195 -0
  354. package/server-managed/repository-policy.ts +27 -0
  355. package/server-managed/repository-pulls.ts +212 -0
  356. package/server-managed/repository-pulse.ts +471 -0
  357. package/server-managed/repository-risk-assess.ts +105 -0
  358. package/server-managed/repository-risk-audit.ts +78 -0
  359. package/server-managed/repository-risk-read.ts +170 -0
  360. package/server-managed/repository-risk.ts +491 -0
  361. package/server-managed/repository-squashes.ts +168 -0
  362. package/server-managed/repository-stamp.ts +41 -0
  363. package/server-managed/request-timing.ts +63 -0
  364. package/server-managed/reviewed-dependencies.ts +149 -0
  365. package/server-managed/revisions.ts +35 -0
  366. package/server-managed/scan-downloads.ts +87 -0
  367. package/server-managed/session.ts +132 -0
  368. package/server-managed/slugs.ts +38 -0
  369. package/server-managed/solidity-entry-points.ts +71 -0
  370. package/server-managed/sql-postgres.ts +34 -0
  371. package/server-managed/sql.ts +89 -0
  372. package/server-managed/stasis-config.ts +55 -0
  373. package/server-managed/static.ts +28 -0
  374. package/server-managed/storage-command.ts +16 -0
  375. package/server-managed/storage-db.ts +183 -0
  376. package/server-managed/storage-encryption.ts +290 -0
  377. package/server-managed/storage-maintenance.ts +259 -0
  378. package/server-managed/storage-payload.ts +121 -0
  379. package/server-managed/storage-stores.ts +91 -0
  380. package/server-managed/storage.ts +82 -0
  381. package/server-managed/team-app-summaries.ts +54 -0
  382. package/server-managed/team-app.ts +157 -0
  383. package/server-managed/team-catalog.ts +7 -0
  384. package/server-managed/team-feed.ts +180 -0
  385. package/server-managed/team-npm-scopes.ts +106 -0
  386. package/server-managed/team-reports.ts +351 -0
  387. package/server-managed/triage-response.ts +16 -0
  388. package/server-managed/type-only-worker.js +19 -0
  389. package/server-managed/type-only.ts +105 -0
  390. package/server-managed/uploads.ts +106 -0
  391. package/server-managed/upstream-cache.ts +252 -0
  392. package/server-managed/view-as.ts +58 -0
  393. package/server-managed/workspace-shares.ts +171 -0
  394. package/server.ts +70 -0
  395. package/server/index.ts +0 -480
  396. package/server/lifecycle.ts +0 -208
  397. /package/{LICENSE → LICENSE-MIT} +0 -0
  398. /package/{server → server-e2e}/auth.ts +0 -0
  399. /package/{server → server-e2e}/config.example.json +0 -0
  400. /package/{server → server-e2e}/objstore/blob-fs.ts +0 -0
  401. /package/{server → server-e2e}/objstore/fs.ts +0 -0
  402. /package/{server → server-e2e}/objstore/sign.ts +0 -0
  403. /package/{server → server-e2e}/peer.ts +0 -0
  404. /package/{server → server-e2e}/sse-session.ts +0 -0
  405. /package/{server → server-e2e}/util.ts +0 -0
  406. /package/{server → server-e2e}/validation.ts +0 -0
@@ -0,0 +1,238 @@
1
+ import { DEPENDENCY_REVIEW_SCHEMA } from './dependency-reviews.ts'
2
+ import { LINK_REPORT_SCHEMA } from './link-reports.ts'
3
+ import { REPOSITORY_ALIAS_SCHEMA } from './repository-aliases.ts'
4
+ import { WORKSPACE_SHARE_SCHEMA } from './workspace-shares.ts'
5
+ import { TEAM_NPM_SCOPE_SCHEMA } from './team-npm-scopes.ts'
6
+ import { GITHUB_LINK_SCHEMA } from './github-links.ts'
7
+
8
+ // Created after the column migration, which predates it on older databases.
9
+ export const VIEW_SESSION_INDEX = 'CREATE INDEX IF NOT EXISTS managed_session_viewer_idx ON managed_session(viewer_session);'
10
+ // What a team's scopes join reports and bundles on: their repository, and the
11
+ // directory in it. Created after the column migrations, as above.
12
+ export const REPO_UPLOAD_INDEXES = `CREATE INDEX IF NOT EXISTS managed_report_repo_idx ON managed_report(repo_id, repo_directory);
13
+ CREATE INDEX IF NOT EXISTS managed_bundle_repo_idx ON managed_bundle(repo_id, repo_directory);`
14
+
15
+ export const MANAGED_SCHEMA = `
16
+ CREATE TABLE IF NOT EXISTS managed_user (
17
+ id TEXT PRIMARY KEY,
18
+ github_user_id INTEGER NOT NULL UNIQUE,
19
+ login TEXT NOT NULL,
20
+ name TEXT,
21
+ avatar_url TEXT,
22
+ role TEXT NOT NULL DEFAULT 'none',
23
+ -- GitHub user-to-server token, persisted so the repositories page can list
24
+ -- the user's repos (GET /user/repos) on demand. Refresh token + expiry are
25
+ -- null for non-expiring tokens (GitHub App with expiring tokens disabled).
26
+ gh_access_token TEXT,
27
+ gh_refresh_token TEXT,
28
+ gh_token_expires_at INTEGER,
29
+ created_at INTEGER NOT NULL,
30
+ updated_at INTEGER NOT NULL,
31
+ -- Presence is independent of admin edits and background token refreshes.
32
+ -- NULL for legacy accounts until a known session authenticates them.
33
+ last_seen_at INTEGER
34
+ ) STRICT;
35
+
36
+ CREATE TABLE IF NOT EXISTS managed_session (
37
+ id TEXT PRIMARY KEY,
38
+ user_id TEXT NOT NULL REFERENCES managed_user(id) ON DELETE CASCADE,
39
+ csrf_token TEXT NOT NULL,
40
+ created_at INTEGER NOT NULL,
41
+ expires_at INTEGER NOT NULL,
42
+ -- Private X25519 JWK that this session's uploads are sealed to (see
43
+ -- common/managed/upload-seal.ts). Created on first use.
44
+ upload_key TEXT,
45
+ -- An administrator's read-only view as user_id: the admin's own session,
46
+ -- which must stay valid. Ending that session ends the view.
47
+ viewer_session TEXT REFERENCES managed_session(id) ON DELETE CASCADE
48
+ ) STRICT;
49
+
50
+ CREATE INDEX IF NOT EXISTS managed_session_user_idx ON managed_session(user_id);
51
+ CREATE INDEX IF NOT EXISTS managed_session_expires_idx ON managed_session(expires_at);
52
+
53
+ -- Repositories selected to operate on. Keyed by GitHub's numeric repo id
54
+ -- (stable across renames). The row carries everything needed to read the repo's
55
+ -- contents server-side later: installation_id mints an App installation token
56
+ -- (Contents: Read) for PRIVATE repos — NULL means a PUBLIC repo readable without
57
+ -- the App — and full_name + default_branch locate the contents. added_by is the
58
+ -- selector, nulled (not cascaded) if that user is removed so the selection
59
+ -- survives.
60
+ CREATE TABLE IF NOT EXISTS managed_selected_repo (
61
+ repo_id INTEGER PRIMARY KEY,
62
+ full_name TEXT NOT NULL,
63
+ is_private INTEGER NOT NULL,
64
+ installation_id INTEGER,
65
+ default_branch TEXT NOT NULL,
66
+ -- Live browser hint; independent of the default discovered at selection.
67
+ cached_default_branch TEXT,
68
+ html_url TEXT NOT NULL,
69
+ added_by TEXT REFERENCES managed_user(id) ON DELETE SET NULL,
70
+ added_at INTEGER NOT NULL,
71
+ updated_at INTEGER NOT NULL,
72
+ active INTEGER NOT NULL DEFAULT 1,
73
+ -- Its pull requests' dependencies reviewed (repository-dependency-reviews.ts):
74
+ -- as many published reviews required of each version, and the commit at
75
+ -- whose lockfiles' versions none is (null for none).
76
+ dependency_review INTEGER NOT NULL DEFAULT 0,
77
+ dependency_reviews_required INTEGER NOT NULL DEFAULT 1,
78
+ dependency_review_since TEXT
79
+ ) STRICT;
80
+
81
+ CREATE INDEX IF NOT EXISTS selected_repo_full_name_idx ON managed_selected_repo(full_name);
82
+
83
+ -- Bundles uploaded to the server (the "Manage bundles" page). Like reports, the
84
+ -- bytes are stored in the clear (blob-store, keyed by this opaque uuid id) for
85
+ -- the server to operate on. integrity is the content hash (sha512-<base64>,
86
+ -- byte-identical to the client's) so re-uploading the same bytes dedupes (UNIQUE);
87
+ -- it is also the key a report's bundleHashes match to auto-link.
88
+ -- uploaded_by / repo_id are the (nullable) user + repo links, both nulled (not
89
+ -- cascaded) when the referenced user / selected repo goes away.
90
+ CREATE TABLE IF NOT EXISTS managed_bundle (
91
+ id TEXT PRIMARY KEY,
92
+ slug TEXT NOT NULL,
93
+ integrity TEXT NOT NULL UNIQUE,
94
+ filename TEXT NOT NULL,
95
+ kind TEXT,
96
+ byte_size INTEGER NOT NULL,
97
+ uploaded_by TEXT REFERENCES managed_user(id) ON DELETE SET NULL,
98
+ -- Durable snapshot of the uploader's login at upload time, so "who uploaded
99
+ -- this" survives the uploader being removed (when uploaded_by nulls out).
100
+ uploaded_by_login TEXT,
101
+ repo_id INTEGER REFERENCES managed_selected_repo(repo_id) ON DELETE SET NULL,
102
+ repo_directory TEXT NOT NULL DEFAULT '',
103
+ visible INTEGER NOT NULL DEFAULT 1,
104
+ uploaded_at INTEGER NOT NULL,
105
+ -- 'upload' or 'build' (generated on this server). A server build of bytes
106
+ -- already stored promotes the row to 'build'; NULL predates the column.
107
+ provenance TEXT,
108
+ -- JSON {preset, conditions, platforms} of a JS/TS server build; NULL otherwise.
109
+ build_conditions TEXT
110
+ ) STRICT;
111
+
112
+ CREATE INDEX IF NOT EXISTS managed_bundle_uploaded_at_idx ON managed_bundle(uploaded_at);
113
+
114
+ -- Reports uploaded to the server (the "Manage reports" page). A managed server
115
+ -- is TRUSTED, so the bytes are stored in the clear (blob-store, keyed by this
116
+ -- opaque uuid id) for the server to operate on later; this row carries the
117
+ -- metadata + attribution. uploaded_by / repo_id are the (nullable) user + repo
118
+ -- links, nulled (not cascaded) when the user / selected repo goes away. bundle_id
119
+ -- is the (nullable) auto-resolved link to a stored bundle; bundle_integrity is
120
+ -- the report's declared primary bundle (from its bundleHashes), kept so a later
121
+ -- bundle upload of that integrity can re-link.
122
+ CREATE TABLE IF NOT EXISTS managed_report (
123
+ id TEXT PRIMARY KEY,
124
+ slug TEXT NOT NULL,
125
+ filename TEXT NOT NULL,
126
+ content_type TEXT NOT NULL,
127
+ byte_size INTEGER NOT NULL,
128
+ sha256 TEXT NOT NULL,
129
+ uploaded_by TEXT REFERENCES managed_user(id) ON DELETE SET NULL,
130
+ -- Durable snapshot of the uploader's login (see managed_bundle).
131
+ uploaded_by_login TEXT,
132
+ repo_id INTEGER REFERENCES managed_selected_repo(repo_id) ON DELETE SET NULL,
133
+ repo_directory TEXT NOT NULL DEFAULT '',
134
+ repo_embedded INTEGER NOT NULL DEFAULT 0,
135
+ analyzer TEXT,
136
+ visible INTEGER NOT NULL DEFAULT 0,
137
+ bundle_id TEXT REFERENCES managed_bundle(id) ON DELETE SET NULL,
138
+ bundle_integrity TEXT,
139
+ uploaded_at INTEGER NOT NULL
140
+ ) STRICT;
141
+
142
+ CREATE INDEX IF NOT EXISTS managed_report_uploaded_at_idx ON managed_report(uploaded_at);
143
+ CREATE INDEX IF NOT EXISTS managed_report_hash_idx ON managed_report(sha256, uploaded_at, id);
144
+ CREATE INDEX IF NOT EXISTS managed_report_bundle_integrity_idx ON managed_report(bundle_integrity);
145
+ CREATE INDEX IF NOT EXISTS managed_report_bundle_hash_idx ON managed_report(bundle_id, sha256);
146
+
147
+ -- Per-finding triage annotations — the managed (trusted-plaintext) counterpart
148
+ -- of the client's localStorage triage map, keyed the same way: by finding id
149
+ -- alone, not by report. Reports mostly repeat one another (a re-scan of the
150
+ -- same code carries the same finding ids) and a finding's triage is shared by
151
+ -- every report that carries it; which ids a viewer may read or write is
152
+ -- decided per report at the endpoint, not here. The wire shape lives in
153
+ -- common/managed/triage.ts. Whole-entry replace, NULL columns are unset;
154
+ -- flagged is tri-state: NULL unset / 1 flagged / 0 an explicit un-flag
155
+ -- tombstone. A cleared entry keeps its row with every field NULL — a
156
+ -- tombstone a reader adopts as "cleared", where a missing row means "never
157
+ -- annotated" (so a stale client copy can't resurrect a teammate's clear).
158
+ -- updated_by is the last writer (nulled when that user is removed);
159
+ -- updated_by_login is the durable login snapshot (see managed_bundle).
160
+ CREATE TABLE IF NOT EXISTS managed_finding_triage (
161
+ finding_id TEXT PRIMARY KEY,
162
+ color TEXT,
163
+ triage TEXT,
164
+ comment TEXT,
165
+ fix TEXT,
166
+ flagged INTEGER,
167
+ updated_by TEXT REFERENCES managed_user(id) ON DELETE SET NULL,
168
+ updated_by_login TEXT,
169
+ updated_at INTEGER NOT NULL
170
+ ) STRICT;
171
+
172
+ -- The trail behind managed_finding_triage: one row per write that CHANGED an entry,
173
+ -- holding the entry as written (every field NULL = a clear), who wrote it and
174
+ -- when. managed_finding_triage stays the current-state projection reads hit; this is
175
+ -- walked only for a finding's history. The wire is whole-entry replace, so a
176
+ -- snapshot per write is exactly what arrived and "what changed" is the diff
177
+ -- against the previous row for the same id, computed on read. batch_id groups
178
+ -- the rows of one request. seq is the rowid: a total order that breaks ties
179
+ -- on at. actor_id / actor_login follow the uploaded_by / uploaded_by_login
180
+ -- convention (live account, durable login snapshot). Everything is kept by
181
+ -- default — the trail is the record; an operator may bound it per finding
182
+ -- (TRIAGE_HISTORY_LIMIT), in which case a finding's older events are trimmed
183
+ -- as new ones land. An id no report carries any more is the tombstone GC's
184
+ -- concern.
185
+ CREATE TABLE IF NOT EXISTS managed_finding_triage_event (
186
+ seq INTEGER PRIMARY KEY,
187
+ finding_id TEXT NOT NULL,
188
+ batch_id TEXT NOT NULL,
189
+ color TEXT,
190
+ triage TEXT,
191
+ comment TEXT,
192
+ fix TEXT,
193
+ flagged INTEGER,
194
+ actor_id TEXT REFERENCES managed_user(id) ON DELETE SET NULL,
195
+ actor_login TEXT,
196
+ at INTEGER NOT NULL
197
+ ) STRICT;
198
+ CREATE INDEX IF NOT EXISTS finding_triage_event_finding_idx ON managed_finding_triage_event(finding_id, seq);
199
+ CREATE INDEX IF NOT EXISTS finding_triage_event_actor_at_idx ON managed_finding_triage_event(actor_id, at);
200
+
201
+ -- Teams group users + repos for access scoping. Hidden teams grant no access;
202
+ -- the two link tables below carry the many-many relations.
203
+ CREATE TABLE IF NOT EXISTS managed_team (
204
+ id TEXT PRIMARY KEY,
205
+ slug TEXT NOT NULL,
206
+ name TEXT NOT NULL UNIQUE,
207
+ hidden INTEGER NOT NULL DEFAULT 0,
208
+ dependency_review INTEGER NOT NULL DEFAULT 0,
209
+ created_at INTEGER NOT NULL,
210
+ updated_at INTEGER NOT NULL
211
+ ) STRICT;
212
+
213
+ -- Team <-> repo, many-many, with an OPTIONAL path (a subpath of the repo the
214
+ -- team is scoped to; empty path = the whole repo). Distinct paths can coexist;
215
+ -- adding the whole repo replaces them. CASCADE removes links with either side. repo_id
216
+ -- references the selected (operate-on) repos.
217
+ CREATE TABLE IF NOT EXISTS managed_team_repo (
218
+ team_id TEXT NOT NULL REFERENCES managed_team(id) ON DELETE CASCADE,
219
+ repo_id INTEGER NOT NULL REFERENCES managed_selected_repo(repo_id) ON DELETE CASCADE,
220
+ path TEXT NOT NULL DEFAULT '',
221
+ PRIMARY KEY (team_id, repo_id, path)
222
+ ) STRICT;
223
+
224
+ CREATE INDEX IF NOT EXISTS team_repo_repo_idx ON managed_team_repo(repo_id);
225
+
226
+ -- Team <-> user, many-many, with per-membership visibility permissions (see
227
+ -- common/managed/permissions.ts) — view_dependencies / view_security, both
228
+ -- default 0 (off). CASCADE so the membership dies with either side.
229
+ CREATE TABLE IF NOT EXISTS managed_team_user (
230
+ team_id TEXT NOT NULL REFERENCES managed_team(id) ON DELETE CASCADE,
231
+ user_id TEXT NOT NULL REFERENCES managed_user(id) ON DELETE CASCADE,
232
+ view_dependencies INTEGER NOT NULL DEFAULT 0,
233
+ view_security INTEGER NOT NULL DEFAULT 0,
234
+ PRIMARY KEY (team_id, user_id)
235
+ ) STRICT;
236
+
237
+ CREATE INDEX IF NOT EXISTS team_user_user_idx ON managed_team_user(user_id);
238
+ ` + WORKSPACE_SHARE_SCHEMA + LINK_REPORT_SCHEMA + REPOSITORY_ALIAS_SCHEMA + TEAM_NPM_SCOPE_SCHEMA + DEPENDENCY_REVIEW_SCHEMA + GITHUB_LINK_SCHEMA
@@ -0,0 +1,22 @@
1
+ // Table identifiers are code constants. Renames retain rows and foreign keys;
2
+ // an ambiguous old/new pair must be resolved before the server can start.
3
+ const MANAGED_TABLE_RENAMES = [
4
+ ['selected_repo', 'managed_selected_repo'],
5
+ ['team_repo', 'managed_team_repo'],
6
+ ['team_user', 'managed_team_user'],
7
+ ['finding_triage', 'managed_finding_triage'],
8
+ ['finding_triage_event', 'managed_finding_triage_event'],
9
+ ['finding_comment', 'managed_finding_comment'],
10
+ ['finding_comment_event', 'managed_finding_comment_event'],
11
+ ] as const
12
+
13
+ export function managedTableRenames(tables: Set<string>) {
14
+ // A fresh installation must not claim similarly named unrelated tables in
15
+ // a shared database. managed_user anchors an existing managed installation.
16
+ if (!tables.has('managed_user')) return []
17
+ const renames = MANAGED_TABLE_RENAMES.filter(([from]) => tables.has(from))
18
+ for (const [from, to] of renames) {
19
+ if (tables.has(to)) throw new Error(`Cannot migrate managed tables: both ${from} and ${to} exist`)
20
+ }
21
+ return renames
22
+ }
@@ -0,0 +1,152 @@
1
+ // SQLite bootstrap and legacy upgrades. Runtime methods are shared with Neon.
2
+ import { mkdirSync } from 'node:fs'
3
+ import { dirname, join } from 'node:path'
4
+ import { DatabaseSync } from 'node:sqlite'
5
+ import { migrateSlugs } from './slugs.ts'
6
+ import { migrateReportLocations } from './report-migration.ts'
7
+ import { initCommentMethods } from './comments.ts'
8
+ import { initActivityMethods } from './activity.ts'
9
+ import { revisionSchema } from './revisions.ts'
10
+ import { MANAGED_SCHEMA, REPO_UPLOAD_INDEXES, VIEW_SESSION_INDEX } from './db-schema.ts'
11
+ import { GITHUB_LINK_USER_COLUMNS } from './github-links.ts'
12
+ import { REPOSITORY_DEPENDENCY_REVIEW_COLUMNS } from './repository-dependency-reviews.ts'
13
+ import { DEPENDENCY_REVIEW_COLUMNS, DEPENDENCY_REVIEW_INDEXES } from './dependency-reviews.ts'
14
+ import { STORAGE_SCHEMA } from './storage-db.ts'
15
+ import { GITHUB_COMMIT_SCHEMA, GITHUB_METADATA_SCHEMA, GITHUB_STATE_REASON_COLUMN } from './github-metadata.ts'
16
+ import { MANAGED_ISSUE_SCHEMA } from './managed-issues.ts'
17
+ import { BUNDLE_BUILD_LEASE_SCHEMA } from './bundle-build-leases.ts'
18
+ import { UPSTREAM_CACHE_SCHEMA } from './upstream-cache.ts'
19
+ import { TEAM_APP_SUMMARY_SCHEMA } from './team-app-summaries.ts'
20
+ import { managedTableRenames } from './db-table-names.ts'
21
+ import { type ManagedDb, type ManagedDbOptions, createManagedMethods } from './db-methods.ts'
22
+ import { createSqliteDriver, scopeManagedMethods } from './sql.ts'
23
+ export type * from './db-methods.ts'
24
+
25
+ function migrateManagedTableNames(db: DatabaseSync): void {
26
+ db.exec('BEGIN IMMEDIATE')
27
+ try {
28
+ db.exec('CREATE TABLE IF NOT EXISTS managed_schema_version (version INTEGER PRIMARY KEY) STRICT;')
29
+ if (!db.prepare('SELECT version FROM managed_schema_version WHERE version = 3').get()) {
30
+ const rows = db.prepare("SELECT name FROM sqlite_schema WHERE type = 'table'").all() as { name: string }[]
31
+ for (const [from, to] of managedTableRenames(new Set(rows.map(row => row.name)))) {
32
+ db.exec(`ALTER TABLE ${from} RENAME TO ${to}`)
33
+ }
34
+ db.exec('INSERT INTO managed_schema_version VALUES (3)')
35
+ }
36
+ db.exec('COMMIT')
37
+ } catch (err) { db.exec('ROLLBACK'); throw err }
38
+ }
39
+
40
+ // Preserve the legacy single-path links while adding path to their identity.
41
+ function migrateTeamRepoPaths(db: DatabaseSync): void {
42
+ const columns = db.prepare('PRAGMA table_info(managed_team_repo)').all() as { name: string; pk: number }[]
43
+ if (columns.some(column => column.name === 'path' && column.pk === 3)) return
44
+ db.exec('BEGIN IMMEDIATE')
45
+ try {
46
+ db.exec(`CREATE TABLE managed_team_repo_paths (
47
+ team_id TEXT NOT NULL REFERENCES managed_team(id) ON DELETE CASCADE,
48
+ repo_id INTEGER NOT NULL REFERENCES managed_selected_repo(repo_id) ON DELETE CASCADE,
49
+ path TEXT NOT NULL DEFAULT '', PRIMARY KEY (team_id, repo_id, path)
50
+ ) STRICT;
51
+ INSERT INTO managed_team_repo_paths SELECT team_id, repo_id, COALESCE(path, '') FROM managed_team_repo;
52
+ DROP TABLE managed_team_repo;
53
+ ALTER TABLE managed_team_repo_paths RENAME TO managed_team_repo;
54
+ CREATE INDEX team_repo_repo_idx ON managed_team_repo(repo_id);`)
55
+ db.exec('COMMIT')
56
+ } catch (err) { db.exec('ROLLBACK'); throw err }
57
+ }
58
+
59
+ // Add `column` to `table` if it's missing (a lightweight migration for DBs that
60
+ // predate the column; `table`/`column` are code constants, never user input).
61
+ function ensureColumn(db: DatabaseSync, table: string, column: string, type: string): boolean {
62
+ const cols = db.prepare(`PRAGMA table_info(${table})`).all() as { name: string }[]
63
+ if (cols.some((c) => c.name === column)) return false
64
+ db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${type}`)
65
+ return true
66
+ }
67
+
68
+ export function openSqliteManagedDb(path: string, options: ManagedDbOptions = {}): ManagedDb {
69
+ mkdirSync(dirname(path), { recursive: true })
70
+ const db = new DatabaseSync(path)
71
+ try {
72
+ db.exec('PRAGMA journal_mode = WAL;')
73
+ // The bootstrap below commits each schema statement on its own: under
74
+ // FULL, ~120 fsyncs on a new file. Each step checks the schema before
75
+ // changing it and only reads outside the database, so a step lost to a
76
+ // power failure reruns on the next open, and WAL stays consistent under
77
+ // NORMAL. FULL is restored after it; the next commit's fsync of the WAL
78
+ // persists the bootstrap's frames too.
79
+ db.exec('PRAGMA synchronous = NORMAL;')
80
+ db.exec('PRAGMA foreign_keys = ON;')
81
+ migrateManagedTableNames(db)
82
+ db.exec(MANAGED_SCHEMA + GITHUB_METADATA_SCHEMA + GITHUB_COMMIT_SCHEMA + MANAGED_ISSUE_SCHEMA + STORAGE_SCHEMA + BUNDLE_BUILD_LEASE_SCHEMA + UPSTREAM_CACHE_SCHEMA + TEAM_APP_SUMMARY_SCHEMA)
83
+ ensureColumn(db, 'managed_selected_repo', 'cached_default_branch', 'TEXT')
84
+ ensureColumn(db, 'managed_session', 'upload_key', 'TEXT')
85
+ ensureColumn(db, 'managed_session', 'viewer_session', 'TEXT REFERENCES managed_session(id) ON DELETE CASCADE')
86
+ db.exec(VIEW_SESSION_INDEX)
87
+ for (const table of ['managed_report', 'managed_bundle']) {
88
+ ensureColumn(db, table, 'data_key', 'TEXT')
89
+ ensureColumn(db, table, 'storage_encrypted', 'INTEGER NOT NULL DEFAULT 0')
90
+ }
91
+ ensureColumn(db, 'managed_user', 'gh_tokens_encrypted', 'INTEGER NOT NULL DEFAULT 0')
92
+ // Existing names are the sign-in accounts' own: none was chosen yet.
93
+ for (const [column, type] of GITHUB_LINK_USER_COLUMNS) {
94
+ if (ensureColumn(db, 'managed_user', column, type) && column === 'github_name') db.exec('UPDATE managed_user SET github_name = name')
95
+ }
96
+ ensureColumn(db, 'managed_github_metadata', 'state_reason', GITHUB_STATE_REASON_COLUMN)
97
+ ensureColumn(db, 'managed_github_metadata', 'attempted_at', 'INTEGER')
98
+ // Existing public links must opt in too; never preserve an implicit grant.
99
+ ensureColumn(db, 'managed_workspace_share', 'dependencies', 'INTEGER NOT NULL DEFAULT 0')
100
+ ensureColumn(db, 'managed_workspace_share', 'security', 'INTEGER NOT NULL DEFAULT 0')
101
+ migrateTeamRepoPaths(db)
102
+ migrateSlugs(db)
103
+ // Migrate DBs created before a column existed (CREATE TABLE IF NOT EXISTS
104
+ // never alters an already-present table). Idempotent — skipped on fresh DBs.
105
+ const addedLastSeen = ensureColumn(db, 'managed_user', 'last_seen_at', 'INTEGER')
106
+ if (addedLastSeen) {
107
+ // Session creation is known authentication activity. updated_at is not:
108
+ // role changes and token refreshes also wrote it. Keep unknowns NULL.
109
+ db.exec(`UPDATE managed_user SET last_seen_at = (
110
+ SELECT MAX(s.created_at) FROM managed_session s WHERE s.user_id = managed_user.id
111
+ )`)
112
+ }
113
+ ensureColumn(db, 'managed_report', 'uploaded_by_login', 'TEXT')
114
+ ensureColumn(db, 'managed_report', 'repo_embedded', 'INTEGER NOT NULL DEFAULT 0')
115
+ migrateReportLocations(db, join(dirname(path), 'reports'))
116
+ ensureColumn(db, 'managed_report', 'analyzer', 'TEXT')
117
+ // Existing rows predate publication controls and were already visible.
118
+ // Backfill only when adding the column; newly inserted rows still use the
119
+ // hidden default and must be explicitly published.
120
+ const addedVisible = ensureColumn(db, 'managed_report', 'visible', 'INTEGER NOT NULL DEFAULT 0')
121
+ if (addedVisible) db.prepare('UPDATE managed_report SET visible = 1 WHERE visible = 0').run()
122
+ ensureColumn(db, 'managed_bundle', 'uploaded_by_login', 'TEXT')
123
+ ensureColumn(db, 'managed_bundle', 'repo_directory', "TEXT NOT NULL DEFAULT ''")
124
+ db.exec(REPO_UPLOAD_INDEXES)
125
+ ensureColumn(db, 'managed_bundle', 'visible', 'INTEGER NOT NULL DEFAULT 1')
126
+ // Existing rows stay NULL (unknown): some may have been built on the server.
127
+ ensureColumn(db, 'managed_bundle', 'provenance', 'TEXT')
128
+ ensureColumn(db, 'managed_bundle', 'build_conditions', 'TEXT')
129
+ ensureColumn(db, 'managed_team', 'hidden', 'INTEGER NOT NULL DEFAULT 0')
130
+ ensureColumn(db, 'managed_team', 'dependency_review', 'INTEGER NOT NULL DEFAULT 0')
131
+ ensureColumn(db, 'managed_finding_issue', 'auto_fix_url', 'TEXT')
132
+ ensureColumn(db, 'managed_finding_issue', 'auto_fix_checked_at', 'INTEGER')
133
+ ensureColumn(db, 'managed_finding_issue', 'duplicate_of_url', 'TEXT')
134
+ ensureColumn(db, 'managed_selected_repo', 'active', 'INTEGER NOT NULL DEFAULT 1')
135
+ for (const [column, type] of REPOSITORY_DEPENDENCY_REVIEW_COLUMNS) ensureColumn(db, 'managed_selected_repo', column, type)
136
+ for (const [column, type] of DEPENDENCY_REVIEW_COLUMNS) ensureColumn(db, 'managed_dependency_review', column, type)
137
+ db.exec(DEPENDENCY_REVIEW_INDEXES)
138
+ ensureColumn(db, 'managed_finding_triage_event', 'report_id', 'TEXT')
139
+ ensureColumn(db, 'managed_finding_triage_event', 'report', 'TEXT')
140
+ ensureColumn(db, 'managed_finding_triage_event', 'repo', 'TEXT')
141
+ initCommentMethods(db)
142
+ initActivityMethods(db)
143
+ db.exec(revisionSchema())
144
+ db.exec('PRAGMA synchronous = FULL;')
145
+ } catch (err) {
146
+ try { db.close() } catch {}
147
+ throw err
148
+ }
149
+
150
+ const driver = createSqliteDriver(db)
151
+ return scopeManagedMethods(createManagedMethods(driver, options), driver)
152
+ }
@@ -0,0 +1,60 @@
1
+ // A repository's Dependabot alerts for a repository risk scan, read with the
2
+ // App's installation token where the admin's own access lets them see them on
3
+ // GitHub, as doc/bundles.md's repository risk scan section tells.
4
+ import type { ManagedConfig } from './config.ts'
5
+ import type { SelectedRepo } from './db.ts'
6
+ import { type DependabotAlert, dependabotAlerts } from './dependabot-alerts.ts'
7
+ import { GithubApiError, type GithubIdentity, githubRepoPermission, repoAccessToken, repositoryInstallation } from './github-app.ts'
8
+
9
+ // The levels GitHub lets see a repository's Dependabot alerts by default
10
+ // (maintain's reads `write`).
11
+ const SEES_ALERTS: ReadonlySet<string> = new Set(['admin', 'write'])
12
+ // Failures of verifying the sign-in that are the sign-in's own: no token, or
13
+ // one of another account (repository-browser.ts getIdentity), or one GitHub no
14
+ // longer takes. Any other, as GitHub failing to answer, is told as it is.
15
+ const UNVERIFIED: ReadonlySet<string> = new Set(['no-repository', 'github-unauthorized'])
16
+
17
+ // What of a repository's reader (repository-browser.ts) the alerts are asked
18
+ // with: the user's verified identity, and their permission level where its
19
+ // access gate asked GitHub of it already.
20
+ export interface AlertsReader {
21
+ identity(): Promise<GithubIdentity>
22
+ permission(): string | null
23
+ }
24
+
25
+ // Alerts read, and their `recheck`: the user's access to them checked again,
26
+ // asked of GitHub afresh, just before they are told (repository-risk.ts), as
27
+ // access to the repository is, as it may be withdrawn meanwhile.
28
+ export interface AlertsRead { alerts: DependabotAlert[]; recheck(): Promise<void> }
29
+
30
+ // `repo`'s Dependabot alerts (dependabot-alerts.ts), read with the token of
31
+ // the App's installation GitHub tells covers it now, its token checked to read
32
+ // it (github-app.ts repositoryInstallation), never the one its connection
33
+ // keeps, which may no longer cover it, once the user's own permission on it
34
+ // lets them see them; else refused, the alerts not asked:
35
+ // `dependabot-not-installed`, `dependabot-unverified`, or
36
+ // `dependabot-forbidden`, naming their permission. The token is asked each
37
+ // time of repoAccessToken, which renews it before it expires, as the recheck
38
+ // may come long after the alerts are read.
39
+ export async function appDependabotAlerts(config: ManagedConfig, repo: Pick<SelectedRepo, 'repoId' | 'fullName'>, reader: AlertsReader,
40
+ fetchImpl: typeof fetch = globalThis.fetch): Promise<AlertsRead> {
41
+ const id = await repositoryInstallation(config, repo.repoId, repo.fullName, fetchImpl)
42
+ const tokenNow = async () => {
43
+ const token = await repoAccessToken(config, id, fetchImpl)
44
+ if (token == null) throw new GithubApiError(403, 'dependabot-not-installed')
45
+ return token
46
+ }
47
+ const token = await tokenNow()
48
+ let who
49
+ try { who = await reader.identity() } catch (error) {
50
+ throw error instanceof GithubApiError && UNVERIFIED.has(error.message) ? new GithubApiError(403, 'dependabot-unverified') : error
51
+ }
52
+ const seen = (permission: string) => {
53
+ if (!SEES_ALERTS.has(permission)) {
54
+ throw new GithubApiError(403, 'dependabot-forbidden', null, `@${who.login}'s access to this repository (${permission}) doesn't let them see its Dependabot alerts on GitHub`)
55
+ }
56
+ }
57
+ seen(reader.permission() ?? await githubRepoPermission(repo.fullName, token, who, fetchImpl))
58
+ const alerts = await dependabotAlerts(repo.fullName, token, fetchImpl)
59
+ return { alerts, recheck: async () => { seen(await githubRepoPermission(repo.fullName, await tokenNow(), who, fetchImpl)) } }
60
+ }