UNPKG

4.36 kBtext/coffeescriptView Raw
1# Description:
2# enable communication with Phabricator via Conduit api
3# listens to conversations and supplement with phabricator metadata
4# when an object is cited.
5#
6# Dependencies:
7#
8# Configuration:
9# PHABRICATOR_URL
10# PHABRICATOR_API_KEY
11#
12# Commands:
13# anything Txxx - complements with the title of the cited object
14#
15# Author:
16# mose
17
18Phabricator = require '../lib/phabricator'
19
20humanFileSize = (size) ->
21 i = Math.floor( Math.log(size) / Math.log(1024) )
22 return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]
23
24module.exports = (robot) ->
25
26 robot.phab ?= new Phabricator robot, process.env
27 phab = robot.phab
28
29 # anything Txxx - complements with the title of the cited object
30 if phab.enabledItemsRegex()?
31 robot.hear new RegExp(
32 "(?:.+|^)(?:(#{process.env.PHABRICATOR_URL})/?| |^)" +
33 phab.enabledItemsRegex()
34 ), (msg) ->
35 url = msg.match[1]
36 type = msg.match[2] ? msg.match[4]
37 id = msg.match[3]
38 unless phab.isBlacklisted "#{type}#{id}"
39 switch
40
41 when 'T' is type
42 phab.taskInfo(id)
43 .then (body) ->
44 closed = ''
45 if body.result.isClosed is true
46 closed = " (#{body.result.status})"
47 if url?
48 msg.send "#{type}#{id}#{closed} - #{body.result.title} " +
49 "(#{body['result']['priority']})"
50 else
51 msg.send "#{body.result.uri}#{closed} - #{body.result.title} " +
52 "(#{body.result.priority})"
53 phab.recordId msg.envelope.user, id
54 .catch (e) ->
55 msg.send "oops #{type}#{id} #{e}"
56
57 when 'F' is type
58 phab.fileInfo(id)
59 .then (body) ->
60 size = humanFileSize(body.result.byteSize)
61 if url?
62 msg.send "#{type}#{id} - #{body.result.name} " +
63 "(#{body.result.mimeType} #{size})"
64 else
65 msg.send "#{body.result.uri} - #{body.result.name} "+
66 "(#{body.result.mimeType} #{size})"
67 .catch (e) ->
68 msg.send "oops #{type}#{id} #{e}"
69
70 when 'P' is type
71 phab.pasteInfo(id)
72 .then (body) ->
73 if Object.keys(body.result).length < 1
74 msg.send "oops #{type}#{id} was not found."
75 else
76 lang = ''
77 key = Object.keys(body.result)[0]
78 if body['result'][key]['language'] isnt ''
79 lang = " (#{body.result[key].language})"
80 if url?
81 msg.send "#{type}#{id} - #{body.result[key].title}#{lang}"
82 else
83 msg.send "#{body.result[key].uri} - #{body.result[key].title}#{lang}"
84 .catch (e) ->
85 msg.send "oops #{type}#{id} #{e}"
86
87 when /^(M|B|Q|L|V)$/.test type
88 phab.genericInfo("#{type}#{id}")
89 .then (body) ->
90 if Object.keys(body.result).length < 1
91 msg.send "oops #{type}#{id} was not found."
92 else
93 v = body.result["#{type}#{id}"]
94 status = ''
95 if v.status is 'closed'
96 status = " (#{v.status})"
97 if url?
98 msg.send "#{v.fullName}#{status}"
99 else
100 fullname = v.fullName.replace("#{type}#{id}: ", '').replace("#{type}#{id} ", '')
101 msg.send "#{v.uri} - #{fullname}#{status}"
102 .catch (e) ->
103 msg.send "oops #{type}#{id} #{e}"
104
105 when /^r[A-Z]+[a-f0-9]{10,}$/.test type
106 phab.genericInfo(type)
107 .then (body) ->
108 if Object.keys(body.result).length < 1
109 msg.send "oops #{type} was not found."
110 else
111 v = body.result[type]
112 status = ''
113 if v['status'] is 'closed'
114 status = " (#{v.status})"
115 if url?
116 msg.send "#{v.fullName}#{status}"
117 else
118 fullname = v.fullName.replace "#{type}: ", ''
119 msg.send "#{v.uri} - #{fullname}#{status}"
120 .catch (e) ->
121 msg.send "oops #{type} #{e}"