Skip to content

Commit

Permalink
add .phab paste and fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mose committed Jul 21, 2016
1 parent 0c551f7 commit e88e476
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Hubot-phabs Changelog
==========================

### 1.0.1 - 2016-07-21
- add a .phab paste some new paste
- fix error handling

### 1.0.0 - 2016-07-19
- add a .phab version command
- add a .phab count proj to count number of tasks in a paroject
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ Commands prefixed by `.phab` are here taking in account we use the `.` as hubot
newly created task.
NOTE: this call will record this Task id associated to you for 5 minutes

.phab paste <new paste title>
creates a new paste and provide the link to edit it

.phab Txxx
.phab
gives the status, priority and owner of the task xxx
Expand Down
43 changes: 31 additions & 12 deletions lib/phabricator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,21 @@ class Phabricator
json_body = JSON.parse(payload)
else
json_body = {
result: {
error_code: 'ENOTJSON',
error_info: 'api did not deliver json'
}
result: { },
error_code: 'ENOTJSON',
error_info: 'api did not deliver json'
}
else
json_body = {
result: {
error_code: res.statusCode,
error_info: "http error #{res.statusCode}"
}
result: { },
error_code: res.statusCode,
error_info: "http error #{res.statusCode}"
}
else
json_body = {
result: {
error_code: err.code,
error_info: err.message
}
result: { },
error_code: err.code,
error_info: err.message
}
cb json_body

Expand Down Expand Up @@ -171,6 +168,28 @@ class Phabricator
cb json_body


createPaste: (msg, title, cb) ->
if @ready(msg) is true
url = @url
apikey = @apikey
bot_phid = @bot_phid
phabGet = @phabGet
@withUser msg, msg.message.user, (userPhid) ->
query = {
'transactions[0][type]': 'title',
'transactions[0][value]': "#{title}",
'transactions[1][type]': 'text',
'transactions[1][value]': "(created by #{msg.message.user.name} on irc)",
'transactions[2][type]': 'subscribers.add',
'transactions[2][value][0]': "#{userPhid}",
'transactions[3][type]': 'subscribers.remove',
'transactions[3][value][0]': "#{bot_phid}",
'api.token': apikey,
}
phabGet msg, query, 'paste.edit', (json_body) ->
cb json_body


