-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathfetch.processor.ts
More file actions
225 lines (197 loc) · 6.09 KB
/
Copy pathfetch.processor.ts
File metadata and controls
225 lines (197 loc) · 6.09 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { Processor, WorkerHost, InjectQueue } from "@nestjs/bullmq";
import { Logger } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { IsNull, Repository } from "typeorm";
import { Job, Queue } from "bullmq";
import { Issue, PullRequest } from "../entities";
import { GitHubFetcherService } from "../webhook/github-fetcher.service";
import {
FETCH_QUEUE,
FETCH_JOBS,
DEFAULT_BACKFILL_DAYS,
prFilesJobId,
} from "./constants";
export interface PrMetadataJobData {
repoFullName: string;
prNumber: number;
}
export interface PrFilesJobData {
repoFullName: string;
prNumber: number;
expectedHeadSha?: string | null;
expectedBaseSha?: string | null;
}
export interface BackfillRepoJobData {
repoFullName: string;
days?: number;
}
interface PrFilesGeneration {
headSha: string | null;
baseSha: string | null;
}
type JobData = PrMetadataJobData | PrFilesJobData | BackfillRepoJobData;
@Processor(FETCH_QUEUE, { concurrency: 5 })
export class FetchProcessor extends WorkerHost {
private readonly logger = new Logger(FetchProcessor.name);
constructor(
private readonly fetcher: GitHubFetcherService,
@InjectRepository(PullRequest)
private readonly prRepo: Repository<PullRequest>,
@InjectRepository(Issue)
private readonly issueRepo: Repository<Issue>,
@InjectQueue(FETCH_QUEUE)
private readonly fetchQueue: Queue,
) {
super();
}
async process(job: Job<JobData>): Promise<void> {
switch (job.name) {
case FETCH_JOBS.PR_METADATA: {
const { repoFullName, prNumber } = job.data as PrMetadataJobData;
await this.handlePrMetadata(repoFullName, prNumber);
break;
}
case FETCH_JOBS.PR_FILES: {
await this.handlePrFiles(job.data as PrFilesJobData);
break;
}
case FETCH_JOBS.BACKFILL_REPO: {
const { repoFullName, days } = job.data as BackfillRepoJobData;
await this.handleBackfill(repoFullName, days ?? DEFAULT_BACKFILL_DAYS);
break;
}
default:
this.logger.warn(`Unknown job name: ${job.name}`);
}
}
private async handlePrMetadata(
repoFullName: string,
prNumber: number,
): Promise<void> {
this.logger.log(`Fetching PR metadata for ${repoFullName}#${prNumber}`);
const { closingIssueNumbers, body, lastEditedAt } =
await this.fetcher.fetchPrMetadata(repoFullName, prNumber);
await this.prRepo.update(
{ repoFullName, prNumber },
{
closingIssueNumbers,
body,
lastEditedAt,
},
);
// If this PR is merged, mark each linked issue as solved_by_pr
const pr = await this.prRepo.findOneBy({ repoFullName, prNumber });
if (pr?.state === "MERGED" && closingIssueNumbers.length > 0) {
for (const issueNumber of closingIssueNumbers) {
await this.issueRepo.update(
{ repoFullName, issueNumber },
{ solvedByPr: prNumber },
);
}
}
}
private async handlePrFiles(data: PrFilesJobData): Promise<void> {
const { repoFullName, prNumber } = data;
this.logger.log(`Fetching PR files for ${repoFullName}#${prNumber}`);
const generation = {
headSha: data.expectedHeadSha ?? null,
baseSha: data.expectedBaseSha ?? null,
};
await this.fetcher.fetchAndStorePrFiles(repoFullName, prNumber);
const updateResult = await this.prRepo.update(
this.prGenerationCriteria(repoFullName, prNumber, generation),
{ scoringDataStored: true },
);
if (!updateResult.affected) {
await this.handleStalePrFilesJob(repoFullName, prNumber);
}
}
private async handleBackfill(
repoFullName: string,
days: number,
): Promise<void> {
this.logger.log(`Backfilling ${repoFullName} — last ${days} days`);
const sinceDate = new Date(Date.now() - days * 24 * 60 * 60 * 1000);
// Fetch and upsert PRs
const prs = await this.fetcher.backfillPullRequests(
repoFullName,
sinceDate,
);
this.logger.log(`Backfilled ${prs.length} PRs from ${repoFullName}`);
// Fetch and upsert issues before PR metadata jobs can link solved_by_pr.
await this.fetcher.backfillIssues(repoFullName, sinceDate);
this.logger.log(`Backfilled issues from ${repoFullName}`);
// Enqueue follow-up jobs (metadata + files for every PR).
for (const { prNumber, headSha, baseSha } of prs) {
await this.fetchQueue.add(
FETCH_JOBS.PR_METADATA,
{ repoFullName, prNumber },
{
jobId: `meta-${repoFullName}-${prNumber}`,
removeOnComplete: true,
removeOnFail: 50,
attempts: 3,
backoff: { type: "exponential", delay: 5000 },
},
);
await this.enqueuePrFilesJob(
repoFullName,
prNumber,
headSha ?? null,
baseSha ?? null,
);
}
}
private async handleStalePrFilesJob(
repoFullName: string,
prNumber: number,
): Promise<void> {
await this.prRepo.update(
{ repoFullName, prNumber },
{ scoringDataStored: false },
);
const pr = await this.prRepo.findOneBy({ repoFullName, prNumber });
if (!pr) return;
await this.enqueuePrFilesJob(
repoFullName,
prNumber,
pr.headSha ?? null,
pr.baseSha ?? null,
);
}
private async enqueuePrFilesJob(
repoFullName: string,
prNumber: number,
expectedHeadSha: string | null,
expectedBaseSha: string | null,
): Promise<void> {
await this.fetchQueue.add(
FETCH_JOBS.PR_FILES,
{ repoFullName, prNumber, expectedHeadSha, expectedBaseSha },
{
jobId: prFilesJobId(
repoFullName,
prNumber,
expectedHeadSha,
expectedBaseSha,
),
removeOnComplete: true,
removeOnFail: 50,
attempts: 3,
backoff: { type: "exponential", delay: 5000 },
},
);
}
private prGenerationCriteria(
repoFullName: string,
prNumber: number,
generation: PrFilesGeneration,
): Record<string, unknown> {
return {
repoFullName,
prNumber,
headSha: generation.headSha ?? IsNull(),
baseSha: generation.baseSha ?? IsNull(),
};
}
}