Skip to content

Instantly share code, notes, and snippets.

@trotzig
Last active March 20, 2018 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trotzig/7fde28a0687339496f9ada7e78898ef8 to your computer and use it in GitHub Desktop.
Save trotzig/7fde28a0687339496f9ada7e78898ef8 to your computer and use it in GitHub Desktop.
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