Skip to content

Instantly share code, notes, and snippets.

@kevinSuttle
Last active October 13, 2017 20:35
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 kevinSuttle/cdb9d9a7c26ce628eaca7fdd01ecee94 to your computer and use it in GitHub Desktop.
Save kevinSuttle/cdb9d9a7c26ce628eaca7fdd01ecee94 to your computer and use it in GitHub Desktop.
Buildstamp banner
<div id="build-banner">
<small>This buildstamp was generated on <time id="buildstamp-time">{{ buildstampTime }}</time></small>
<section id="github-data">
<h2>GitHub</h2>
<dl id="latest-commit-data">
<dt>Commit hash:</dt>
<dd><a href="https://github.com//{{ORG}}/{{REPO}}/commit/{{ commitHash }}"><data id="git-commit_hash">{{ commitHash }}</data></a></dd>
<dt>Commit message:</dt>
<dd id="git-commit_message"><a href="https://github.com/{{ORG}}/{{REPO}}/commit/{{ commitHash }}">{{ commitMessage }}</a></dd>
<dt>Commit time:</dt>
<dd><time id="git-commit_time">{{ commitTime }}</time></dd>
<dt>Commit author:</dt>
<dd id="git-commit_author">{{ commitAuthor }}</dd>
<dt>Status:</dt>
<dd id="git-branch_status">{{ commitStatus }}</dd>
<dt>Tag:</dt>
<dd><data id="git-tag">{{ commitTag }}</data></dd>
</dl>
</section>
<section id="circle-data">
<h2>Circle</h2>
<dl id="latest-build-data">
<dt>Branch</dt>
<dd><data id="circle-branch">{{ circleBranch }}</data></dd>
<dt>Build</dt>
<dd><a href="https://circleci.com/gh//{{ORG}}/{{REPO}}/{{circleBuildNumber}}"><data id="circle-build-number">{{ circleBuildNumber }}</data></a></dd>
</dl>
</section>
</div>
const git = require('git-rev-sync');
const pkg = require('../package.json');
const moment = require('moment');
const tz = require('moment-timezone');
const timeFormat = 'MMMM Do YYYY, h:mm:ss a';
const timeStamp = moment().tz('America/New_York').format(timeFormat);
const childProcess = require('child_process');
const fs = require('fs');
const jsonTemplate = './src/assets/data/buildstamp.json';
const trackingBranch = 'origin/develop';
const workingBranch = process.env.CIRCLE_BRANCH || git.branch();
const gitRevList =
childProcess
.execSync(`git rev-list origin/develop...HEAD --left-right --count`)
.toString();
const aheadNumOfCommits = parseInt(gitRevList.slice(2, -1));
let aheadMessage;
const behindNumOfCommits = parseInt(gitRevList.slice(0, 1));
let behindMessage;
let relativeBranchStatus;
const getCommitsBehind = () => {
switch (behindNumOfCommits) {
case 0:
break;
case 1:
behindMessage = '1 commit behind';
break;
default:
behindMessage = `${behindNumOfCommits} commits behind`;
break;
}
return behindMessage;
}
function getCommitsAhead() {
switch (aheadNumOfCommits) {
case 0:
break;
case 1:
aheadMessage = '1 commit ahead';
break;
default:
aheadMessage = `${aheadNumOfCommits} commits ahead`;
break;
}
return aheadMessage;
}
function assembleStatusSentence() {
if (behindNumOfCommits === 0 && aheadNumOfCommits === 0) {
relativeBranchStatus = `The branch ${workingBranch} is up to date with ${trackingBranch}`;
console.log(`up to date`);
} else if (behindNumOfCommits === 0 && aheadNumOfCommits !== 0) {
relativeBranchStatus = `The branch ${workingBranch} is ${getCommitsAhead()} of ${trackingBranch}`;
console.log(`ahead`);
} else if (behindNumOfCommits !== 0 && aheadNumOfCommits === 0) {
relativeBranchStatus = `The branch ${workingBranch} is ${getCommitsBehind()} ${trackingBranch}`;
console.log(`behind`);
} else if (behindNumOfCommits !== 0 && aheadNumOfCommits !== 0) {
relativeBranchStatus = `The branch ${workingBranch} is ${getCommitsBehind()} and ${getCommitsAhead()} of ${trackingBranch}`;
console.log(`both off`);
} else {
relativeBranchStatus = 'No branch tracking information available';
console.log(`wtf`);
}
return relativeBranchStatus;
}
const getCommitMessageTitle = function() {
return childProcess
.execSync(`git --no-pager show -s --format=%s ${git.long()}`)
.toString()
.trim();
};
const getCommitAuthor = function() {
return childProcess
.execSync(`git --no-pager show -s --format='%an'`)
.toString()
.trim();
};
const banner = `
[{
"buildstampTime": "${timeStamp} (EST)",
"hash": "${git.long()}",
"message": "${getCommitMessageTitle()}",
"time": "${moment(git.date()).tz('America/New_York').format(timeFormat)} (EST)",
"author": "${getCommitAuthor()}",
"branch": "${workingBranch}",
"tag": "${git.tag()}",
"status": "${assembleStatusSentence()}",
"package": "${pkg.name}",
"version": "${pkg.version}",
"circleBranch": "${workingBranch}",
"circleBuildNumber": "${process.env.CIRCLE_BUILD_NUM}",
"circleBuildUrl": "${process.env.CIRCLE_BUILD_URL}"
}]
`;
const existingJSON = fs.readFileSync(jsonTemplate);
const fileWriter = fs.openSync(jsonTemplate, 'w+');
const buffer = new Buffer(banner);
childProcess.execSync('git update-index --skip-worktree ./src/assets/data/buildstamp.json');
fs.writeSync(fileWriter, buffer, 0, buffer.length, 0);
fs.close(fileWriter);
// tslint:disable-next-line: no-console
console.info(`File written to ${jsonTemplate}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment