3
3
const request = require ( 'request' )
4
4
5
5
const githubClient = require ( '../lib/github-client' )
6
+ const botUsername = require ( '../lib/bot-username' )
6
7
7
- const botUsername = process . env . BOT_USERNAME || ''
8
8
const jenkinsApiCredentials = process . env . JENKINS_API_CREDENTIALS || ''
9
9
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
+ }
13
15
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
+ } )
16
21
}
17
22
18
23
function isCiRunComment ( commentBody ) {
@@ -95,7 +100,7 @@ module.exports = (app) => {
95
100
logger
96
101
}
97
102
98
- if ( ! wasBotMentioned ( comment . body ) || ! isCiRunComment ( comment . body ) ) return
103
+ if ( ! isCiRunComment ( comment . body ) ) return
99
104
100
105
function replyToCollabWithBuildStarted ( err , buildUrl ) {
101
106
if ( err ) {
@@ -116,6 +121,14 @@ module.exports = (app) => {
116
121
triggerBuild ( options , replyToCollabWithBuildStarted )
117
122
}
118
123
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
+ } )
120
133
} )
121
134
}
0 commit comments