Skip to content

Instantly share code, notes, and snippets.

@ZJONSSON
Created January 22, 2019 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZJONSSON/498a297186aeef342154011a6f6e982c to your computer and use it in GitHub Desktop.
Save ZJONSSON/498a297186aeef342154011a6f6e982c to your computer and use it in GitHub Desktop.
CircleCI coverage lambda
const Promise = require('bluebird');
const request = require('request');
const requestAsync = Promise.promisify(request);
const USER = process.env.GITHUB_USER;
exports.handler = async (event, context) => {
try {
if (!USER) throw new Error('GITHUB_USER env missing');
const module = event.pathParameters.module;
const builds = await requestAsync({
url: `https://circleci.com/api/v1.1/project/github/${USER}/${module}/tree/master?shallow=true&offset=0&limit=30&mine=false`,
gzip: true,
json: true
});
const lastBuild = builds.body.find(d => d.status === 'success');
if (!lastBuild) return context.succeed({
statusCode: 500,
body: 'No last build found'
});
const artifacts = await requestAsync({
url: `https://circleci.com/api/v1.1/project/github/${USER}/${module}/${lastBuild.build_num}/artifacts`,
gzip: true,
json: true
});
const coverage = artifacts.body.find( d => /coverage\/index\.html$/.test(d.path) || d.path.includes('lcov-report/index.html'));
if (/url$/.test(event.path)) return context.succeed({
statusCode: 307,
headers: {
location: coverage.url
}
});
const report = await requestAsync({
url: coverage.url,
gzip: true
});
let percent = (/"strong">(.*?)%/.exec(report.body) || [])[1] || 0;
percent = Math.round(percent)+'%';
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="99" height="20"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><rect rx="3" width="99" height="20" fill="#555"/><rect rx="3" x="63" width="36" height="20" fill="#97CA00"/><path fill="#97CA00" d="M63 0h4v20h-4z"/><rect rx="3" width="99" height="20" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="32.5" y="15" fill="#010101" fill-opacity=".3">coverage</text><text x="32.5" y="14">coverage</text><text x="80" y="15" fill="#010101" fill-opacity=".3">${percent}</text><text x="80" y="14">${percent}</text></g></svg>`;
context.succeed({
statusCode:200,
headers:{
'content-type':'image/svg+xml'
},
body:svg
});
} catch(e) {
context.succeed({
statusCode: 500,
error: e.message,
stack: e.stack
});
}
};
if (!module.parent) {
exports.handler({
path: '.../url',
pathParameters: {
module: 'node-unzipper'
}
},{
succeed: console.log,
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment