1 | Config = require "../config"
|
2 | User = require "./user"
|
3 | Utils = require "../utils"
|
4 |
|
5 | class Watch
|
6 | @forTicketKeyForPerson: (key, person, context, includeAttachment=no, remove=no, emit=yes) ->
|
7 | person = if person is "me" or not person then context.message.user.name else person
|
8 |
|
9 | key = key.toUpperCase()
|
10 | chatUser = Utils.lookupChatUser person
|
11 |
|
12 | if chatUser?.profile?.email?
|
13 | User.withEmail(chatUser.profile.email)
|
14 | .then (user) ->
|
15 | Utils.fetch "#{Config.jira.url}/rest/api/2/issue/#{key}/watchers#{if remove then "?username=#{user.name}" else ""}",
|
16 | method: if remove then "DELETE" else "POST"
|
17 | body: JSON.stringify user.name unless remove
|
18 | .then ->
|
19 | Create = require "./create"
|
20 | Create.fromKey key
|
21 | .then (ticket) ->
|
22 | if remove
|
23 | context.robot.emit "JiraTicketUnwatched", ticket, chatUser, context, includeAttachment if emit
|
24 | else
|
25 | context.robot.emit "JiraTicketWatched", ticket, chatUser, context, includeAttachment if emit
|
26 | .catch (error) ->
|
27 | context.robot.emit "JiraTicketWatchFailed", error, context if emit
|
28 | Promise.reject error
|
29 | else
|
30 | error = "Cannot find chat user `#{person}`"
|
31 | context.robot.emit "JiraTicketWatchFailed", error, context if emit
|
32 | Promise.reject error
|
33 |
|
34 | @forTicketKeyRemovePerson: (key, person, context, includeAttachment=no, emit) ->
|
35 | Watch.forTicketKeyForPerson key, person, context, includeAttachment, yes, emit
|
36 |
|
37 | module.exports = Watch
|