diff --git a/lib/bot-username.js b/lib/bot-username.js index fa726e75..cfd9121e 100644 --- a/lib/bot-username.js +++ b/lib/bot-username.js @@ -1,14 +1 @@ -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) +module.exports = 'nodejs-github-bot' diff --git a/scripts/trigger-jenkins-build.js b/scripts/trigger-jenkins-build.js index dc201b02..d1cdae7f 100644 --- a/scripts/trigger-jenkins-build.js +++ b/scripts/trigger-jenkins-build.js @@ -8,17 +8,9 @@ const { createPrComment } = require('../lib/github-comment') const jenkinsApiCredentials = process.env.JENKINS_API_CREDENTIALS || '' -function ifBotWasMentionedInCiComment (commentBody, cb) { - botUsername.resolve((err, username) => { - if (err) { - return cb(err) - } - - const atBotName = new RegExp(`^@${username} run CI`, 'mi') - const wasMentioned = commentBody.match(atBotName) !== null - - cb(null, wasMentioned) - }) +function wasBotMentionedInCiComment (commentBody) { + const atBotName = new RegExp(`^@${botUsername} run CI`, 'mi') + return commentBody.match(atBotName) !== null } // Name for the Jenkins job should be triggered for a given repository @@ -123,15 +115,9 @@ function handleCommentCreated (event, owner, repo) { author: commentAuthor } - ifBotWasMentionedInCiComment(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 - + if (wasBotMentionedInCiComment(comment.body)) { triggerBuildIfValid(options) - }) + } } function handlePullCreated (event, owner, repo) { diff --git a/test/unit/bot-username.test.js b/test/unit/bot-username.test.js deleted file mode 100644 index db9fbe60..00000000 --- a/test/unit/bot-username.test.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict' - -const proxyquire = require('proxyquire') -const sinon = require('sinon') -const tap = require('tap') - -const githubClient = require('../../lib/github-client') - -tap.test('botUsername.resolve(): returns username of current user', (t) => { - sinon.stub(githubClient.users, 'get').yields(null, { login: 'nodejs-github-bot' }) - const botUsername = proxyquire('../../lib/bot-username', { - './github-client': githubClient - }) - - t.plan(1) - t.tearDown(() => { - githubClient.users.get.restore() - }) - - botUsername.resolve((_, username) => { - t.same(username, 'nodejs-github-bot') - }) -})