Fix module.exports in github-context.js

This commit is contained in:
Pedro Pombeiro 2018-01-19 22:59:32 +01:00
parent 3a9877ee12
commit 0d4088415c
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
5 changed files with 61 additions and 60 deletions

View File

@ -16,7 +16,7 @@
module.exports = function(robot) {
const gitHubContext = require('./github-context.js');
const gitHubContext = require('./github-context.js')();
return robot.on("github-repo-event", function(repo_event) {
const githubPayload = repo_event.payload;

View File

@ -9,7 +9,7 @@
module.exports = function(robot) {
robot.brain.on('loaded', function() {
const gitHubContext = require('./github-context.js');
const gitHubContext = require('./github-context.js')();
appID = robot.brain.get("github-app_id");
if (appID) {

View File

@ -17,73 +17,74 @@ const githubAPI = new GitHubApi({
});
let githubConfig = null;
module.exports = {
module.exports = function() {
return {
api() { return githubAPI; },
api() { return githubAPI; },
config() { return githubConfig; },
config() { return githubConfig; },
initialize(robot, integrationID) {
if (initialized) { return; }
initialize(robot, integrationID) {
if (initialized) { return; }
githubConfig = loadConfig(robot, './github.yaml')
githubConfig = loadConfig(robot, './github.yaml')
const token = robot.brain.get('github-token');
if (token) {
initialized = true;
process.env.HUBOT_GITHUB_TOKEN = token;
robot.logger.debug("Reused cached GitHub token");
githubAPI.authenticate({ type: 'token', token });
return;
}
const jwtLib = require('jwt-simple');
// Private key contents
let privateKey = process.env.GITHUB_PEM;
const now = Math.round(Date.now() / 1000);
// Generate the JWT
const payload = {
// issued at time
iat: now,
// JWT expiration time (10 minute maximum)
exp: now + (1 * 60),
// GitHub App's identifier
iss: integrationID
};
jwt = jwtLib.encode(payload, privateKey, 'RS256');
githubAPI.authenticate({
type: 'integration',
token: jwt
});
robot.logger.debug("Configured integration authentication with JWT", jwt);
const token = robot.brain.get('github-token');
if (token) {
initialized = true;
process.env.HUBOT_GITHUB_TOKEN = token;
robot.logger.debug("Reused cached GitHub token");
githubAPI.authenticate({ type: 'token', token });
return;
},
equalsRobotName(robot, str) {
return getRegexForRobotName(robot).test(str);
}
};
const jwtLib = require('jwt-simple');
function getRegexForRobotName(robot) {
// This comes straight out of Hubot's Robot.coffee
// - they didn't get a nice way of extracting that method though
if (!cachedRobotNameRegex) {
let namePattern;
const name = robot.name.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
// Private key contents
let privateKey = process.env.GITHUB_PEM;
const now = Math.round(Date.now() / 1000);
// Generate the JWT
const payload = {
// issued at time
iat: now,
// JWT expiration time (10 minute maximum)
exp: now + (1 * 60),
// GitHub App's identifier
iss: integrationID
};
jwt = jwtLib.encode(payload, privateKey, 'RS256');
githubAPI.authenticate({
type: 'integration',
token: jwt
});
robot.logger.debug("Configured integration authentication with JWT", jwt);
initialized = true;
},
equalsRobotName(robot, str) {
return getRegexForRobotName(robot).test(str);
}
};
function getRegexForRobotName(robot) {
// This comes straight out of Hubot's Robot.coffee
// - they didn't get a nice way of extracting that method though
if (!cachedRobotNameRegex) {
let namePattern;
const name = robot.name.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
if (robot.alias) {
const alias = robot.alias.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
namePattern = `^\\s*[@]?(?:${alias}|${name})`;
} else {
namePattern = `^\\s*[@]?${name}`;
if (robot.alias) {
const alias = robot.alias.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
namePattern = `^\\s*[@]?(?:${alias}|${name})`;
} else {
namePattern = `^\\s*[@]?${name}`;
}
cachedRobotNameRegex = new RegExp(namePattern, 'i');
}
cachedRobotNameRegex = new RegExp(namePattern, 'i');
return cachedRobotNameRegex;
}
return cachedRobotNameRegex;
};
function loadConfig(robot, fileName) {

View File

@ -10,7 +10,7 @@
module.exports = function(robot) {
const gitHubContext = require('./github-context.js');
const gitHubContext = require('./github-context.js')();
return robot.on("github-repo-event", async function(repo_event) {
const githubPayload = repo_event.payload;

View File

@ -11,7 +11,7 @@
module.exports = function(robot) {
const gitHubContext = require('./github-context.js');
const gitHubContext = require('./github-context.js')();
return robot.on("github-repo-event", function(repo_event) {
const githubPayload = repo_event.payload;