Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOP-4352: Set completion time for Netlify builds #1002

Merged
merged 5 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading