Skip to content

Commit 8fa4cff

Browse files
authored
trigger-jenkins-build: DRY up event handlers
PR-URL: nodejs#191 Reviewed-By: Phillip Johnsen <johphi@gmail.com>
1 parent 63e716b commit 8fa4cff

File tree

1 file changed

+26
-40
lines changed

1 file changed

+26
-40
lines changed

scripts/trigger-jenkins-build.js

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -98,33 +98,36 @@ function createPrComment ({ owner, repo, number, logger }, body) {
9898
})
9999
}
100100

101-
module.exports = (app) => {
102-
app.on('issue_comment.created', function handleCommentCreated (event, owner, repo) {
103-
const { number, logger, comment } = event
104-
const commentAuthor = comment.user.login
105-
const options = {
106-
owner,
107-
repo,
108-
number,
109-
logger
101+
function triggerBuildIfValid (options) {
102+
const { owner, repo, author, logger } = options
103+
104+
githubClient.repos.checkCollaborator({ owner, repo, username: author }, function onResponse (err) {
105+
if (err) {
106+
return logger.debug(`Ignoring comment to me by @${options.author} because they are not a repo collaborator`)
110107
}
111108

112-
function replyToCollabWithBuildStarted (err, buildUrl) {
109+
triggerBuild(options, function onBuildTriggered (err, buildUrl) {
113110
if (err) {
114111
logger.error(err, 'Error while triggering Jenkins build')
115-
return createPrComment(options, `@${commentAuthor} sadly an error occured when I tried to trigger a build :(`)
112+
return createPrComment(options, `@${options.author} sadly an error occured when I tried to trigger a build :(`)
116113
}
117114

118-
createPrComment(options, `@${commentAuthor} build started: ${buildUrl}`)
115+
createPrComment(options, `@${options.author} build started: ${buildUrl}`)
119116
logger.info({ buildUrl }, 'Jenkins build started')
120-
}
121-
122-
function triggerBuildWhenCollaborator (err) {
123-
if (err) {
124-
return logger.debug(`Ignoring comment to me by @${commentAuthor} because they are not a repo collaborator`)
125-
}
117+
})
118+
})
119+
}
126120

127-
triggerBuild(options, replyToCollabWithBuildStarted)
121+
module.exports = (app) => {
122+
app.on('issue_comment.created', function handleCommentCreated (event, owner, repo) {
123+
const { number, logger, comment } = event
124+
const commentAuthor = comment.user.login
125+
const options = {
126+
owner,
127+
repo,
128+
number,
129+
logger,
130+
author: commentAuthor
128131
}
129132

130133
ifBotWasMentionedInCiComment(comment.body, (err, wasMentioned) => {
@@ -134,7 +137,7 @@ module.exports = (app) => {
134137

135138
if (!wasMentioned) return
136139

137-
githubClient.repos.checkCollaborator({ owner, repo, username: commentAuthor }, triggerBuildWhenCollaborator)
140+
triggerBuildIfValid(options)
138141
})
139142
})
140143

@@ -145,29 +148,12 @@ module.exports = (app) => {
145148
owner,
146149
repo,
147150
number,
148-
logger
149-
}
150-
151-
function replyToCollabWithBuildStarted (err, buildUrl) {
152-
if (err) {
153-
logger.error(err, 'Error while triggering Jenkins build')
154-
return createPrComment(options, `@${pullRequestAuthor} sadly an error occured when I tried to trigger a build :(`)
155-
}
156-
157-
createPrComment(options, `@${pullRequestAuthor} build started: ${buildUrl}`)
158-
logger.info({ buildUrl }, 'Jenkins build started')
159-
}
160-
161-
function triggerBuildWhenCollaborator (err) {
162-
if (err) {
163-
return logger.debug(`Ignoring comment to me by @${pullRequestAuthor} because they are not a repo collaborator`)
164-
}
165-
166-
triggerBuild(options, replyToCollabWithBuildStarted)
151+
logger,
152+
author: pullRequestAuthor
167153
}
168154

169155
if (repo === 'node') {
170-
githubClient.repos.checkCollaborator({ owner, repo, username: pullRequestAuthor }, triggerBuildWhenCollaborator)
156+
triggerBuildIfValid(options)
171157
}
172158
})
173159
}

0 commit comments

Comments
 (0)