Last active
March 20, 2018 15:01
-
-
Save trotzig/7fde28a0687339496f9ada7e78898ef8 to your computer and use it in GitHub Desktop.
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
const octokit = require('@octokit/rest'); | |
const jsonwebtoken = require('jsonwebtoken'); | |
const PEM = `-----BEGIN RSA PRIVATE KEY----- | |
<REDACTED> | |
-----END RSA PRIVATE KEY-----`; | |
function generateJwtToken() { | |
// Sign with RSA SHA256 | |
return jsonwebtoken.sign( | |
{ | |
iat: Math.floor(new Date() / 1000), | |
exp: Math.floor(new Date() / 1000) + 60, | |
iss: <the id of your github app>, | |
}, | |
PEM, | |
{ algorithm: 'RS256' }, | |
); | |
} | |
module.exports = async function postStatus({ | |
repo, | |
owner, | |
sha, | |
installationId, | |
triggeredByUrl, | |
}) { | |
const octokitClient = octokit(); | |
octokitClient.authenticate({ | |
type: 'integration', | |
token: generateJwtToken(), | |
}); | |
const { data: { token } } = await octokitClient.apps.createInstallationToken({ | |
installation_id: installationId, | |
}); | |
octokitClient.authenticate({ type: 'token', token }); | |
await octokitClient.repos.createStatus({ | |
owner, | |
repo, | |
sha, | |
state, | |
context: 'Happo', | |
target_url: triggeredByUrl, | |
description: 'All good from over here', | |
headers: { | |
accept: 'application/vnd.github.machine-man-preview+json', | |
}, | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment