-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1025 from bolt-design-system/feature/checks-api
checks api setup
- Loading branch information
Showing
7 changed files
with
330 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.