Skip to content

Commit

Permalink
Merge pull request #1025 from bolt-design-system/feature/checks-api
Browse files Browse the repository at this point in the history
checks api setup
  • Loading branch information
sghoweri authored Jan 10, 2019
2 parents e91dcaa + 59dfdd0 commit 3b1f2a0
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 72 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- phpenv config-rm xdebug.ini
- composer global require hirak/prestissimo
- yarn run setup:quick
#- node ./scripts/create-check-suite.js
script: yarn run test
cache:
yarn: true
Expand Down Expand Up @@ -69,6 +70,7 @@ jobs:
- www/pattern-lab/index.html

before_install:
- openssl aes-256-cbc -K $encrypted_4537e53f71e7_key -iv $encrypted_4537e53f71e7_iv -in scripts/bolt-design-system-bot.private-key.pem.enc -out scripts/bolt-design-system-bot.private-key.pem -d
- nvm install # version lifted from `.nvmrc`
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.9.4
- export PATH="$HOME/.yarn/bin:$PATH"
Expand All @@ -88,4 +90,3 @@ notifications:
on_failure: never # @todo re-enable
slack:
secure: cNto+gWAoK1JM9jBNG4i4rMSybv3twMbqlFSCohQFBDMwKFMdlyWqFDX6iYKtHxWEDzrZyRz3qiJ8/S44mgjeKJ/xHbHDtPchp/KL2P1htipvwD2EZXobcBEGl83v2rmtFO1WNJUPB3RIJE2yt1wJsX7NIXpDw82hePmaIvNJmtbLpK/J5uaFqGNHIsctmULgVmGSNSTyK4nYxxjNNLd0EvO37Y6VN8FhsKNu2NHMKeeQxinEvETDUh8XuqXZYNWE3PBvVa4OiDhgnr5K27jsnWX+wEmqg0xY+CMf7mUSTqVN61fA7LnHyM0qcGGmB6YTv4QYLMwPydp+nsjDcm3St9D+KOTsQ4ExOaEAL/6EnAEpl8GtxST+ytdqswhCC4yMCO61Hy+M5AoXgDSGrrXHgZakDMAcEVcJdH38791hRxcuM3ldVmHAlAWFdgRLG5rRMVh3qoXz7jbraoTdjyKMegQIQdKR2SX7O9Dv0EEtLz4lTFN2RENvAjLggUPPU+ESoUHmSbwmPGnt7jy3ra2AI3nnYpfn/0e6Op/A3z7HLbdm3XyuNWoTPhy1mc4Adca+HosJ37UPv7nDRIGds1sKYAeWq94+rEk+/6IQ/oRIDRhSYsQbLLWnU6DH4o7iOj7D+X/ngjqmF75nW2s5+7rtdBHFvNzOJalCKHiDTMfdlQ=

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@
},
"dependencies": {
"ci-utils": "^0.5.0",
"sassdoc": "^2.5.0",
"npm-run-all": "^4.1.5",
"jest": "^23.6.0",
"jest-serializer-html": "^5.0.0"
"jest-serializer-html": "^5.0.0",
"jsonwebtoken": "^8.4.0",
"npm-run-all": "^4.1.5",
"sassdoc": "^2.5.0"
},
"husky": {
"hooks": {
Expand Down
Binary file not shown.
169 changes: 169 additions & 0 deletions scripts/check-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
const jwt = require('jsonwebtoken');
const fs = require('fs');
const { join } = require('path');
const fetch = require('node-fetch');
const { getGitSha } = require('ci-utils');

const installationId = '572023';
const repoSlug = 'bolt-design-system/bolt';
const privateKey = fs.readFileSync(
join(__dirname, './bolt-design-system-bot.private-key.pem'),
'utf8',
);

if (!privateKey) {
console.error('Could not find private key PEM file');
process.exit(1);
}

const jwtToken = jwt.sign(
{
iss: '23351',
},
privateKey,
{
algorithm: 'RS256',
expiresIn: '10m',
},
);

let accessToken;

/**
* Get Date in format GitHub wants
* @return {string} - current date like '2019-01-09T23:31:24.748Z'
*/
function getDate() {
return new Date().toISOString();
}

const getHeaders = token => ({
'Content-Type': 'application/json',
Accept:
'application/vnd.github.antiope-preview+json, application/vnd.github.machine-man-preview+json, application/vnd.github.v3+json',
Authorization: `Bearer ${token}`,
});

/**
* @return {Promise<string>}
*/
async function getAccessToken() {
const response = await fetch(
`https://api.github.com/app/installations/${installationId}/access_tokens`,
{
method: 'POST',
headers: {
...getHeaders(jwtToken),
},
},
).catch(err => {
console.error('error getting access token', err);
process.exit(1);
});

const results = await response.json();
if (!response.ok) {
console.log(results);
throw new Error(
`Error getting access token: ${response.status} ${response.statusText}. ${
results.message
}`,
);
}

accessToken = results.token;
return results.token;
}

/**
* @return {Promise<Object>}
* @link https://developer.github.com/v3/checks/suites/#create-a-check-suite
*/
async function createCheckSuite() {
const token = accessToken || (await getAccessToken());
const response = await fetch(
`https://api.github.com/repos/${repoSlug}/check-suites`,
{
method: 'POST',
headers: {
...getHeaders(token),
},
body: JSON.stringify({
head_sha: getGitSha(),
}),
},
).catch(err => {
console.error('error creating check suite', err);
process.exit(1);
});

const results = await response.json();
if (!response.ok) {
console.log(results);
throw new Error(
`Error creating check suite: ${response.status} ${response.statusText}. ${
results.message
}`,
);
}

return results;
}

/**
* @param opt
* @param opt.name
* @param opt.status
* @param opt.output
* @param [opt.conclusion] - The final conclusion of the check. Can be one of success, failure, neutral, cancelled, timed_out, or action_required. When the conclusion is action_required, additional details should be provided on the site specified by details_url.
* @return {Promise<Object>}
* @link https://developer.github.com/v3/checks/runs/#create-a-check-run
*/
async function setCheckRun({ name, status, output, conclusion }) {
const token = accessToken || (await getAccessToken());
const body = {
name,
status,
output,
head_sha: getGitSha(),
};
switch (status) {
case 'in_progress':
body.started_at = getDate();
break;
case 'completed':
body.completed_at = getDate();
body.conclusion = conclusion;
break;
}
const response = await fetch(
`https://api.github.com/repos/${repoSlug}/check-runs`,
{
method: 'POST',
headers: {
...getHeaders(token),
},
body: JSON.stringify(body),
},
).catch(err => {
console.error('error setting check run ' + name + ' to ' + status, err);
process.exit(1);
});

const results = await response.json();
if (!response.ok) {
console.log(results);
throw new Error(
`Error creating check run: ${response.status} ${response.statusText}. ${
results.message
}`,
);
}

return results;
}

module.exports = {
createCheckSuite,
setCheckRun,
};
10 changes: 10 additions & 0 deletions scripts/create-check-suite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node
const { createCheckSuite } = require('./check-run');

createCheckSuite()
.then(results => {
console.log(`Check Suite Created ${results.id}`);
})
.catch(() => {
process.exit(1);
});
Loading

0 comments on commit 3b1f2a0

Please sign in to comment.