Skip to content

Commit 458fdc6

Browse files
committed
fetch bot username from gh.com rather than $BOT_USERNAME env
1 parent 31e3ebc commit 458fdc6

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

README.md

-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).
2929
Only required for the trigger Jenkins build script. The authentication token configured for a particular
3030
Jenkins job, for remote scripts to trigger builds remotely. Found on the job configuration page in
3131
`Build Triggers -> Trigger builds remotely (e.g., from scripts)`.
32-
- **`BOT_USERNAME`** (optional)<br>
33-
Only required for the trigger Jenkins build script. Needed to know if comments made in a repository
34-
meant directored to the bot, by mentioning the bot's username in the start of a comment.
3532
- **`LOGIN_CREDENTIALS`**<br>
3633
Username and password used to protected the log files exposed in /logs. Expected format: `username:password`.
3734
- **`KEEP_LOGS`**<br>

lib/bot-username.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const memoize = require('async').memoize
2+
3+
const githubClient = require('./github-client')
4+
5+
function requestGitHubForUsername (cb) {
6+
githubClient.users.get({}, (err, currentUser) => {
7+
if (err) {
8+
return cb(err)
9+
}
10+
cb(null, currentUser.login)
11+
})
12+
}
13+
14+
exports.resolve = memoize(requestGitHubForUsername)

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"private": true,
1414
"license": "MIT",
1515
"dependencies": {
16+
"async": "2.1.5",
1617
"basic-auth": "^1.0.4",
1718
"body-parser": "^1.15.0",
1819
"bunyan": "^1.8.1",

scripts/trigger-jenkins-build.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
const request = require('request')
44

55
const githubClient = require('../lib/github-client')
6+
const botUsername = require('../lib/bot-username')
67

7-
const botUsername = process.env.BOT_USERNAME || ''
88
const jenkinsApiCredentials = process.env.JENKINS_API_CREDENTIALS || ''
99

10-
function wasBotMentioned (commentBody) {
11-
// no need to check when we haven't specified the bot's current username
12-
if (!botUsername) return false
10+
function ifBotWasMentioned (commentBody, cb) {
11+
botUsername.resolve((err, username) => {
12+
if (err) {
13+
return cb(err)
14+
}
1315

14-
const atBotName = new RegExp('^@' + botUsername)
15-
return commentBody.match(atBotName) !== null
16+
const atBotName = new RegExp('^@' + username)
17+
const wasMentioned = commentBody.match(atBotName) !== null
18+
19+
cb(null, wasMentioned)
20+
})
1621
}
1722

1823
function isCiRunComment (commentBody) {
@@ -95,7 +100,7 @@ module.exports = (app) => {
95100
logger
96101
}
97102

98-
if (!wasBotMentioned(comment.body) || !isCiRunComment(comment.body)) return
103+
if (!isCiRunComment(comment.body)) return
99104

100105
function replyToCollabWithBuildStarted (err, buildUrl) {
101106
if (err) {
@@ -116,6 +121,14 @@ module.exports = (app) => {
116121
triggerBuild(options, replyToCollabWithBuildStarted)
117122
}
118123

119-
githubClient.repos.checkCollaborator({ owner, repo, username: commentAuthor }, triggerBuildWhenCollaborator)
124+
ifBotWasMentioned(comment.body, (err, wasMentioned) => {
125+
if (err) {
126+
return logger.error(err, 'Error while checking if the bot username was mentioned in a comment')
127+
}
128+
129+
if (!wasMentioned) return
130+
131+
githubClient.repos.checkCollaborator({ owner, repo, username: commentAuthor }, triggerBuildWhenCollaborator)
132+
})
120133
})
121134
}

0 commit comments

Comments
 (0)