Add webhook to welcome user submitting first PR to repo. Part of #1
This commit is contained in:
parent
40c84d3199
commit
abb544feed
|
@ -2,13 +2,13 @@
|
|||
# Script that listens to new GitHub pull requests
|
||||
# and assigns them to the REVIEW column on the "Pipeline for QA" project
|
||||
|
||||
projectBoardName = "Pipeline for QA"
|
||||
reviewColumnName = "REVIEW"
|
||||
notifyRoomName = "core"
|
||||
|
||||
module.exports = (robot) ->
|
||||
|
||||
projectBoardName = "Pipeline for QA"
|
||||
reviewColumnName = "REVIEW"
|
||||
notifyRoomName = "core"
|
||||
|
||||
GitHubApi = require("github")
|
||||
context = require("./github-context.coffee")
|
||||
|
||||
robot.on "github-repo-event", (repo_event) ->
|
||||
githubPayload = repo_event.payload
|
||||
|
@ -16,21 +16,15 @@ module.exports = (robot) ->
|
|||
switch(repo_event.eventType)
|
||||
when "pull_request"
|
||||
# Make sure we don't listen to our own messages
|
||||
return if equalsRobotName(robot, githubPayload.pull_request.user.login)
|
||||
|
||||
token = process.env.HUBOT_GITHUB_TOKEN
|
||||
return console.error "No Github token provided to Hubot" unless token
|
||||
return if context.equalsRobotName(robot, githubPayload.pull_request.user.login)
|
||||
return console.error "No Github token provided to Hubot" unless process.env.HUBOT_GITHUB_TOKEN
|
||||
|
||||
action = githubPayload.action
|
||||
if action == "opened"
|
||||
# A new PR was opened
|
||||
github = new GitHubApi { version: "3.0.0" }
|
||||
github.authenticate({
|
||||
type: "token",
|
||||
token: token
|
||||
})
|
||||
|
||||
assignPullRequestToReview github, githubPayload, robot
|
||||
context.initialize()
|
||||
|
||||
assignPullRequestToReview context.github, githubPayload, robot
|
||||
|
||||
assignPullRequestToReview = (github, githubPayload, robot) ->
|
||||
ownerName = githubPayload.repository.owner.login
|
||||
|
@ -102,21 +96,3 @@ findColumn = (columns, name) ->
|
|||
for idx, column of columns
|
||||
return column if column.name == name
|
||||
return null
|
||||
|
||||
equalsRobotName = (robot, str) ->
|
||||
return getRegexForRobotName(robot).test(str)
|
||||
|
||||
RegExp cachedRobotNameRegex = null
|
||||
getRegexForRobotName = (robot) ->
|
||||
# This comes straight out of Hubot's Robot.coffee
|
||||
# - they didn't get a nice way of extracting that method though
|
||||
if !cachedRobotNameRegex
|
||||
name = robot.name.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
|
||||
|
||||
if robot.alias
|
||||
alias = robot.alias.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
|
||||
namePattern = "^\\s*[@]?(?:#{alias}|#{name})"
|
||||
else
|
||||
namePattern = "^\\s*[@]?#{name}"
|
||||
cachedRobotNameRegex = new RegExp(namePattern, 'i')
|
||||
return cachedRobotNameRegex
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
# Description:
|
||||
# Script that keeps GitHub-related context to be shared among scripts
|
||||
|
||||
GitHubApi = require("github")
|
||||
|
||||
RegExp cachedRobotNameRegex = null
|
||||
initialized = false
|
||||
github = new GitHubApi { version: "3.0.0" }
|
||||
|
||||
module.exports.github = github
|
||||
|
||||
module.exports.initialize = ->
|
||||
return if initialized
|
||||
|
||||
initialized = true
|
||||
github.authenticate({
|
||||
type: "token",
|
||||
token: process.env.HUBOT_GITHUB_TOKEN
|
||||
})
|
||||
|
||||
module.exports.equalsRobotName = (robot, str) ->
|
||||
return module.exports.getRegexForRobotName(robot).test(str)
|
||||
|
||||
module.exports.getRegexForRobotName = (robot) ->
|
||||
# This comes straight out of Hubot's Robot.coffee
|
||||
# - they didn't get a nice way of extracting that method though
|
||||
if !cachedRobotNameRegex
|
||||
name = robot.name.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
|
||||
|
||||
if robot.alias
|
||||
alias = robot.alias.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
|
||||
namePattern = "^\\s*[@]?(?:#{alias}|#{name})"
|
||||
else
|
||||
namePattern = "^\\s*[@]?#{name}"
|
||||
cachedRobotNameRegex = new RegExp(namePattern, 'i')
|
||||
return cachedRobotNameRegex
|
|
@ -0,0 +1,55 @@
|
|||
# Description:
|
||||
# Script that listens to new GitHub pull requests
|
||||
# and assigns them to the REVIEW column on the "Pipeline for QA" project
|
||||
|
||||
module.exports = (robot) ->
|
||||
|
||||
context = require("./github-context.coffee")
|
||||
|
||||
robot.on "github-repo-event", (repo_event) ->
|
||||
githubPayload = repo_event.payload
|
||||
|
||||
switch(repo_event.eventType)
|
||||
when "pull_request"
|
||||
# Make sure we don't listen to our own messages
|
||||
return if context.equalsRobotName(robot, githubPayload.pull_request.user.login)
|
||||
return console.error "No Github token provided to Hubot" unless process.env.HUBOT_GITHUB_TOKEN
|
||||
|
||||
action = githubPayload.action
|
||||
if action == "opened"
|
||||
# A new PR was opened
|
||||
context.initialize()
|
||||
|
||||
greetNewContributor context.github, githubPayload, robot
|
||||
|
||||
greetNewContributor = (github, githubPayload, robot) ->
|
||||
welcomeMessage = "Thanks for making your first PR here!" # TODO: Read the welcome message from a (per-repo?) file (e.g. status-react.welcome-msg.md)
|
||||
ownerName = githubPayload.repository.owner.login
|
||||
repoName = githubPayload.repository.name
|
||||
prNumber = githubPayload.pull_request.number
|
||||
robot.logger.info "greetNewContributor - Handling Pull Request ##{prNumber} on repo #{ownerName}/#{repoName}"
|
||||
|
||||
github.issues.getForRepo {
|
||||
owner: ownerName,
|
||||
repo: repoName
|
||||
state: 'all',
|
||||
creator: githubPayload.pull_request.user.login
|
||||
}, (err, ghissues) ->
|
||||
if err
|
||||
robot.logger.error "Couldn't fetch the user's github issues for repo: #{err}",
|
||||
ownerName, repoName
|
||||
return
|
||||
|
||||
userPullRequests = ghissues.data.filter (issue) -> issue.pull_request
|
||||
if userPullRequests.length == 1
|
||||
github.issues.createComment {
|
||||
owner: ownerName,
|
||||
repo: repoName,
|
||||
number: prNumber,
|
||||
body: welcomeMessage
|
||||
}, (err, result) ->
|
||||
if err
|
||||
robot.logger.error "Couldn't fetch the github projects for repo: #{err}", ownerName, repoName unless err.code == 404
|
||||
robot.logger.info "Commented on PR with welcome message", ownerName, repoName
|
||||
else
|
||||
robot.logger.debug "This is not the user's first PR on the repo, ignoring", ownerName, repoName, githubPayload.pull_request.user.login
|
Loading…
Reference in New Issue