-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.js
More file actions
142 lines (121 loc) · 5.45 KB
/
Copy pathfetch.js
File metadata and controls
142 lines (121 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { loadConfig } from './lib/config.js'
import { discoverPullRequests } from './lib/discover.js'
import { fetchPullRequestDetails } from './lib/detail.js'
import { diffSnapshots, resolveDepartedPullRequests } from './lib/diff.js'
import { loadFamiliarityIndex, scoreFamiliarity } from './lib/familiarity.js'
import { assertGhReady, fetchAuthenticatedLogin } from './lib/gh.js'
import { assignBucket, buildFacts, loadBucketDefinitions } from './lib/buckets.js'
import { buildReasonBits, estimateEffort, scoreSignals } from './lib/score.js'
import { computeSignals, extractRawFields } from './lib/signals.js'
import { PREVIOUS_SNAPSHOT_PATH, SNAPSHOT_PATH } from './lib/paths.js'
import { readJsonFile, writeJsonFileAtomically } from './lib/store.js'
const SNAPSHOT_VERSION = 1
async function main() {
const isDryRun = process.argv.includes('--dry-run')
const config = loadConfig()
await assertGhReady()
const authenticatedLogin = await fetchAuthenticatedLogin()
if (authenticatedLogin !== config.user) {
console.warn(
`gh is authenticated as ${authenticatedLogin} but reference/config.json says ${config.user}.`
)
}
const discovery = await discoverPullRequests(config)
console.log(`Discovered ${discovery.refs.length} pull requests after dedup`)
for (const [source, count] of Object.entries(discovery.sourceCounts)) {
console.log(` ${source}: ${count}`)
}
if (discovery.droppedOwnPullRequests > 0) {
console.log(` dropped ${discovery.droppedOwnPullRequests} authored by ${config.user}`)
}
if (isDryRun) {
console.log(`Discovery API cost: ${discovery.apiCost}`)
return
}
const { detailsByNodeId, apiCost, unfetchable } = await fetchPullRequestDetails(discovery.refs)
const now = new Date()
const familiarity = await buildFamiliarity({ config, refs: discovery.refs, detailsByNodeId, now })
const prs = discovery.refs
.filter((ref) => detailsByNodeId.has(ref.nodeId))
.map((ref) => buildSnapshotEntry(ref, detailsByNodeId.get(ref.nodeId), config, now, familiarity.index))
.sort((left, right) => right.score - left.score)
const previous = readJsonFile(SNAPSHOT_PATH)
const snapshot = {
version: SNAPSHOT_VERSION,
generatedAt: now.toISOString(),
user: config.user,
org: config.org,
sourceCounts: discovery.sourceCounts,
familiarityIndexAt: familiarity.index?.builtAt ?? null,
apiCost: discovery.apiCost + apiCost + familiarity.apiCost,
unfetchable: unfetchable.map((ref) => ({ key: ref.key, url: ref.url, title: ref.title })),
prs,
}
const { deltasByKey, departedNodeIds } = diffSnapshots(previous, snapshot)
for (const pr of snapshot.prs) pr.deltas = deltasByKey.get(pr.key) ?? []
const departed = await resolveDepartedPullRequests(departedNodeIds)
snapshot.departed = departed.entries
snapshot.apiCost += departed.apiCost
snapshot.changedCount = [...deltasByKey.values()].length
if (previous) writeJsonFileAtomically(PREVIOUS_SNAPSHOT_PATH, previous)
writeJsonFileAtomically(SNAPSHOT_PATH, snapshot)
const { buckets, problems } = loadBucketDefinitions()
for (const problem of problems) console.warn(`Bucket definitions: ${problem}`)
const bucketCounts = countByBucket(prs, buckets)
console.log(`Wrote ${prs.length} pull requests to ${SNAPSHOT_PATH}`)
for (const [bucket, count] of Object.entries(bucketCounts)) console.log(` ${bucket}: ${count}`)
if (unfetchable.length > 0) console.log(` could not fetch: ${unfetchable.length}`)
if (snapshot.changedCount > 0) console.log(`Changed since last refresh: ${snapshot.changedCount}`)
if (departed.entries.length > 0) console.log(`Left the queue: ${departed.entries.length}`)
console.log(`Total API cost: ${snapshot.apiCost}`)
}
async function buildFamiliarity({ config, refs, detailsByNodeId, now }) {
if (process.argv.includes('--no-familiarity')) return { index: null, apiCost: 0 }
const repos = [...new Set(refs.filter((ref) => detailsByNodeId.has(ref.nodeId)).map((ref) => ref.repo))]
try {
const result = await loadFamiliarityIndex({ config, repos, now })
if (result.wasRebuilt) console.log(`Rebuilt familiarity index for ${repos.length} repositories`)
return result
} catch (error) {
console.warn(`Familiarity index unavailable, scoring without it: ${error.message}`)
return { index: null, apiCost: 0 }
}
}
function buildSnapshotEntry(ref, detail, config, now, familiarityIndex) {
const changedFiles = (detail.files?.nodes ?? []).map((file) => file.path)
const familiarity = familiarityIndex ? scoreFamiliarity(familiarityIndex, ref.repo, changedFiles) : null
const signals = computeSignals({ ref, detail, config, now, familiarity })
const { score, contributions } = scoreSignals(signals)
return {
key: ref.key,
nodeId: ref.nodeId,
repo: ref.repo,
repoName: ref.repoName,
number: ref.number,
url: ref.url,
title: detail.title ?? ref.title,
author: detail.author?.login ?? ref.author,
createdAt: detail.createdAt ?? ref.createdAt,
updatedAt: detail.updatedAt ?? ref.updatedAt,
sources: ref.sources,
raw: extractRawFields(detail),
signals,
score,
contributions,
reasonBits: buildReasonBits(signals),
effort: estimateEffort(signals),
deltas: [],
}
}
function countByBucket(prs, buckets) {
const counts = {}
for (const pr of prs) {
const bucket = assignBucket(buildFacts(pr), buckets)
counts[bucket] = (counts[bucket] ?? 0) + 1
}
return counts
}
main().catch((error) => {
console.error(error.message)
process.exit(1)
})