33const request = require ( 'request' )
44
55const githubClient = require ( '../lib/github-client' )
6+ const botUsername = require ( '../lib/bot-username' )
67
7- const botUsername = process . env . BOT_USERNAME || ''
88const 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
1823function 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