Skip to content

Instantly share code, notes, and snippets.

@AubreyHewes
Created December 21, 2016 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AubreyHewes/1e875d82c058f077a12829a6d48b204d to your computer and use it in GitHub Desktop.
Save AubreyHewes/1e875d82c058f077a12829a6d48b204d to your computer and use it in GitHub Desktop.
Wercker NotifyDeployment to Trello Card
// --------------------------------------------------------
//
// Notify a Trello card on a new deployment
//
// NOTE: This is a Wercker related CI script
// NOTE: This depends on the Wercker CI environment!
//
// Required ENV:
// TRELLO_API_KEY, TRELLO_API_TOKEN, TRELLO_CARD_ID
// Wercker ENV (supplied by Wercker):
// WERCKER_GIT_DOMAIN, WERCKER_GIT_OWNER, WERCKER_GIT_REPOSITORY,
// WERCKER_GIT_BRANCH, WERCKER_GIT_COMMIT,
// WERCKER_RUN_URL
//
// Test execution example:
// TRELLO_API_KEY=X TRELLO_API_TOKEN=Y TRELLO_CARD_ID=X \
// WERCKER_GIT_DOMAIN=github.org WERCKER_GIT_OWNER=example WERCKER_GIT_REPOSITORY=repo \
// WERCKER_GIT_BRANCH=example WERCKER_GIT_COMMIT=123 node bin/notifyDeployment.js
// --------------------------------------------------------
// --------------------------------------------------------
// CONFIGURATION
// --------------------------------------------------------
// nothing to configure
// TODO support hardcoded config vs env config; i.e. allow running via local cli
// --------------------------------------------------------
// DO NOT CHANGE BELOW HERE ;-)
// --------------------------------------------------------
const Promise = require('bluebird'); // todo remove this dependency
const arrayIntersect = require('lodash/intersection');
const arrayDiff = require('lodash/difference');
const Trello = require('node-trello'); // todo remove this dependency
/**
* Exit if the given envVars are not in the current environment
*
* @param envVars
*/
const guardEnvVarsExist = (envVars) => {
const hasEnvKeys = arrayIntersect(Object.keys(process.env), envVars);
if (hasEnvKeys.length !== envVars.length) {
console.log(`ERROR: Missing environment variables: ${arrayDiff(envVars, hasEnvKeys).join(', ')}`);
process.exit(-1);
}
};
// Check required vars exist
guardEnvVarsExist([
'TRELLO_API_KEY', 'TRELLO_API_TOKEN', 'TRELLO_CARD_ID',
'WERCKER_GIT_DOMAIN', 'WERCKER_GIT_OWNER', 'WERCKER_GIT_REPOSITORY',
'WERCKER_GIT_BRANCH', 'WERCKER_GIT_COMMIT',
'WERCKER_RUN_URL'
]);
// extract required vars
const {
TRELLO_API_KEY, TRELLO_API_TOKEN, TRELLO_CARD_ID,
WERCKER_GIT_DOMAIN, WERCKER_GIT_OWNER, WERCKER_GIT_REPOSITORY,
WERCKER_GIT_BRANCH, WERCKER_GIT_COMMIT,
WERCKER_RUN_URL
} = process.env;
// todo integrate above commands; reduce duplicity
//
// currently only supports GitHub(like, default) and BitBucket...
// https by default
const repoUrl = `https://${WERCKER_GIT_DOMAIN}/${WERCKER_GIT_OWNER}/${WERCKER_GIT_REPOSITORY}`;
// todo proper templating
// GitHub templates
// COMMIT URI: ${repoUrl}/commit/${WERCKER_GIT_COMMIT}
// BRANCH URI: ${repoUrl}/tree/${WERCKER_GIT_BRANCH}
// BitBucket templates
// COMMIT URI: ${repoUrl}/commits/${WERCKER_GIT_COMMIT}
// BRANCH URI: ${repoUrl}/branch/${WERCKER_GIT_BRANCH}
let repoBranchPart = 'tree';
let repoCommitPart = 'commit';
if (WERCKER_GIT_DOMAIN === 'bitbucket.org') {
repoBranchPart = 'branch';
repoCommitPart = 'commits';
}
const repoBranchUrl = (branch) => {
return `${repoUrl}/${repoBranchPart}/${branch}`;
};
const repoCommitUrl = (commit) => {
return `${repoUrl}/${repoCommitPart}/${commit}`;
};
// NOW do something...
const trello = Promise.promisifyAll(new Trello(TRELLO_API_KEY, TRELLO_API_TOKEN));
return trello.postAsync(`/1/cards/${TRELLO_CARD_ID}/actions/comments`, {
text: 'Deployed new version (' +
`[\`${WERCKER_GIT_BRANCH}\`](${repoBranchUrl(WERCKER_GIT_BRANCH)})@` +
`[\`${WERCKER_GIT_COMMIT}\`](${repoCommitUrl(WERCKER_GIT_COMMIT.substring(0, 8))})` +
')' + ` [CI](${WERCKER_RUN_URL})`
}).then(card => {
// put the card to the top of the list...
return trello.putAsync(`/1/cards/${card.data.card.id}`, { pos: 'top' });
}).catch((err) => {
console.log(err); // we don't fail a deployment if we cant log to trello!
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment