@preventive/triage 1.0.0-alpha.84 → 1.0.0-alpha.86

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.
@@ -4,8 +4,8 @@
4
4
  // remove, those an agent made or co-authored, who made them and when in the
5
5
  // week, counted from its commits as the Commits tab reads and keeps them
6
6
  // (repository-history.ts historyFrom), so that only those not kept yet are
7
- // read of GitHub, a few queries an answer, the client asking again for the
8
- // rest; a year past YEAR_COMMITS is counted by GitHub's statistics instead,
7
+ // read of GitHub, a few queries a part, each going on from what the last
8
+ // walked (pulse-walk.ts); a year past YEAR_COMMITS is counted by GitHub's statistics instead,
9
9
  // which GitHub computes when first asked and again once the default branch
10
10
  // moves, answering 202 till they are (told pending, StatsPending, for the
11
11
  // client to ask again). And, since
@@ -19,10 +19,11 @@ import { githubGraphql } from './github-graphql.ts'
19
19
  import { githubJson, repoApi, shareGithubRead } from './github-requests.ts'
20
20
  import { SHA, count, timeOf } from './github-values.ts'
21
21
  import { coauthorsOf } from './commit-coauthors.ts'
22
- import { windowsOn } from './history-windows.ts'
23
- import { type HistoryCommit, type HistoryPages, historyFrom } from './repository-history.ts'
22
+ import type { HistoryCommit } from './repository-history.ts'
24
23
  import { issueState, itemOf, pullState } from './repository-issues.ts'
25
24
 
25
+ export { YEAR_COMMITS, yearCommits } from './pulse-walk.ts'
26
+
26
27
  export const PULSE_WEEKS = [1, 4, 13, 52]
27
28
  // What a year counted from its commits holds, its version: kept years of
28
29
  // another are counted again.
@@ -36,18 +37,6 @@ export const LISTED = 10
36
37
  // updated, GitHub sorting by no merge or close: as many as this are read, and
37
38
  // the latest merged or closed of them listed.
38
39
  const SEARCHED = 50
39
- // A year of more commits than this is counted by GitHub's statistics: read a
40
- // page at a time, it would take a hundred queries. Of those not kept, as
41
- // many queries are made an answer as YEAR_QUERIES, each reading on from as
42
- // many commits as YEAR_STARTS.
43
- export const YEAR_COMMITS = 10_000
44
- const YEAR_QUERIES = 5
45
- const YEAR_STARTS = 10
46
- // Commits are read on from as far as this before the year, a commit's clock
47
- // not always later than its parents': one dated before the year may have a
48
- // parent dated in it.
49
- const YEAR_SKEW_MS = 30 * DAY_MS
50
-
51
40
  // GitHub still computing a statistic.
