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

populate reporting endpoints #16

Merged
merged 5 commits into from
Oct 17, 2020
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: 18 additions & 0 deletions firestore.indexes.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
{
"indexes": [
{
"collectionGroup": "repoVersions",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "major",
"order": "DESCENDING"
},
{
"fieldPath": "minor",
"order": "DESCENDING"
},
{
"fieldPath": "patch",
"order": "DESCENDING"
}
]
},
{
"collectionGroup": "unityVersions",
"queryScope": "COLLECTION",
Expand Down
33 changes: 26 additions & 7 deletions functions/src/api/reportBuildFailure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,34 @@ import { firebase, functions } from '../config/firebase';
import { Request } from 'firebase-functions/lib/providers/https';
import { Response } from 'express-serve-static-core';
import { Token } from '../config/token';
import { BuildFailure, CiBuilds } from '../model/ciBuilds';
import { CiJobs } from '../model/ciJobs';
import { Discord } from '../config/discord';

export const reportBuildFailure = functions.https.onRequest(async (req: Request, res: Response) => {
if (!Token.isValid(req.header('Authorisation'))) {
firebase.logger.warn('unauthorised request', req);
res.status(403).send('Unauthorized');
return;
}
try {
if (!Token.isValid(req.header('authorization'))) {
firebase.logger.warn('unauthorised request', req.headers);
res.status(403).send('Unauthorized');
return;
}

const { body } = req;
const { jobId, buildId, reason } = body;
const failure: BuildFailure = { reason };

firebase.logger.info('Build failure reported.', req);
await CiJobs.markFailureForJob(jobId);
await CiBuilds.markBuildAsFailed(buildId, failure);

res.status(200).send('OK');
firebase.logger.info('Build failure reported.', body);
res.status(200).send('OK');
} catch (err) {
const message = `
Something went wrong while wrong while reporting a build failure
${err.message} (${err.status})\n${err.stackTrace}
`;
firebase.logger.error(message);
await Discord.sendAlert(message);
res.status(500).send('Something went wrong');
}
});
55 changes: 48 additions & 7 deletions functions/src/api/reportNewBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,56 @@ import { firebase, functions } from '../config/firebase';
import { Request } from 'firebase-functions/lib/providers/https';
import { Response } from 'express-serve-static-core';
import { Token } from '../config/token';
import { BuildInfo, CiBuilds, ImageType } from '../model/ciBuilds';
import { CiJobs } from '../model/ciJobs';
import { Discord } from '../config/discord';
import { EditorVersionInfo } from '../model/editorVersionInfo';
import { RepoVersionInfo } from '../model/repoVersions';

export const reportNewBuild = functions.https.onRequest(async (req: Request, res: Response) => {
if (!Token.isValid(req.header('Authorisation'))) {
firebase.logger.warn('unauthorised request', req);
res.status(403).send('Unauthorized');
return;
}
try {
if (!Token.isValid(req.header('authorization'))) {
firebase.logger.warn('unauthorised request', req.headers);
res.status(403).send('Unauthorized');
return;
}

const { body } = req;
const { buildId, jobId, imageType, baseOs, repoVersion, editorVersion, targetPlatform } = body;
const buildInfo: BuildInfo = {
baseOs,
repoVersion,
editorVersion,
targetPlatform,
};

firebase.logger.info('new build reported', req);
if (jobId === 'dryRun') {
await createDryRunJob(imageType, editorVersion);
}

res.status(200).send('OK');
await CiJobs.markJobAsInProgress(jobId);
await CiBuilds.registerNewBuild(buildId, jobId, imageType, buildInfo);

firebase.logger.info('new build reported', body);
res.status(200).send('OK');
} catch (err) {
const message = `
Something went wrong while wrong while reporting a new build.
${err.message} (${err.status})\n${err.stackTrace}
`;
firebase.logger.error(message);
await Discord.sendAlert(message);
res.status(500).send('Something went wrong');
}
});

const createDryRunJob = async (imageType: ImageType, editorVersion: string) => {
const repoVersionInfo = await RepoVersionInfo.getLatest();

if (imageType === 'editor') {
const editorVersionInfo = await EditorVersionInfo.get(editorVersion);
await CiJobs.create(imageType, repoVersionInfo, editorVersionInfo);
} else {
await CiJobs.create(imageType, repoVersionInfo);
}
};
40 changes: 33 additions & 7 deletions functions/src/api/reportPublication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,41 @@ import { firebase, functions } from '../config/firebase';
import { Request } from 'firebase-functions/lib/providers/https';
import { Response } from 'express-serve-static-core';
import { Token } from '../config/token';
import { CiBuilds, DockerInfo } from '../model/ciBuilds';
import { CiJobs } from '../model/ciJobs';
import { Discord } from '../config/discord';

export const reportPublication = functions.https.onRequest(async (req: Request, res: Response) => {
if (!Token.isValid(req.header('Authorisation'))) {
firebase.logger.warn('unauthorised request', req);
res.status(403).send('Unauthorized');
return;
}
try {
if (!Token.isValid(req.header('authorization'))) {
firebase.logger.warn('unauthorised request', req.headers);
res.status(403).send('Unauthorized');
return;
}

const { body } = req;
const { jobId, buildId, imageRepo, imageName, friendlyTag, specificTag, digest } = body;
const dockerInfo: DockerInfo = { imageRepo, imageName, friendlyTag, specificTag, digest };

firebase.logger.info('Publication reported.', req);
await CiBuilds.markBuildAsPublished(buildId, dockerInfo);
const jobHasCompleted = await CiBuilds.haveAllBuildsForJobBeenPublished(jobId);
firebase.logger.info('Publication reported.', body);

res.status(200).send('OK');
if (jobHasCompleted) {
await CiJobs.markJobAsCompleted(jobId);
const message = `Job completed for ${jobId}.`;
firebase.logger.info(message);
await Discord.sendMessageToMaintainers(message);
}

res.status(200).send('OK');
} catch (err) {
const message = `
Something went wrong while wrong while reporting a new publication
${err.message} (${err.status})\n${err.stackTrace}
`;
firebase.logger.error(message);
await Discord.sendAlert(message);
res.status(500).send('Something went wrong');
}
});
4 changes: 2 additions & 2 deletions functions/src/api/unityVersions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Request } from 'firebase-functions/lib/providers/https';
import { Response } from 'express-serve-static-core';
import { firebase, functions } from '../config/firebase';
import { UnityVersionInfo } from '../model/unityVersionInfo';
import { EditorVersionInfo } from '../model/editorVersionInfo';

