Skip to content

Commit

Permalink
DOP-4352: Set completion time for Netlify builds (#1002)
Browse files Browse the repository at this point in the history
  • Loading branch information
rayangler authored Feb 28, 2024
1 parent 30b371b commit 91761b1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
18 changes: 14 additions & 4 deletions api/handlers/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,20 @@ export async function snootyBuildComplete(event: APIGatewayEvent): Promise<APIGa
await client.connect();
const db = client.db(c.get<string>('dbName'));
const jobRepository = new JobRepository(db, c, consoleLogger);
await jobRepository.updateExecutionTime(jobId, { gatsbyCloudEndTime: new Date() });
const updateResponse = await jobRepository.updateWithStatus(jobId, null, payload.status || JobStatus.failed, false);
const previewUrl = getPreviewUrl(updateResponse.payload, c.get<string>('env'));
await notifyBuildSummary(jobId, { mongoClient: client, previewUrl });

if (event.queryStringParameters?.builder === 'netlify') {
await jobRepository.updateExecutionTime(jobId, { netlifyEndTime: new Date() });
} else {
await jobRepository.updateExecutionTime(jobId, { gatsbyCloudEndTime: new Date() });
const updateResponse = await jobRepository.updateWithStatus(
jobId,
null,
payload.status || JobStatus.failed,
false
);
const previewUrl = getPreviewUrl(updateResponse.payload, c.get<string>('env'));
await notifyBuildSummary(jobId, { mongoClient: client, previewUrl });
}
} catch (e) {
consoleLogger.error('SnootyBuildCompleteError', e);
return {
Expand Down
6 changes: 4 additions & 2 deletions src/repositories/jobRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ export class JobRepository extends BaseRepository {
_id: new objectId(id),
};
const update = { $set: setValues };
this.updateOne(query, update, `Mongo Timeout Error: Timed out while retrieving job`).catch((err) => {
try {
await this.updateOne(query, update, `Mongo Timeout Error: Timed out while retrieving job`);
} catch (err) {
this._logger.error('updateExecutionTime', `Error: ${err}`);
});
}
}

async findOneAndUpdateJob(query): Promise<Job | null> {
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/job/api/jobs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,15 @@ describe('Post-build webhook tests', () => {
);
expect(res.statusCode).toBe(400);
});

test('successfully handles builder=netlify query string param', async () => {
const signature = createSha256Signature(payloadString, 'SNOOTY_SECRET');
const mockReqEvent = createMockAPIGatewayEvent(payloadString, { 'x-snooty-signature': signature });
mockReqEvent.queryStringParameters = {
builder: 'netlify',
};
const res = await SnootyBuildComplete(mockReqEvent);
// Ideally, we would have a more robust way to check that only specific data is updated
expect(res.statusCode).toBe(200);
});
});

0 comments on commit 91761b1

Please sign in to comment.