From 2cfe7d04b60526b002322945ccb15f2dedc5238d Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Sat, 22 Feb 2014 14:04:18 -0500 Subject: [PATCH 1/3] Created github-issue-assign script. --- package.json | 3 +- scripts/github-issue-assignment.coffee | 63 ++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 scripts/github-issue-assignment.coffee diff --git a/package.json b/package.json index 94bfae0..8c08f28 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "hubot-scripts": ">= 2.5.0 < 3.0.0", "hubot-irc": "~0.1.25", "coffee-script": "~1.7.1", - "hubot-factoid": "0.0.1" + "hubot-factoid": "0.0.1", + "githubot": "~0.5.0" }, "devDependencies": { "grunt": "~0.4.2", diff --git a/scripts/github-issue-assignment.coffee b/scripts/github-issue-assignment.coffee new file mode 100644 index 0000000..e81a889 --- /dev/null +++ b/scripts/github-issue-assignment.coffee @@ -0,0 +1,63 @@ +# Description: +# Assign Github issues by username from within chat. +# +# Dependencies: +# githubot +# +# Configuration: +# HUBOT_GITHUB_TOKEN +# HUBOT_GITHUB_USER - default repo user to assume (optional) +# HUBOT_GITHUB_REPO - default repo name to assume (optional) +# HUBOT_GITHUB_ISSUE_ASSIGN_TEAMID - team id (with pull access) to add assignee to +# +# Commands: +# hubot assign [[]/]<#issue> +# +# Notes: +# - You can create a personal access token here: https://github.com/settings/applications +# - Token will need to the following permissions: +# - `admin:org` (`write:org` once this bug is fixed: https://github.com/isaacs/github/issues/154) +# - `public_repo` + +module.exports = (robot) -> + github = require('githubot')(robot) + #github.handleErrors (response) -> + # console.log response + + REGEX = /// # match [#] + assign + \ + + ([\w._-]+) # assignee [1] + \ + + ( + ( + ([\w._-]+)\/ # repo user [4] + )? + ([\w._-]+) # repo name [5] + )? + ((gh|\#)(\d+)) # issue number [8] + ///i + + team_id = process.env.HUBOT_GITHUB_ISSUE_ASSIGN_TEAMID + + github.handleErrors (response) -> + console.log response + + robot.respond REGEX, (msg) -> + assignee = msg.match[1] + repo_user = msg.match[4] || process.env.HUBOT_GITHUB_USER + repo_name = msg.match[5] || process.env.HUBOT_GITHUB_REPO + issue_id = msg.match[8] + + github.get "teams/#{team_id}/members", (team_members) -> + on_team = false + for user in team_members + on_team = true if user.login == assignee + + unless on_team + github.put "teams/#{team_id}/members/#{assignee}", {}, (team) -> + return + + github.patch "repos/#{repo_user}/#{repo_name}/issues/#{issue_id}", {assignee: assignee}, (issue) -> + msg.send "Issue #{repo_user}/#{repo_name}##{issue_id} assigned to #{assignee}!" + msg.send issue.html_url From 17a60cfc4b8275cca10d760ce79f2ec9bed0b614 Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Mon, 24 Feb 2014 00:34:45 -0500 Subject: [PATCH 2/3] Make use of github.qualified_repo helper. --- scripts/github-issue-assignment.coffee | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/scripts/github-issue-assignment.coffee b/scripts/github-issue-assignment.coffee index e81a889..044a893 100644 --- a/scripts/github-issue-assignment.coffee +++ b/scripts/github-issue-assignment.coffee @@ -20,16 +20,21 @@ # - `public_repo` module.exports = (robot) -> + + unless process.env.HUBOT_GITHUB_USER? and process.env.HUBOT_GITHUB_REPO? + robot.logger.warning 'You likely want to set both HUBOT_GITHUB_USER and HUBOT_GITHUB_REPO to sensible defaults [hubot-auth]' + github = require('githubot')(robot) - #github.handleErrors (response) -> - # console.log response + + github.handleErrors (response) -> + console.log response REGEX = /// # match [#] assign \ + ([\w._-]+) # assignee [1] \ + - ( + ( # qualified repo [2] ( ([\w._-]+)\/ # repo user [4] )? @@ -40,14 +45,15 @@ module.exports = (robot) -> team_id = process.env.HUBOT_GITHUB_ISSUE_ASSIGN_TEAMID - github.handleErrors (response) -> - console.log response - robot.respond REGEX, (msg) -> + assignee = msg.match[1] - repo_user = msg.match[4] || process.env.HUBOT_GITHUB_USER - repo_name = msg.match[5] || process.env.HUBOT_GITHUB_REPO issue_id = msg.match[8] + qualified_repo = github.qualified_repo msg.match[2] + + unless qualified_repo? + msg.send "Not enough information provided to determine desired repo." + return github.get "teams/#{team_id}/members", (team_members) -> on_team = false @@ -58,6 +64,6 @@ module.exports = (robot) -> github.put "teams/#{team_id}/members/#{assignee}", {}, (team) -> return - github.patch "repos/#{repo_user}/#{repo_name}/issues/#{issue_id}", {assignee: assignee}, (issue) -> - msg.send "Issue #{repo_user}/#{repo_name}##{issue_id} assigned to #{assignee}!" + github.patch "repos/#{qualified_repo}/issues/#{issue_id}", {assignee: assignee}, (issue) -> + msg.send "Issue #{qualified_repo}##{issue_id} assigned to #{assignee}!" msg.send issue.html_url From c01743d941fde58e6c5f25ecc57480b1af163a59 Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Mon, 24 Feb 2014 00:47:14 -0500 Subject: [PATCH 3/3] issue-assign: Error out when critical envvars missing. --- scripts/github-issue-assignment.coffee | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/scripts/github-issue-assignment.coffee b/scripts/github-issue-assignment.coffee index 044a893..60dd9fd 100644 --- a/scripts/github-issue-assignment.coffee +++ b/scripts/github-issue-assignment.coffee @@ -6,9 +6,9 @@ # # Configuration: # HUBOT_GITHUB_TOKEN +# HUBOT_GITHUB_ISSUE_ASSIGN_TEAMID - team id (with pull access) to add assignee to # HUBOT_GITHUB_USER - default repo user to assume (optional) # HUBOT_GITHUB_REPO - default repo name to assume (optional) -# HUBOT_GITHUB_ISSUE_ASSIGN_TEAMID - team id (with pull access) to add assignee to # # Commands: # hubot assign [[]/]<#issue> @@ -19,9 +19,19 @@ # - `admin:org` (`write:org` once this bug is fixed: https://github.com/isaacs/github/issues/154) # - `public_repo` +config = + token: process.env.HUBOT_GITHUB_TOKEN + team_id: process.env.HUBOT_GITHUB_ISSUE_ASSIGN_TEAMID + default_user: process.env.HUBOT_GITHUB_USER + default_repo: process.env.HUBOT_GITHUB_REPO + +unless config.token && config.team_id + console.log "Please set the HUBOT_GITHUB_TOKEN and HUBOT_GITHUB_ISSUE_ASSIGN_TEAMID [hubot-auth]" + return + module.exports = (robot) -> - unless process.env.HUBOT_GITHUB_USER? and process.env.HUBOT_GITHUB_REPO? + unless config.default_user && config.default_repo robot.logger.warning 'You likely want to set both HUBOT_GITHUB_USER and HUBOT_GITHUB_REPO to sensible defaults [hubot-auth]' github = require('githubot')(robot) @@ -43,8 +53,6 @@ module.exports = (robot) -> ((gh|\#)(\d+)) # issue number [8] ///i - team_id = process.env.HUBOT_GITHUB_ISSUE_ASSIGN_TEAMID - robot.respond REGEX, (msg) -> assignee = msg.match[1] @@ -55,13 +63,13 @@ module.exports = (robot) -> msg.send "Not enough information provided to determine desired repo." return - github.get "teams/#{team_id}/members", (team_members) -> + github.get "teams/#{config.team_id}/members", (team_members) -> on_team = false for user in team_members on_team = true if user.login == assignee unless on_team - github.put "teams/#{team_id}/members/#{assignee}", {}, (team) -> + github.put "teams/#{config.team_id}/members/#{assignee}", {}, (team) -> return github.patch "repos/#{qualified_repo}/issues/#{issue_id}", {assignee: assignee}, (issue) ->