52
41
  export class StatsPending extends Error {
53
42
  constructor() { super('github-stats-pending') }
@@ -91,11 +80,6 @@ interface PulseLines { weeks: [number, number, number][]; contributors: { login:
91
80
  // The days (`from`, `to`, both days, UTC; `to` none, on) and the authors (GitHub
92
81
  // logins, any of them) a period's pull requests and issues are narrowed to.
93
82
  export interface PulseNarrowing { from: number; to: number | null; authors: string[] }
94
- // The year's commits read so far: all of them, or as far back as asked
95
- // (`commits`, `from`), else how many (`read`), and whether past
96
- // YEAR_COMMITS (`tooMany`).
97
- type YearCommits = { complete: true; commits: HistoryCommit[]; passed: HistoryCommit[]; from: number | null } | { complete: false; read: number; tooMany: boolean }
98
-
99
83
  const emptyPunch = () => Array.from({ length: 7 }, () => Array.from({ length: 24 }, () => 0))
100
84
 
101
85
  // One statistic: its rows once GitHub has computed it, 'pending' while it
@@ -197,58 +181,6 @@ export async function defaultHead(fullName: string, token: string, fetchImpl: ty
197
181
  return typeof oid === 'string' && SHA.test(oid) ? oid : null
198
182
  }
199
183
 
200
- // The commits from `head` made since `since`, as the Commits tab reads and
201
- // keeps them (`pages`, historyFrom): on from the parents of those since (or
202
- // as much as YEAR_SKEW_MS before), not yet listed, YEAR_STARTS at a time,
203
- // till none is left; the pages kept read
204
- // of the store, those not of GitHub, as many queries of them as
205
- // YEAR_QUERIES, the rest left for the next to ask (`complete` false, how
206
- // many are `read`); more than YEAR_COMMITS, too many (`tooMany`). Read
207
- // whole, those it was read on past besides (`passed`): undated, or dated
208
- // before it, a clock behind, the branch's commits since beneath them. Read
209
- // no further than `need` asks, those since it read whole (`complete`, `from`
210
- // the newest of those read whose parents are not, every commit after it
211
- // read; null, the year read whole). How many are read so far `told` as they
212
- // are.
213
- export async function yearCommits(fullName: string, token: string, head: string, since: number, pages: HistoryPages,
214
- fetchImpl: typeof fetch = globalThis.fetch, need = since, told: (read: number) => void = () => {}): Promise<YearCommits> {
215
- const found = new Map<string, HistoryCommit>()
216
- const tried = new Set<string>()
217
- const within = (commit: HistoryCommit) => commit.committedAt === null || commit.committedAt >= since - YEAR_SKEW_MS
218
- const dated = (commit: HistoryCommit) => commit.committedAt !== null && commit.committedAt >= since
219
- const read = () => [...found.values()].filter(dated).length
220
- const passed = () => [...found.values()].filter(commit => !dated(commit) && within(commit))
221
- let queries = 0
222
- let windowed = false
223
- let starts = [head]
224
- while (starts.length > 0) {
225
- // Those not read yet, the head before any, else the parents of those
226
- // read, no later than the newest of them (but for a clock behind): read
227
- // whole after it.
228
- const unread = new Set(starts)
229
- const from = found.size === 0 ? Number.POSITIVE_INFINITY : Math.max(Number.NEGATIVE_INFINITY, ...[...found.values()]
230
- .filter(commit => commit.parents.some(sha => unread.has(sha))).map(commit => commit.committedAt ?? Number.POSITIVE_INFINITY))
231
- if (need > since && from < need) return { complete: true, commits: [...found.values()].filter(dated), passed: passed(), from }
232
- if (queries >= YEAR_QUERIES) return { complete: false, read: read(), tooMany: false }
233
- // The pages kept read first; once past them, the rest read at once by
234
- // when its commits were made (history-windows.ts windowsOn), as far back
235
- // as it needs, the year's as far as its commits are followed.
236
- const next: Awaited<ReturnType<typeof windowsOn>> | null = windowed || found.size === 0 ? null
237
- : await windowsOn(fullName, token, head, starts, found, need > since ? need : since - YEAR_SKEW_MS, from, pages, fetchImpl, queries)
238
- windowed ||= next?.windowed === true
239
- queries += next?.queries ?? 0
240
- const batch = (next?.batch ?? starts).slice(0, YEAR_STARTS)
241
- for (const sha of batch) tried.add(sha)
242
- const got = batch.length > 0 ? await historyFrom(fullName, token, batch, pages, fetchImpl) : { commits: [], read: 0 }
243
- if (got.read > 0) queries++
244
- for (const commit of got.commits) if (!found.has(commit.sha)) found.set(commit.sha, commit)
245
- told(read())
246
- if (read() > YEAR_COMMITS) return { complete: false, read: read(), tooMany: true }
247
- starts = [...new Set([...found.values()].filter(within).flatMap(commit => commit.parents))].filter(sha => !found.has(sha) && !tried.has(sha))
248
- }
249
- return { complete: true, commits: [...found.values()].filter(dated), passed: passed(), from: null }
250
- }
251
-
252
184
  // Whether an author is a GitHub App's bot: by its login (`<app>[bot]`, as a
253
185
  // commit's no-reply address names it, github-accounts.ts commitAccount),
254
186
  // else, for one GitHub names no account for, by the name it commits as.
@@ -49,10 +49,12 @@ export interface AssessedPackage { name: string; version: string; integrity?: st
49
49
  export type Assessment = { published: string | null; findings: ({ id: string; severity: Severity } & Record<string, unknown>)[]; score: { score: number } }
50
50
 
51
51
  // How many of what a finding found a risk line keeps, the first (its files,
52
- // hosts, dependencies or scripts), with how many there are (`count`), for the
53
- // Packages tab's tooltips (repository-risk.js); and how long a text of it
54
- // (npm's deprecation message, a script's command) may be.
52
+ // dependencies or scripts; the hosts it names anew, short as each is, up to
53
+ // TOLD_HOSTS), with how many there are (`count`), for the Packages tab's
54
+ // tooltips (repository-risk.js); and how long a text of it (npm's
55
+ // deprecation message, a script's command) may be.
55
56
  export const TOLD_ROWS = 5
57
+ export const TOLD_HOSTS = 10
56
58
  const TOLD_TEXT = 300
57
59
 
58
60
  // What a risk line tells of a finding: its id and severity, whether npm took
@@ -69,8 +71,8 @@ export type ToldFinding = { id: string; severity: Severity; takenDown?: true } &
69
71
  type Row = Record<string, unknown>
70
72
  const cut = (text: unknown) => typeof text === 'string' && text.length > TOLD_TEXT ? `${text.slice(0, TOLD_TEXT - 1)}…` : typeof text === 'string' ? text : undefined
71
73
  const keysOf = (found: unknown) => found && typeof found === 'object' ? Object.keys(found) : []
72
- const first = (key: string, rows: unknown, pick: (row: Row) => Row) => Array.isArray(rows)
73
- ? { [key]: (rows as Row[]).slice(0, TOLD_ROWS).map(pick), count: rows.length } : {}
74
+ const first = (key: string, rows: unknown, pick: (row: Row) => Row, most = TOLD_ROWS) => Array.isArray(rows)
75
+ ? { [key]: (rows as Row[]).slice(0, most).map(pick), count: rows.length } : {}
74
76
  const FILES: Partial<Record<string, (row: Row) => Row>> = {
75
77
  'held-runs-code': row => ({ path: row['path'], kinds: keysOf(row['found']) }),
76
78
  'held-credentials': row => ({ path: row['path'], kinds: keysOf(row['found']) }),
@@ -102,7 +104,7 @@ export function toldFinding({ id, severity, ...found }: { id: string; severity:
102
104
  case 'new-hosts': return { ...told, ...first('hosts', found['hosts'], row => {
103
105
  const [place] = Array.isArray(row['places']) ? row['places'] as Row[] : []
104
106
  return { host: row['host'], ...place ? { path: place['path'], line: place['line'] } : {} }
105
- }) }
107
+ }, TOLD_HOSTS) }
106
108
  default: return told
107
109
  }
108
110
  }
File without changes