export const unityVersions = functions.https.onRequest(
async (request: Request, response: Response) => {
try {
const versions = await UnityVersionInfo.getAllIds();
const versions = await EditorVersionInfo.getAllIds();

response.send(versions);
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions functions/src/config/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export class Discord {
const i = await this.getInstance();

// Todo - retry mechanism

await i.createMessage(channelId, message, files);
await this.disconnect();
return true;
} catch (err) {
firebase.logger.error('An error occurred while trying to send a message to discord.', err);
Expand Down Expand Up @@ -72,7 +72,7 @@ export class Discord {
await new Promise((resolve) => setTimeout(resolve, 1000));
secondsWaited += 1;

if (secondsWaited >= 8) {
if (secondsWaited >= 15) {
throw new Error('Bot never became ready');
}
}
Expand Down
2 changes: 0 additions & 2 deletions functions/src/cron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export const trigger = functions.pubsub

firebase.logger.error(message);
await Discord.sendAlert(message);
} finally {
await Discord.disconnect();
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { UnityVersionInfo } from '../../model/unityVersionInfo';
import { EditorVersionInfo } from '../../model/editorVersionInfo';
import { db } from '../../config/firebase';

export const generateBuildQueueFromNewVersionInfoList = async (
versionInfoList: UnityVersionInfo[],
versionInfoList: EditorVersionInfo[],
) => {
const batch = db.batch();

Expand Down
2 changes: 0 additions & 2 deletions functions/src/logic/ingestUnityVersions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@ export const ingestUnityVersions = async () => {

firebase.logger.error(message);
await Discord.sendAlert(message);
} finally {
await Discord.disconnect();
}
};
4 changes: 2 additions & 2 deletions functions/src/logic/ingestUnityVersions/scrapeVersions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { getDocumentFromUrl } from '../utils/get-document-from-url';
import { UnityVersionInfo } from '../../model/unityVersionInfo';
import { EditorVersionInfo } from '../../model/editorVersionInfo';

const UNITY_ARCHIVE_URL = 'https://unity3d.com/get-unity/download/archive';

/**
* Based on https://github.com/BLaZeKiLL/unity-scraper
*/
export const scrapeVersions = async (): Promise<UnityVersionInfo[]> => {
export const scrapeVersions = async (): Promise<EditorVersionInfo[]> => {
const document = await getDocumentFromUrl(UNITY_ARCHIVE_URL);

const links = Array.from(document.querySelectorAll('a.unityhub[href]'));
Expand Down
14 changes: 7 additions & 7 deletions functions/src/logic/ingestUnityVersions/updateDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UnityVersionInfo } from '../../model/unityVersionInfo';
import { EditorVersionInfo } from '../../model/editorVersionInfo';
import { isMatch } from 'lodash';
import { firebase } from '../../config/firebase';
import { Discord } from '../../config/discord';
Expand All @@ -7,11 +7,11 @@ const plural = (amount: number) => {
return amount === 1 ? 'version' : 'versions';
};

export const updateDatabase = async (ingestedInfoList: UnityVersionInfo[]): Promise<void> => {
const existingInfoList = await UnityVersionInfo.getAll();
export const updateDatabase = async (ingestedInfoList: EditorVersionInfo[]): Promise<void> => {
const existingInfoList = await EditorVersionInfo.getAll();

const newVersions: UnityVersionInfo[] = [];
const updatedVersions: UnityVersionInfo[] = [];
const newVersions: EditorVersionInfo[] = [];
const updatedVersions: EditorVersionInfo[] = [];

ingestedInfoList.forEach((scrapedInfo) => {
const { version } = scrapedInfo;
Expand All @@ -31,12 +31,12 @@ export const updateDatabase = async (ingestedInfoList: UnityVersionInfo[]): Prom
let message = '';

if (newVersions.length >= 1) {
await UnityVersionInfo.createMany(newVersions);
await EditorVersionInfo.createMany(newVersions);
message += `${newVersions.length} new ${plural(newVersions.length)} detected. `;
}

if (updatedVersions.length >= 1) {
await UnityVersionInfo.updateMany(updatedVersions);
await EditorVersionInfo.updateMany(updatedVersions);
message += `${updatedVersions.length} updated ${plural(updatedVersions.length)} detected. `;
}

Expand Down
Loading