Skip to content
This repository was archived by the owner on Sep 14, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions docs/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
//
// Usage: APP_ID=417 node example.js

const http = require('http');
const fs = require('fs');
const http = require('http')
const fs = require('fs')
/* eslint-disable import/no-extraneous-dependencies, import/no-unresolved */
const createWebhook = require('github-webhook-handler');
const createGitHubApp = require('../index.js');
const createWebhook = require('github-webhook-handler')
const createGitHubApp = require('../index.js')

const webhook = createWebhook({
path: '/',
secret: process.env.WEBHOOK_SECRET || 'development'
});
})

const app = createGitHubApp({
id: process.env.APP_ID,
cert: process.env.PRIVATE_KEY || fs.readFileSync('private-key.pem')
});
})

const server = http.createServer((req, res) => {
webhook(req, res, err => {
if (err) {
console.error(err);
res.statusCode = 500;
res.end('Something has gone terribly wrong.');
console.error(err)
res.statusCode = 500
res.end('Something has gone terribly wrong.')
} else {
res.statusCode = 404;
res.end('no such location');
res.statusCode = 404
res.end('no such location')
}
});
});
})
})

// Comment on opened issues
webhook.on('issues', event => {
Expand All @@ -42,10 +42,10 @@ webhook.on('issues', event => {
repo: event.payload.repository.name,
number: event.payload.issue.number,
body: 'Welcome to the robot uprising.'
});
});
})
})
}
});
})

console.log('Listing on http://localhost:3000');
server.listen(3000);
console.log('Listing on http://localhost:3000')
server.listen(3000)
38 changes: 19 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
const jwt = require('jsonwebtoken');
const GitHubApi = require('github');
const jwt = require('jsonwebtoken')
const GitHubApi = require('github')

module.exports = function ({id, cert, debug = false}) {
function asApp() {
const github = new GitHubApi({debug});
github.authenticate({type: 'integration', token: generateJwt(id, cert)});
function asApp () {
const github = new GitHubApi({debug})
github.authenticate({type: 'integration', token: generateJwt(id, cert)})
// Return a promise to keep API consistent
return Promise.resolve(github);
return Promise.resolve(github)
}

// Authenticate as the given installation
function asInstallation(installationId) {
function asInstallation (installationId) {
return createToken(installationId).then(res => {
const github = new GitHubApi({debug});
github.authenticate({type: 'token', token: res.data.token});
return github;
});
const github = new GitHubApi({debug})
github.authenticate({type: 'token', token: res.data.token})
return github
})
}

// https://developer.github.com/early-access/integrations/authentication/#as-an-installation
function createToken(installationId) {
function createToken (installationId) {
return asApp().then(github => {
return github.apps.createInstallationToken({
installation_id: installationId
});
});
})
})
}

// Internal - no need to exose this right now
function generateJwt(id, cert) {
function generateJwt (id, cert) {
const payload = {
iat: Math.floor(new Date() / 1000), // Issued at time
exp: Math.floor(new Date() / 1000) + 60, // JWT expiration time
iss: id // Integration's GitHub id
};
}

// Sign with RSA SHA256
return jwt.sign(payload, cert, {algorithm: 'RS256'});
return jwt.sign(payload, cert, {algorithm: 'RS256'})
}

return {asApp, asInstallation, createToken};
};
return {asApp, asInstallation, createToken}
}
Loading