recordPhid: (msg, id) ->
msg.message.user.lastTask = moment().utc()
msg.message.user.lastPhid = id
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hubot-phabs",
"description": "A hubot plugin for communication with a Phabricator instance.",
"version": "1.0.0",
"version": "1.0.1",
"author": {
"name": "mose",
"email": "mose@gandi.net",
Expand Down
41 changes: 26 additions & 15 deletions scripts/phabs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ module.exports = (robot) ->
name = msg.match[2]
if column?
phab.createTask msg, column, name, (body) ->
if body['result']['error_info']?
msg.send "#{body['result']['error_info']}"
if body['error_info']?
msg.send "#{body['error_info']}"
else
id = body['result']['object']['id']
url = process.env.PHABRICATOR_URL + "/T#{id}"
Expand All @@ -77,6 +77,18 @@ module.exports = (robot) ->
else
msg.send 'Command incomplete.'

robot.respond /ph(?:ab)? paste (.*)$/, (msg) ->
title = msg.match[1]
phab.createPaste msg, 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
msg.send "Paste P#{id} created = edit on #{url}"


robot.respond (/ph(?:ab)? count ([-_a-zA-Z0-9]+)/), (msg) ->
column = phabColumns[msg.match[1]]
if column?
Expand All @@ -96,15 +108,15 @@ module.exports = (robot) ->
msg.finish()
return
phab.taskInfo msg, id, (body) ->
if body['result']['error_info'] is undefined
if body['error_info'] is undefined
phab.withUserByPhid robot, body.result.ownerPHID, (owner) ->
status = body.result.status
priority = body.result.priority
phab.recordPhid msg, id
msg.send "T#{id} has status #{status}, " +
"priority #{priority}, owner #{owner.name}"
else
msg.send "oops T#{id} #{body['result']['error_info']}"
msg.send "oops T#{id} #{body['error_info']}"
msg.finish()


Expand All @@ -116,10 +128,10 @@ module.exports = (robot) ->
return
status = msg.match[2]
phab.updateStatus msg, id, status, (body) ->
if body['result']['error_info'] is undefined
if body['error_info'] is undefined
msg.send "Ok, T#{id} now has status #{body['result']['statusName']}."
else
msg.send "oops T#{id} #{body['result']['error_info']}"
msg.send "oops T#{id} #{body['error_info']}"
msg.finish()


Expand All @@ -133,10 +145,10 @@ module.exports = (robot) ->
return
priority = msg.match[2]
phab.updatePriority msg, id, priority, (body) ->
if body['result']['error_info'] is undefined
if body['error_info'] is undefined
msg.send "Ok, T#{id} now has priority #{body['result']['priority']}"
else
msg.send "oops T#{id} #{body['result']['error_info']}"
msg.send "oops T#{id} #{body['error_info']}"
msg.finish()


Expand Down Expand Up @@ -167,7 +179,6 @@ module.exports = (robot) ->
assignee.email_address = email
msg.send "Okay, I'll remember #{who} email as #{email}"


robot.respond new RegExp(
'ph(?:ab)?(?: assign)? (?:([^ ]+)(?: (?:to|on) (T)([0-9]+))?|(?:T([0-9]+) )?(?:to|on) ([^ ]+))$'
), (msg) ->
Expand All @@ -186,10 +197,10 @@ module.exports = (robot) ->
if assignee?
phab.withUser msg, assignee, (userPhid) ->
phab.assignTask msg, id, userPhid, (body) ->
if body['result']['error_info'] is undefined
if body['error_info'] is undefined
msg.send "Ok. T#{id} is now assigned to #{assignee.name}"
else
msg.send "#{body['result']['error_info']}"
msg.send "#{body['error_info']}"
else
msg.send "Sorry I don't know who is #{who}, can you .phab #{who} = <email>"
msg.finish()
Expand All @@ -204,8 +215,8 @@ module.exports = (robot) ->
switch type
when 'T'
phab.taskInfo msg, id, (body) ->
if body['result']['error_info']?
msg.send "oops T#{id} #{body['result']['error_info']}"
if body['error_info']?
msg.send "oops T#{id} #{body['error_info']}"
else
closed = ''
if body['result']['isClosed'] is true
Expand All @@ -219,8 +230,8 @@ module.exports = (robot) ->
phab.recordPhid msg, id
when 'F'
phab.fileInfo msg, id, (body) ->
if body['result']['error_info']?
msg.send "oops F#{id} #{body['result']['error_info']}"
if body['error_info']?
msg.send "oops F#{id} #{body['error_info']}"
else
size = humanFileSize(body['result']['byteSize'])
if url?
Expand Down
49 changes: 42 additions & 7 deletions test/phabs_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe 'hubot-phabs module', ->
do nock.disableNetConnect
nock(process.env.PHABRICATOR_URL)
.get('/api/maniphest.info')
.reply(200, { result: { error_info: 'No such Maniphest task exists.' } })
.reply(200, { error_info: 'No such Maniphest task exists.' })

afterEach ->
nock.cleanAll()
Expand Down Expand Up @@ -306,7 +306,7 @@ describe 'hubot-phabs module', ->
.get('/api/user.query')
.reply(200, { result: [ { phid: 'PHID-USER-42' } ] })
.get('/api/maniphest.edit')
.reply(200, { result: { error_info: 'Something went wrong' } })
.reply(200, { error_info: 'Something went wrong' })

afterEach ->
nock.cleanAll()
Expand Down Expand Up @@ -360,6 +360,41 @@ describe 'hubot-phabs module', ->
it 'replies with the object id', ->
expect(hubotResponse(1)).to.eql 'Task T24 created = http://example.com/T24'
expect(hubotResponse(3)).to.eql 'T24 has status open, priority Low, owner user_with_phid'
# ---------------------------------------------------------------------------------
context 'someone creates a new paste', ->
context 'something goes wrong', ->
beforeEach ->
do nock.disableNetConnect
nock(process.env.PHABRICATOR_URL)
.get('/api/paste.edit')
.reply(200, { error_info: 'Something went wrong' })

afterEach ->
nock.cleanAll()

context 'when something goes wrong on phabricator side', ->
hubot 'ph paste a new paste', 'user_with_phid'
it 'informs that something went wrong', ->
expect(hubotResponse()).to.eql 'Something went wrong'

context 'nothing goes wrong', ->
beforeEach ->
do nock.disableNetConnect
nock(process.env.PHABRICATOR_URL)
.get('/api/paste.edit')
.query({
title: 'a new paste'
})
.reply(200, { result: { object: { id: 24 } } })

afterEach ->
nock.cleanAll()

context 'ph paste a new paste', ->
hubot 'ph paste a new paste', 'user_with_phid'
it 'gives the link to fill up the paste Paste', ->
expect(hubotResponse()).to.eql 'Paste P24 created = ' +
'edit on http://example.com/paste/edit/24'

# ---------------------------------------------------------------------------------
context 'user asks to count tasks in a project or column', ->
Expand Down Expand Up @@ -410,7 +445,7 @@ describe 'hubot-phabs module', ->
do nock.disableNetConnect
nock(process.env.PHABRICATOR_URL)
.get('/api/maniphest.update')
.reply(200, { result: { error_info: 'No such Maniphest task exists.' } })
.reply(200, { error_info: 'No such Maniphest task exists.' })

afterEach ->
nock.cleanAll()
Expand Down Expand Up @@ -602,7 +637,7 @@ describe 'hubot-phabs module', ->
do nock.disableNetConnect
nock(process.env.PHABRICATOR_URL)
.get('/api/maniphest.update')
.reply(200, { result: { error_info: 'No such Maniphest task exists.' } })
.reply(200, { error_info: 'No such Maniphest task exists.' })

afterEach ->
nock.cleanAll()
Expand Down Expand Up @@ -730,7 +765,7 @@ describe 'hubot-phabs module', ->
do nock.disableNetConnect
nock(process.env.PHABRICATOR_URL)
.get('/api/maniphest.edit')
.reply(200, { result: { error_info: 'No such Maniphest task exists.' } })
.reply(200, { error_info: 'No such Maniphest task exists.' })

afterEach ->
nock.cleanAll()
Expand Down Expand Up @@ -777,7 +812,7 @@ describe 'hubot-phabs module', ->
do nock.disableNetConnect
nock(process.env.PHABRICATOR_URL)
.get('/api/maniphest.info')
.reply(200, { result: { error_info: 'No such Maniphest task exists.' } })
.reply(200, { error_info: 'No such Maniphest task exists.' })

afterEach ->
nock.cleanAll()
Expand Down Expand Up @@ -845,7 +880,7 @@ describe 'hubot-phabs module', ->
do nock.disableNetConnect
nock(process.env.PHABRICATOR_URL)
.get('/api/file.info')
.reply(200, { result: { error_info: 'No such file exists.' } })
.reply(200, { error_info: 'No such file exists.' })

afterEach ->
nock.cleanAll()
Expand Down

0 comments on commit e88e476

Please sign in to comment.