Skip to content

Commit

Permalink
DOP-3604: Github comment on Job Deploy (#842)
Browse files Browse the repository at this point in the history
* clean slate

* npm i and ci

* bigger ephemeral - handleJobs

* bigger ephemeral - all stg lambdas

* expanding brain

* esbuild

* individual: true

* move aws-sdk to devdeps

* add serverless plugins

* install oops

* Use esbuild for lambdas

* legacy-peer-deps, our old friend

* more peers

* upgrade memory-server to get rid of @types/mongodb

* deploy lambdas, peer-deps

* log

* correct token

* pass bot creds through env vars

* fix links

* look for builder-bot comments, fix links

* log payload

* remove mmeigs

* remove org

* cleaned, tested

* remove gh-comment as github workflow trigger

* PR feedback, clean, resolve dependencies

* remove legacy-peer-deps

* add branch to staging ecs workflow

* updated deps

* remove branch from staging ecs trigger

* clean up

* removed config input

* remove config mentions

---------

Co-authored-by: branberry <[email protected]>
  • Loading branch information
mmeigs and branberry authored Jun 28, 2023
1 parent 34953ea commit 3df315c
Show file tree
Hide file tree
Showing 10 changed files with 29,069 additions and 4,485 deletions.
1 change: 1 addition & 0 deletions api/config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dbUrl": "MONGO_ATLAS_URL",
"fastlyDochubMap": "FASTLY_DOCHUB_MAP",
"githubSecret": "GITHUB_SECRET",
"githubBotPW": "GITHUB_BOT_PASSWORD",
"slackSecret": "SLACK_SECRET",
"slackAuthToken": "SLACK_TOKEN",
"slackViewOpenUrl": "https://slack.com/api/views.open",
Expand Down
1 change: 1 addition & 0 deletions api/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dbUrl": "url",
"fastlyDochubMap": "FASTLY_DOCHUB_MAP",
"githubSecret": "GITHUB_SECRET",
"githubBotPW": "GITHUB_BOT_PASSWORD",
"slackSecret": "SLACK_SECRET",
"slackAuthToken": "SLACK_TOKEN",
"slackViewOpenUrl": "https://slack.com/api/views.open",
Expand Down
38 changes: 38 additions & 0 deletions api/config/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// source: https://github.com/evanw/esbuild/issues/1051
const plugin = {
name: 'fix',
setup(build) {
// If a ".node" file is imported within a module in the "file" namespace, resolve
// it to an absolute path and put it into the "node-file" virtual namespace.
build.onResolve({ filter: /\.node$/, namespace: 'file' }, (args) => ({
path: require.resolve(args.path, { paths: [args.resolveDir] }),
namespace: 'node-file',
}));

// Files in the "node-file" virtual namespace call "require()" on the
// path from esbuild of the ".node" file in the output directory.
build.onLoad({ filter: /.*/, namespace: 'node-file' }, (args) => ({
contents: `
import path from ${JSON.stringify(args.path)}
try { module.exports = require(path) }
catch {}
`,
}));

// If a ".node" file is imported within a module in the "node-file" namespace, put
// it in the "file" namespace where esbuild's default loading behavior will handle
// it. It is already an absolute path since we resolved it to one above.
build.onResolve({ filter: /\.node$/, namespace: 'node-file' }, (args) => ({
path: args.path,
namespace: 'file',
}));

// Tell esbuild's default loading behavior to use the "file" loader for
// these ".node" files.
let opts = build.initialOptions;
opts.loader = opts.loader || {};
opts.loader['.node'] = 'file';
},
};

module.exports = [plugin];
40 changes: 40 additions & 0 deletions api/controllers/v1/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IConfig } from 'config';
import { RepoEntitlementsRepository } from '../../../src/repositories/repoEntitlementsRepository';
import { BranchRepository } from '../../../src/repositories/branchRepository';
import { ConsoleLogger } from '../../../src/services/logger';
import { GithubCommenter } from '../../../src/services/github';
import { SlackConnector } from '../../../src/services/slack';
import { JobRepository } from '../../../src/repositories/jobRepository';
import { JobQueueMessage } from '../../../src/entities/queueMessage';
Expand Down Expand Up @@ -165,13 +166,40 @@ async function NotifyBuildSummary(jobId: string): Promise<any> {
await client.connect();
const db = client.db(c.get('dbName'));
const env = c.get<string>('env');
const githubToken = c.get<string>('githubBotPW');

const jobRepository = new JobRepository(db, c, consoleLogger);
// TODO: Make fullDocument be of type Job, validate existence
const fullDocument = await jobRepository.getJobById(jobId);
if (!fullDocument) {
consoleLogger.error(jobId, 'Cannot find job entry in db');
return;
}
const repoName = fullDocument.payload.repoName;
const username = fullDocument.user;
const githubCommenter = new GithubCommenter(consoleLogger, githubToken);
const slackConnector = new SlackConnector(consoleLogger, c);

// Create/Update Github comment
try {
const parentPRs = await githubCommenter.getParentPRs(fullDocument.payload);
for (const pr of parentPRs) {
const prCommentId = await githubCommenter.getPullRequestCommentId(fullDocument.payload, pr);
const fullJobDashboardUrl = c.get<string>('dashboardUrl') + jobId;

if (prCommentId !== undefined) {
const ghMessage = prepGithubComment(fullDocument, fullJobDashboardUrl, true);
githubCommenter.updateComment(fullDocument.payload, prCommentId, ghMessage);
} else {
const ghMessage = prepGithubComment(fullDocument, fullJobDashboardUrl, false);
githubCommenter.postComment(fullDocument.payload, pr, ghMessage);
}
}
} catch (err) {
consoleLogger.error(jobId, `Failed to comment on GitHub: ${err}`);
}

// Slack notification
const repoEntitlementRepository = new RepoEntitlementsRepository(db, c, consoleLogger);
const entitlement = await repoEntitlementRepository.getSlackUserIdByGithubUsername(username);
if (!entitlement?.['slack_user_id']) {
Expand Down Expand Up @@ -201,6 +229,18 @@ export const extractUrlFromMessage = (fullDocument): string[] => {
return urls.map((url) => url.replace(/([^:]\/)\/+/g, '$1'));
};

function prepGithubComment(fullDocument: Job, jobUrl: string, isUpdate = false): string {
if (isUpdate) {
return `\n* job log: [${fullDocument.payload.newHead}](${jobUrl})`;
}
const urls = extractUrlFromMessage(fullDocument);
let stagingUrl = '';
if (urls.length > 0) {
stagingUrl = urls[urls.length - 1];
}
return `✨ Staging URL: [${stagingUrl}](${stagingUrl})\n\n#### 🪵 Logs\n\n* job log: [${fullDocument.payload.newHead}](${jobUrl})`;
}

async function prepSummaryMessage(
env: string,
fullDocument: Job,
Expand Down
Loading

0 comments on commit 3df315c

Please sign in to comment.