Skip to content

Commit

Permalink
Better SHA extraction from GitHub webhook payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
humphd committed Jan 31, 2022
1 parent 77e5d50 commit 7368513
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
28 changes: 13 additions & 15 deletions tools/autodeployment/builds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const mergeStream = require('merge-stream');
const { ReReadable } = require('rereadable-stream');

class Build {
constructor(type, githubData) {
constructor(type, githubData, sha) {
this.type = type;
this.githubData = githubData;
this.sha = sha;
this.startedDate = new Date();

// Start a build log cache that we can re-play as many times as necessary
Expand All @@ -33,6 +34,7 @@ class Build {
const build = {
type: this.type,
githubData: this.githubData,
sha: this.sha,
startedDate: this.startedDate,
code: this.code,
};
Expand Down Expand Up @@ -76,30 +78,26 @@ function run() {
builds.pending = null;

logger.debug('run() called, starting pending build');
build.proc = shell.exec(
`./deploy.sh ${build.type} ${build.githubData.after}`,
{ silent: true },
(code) => {
build.finish(code);
builds.previous = builds.current;
builds.current = null;

// See if there's another build ready to go
run();
}
);
build.proc = shell.exec(`./deploy.sh ${build.type} ${build.sha}`, { silent: true }, (code) => {
build.finish(code);
builds.previous = builds.current;
builds.current = null;

// See if there's another build ready to go
run();
});

// Combine stderr and stdout, like 2>&1
build.out = mergeStream(build.proc.stdout, build.proc.stderr);
// Pipe the output of the build process into our build's cache log stream
build.out.pipe(cache);
}

module.exports.addBuild = function (type, githubData) {
module.exports.addBuild = function (type, githubData, sha) {
// If we're in the middle of a build, queue this build until we're done.
// If there's already a queued build waiting, replace it with this one,
// since the most recent one is the one we really care about.
builds.pending = new Build(type, githubData);
builds.pending = new Build(type, githubData, sha);
logger.debug({ type }, 'addBuild() called');
run();
};
Expand Down
21 changes: 20 additions & 1 deletion tools/autodeployment/webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ function requestFilter(repo, buildType, action, ref, mainBranch) {
);
}

/**
* Extract the commit for this GitHub payload, depending on the event type
* @param {'push' | 'release'} eventType one of 'push' or 'release'
* @param {Object} githubData webhook payload from GitHub, see https://github.com/Seneca-CDOT/telescope/settings/hooks
*/
function getSha(eventType, githubData) {
if (eventType === 'push') {
// "after": "adf299da6e6e9c3f20208ee6ab61d205a9f70eab",
return githubData.after;
}
if (eventType === 'release') {
// "target_commitish": "1a4594cfc443d307679576d9fdfa402f530a570c",
return githubData.release.target_commitish;
}
logger.warn(`Unknown GitHub event type ${eventType}. Unable to parse git SHA`);
return 'unknown';
}

/**
* Create a handler for the particular GitHub push event and build type
* @param {string} buildType - one of `production` or `staging`
Expand All @@ -74,7 +92,8 @@ function handleEventType(buildType, gitHubEvent) {
githubData.repository.master_branch
)
) {
addBuild(buildType, githubData);
const sha = getSha(gitHubEvent, githubData);
addBuild(buildType, githubData, sha);
}
});
}
Expand Down

0 comments on commit 7368513

Please sign in to comment.