Skip to content

Commit

Permalink
Merge pull request #65 from HHS/circleci-export
Browse files Browse the repository at this point in the history
CircleCI results export script
  • Loading branch information
rahearn authored Nov 23, 2020
2 parents f7c66d3 + 31e4ed9 commit 02071b8
Show file tree
Hide file tree
Showing 7 changed files with 466 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ coverage
junit.xml
reports
yarn-error.log
docs/circleci/test_report.md

# Build related
build/
Expand Down
14 changes: 14 additions & 0 deletions docs/circleci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# CircleCI Export Script

This script exports the test results and artifacts from CircleCI to a markdown file for including in governance documents.

## Setup

Set `CIRCLECI_AUTH_TOKEN` environment variable with your [personal auth token](https://circleci.com/docs/2.0/managing-api-tokens/#creating-a-personal-api-token).

## Things to potentially update:

in `src/circleci.js`

1) `artifactFileNames` regex to determine which artifacts to include
2) `buildJobsInWorkflow` count to select the number of jobs in a single workflow
4 changes: 4 additions & 0 deletions docs/circleci/bin/export-ci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

require = require('esm')(module /*, options*/);
require("../index.js").exportLastMainTests();
33 changes: 33 additions & 0 deletions docs/circleci/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getLastTest, getTestMetadata, getTestArtifacts } from './src/circleci';
import fs from 'fs';

const exportLastMainTests = async (fileName = "test_report.md") => {
const lastTestPipeline = await getLastTest();
let data = "# Test Results Report\n";
for (let job of lastTestPipeline) {
let { success, other, exceptions } = await getTestMetadata(job.build_num);
let artifacts = await getTestArtifacts(job.build_num);
const hasTestReults = success.length > 0 || other.length > 0 || exceptions != null;
const hasArtifacts = artifacts.length > 0;
if (hasTestReults || hasArtifacts) {
data += `\n## ${job.workflows.job_name}\n`;
if (hasTestReults) {
data += "### Test Results:\n"
data += `#### Success:\n\`\`\`\n${JSON.stringify(success, null, 2)}\n\`\`\`\n`;
data += `#### Failure:\n\`\`\`\n${JSON.stringify(other, null, 2)}\n\`\`\`\n`
data += `#### Exceptions:\n\`\`\`\n${JSON.stringify(exceptions, null, 2)}\n\`\`\`\n`
}
if (hasArtifacts) {
data += `### Artifacts:\n\`\`\`\n${JSON.stringify(artifacts, null, 2)}\n\`\`\`\n`;
}
}
}
fs.writeFile(fileName, data, (err) => {
if (err) throw err;
console.log("Done");
})
}

export {
exportLastMainTests
}
15 changes: 15 additions & 0 deletions docs/circleci/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "circleci-export",
"version": "0.0.1",
"description": "Export build results out of CircleCI to package for documentation",
"main": "index.js",
"license": "MIT",
"bin": {
"export-ci": "bin/export-ci.js"
},
"dependencies": {
"circleci": "^0.3.3",
"esm": "^3.2.25",
"lodash": "^4.17.20"
}
}
55 changes: 55 additions & 0 deletions docs/circleci/src/circleci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import CircleCI from 'circleci';
import { filter, take, partition } from 'lodash';

const artifactFileNames = /(?:(?:lcov-report\/index|cucumber_report)\.html)|(?:^reports\/.*\.png)$/;
const buildJobsInWorkflow = 7;
const workflowName = "build_test_deploy";

const ci = new CircleCI({
auth: process.env.CIRCLECI_AUTH_TOKEN
});

const getLastTest = async () => {
const lastBuilds = await ci.getBranchBuilds({
username: "HHS",
project: "Head-Start-TTADP",
branch: "main",
});
return take(
filter(
lastBuilds,
(b) => (b.workflows.workflow_name === workflowName)
),
buildJobsInWorkflow);
}

const getTestMetadata = async (buildNum) => {
const { tests, exceptions } = await ci.getTestMetadata({
username: "HHS",
project: "Head-Start-TTADP",
build_num: buildNum
});
const [ success, other ] = partition(tests, ({result}) => (result === "success"));

return {
success,
other,
exceptions
}
}

const getTestArtifacts = async (buildNum) => {
const artifacts = await ci.getBuildArtifacts({
username: "HHS",
project: "Head-Start-TTADP",
build_num: buildNum
});
return filter(artifacts, ({path}) => (path.match(artifactFileNames)));
}

export default ci;
export {
getLastTest,
getTestMetadata,
getTestArtifacts
}
Loading

0 comments on commit 02071b8

Please sign in to comment.