Skip to content

Commit 85d7e45

Browse files
committed
feat: persist well-known files inventory from enrichment loop
Signed-off-by: Mouad BANI <mouad-mb@outlook.com>
1 parent 8279ef7 commit 85d7e45

1 file changed

Lines changed: 40 additions & 5 deletions

File tree

services/apps/packages_worker/src/enricher/runEnrichmentLoop.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ import { fetchActivitySnapshot } from './fetchActivitySnapshot'
77
import { fetchLightRepo, parseGithubUrl } from './fetchLightRepo'
88
import { GithubAppConfig, getInstallationToken } from './githubAppAuth'
99
import { InstallationPool } from './installationPool'
10-
import { FetchError, LightRepoResult, RepoActivitySnapshot } from './types'
10+
import {
11+
FetchError,
12+
LightRepoResult,
13+
RepoActivitySnapshot,
14+
RepoWellKnownFilesUpdate,
15+
} from './types'
1116
import { bulkUpdateEnrichedRepos, markReposSkipped } from './updateEnrichedRepos'
1217
import { bulkUpsertRepoActivitySnapshot } from './updateRepoActivitySnapshot'
18+
import { bulkUpsertRepoWellKnownFiles } from './updateWellKnownFiles'
1319

1420
const log = getServiceChildLogger('github-repos-enricher')
1521

@@ -67,6 +73,7 @@ class WriteBuffer {
6773
private results: LightRepoResult[] = []
6874
private snapshots: RepoActivitySnapshot[] = []
6975
private skipUrls: string[] = []
76+
private wellKnownFiles: RepoWellKnownFilesUpdate[] = []
7077
private lastFlushAt = Date.now()
7178
private flushing = false
7279
private flushFailures = 0
@@ -81,6 +88,10 @@ class WriteBuffer {
8188
this.snapshots.push(snapshot)
8289
}
8390

91+
addWellKnownFiles(update: RepoWellKnownFilesUpdate): void {
92+
this.wellKnownFiles.push(update)
93+
}
94+
8495
addSkip(url: string): void {
8596
this.skipUrls.push(url)
8697
}
@@ -94,22 +105,34 @@ class WriteBuffer {
94105
)
95106
}
96107

97-
private clearBatch(resultCount: number, snapshotCount: number, skipCount: number): void {
108+
private clearBatch(
109+
resultCount: number,
110+
snapshotCount: number,
111+
skipCount: number,
112+
wellKnownFilesCount: number,
113+
): void {
98114
this.results.splice(0, resultCount)
99115
this.snapshots.splice(0, snapshotCount)
100116
this.skipUrls.splice(0, skipCount)
117+
this.wellKnownFiles.splice(0, wellKnownFilesCount)
101118
this.flushFailures = 0
102119
}
103120

104121
async flush(): Promise<number> {
105122
this.lastFlushAt = Date.now()
106-
if (this.results.length === 0 && this.snapshots.length === 0 && this.skipUrls.length === 0) {
123+
if (
124+
this.results.length === 0 &&
125+
this.snapshots.length === 0 &&
126+
this.skipUrls.length === 0 &&
127+
this.wellKnownFiles.length === 0
128+
) {
107129
return 0
108130
}
109131

110132
const batch = [...this.results]
111133
const snapshotBatch = [...this.snapshots]
112134
const skips = [...this.skipUrls]
135+
const wellKnownFilesBatch = [...this.wellKnownFiles]
113136
this.flushing = true
114137
try {
115138
// The snapshot upsert also updates repos rows — run in one transaction to avoid
@@ -118,8 +141,9 @@ class WriteBuffer {
118141
await bulkUpdateEnrichedRepos(tx, batch)
119142
await markReposSkipped(tx, skips)
120143
await bulkUpsertRepoActivitySnapshot(tx, snapshotBatch)
144+
await bulkUpsertRepoWellKnownFiles(tx, wellKnownFilesBatch)
121145
})
122-
this.clearBatch(batch.length, snapshotBatch.length, skips.length)
146+
this.clearBatch(batch.length, snapshotBatch.length, skips.length, wellKnownFilesBatch.length)
123147
return batch.length
124148
} catch (err) {
125149
this.flushFailures++
@@ -139,7 +163,13 @@ class WriteBuffer {
139163
? 'Flush failed repeatedly — dropping batch, repos will be re-enriched next sweep'
140164
: 'Flush failed — will retry on next cycle',
141165
)
142-
if (dropBatch) this.clearBatch(batch.length, snapshotBatch.length, skips.length)
166+
if (dropBatch)
167+
this.clearBatch(
168+
batch.length,
169+
snapshotBatch.length,
170+
skips.length,
171+
wellKnownFilesBatch.length,
172+
)
143173
return 0
144174
} finally {
145175
this.flushing = false
@@ -360,6 +390,11 @@ async function processRepo(row: RepoRow, ctx: WorkerContext): Promise<void> {
360390
if (outcome.kind === 'success') {
361391
metrics.totalFetched++
362392
writeBuffer.add(outcome.data)
393+
writeBuffer.addWellKnownFiles({
394+
repoId: row.id,
395+
checkedAt: new Date().toISOString(),
396+
files: outcome.data.wellKnownFiles,
397+
})
363398
pool.parkIfBudgetLow(
364399
installationId,
365400
outcome.data.rateLimit?.remaining,

0 commit comments

Comments
 (0)