UNPKG

6.24 kBtext/coffeescriptView Raw
1# Description:
2# enable communication with Phabricator via Conduit api
3#
4# Dependencies:
5#
6# Configuration:
7# PHABRICATOR_URL
8# PHABRICATOR_API_KEY
9#
10# Commands:
11# hubot phad projects
12# hubot phad delete <project>
13# hubot phad info <project>
14# hubot phad refresh <project>
15# hubot phad alias <project> as <alias>
16# hubot phad forget <alias>
17# hubot phad feed <project> to <room>
18# hubot phad remove <project> from <room>
19#
20# Author:
21# mose
22
23Phabricator = require '../lib/phabricator'
24moment = require 'moment'
25path = require 'path'
26
27module.exports = (robot) ->
28
29 robot.phab ?= new Phabricator robot, process.env
30 phab = robot.phab
31 data = robot.brain.data.phabricator
32
33 # hubot phad projects
34 robot.respond /phad (?:projects|list) *$/, (msg) ->
35 projects = Object.keys(data.projects).filter (i) ->
36 i isnt '*'
37 if projects.length > 0
38 msg.send "Known Projects: #{projects.join(', ')}"
39 else
40 msg.send 'There is no project.'
41
42 # hubot phad delete <project>
43 robot.respond /phad del(?:ete)? (.+) *$/, (msg) ->
44 project = msg.match[1]
45 phab.getPermission(msg.envelope.user, 'phadmin')
46 .then ->
47 if data.projects[project]?
48 delete data.projects[project]
49 for alias, proj of data.aliases
50 delete data.aliases[alias] if proj is project
51 msg.send "#{project} erased from memory."
52 else
53 msg.send "#{project} not found in memory."
54 .catch (e) ->
55 msg.send e
56
57 # hubot phad info|refresh <project>
58 robot.respond /phad (info|show|refresh) (.+) *$/, (msg) ->
59 refresh = (msg.match[1] is 'refresh')
60 project = msg.match[2]
61 phab.getProject(project, refresh)
62 .then (proj) ->
63 response = "'#{project}' is '#{proj.data.name}'"
64 if proj.aliases? and proj.aliases.length > 0
65 response += " (aka #{proj.aliases.join(', ')})"
66 else
67 response += ', with no alias'
68 if proj.data.feeds? and proj.data.feeds.length > 0
69 response += ", announced on #{proj.data.feeds.join(', ')}"
70 else
71 response += ', with no feed'
72 if proj.data.columns? and Object.keys(proj.data.columns).length > 0
73 response += ", columns #{Object.keys(proj.data.columns).join(', ')}"
74 else
75 response += ', and no columns'
76 if proj.data.parent?
77 response += " (child of #{proj.data.parent})"
78 response += '.'
79 msg.send response
80 .catch (e) ->
81 msg.send e
82
83 # hubot phad alias <project> as <alias>
84 robot.respond /phad alias (.+) as (.+)$/, (msg) ->
85 project = msg.match[1]
86 alias = msg.match[2]
87 phab.getPermission(msg.envelope.user, 'phadmin')
88 .then ->
89 phab.getProject(project)
90 .then (proj) ->
91 if data.aliases[alias]?
92 msg.send "The alias '#{alias}' already exists for project '#{data.aliases[alias]}'."
93 else
94 data.aliases[alias] = proj.data.name
95 msg.send "Ok, '#{proj.data.name}' will be known as '#{alias}'."
96 .catch (e) ->
97 msg.send e
98
99 # hubot phad forget <alias>
100 robot.respond /phad forget (.+)$/, (msg) ->
101 alias = msg.match[1]
102 phab.getPermission(msg.envelope.user, 'phadmin')
103 .then ->
104 if data.aliases[alias]
105 delete data.aliases[alias]
106 msg.send "Ok, the alias '#{alias}' is forgotten."
107 else
108 msg.send "Sorry, I don't know the alias '#{alias}'."
109 .catch (e) ->
110 msg.send e
111
112 # hubot phad feed <project> to <room>
113 robot.respond /phad feeds? (.+) to (.+)$/, (msg) ->
114 project = msg.match[1]
115 room = msg.match[2]
116 phab.getPermission(msg.envelope.user, 'phadmin')
117 .then ->
118 phab.getProject(project)
119 .then (proj) ->
120 proj.data.feeds ?= [ ]
121 if room in proj.data.feeds
122 msg.send "The feed from '#{proj.data.name}' to '#{room}' already exist."
123 else
124 data.projects[proj.data.name].feeds ?= [ ]
125 data.projects[proj.data.name].feeds.push room
126 msg.send "Ok, '#{proj.data.name}' is now feeding '#{room}'."
127 .catch (e) ->
128 msg.send e
129
130 # hubot phad feedall to <room>
131 robot.respond /phad feedall to (.+)$/, (msg) ->
132 room = msg.match[1]
133 phab.getPermission(msg.envelope.user, 'phadmin')
134 .then ->
135 phab.getProject('*')
136 .then (proj) ->
137 proj.data.feeds ?= [ ]
138 if room in proj.data.feeds
139 msg.send "The catchall feed to '#{room}' already exist."
140 else
141 data.projects['*'].feeds ?= [ ]
142 data.projects['*'].feeds.push room
143 msg.send "Ok, all feeds will be announced on '#{room}'."
144
145 # hubot phad remove <project> from <room>
146 robot.respond /phad remove (.+) from (.+)$/, (msg) ->
147 project = msg.match[1]
148 room = msg.match[2]
149 phab.getPermission(msg.envelope.user, 'phadmin')
150 .then ->
151 phab.getProject(project)
152 .then (proj) ->
153 proj.data.feeds ?= [ ]
154 if room in proj.data.feeds
155 idx = data.projects[proj.data.name].feeds.indexOf room
156 data.projects[proj.data.name].feeds.splice(idx, 1)
157 msg.send "Ok, The feed from '#{proj.data.name}' to '#{room}' was removed."
158 else
159 msg.send "Sorry, '#{proj.data.name}' is not feeding '#{room}'."
160 .catch (e) ->
161 msg.send e
162
163 # hubot phad removeall from <room>
164 robot.respond /phad removeall from (.+)$/, (msg) ->
165 room = msg.match[1]
166 phab.getPermission(msg.envelope.user, 'phadmin')
167 .then ->
168 phab.getProject('*')
169 .then (proj) ->
170 proj.data.feeds ?= [ ]
171 if room in proj.data.feeds
172 idx = data.projects['*'].feeds.indexOf room
173 data.projects['*'].feeds.splice(idx, 1)
174 msg.send "Ok, The catchall feed to '#{room}' was removed."
175 else
176 msg.send "Sorry, the catchall feed for '#{room}' doesn't exist."
177
178 # hubot phad columns <project>
179 robot.respond /phad columns (.+)$/, (msg) ->
180 project = msg.match[1]
181 phab.getPermission(msg.envelope.user, 'phadmin')
182 .then ->
183 phab.getProject(project)
184 .then (data) ->
185 if data.data.columns? and Object.keys(data.data.columns).length > 0
186 msg.send "Columns for #{project}: #{Object.keys(data.data.columns).join(', ')}"
187 else
188 msg.send "The project #{project} has no columns."
189 .catch (e) ->
190 msg.send e.message