From abb544feed796afca66b5017421de8611dbaea9e Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Wed, 17 Jan 2018 00:08:51 +0100 Subject: [PATCH] Add webhook to welcome user submitting first PR to repo. Part of #1 --- scripts/assign-new-pr-to-review.coffee | 44 +++++---------------- scripts/github-context.coffee | 36 +++++++++++++++++ scripts/greet-new-contributor.coffee | 55 ++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 34 deletions(-) create mode 100644 scripts/github-context.coffee create mode 100644 scripts/greet-new-contributor.coffee diff --git a/scripts/assign-new-pr-to-review.coffee b/scripts/assign-new-pr-to-review.coffee index fc43745..769d67a 100644 --- a/scripts/assign-new-pr-to-review.coffee +++ b/scripts/assign-new-pr-to-review.coffee @@ -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 diff --git a/scripts/github-context.coffee b/scripts/github-context.coffee new file mode 100644 index 0000000..ed6119b --- /dev/null +++ b/scripts/github-context.coffee @@ -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 diff --git a/scripts/greet-new-contributor.coffee b/scripts/greet-new-contributor.coffee new file mode 100644 index 0000000..9c8d918 --- /dev/null +++ b/scripts/greet-new-contributor.coffee @@ -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 \ No newline at end of file