Move hardcoded configuration to a `github.yaml` file. Part of #1

This commit is contained in:
Pedro Pombeiro 2018-01-18 18:54:10 +01:00
parent 2c60413cad
commit 9f2b025232
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
6 changed files with 69 additions and 11 deletions

11
github.yaml Normal file
View File

@ -0,0 +1,11 @@
pull-requests:
project-board:
name: 'Pipeline for QA'
review-column-name: 'REVIEW'
welcome-bot:
message: 'Thanks for making your first PR here!'
slack:
notification:
room: 'core'

27
package-lock.json generated
View File

@ -78,6 +78,14 @@
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz",
"integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4="
},
"argparse": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.9.tgz",
"integrity": "sha1-c9g7wmP4bpf4zE9rrhsOkKfSLIY=",
"requires": {
"sprintf-js": "1.0.3"
}
},
"asn1": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.3.tgz",
@ -652,6 +660,11 @@
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
"integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ="
},
"esprima": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.0.tgz",
"integrity": "sha512-oftTcaMu/EGrEIu904mWteKIv8vMuOgGYo7EhVJJN00R/EED9DCua/xxHRdYnKtcECzVg7xOWhflvJMnqcFZjw=="
},
"etag": {
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/etag/-/etag-1.7.0.tgz",
@ -1153,6 +1166,15 @@
"resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
"integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo="
},
"js-yaml": {
"version": "3.10.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.10.0.tgz",
"integrity": "sha512-O2v52ffjLa9VeM43J4XocZE//WT9N0IiwDa3KSHH7Tu8CtH+1qM8SIZvnsTh6v+4yFy5KUY3BHUVwjpfAWsjIA==",
"requires": {
"argparse": "1.0.9",
"esprima": "4.0.0"
}
},
"jsbn": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz",
@ -1737,6 +1759,11 @@
"hoek": "2.16.3"
}
},
"sprintf-js": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw="
},
"sshpk": {
"version": "1.13.1",
"resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.13.1.tgz",

View File

@ -22,6 +22,7 @@
"hubot-scripts": "^2.17.2",
"hubot-shipit": "^0.2.1",
"hubot-slack": "^4.4.0",
"js-yaml": "^3.10.0",
"jwt-simple": "^0.5.1"
},
"engines": {

View File

@ -14,10 +14,6 @@
// Author:
// PombeirP
const projectBoardName = "Pipeline for QA";
const reviewColumnName = "REVIEW";
const notifyRoomName = "core";
module.exports = function(robot) {
const context = require('./github-context.js');
@ -33,14 +29,16 @@ module.exports = function(robot) {
var { action } = githubPayload;
if (action === "opened") {
// A new PR was opened
return assignPullRequestToReview(context.github(), githubPayload, robot);
return assignPullRequestToReview(context, githubPayload, robot);
}
break;
}
});
};
async function assignPullRequestToReview(github, githubPayload, robot) {
async function assignPullRequestToReview(context, githubPayload, robot) {
const github = context.github();
const githubConfig = context.config();
const ownerName = githubPayload.repository.owner.login;
const repoName = githubPayload.repository.name;
const prNumber = githubPayload.pull_request.number;
@ -58,6 +56,7 @@ async function assignPullRequestToReview(github, githubPayload, robot) {
});
// Find "Pipeline for QA" project
const projectBoardName = githubConfig['pull-requests']['project-board'].name;
const project = ghprojects.data.find(function(p) { return p.name === projectBoardName });
if (!project) {
robot.logger.warn(`Couldn't find project ${projectBoardName} in repo ${ownerName}/${repoName}`);
@ -70,6 +69,7 @@ async function assignPullRequestToReview(github, githubPayload, robot) {
try {
ghcolumns = await github.projects.getProjectColumns({ project_id: project.id });
const reviewColumnName = githubConfig['pull-requests']['project-board']['review-column-name'];
const column = ghcolumns.data.find(function(c) { return c.name === reviewColumnName });
if (!column) {
robot.logger.warn(`Couldn't find ${projectBoardName} column in project ${project.name}`);
@ -89,8 +89,7 @@ async function assignPullRequestToReview(github, githubPayload, robot) {
robot.logger.debug(`Created card: ${ghcard.data.url}`, ghcard.data.id);
// Send message to Slack
robot.messageRoom(notifyRoomName, `Moved PR ${githubPayload.pull_request.number} to ${reviewColumnName} in ${projectBoardName} project`
);
robot.messageRoom(githubConfig.slack.notification.room, `Moved PR ${githubPayload.pull_request.number} to ${reviewColumnName} in ${projectBoardName} project`);
} catch (err) {
robot.logger.error(`Couldn't create project card for the PR: ${err}`, column.id, githubPayload.pull_request.id);
}

View File

@ -15,14 +15,19 @@ const githubAPI = new GitHubApi({
timeout: 15000,
requestMedia: 'application/vnd.github.v3+json'
});
let githubConfig = null;
module.exports = {
github() { return githubAPI; },
config() { return githubConfig; },
initialize(robot, integrationID) {
if (initialized) { return; }
githubConfig = loadConfig(robot, './github.yaml')
const token = robot.brain.get('github-token');
if (token) {
initialized = true;
@ -89,3 +94,17 @@ function getRegexForRobotName(robot) {
}
return cachedRobotNameRegex;
};
function loadConfig(robot, fileName) {
// Get document, or throw exception on error
try {
const yaml = require('js-yaml');
const fs = require('fs');
return yaml.safeLoad(fs.readFileSync(fileName, 'utf8'));
} catch (e) {
robot.logger.error(e);
}
return null;
}

View File

@ -24,16 +24,17 @@ module.exports = function(robot) {
var { action } = githubPayload;
if (action === "opened") {
// A new PR was opened
return greetNewContributor(context.github(), githubPayload, robot);
return greetNewContributor(context, githubPayload, robot);
}
break;
}
});
};
async function greetNewContributor(github, githubPayload, robot) {
async function greetNewContributor(context, githubPayload, robot) {
// TODO: Read the welcome message from a (per-repo?) file (e.g. status-react.welcome-msg.md)
const welcomeMessage = "Thanks for making your first PR here!";
const github = context.github();
const welcomeMessage = context.config()['pull-requests']['welcome-bot'].message;
const ownerName = githubPayload.repository.owner.login;
const repoName = githubPayload.repository.name;
const prNumber = githubPayload.pull_request.number;