From a90adbd0c73f4b9f511e85d9529e5b65e5b210c5 Mon Sep 17 00:00:00 2001 From: Phillip Johnsen Date: Wed, 22 Mar 2017 20:15:36 +0100 Subject: [PATCH] fetch bot username from gh.com rather than --- README.md | 3 --- lib/bot-username.js | 14 ++++++++++++++ package.json | 1 + scripts/trigger-jenkins-build.js | 29 +++++++++++++++++++++-------- 4 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 lib/bot-username.js diff --git a/README.md b/README.md index 2454914b..a9a7631c 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,6 @@ See [CONTRIBUTING.md](CONTRIBUTING.md). Only required for the trigger Jenkins build script. The authentication token configured for a particular Jenkins job, for remote scripts to trigger builds remotely. Found on the job configuration page in `Build Triggers -> Trigger builds remotely (e.g., from scripts)`. -- **`BOT_USERNAME`** (optional)
- Only required for the trigger Jenkins build script. Needed to know if comments made in a repository - meant directored to the bot, by mentioning the bot's username in the start of a comment. - **`LOGIN_CREDENTIALS`**
Username and password used to protected the log files exposed in /logs. Expected format: `username:password`. - **`KEEP_LOGS`**
diff --git a/lib/bot-username.js b/lib/bot-username.js new file mode 100644 index 00000000..fa726e75 --- /dev/null +++ b/lib/bot-username.js @@ -0,0 +1,14 @@ +const memoize = require('async').memoize + +const githubClient = require('./github-client') + +function requestGitHubForUsername (cb) { + githubClient.users.get({}, (err, currentUser) => { + if (err) { + return cb(err) + } + cb(null, currentUser.login) + }) +} + +exports.resolve = memoize(requestGitHubForUsername) diff --git a/package.json b/package.json index ba78b5f4..80c42890 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "private": true, "license": "MIT", "dependencies": { + "async": "2.1.5", "basic-auth": "^1.0.4", "body-parser": "^1.15.0", "bunyan": "^1.8.1", diff --git a/scripts/trigger-jenkins-build.js b/scripts/trigger-jenkins-build.js index 801a6293..b0341b9c 100644 --- a/scripts/trigger-jenkins-build.js +++ b/scripts/trigger-jenkins-build.js @@ -3,16 +3,21 @@ const request = require('request') const githubClient = require('../lib/github-client') +const botUsername = require('../lib/bot-username') -const botUsername = process.env.BOT_USERNAME || '' const jenkinsApiCredentials = process.env.JENKINS_API_CREDENTIALS || '' -function wasBotMentioned (commentBody) { - // no need to check when we haven't specified the bot's current username - if (!botUsername) return false +function ifBotWasMentioned (commentBody, cb) { + botUsername.resolve((err, username) => { + if (err) { + return cb(err) + } - const atBotName = new RegExp('^@' + botUsername) - return commentBody.match(atBotName) !== null + const atBotName = new RegExp('^@' + username) + const wasMentioned = commentBody.match(atBotName) !== null + + cb(null, wasMentioned) + }) } function isCiRunComment (commentBody) { @@ -95,7 +100,7 @@ module.exports = (app) => { logger } - if (!wasBotMentioned(comment.body) || !isCiRunComment(comment.body)) return + if (!isCiRunComment(comment.body)) return function replyToCollabWithBuildStarted (err, buildUrl) { if (err) { @@ -116,6 +121,14 @@ module.exports = (app) => { triggerBuild(options, replyToCollabWithBuildStarted) } - githubClient.repos.checkCollaborator({ owner, repo, username: commentAuthor }, triggerBuildWhenCollaborator) + ifBotWasMentioned(comment.body, (err, wasMentioned) => { + if (err) { + return logger.error(err, 'Error while checking if the bot username was mentioned in a comment') + } + + if (!wasMentioned) return + + githubClient.repos.checkCollaborator({ owner, repo, username: commentAuthor }, triggerBuildWhenCollaborator) + }) }) }