Skip to content

Commit

Permalink
remove most msg from lib for separation of concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
mose committed Aug 5, 2016
1 parent f3b268d commit 9c911d0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 82 deletions.
121 changes: 62 additions & 59 deletions lib/phabricator.coffee
Expand Up @@ -172,30 +172,34 @@ 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
cb(id)
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} = <email>"
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} = <email>"
}
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) =>
Expand Down Expand Up @@ -275,71 +279,72 @@ 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
else
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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
50 changes: 28 additions & 22 deletions scripts/phabs_commands.coffee
Expand Up @@ -48,28 +48,28 @@ 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()

# hubot phab paste <name of the paste> - creates a new paste
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()

Expand All @@ -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()
Expand All @@ -100,21 +100,21 @@ 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()

# hubot phab Txx + <some comment> - 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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 <email> - makes caller known with <email>
Expand Down Expand Up @@ -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} = <email>"
msg.finish()
Expand Down
2 changes: 1 addition & 1 deletion scripts/phabs_hear.coffee
Expand Up @@ -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) ->
Expand Down

0 comments on commit 9c911d0

Please sign in to comment.