diff --git a/lib/phabricator.coffee b/lib/phabricator.coffee index 87b7c2f..a545c04 100644 --- a/lib/phabricator.coffee +++ b/lib/phabricator.coffee @@ -172,7 +172,7 @@ class Phabricator cb { error_info: "Sorry, #{project} not found." } - withUser: (msg, user, cb) => + withUser: (from, user, cb) => if @ready() is true id = user.phid if id @@ -180,22 +180,26 @@ class Phabricator else email = user.email_address or user.pagerdutyEmail unless email - if msg.envelope.user.name is user.name - msg.send "Sorry, I can't figure out your email address :( " + - 'Can you tell me with `.phab me as you@yourdomain.com`?' + if from.name is user.name + cb { + error_info: "Sorry, I can't figure out your email address :( " + + 'Can you tell me with `.phab me as you@yourdomain.com`?' + } else - msg.send "Sorry, I can't figure #{user.name} email address. " + - "Can you help me with .phab #{user.name} = " - msg.finish() - return - query = { 'emails[0]': email } - @phabGet query, 'user.query', (json_body) -> - unless json_body['result']['0']? - msg.send "Sorry, I cannot find #{email} :(" - msg.finish() - return - user.phid = json_body['result']['0']['phid'] - cb user.phid + cb { + error_info: "Sorry, I can't figure #{user.name} email address. " + + "Can you help me with .phab #{user.name} = " + } + else + query = { 'emails[0]': email } + @phabGet query, 'user.query', (json_body) -> + unless json_body['result']['0']? + cb { + error_info: "Sorry, I cannot find #{email} :(" + } + else + user.phid = json_body['result']['0']['phid'] + cb user.phid withUserByPhid: (phid, cb) => @@ -275,61 +279,63 @@ class Phabricator cb json_body - createTask: (msg, phid, title, description, cb) -> + createTask: (user, phid, title, description, cb) -> if @ready() is true - user = msg.envelope.user adapter = @robot.adapterName user = @robot.brain.userForName user.name - @withUser msg, user, (userPhid) => - @withBotPHID (bot_phid) => + @withUser user, user, (userPhid) => + if userPhid.error_info? + cb userPhid + else + @withBotPHID (bot_phid) => + query = { + 'transactions[0][type]': 'title', + 'transactions[0][value]': "#{title}", + 'transactions[1][type]': 'comment', + 'transactions[1][value]': "(created by #{user.name} on #{adapter})", + 'transactions[2][type]': 'subscribers.add', + 'transactions[2][value][0]': "#{userPhid}", + 'transactions[3][type]': 'subscribers.remove', + 'transactions[3][value][0]': "#{bot_phid}", + 'transactions[4][type]': 'projects.add', + 'transactions[4][value][]': "#{phid}" + } + if description? + query['transactions[5][type]'] = 'description' + query['transactions[5][value]'] = "#{description}" + @phabGet query, 'maniphest.edit', (json_body) -> + cb json_body + + + createPaste: (user, title, cb) -> + if @ready() is true + bot_phid = @robot.brain.data.phabricator.bot_phid + adapter = @robot.adapterName + user = @robot.brain.userForName user.name + @withUser user, user, (userPhid) => + if userPhid.error_info? + cb userPhid + else query = { 'transactions[0][type]': 'title', 'transactions[0][value]': "#{title}", - 'transactions[1][type]': 'comment', + 'transactions[1][type]': 'text', 'transactions[1][value]': "(created by #{user.name} on #{adapter})", 'transactions[2][type]': 'subscribers.add', 'transactions[2][value][0]': "#{userPhid}", 'transactions[3][type]': 'subscribers.remove', - 'transactions[3][value][0]': "#{bot_phid}", - 'transactions[4][type]': 'projects.add', - 'transactions[4][value][]': "#{phid}" + 'transactions[3][value][0]': "#{bot_phid}" } - if description? - query['transactions[5][type]'] = 'description' - query['transactions[5][value]'] = "#{description}" - @phabGet query, 'maniphest.edit', (json_body) -> + @phabGet query, 'paste.edit', (json_body) -> cb json_body - createPaste: (msg, title, cb) -> - if @ready() is true - user = msg.envelope.user - bot_phid = @robot.brain.data.phabricator.bot_phid - adapter = @robot.adapterName - user = @robot.brain.userForName user.name - @withUser msg, user, (userPhid) => - query = { - 'transactions[0][type]': 'title', - 'transactions[0][value]': "#{title}", - 'transactions[1][type]': 'text', - 'transactions[1][value]': "(created by #{user.name} on #{adapter})", - 'transactions[2][type]': 'subscribers.add', - 'transactions[2][value][0]': "#{userPhid}", - 'transactions[3][type]': 'subscribers.remove', - 'transactions[3][value][0]': "#{bot_phid}" - } - @phabGet query, 'paste.edit', (json_body) -> - cb json_body - - - recordPhid: (msg, id) -> - user = msg.envelope.user + recordPhid: (user, id) -> user.lastTask = moment().utc() user.lastPhid = id - retrievePhid: (msg) -> - user = msg.envelope.user + retrievePhid: (user) -> expires_at = moment(user.lastTask).add(5, 'minutes') if user.lastPhid? and moment().utc().isBefore(expires_at) user.lastPhid @@ -337,9 +343,8 @@ class Phabricator null - addComment: (msg, id, comment, cb) -> + addComment: (user, id, comment, cb) -> if @ready() is true - user = msg.envelope.user query = { 'objectIdentifier': id, 'transactions[0][type]': 'comment', @@ -351,9 +356,8 @@ class Phabricator cb json_body - updateStatus: (msg, id, status, comment, cb) -> + updateStatus: (user, id, status, comment, cb) -> if @ready() is true - user = msg.envelope.user query = { 'objectIdentifier': id, 'transactions[0][type]': 'status', @@ -372,9 +376,8 @@ class Phabricator cb json_body - updatePriority: (msg, id, priority, comment, cb) -> + updatePriority: (user, id, priority, comment, cb) -> if @ready() is true - user = msg.envelope.user query = { 'objectIdentifier': id, 'transactions[0][type]': 'priority', diff --git a/scripts/phabs_commands.coffee b/scripts/phabs_commands.coffee index e7faf17..50b11f7 100644 --- a/scripts/phabs_commands.coffee +++ b/scripts/phabs_commands.coffee @@ -48,14 +48,14 @@ module.exports = (robot) -> if projectData.error_info? msg.send projectData.error_info else - phab.createTask msg, projectData.data.phid, name, description, (body) -> + phab.createTask msg.envelope.user, projectData.data.phid, name, description, (body) -> # console.log body if body['error_info']? msg.send "#{body['error_info']}" else id = body['result']['object']['id'] url = process.env.PHABRICATOR_URL + "/T#{id}" - phab.recordPhid msg, id + phab.recordPhid msg.envelope.user, id msg.send "Task T#{id} created = #{url}" msg.finish() @@ -63,13 +63,13 @@ module.exports = (robot) -> robot.respond /ph(?:ab)? paste (.*)$/, (msg) -> phab.withPermission msg, msg.envelope.user, 'phuser', -> title = msg.match[1] - phab.createPaste msg, title, (body) -> + phab.createPaste msg.envelope.user, title, (body) -> if body['error_info']? msg.send "#{body['error_info']}" else id = body['result']['object']['id'] url = process.env.PHABRICATOR_URL + "/paste/edit/#{id}" - phab.recordPhid msg, id + phab.recordPhid msg.envelope.user, id msg.send "Paste P#{id} created = edit on #{url}" msg.finish() @@ -88,7 +88,7 @@ module.exports = (robot) -> # hubot phab Txx - gives information about task Txxx robot.respond /ph(?:ab)?(?: T([0-9]+) ?)?$/, (msg) -> - id = msg.match[1] ? phab.retrievePhid(msg) + id = msg.match[1] ? phab.retrievePhid(msg.envelope.user) unless id? msg.send "Sorry, you don't have any task active right now." msg.finish() @@ -100,7 +100,7 @@ module.exports = (robot) -> phab.withUserByPhid body.result.ownerPHID, (owner) -> status = body.result.status priority = body.result.priority - phab.recordPhid msg, id + phab.recordPhid msg.envelope.user, id msg.send "T#{id} has status #{status}, " + "priority #{priority}, owner #{owner.name}" msg.finish() @@ -108,13 +108,13 @@ module.exports = (robot) -> # hubot phab Txx + - add a comment to task Txx robot.respond /ph(?:ab)?(?: T([0-9]+))? \+ (.+)$/, (msg) -> phab.withPermission msg, msg.envelope.user, 'phuser', -> - id = msg.match[1] ? phab.retrievePhid(msg) + id = msg.match[1] ? phab.retrievePhid(msg.envelope.user) unless id? msg.send "Sorry, you don't have any task active right now." msg.finish() return comment = msg.match[2] - phab.addComment msg, id, comment, (body) -> + phab.addComment msg.envelope.user, id, comment, (body) -> if body['error_info']? msg.send "oops T#{id} #{body['error_info']}" else @@ -128,14 +128,14 @@ module.exports = (robot) -> '(?: = (.+))?$' ), (msg) -> phab.withPermission msg, msg.envelope.user, 'phuser', -> - id = msg.match[1] ? phab.retrievePhid(msg) + id = msg.match[1] ? phab.retrievePhid(msg.envelope.user) unless id? msg.send "Sorry, you don't have any task active right now." msg.finish() return status = msg.match[2] comment = msg.match[3] - phab.updateStatus msg, id, status, comment, (body) -> + phab.updateStatus msg.envelope.user, id, status, comment, (body) -> if body['error_info']? msg.send "oops T#{id} #{body['error_info']}" else @@ -148,14 +148,14 @@ module.exports = (robot) -> '(?: = (.+))?$' ), (msg) -> phab.withPermission msg, msg.envelope.user, 'phuser', -> - id = msg.match[1] ? phab.retrievePhid(msg) + id = msg.match[1] ? phab.retrievePhid(msg.envelope.user) unless id? msg.send "Sorry, you don't have any task active right now." msg.finish() return priority = msg.match[2] comment = msg.match[3] - phab.updatePriority msg, id, priority, comment, (body) -> + phab.updatePriority msg.envelope.user, id, priority, comment, (body) -> if body['error_info']? msg.send "oops T#{id} #{body['error_info']}" else @@ -171,8 +171,11 @@ module.exports = (robot) -> msg.send "Sorry, I have no idea who #{name} is. Did you mistype it?" msg.finish() return - phab.withUser msg, assignee, (userPhid) -> - msg.send "Hey I know #{name}, he's #{userPhid}" + phab.withUser msg.envelope.user, assignee, (userPhid) -> + if userPhid.error_info? + msg.send userPhid.error_info + else + msg.send "Hey I know #{name}, he's #{userPhid}" msg.finish() # hubot phab me as - makes caller known with @@ -210,20 +213,23 @@ module.exports = (robot) -> else who = msg.match[5] what = msg.match[4] - id = what ? phab.retrievePhid(msg) + id = what ? phab.retrievePhid(msg.envelope.user) unless id? msg.send "Sorry, you don't have any task active right now." msg.finish() return assignee = robot.brain.userForName(who) if assignee? - phab.withUser msg, assignee, (userPhid) -> - phab.assignTask id, userPhid, (body) -> - if body['error_info']? - msg.send "#{body['error_info']}" - else - msg.send "Ok. T#{id} is now assigned to #{assignee.name}" - msg.finish() + phab.withUser msg.envelope.user, assignee, (userPhid) -> + if userPhid.error_info? + msg.send userPhid.error_info + else + phab.assignTask id, userPhid, (body) -> + if body['error_info']? + msg.send "#{body['error_info']}" + else + msg.send "Ok. T#{id} is now assigned to #{assignee.name}" + msg.finish() else msg.send "Sorry I don't know who is #{who}, can you .phab #{who} = " msg.finish() diff --git a/scripts/phabs_hear.coffee b/scripts/phabs_hear.coffee index 10ddd7c..8cc0972 100644 --- a/scripts/phabs_hear.coffee +++ b/scripts/phabs_hear.coffee @@ -50,7 +50,7 @@ module.exports = (robot) -> else msg.send "#{body['result']['uri']}#{closed} - #{body['result']['title']} " + "(#{body['result']['priority']})" - phab.recordPhid msg, id + phab.recordPhid msg.envelope.user, id when 'F' is type phab.fileInfo id, (body) ->