Skip to content

Instantly share code, notes, and snippets.

@trotzig

trotzig/postStatus.js

Last active Mar 20, 2018
Embed
What would you like to do?
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