Skip to content

Commit

Permalink
fetch bot username from gh.com rather than $BOT_USERNAME env
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipj committed Mar 22, 2017
1 parent 31e3ebc commit 458fdc6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)<br>
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`**<br>
Username and password used to protected the log files exposed in /logs. Expected format: `username:password`.
- **`KEEP_LOGS`**<br>
Expand Down
14 changes: 14 additions & 0 deletions lib/bot-username.js
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 21 additions & 8 deletions scripts/trigger-jenkins-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
})
})
}

0 comments on commit 458fdc6

Please sign in to comment.