1 | Config = require "../config"
|
2 | User = require "./user"
|
3 | Utils = require "../utils"
|
4 |
|
5 | class Assign
|
6 |
|
7 | @forTicketToPerson: (ticket, person, context, includeAttachment=no, emit=yes) ->
|
8 | person = if person is "me" then context.message.user.name else person
|
9 | chatUser = Utils.lookupChatUser person
|
10 |
|
11 | if chatUser?.profile?.email?
|
12 | User.withEmail(chatUser.profile.email)
|
13 | .then (user) ->
|
14 | Utils.fetch "#{Config.jira.url}/rest/api/2/issue/#{ticket.key}",
|
15 | method: "PUT"
|
16 | body: JSON.stringify
|
17 | fields:
|
18 | assignee:
|
19 | name: user.name
|
20 | .then ->
|
21 | Create = require "./create"
|
22 | Create.fromKey ticket.key
|
23 | .then (ticket) ->
|
24 | Utils.robot.logger.debug "#{ticket.key}:Assigned", context.message.user.profile.email
|
25 | Utils.cache.put "#{ticket.key}:Assigned", context.message.user.profile.email
|
26 | context.robot.emit "JiraTicketAssigned", ticket, chatUser, context, includeAttachment if emit
|
27 | text: "<@#{chatUser.id}> is now assigned to this ticket"
|
28 | fallback: "@#{chatUser.name} is now assigned to this ticket"
|
29 | .catch (error) ->
|
30 | context.robot.emit "JiraTicketAssignmentFailed", error, context if emit
|
31 | Promise.reject error
|
32 | else
|
33 | error = "Cannot find chat user `#{person}`"
|
34 | context.robot.emit "JiraTicketAssignmentFailed", error, context if emit
|
35 | Promise.reject error
|
36 |
|
37 | @forTicketKeyToPerson: (key, person, context, includeAttachment=no, emit=yes) ->
|
38 | Create = require "./create"
|
39 | Create.fromKey(key)
|
40 | .then (ticket) ->
|
41 | Assign.forTicketToPerson ticket, person, context, includeAttachment, emit
|
42 |
|
43 | @forTicketKeyToUnassigned: (key, context, includeAttachment=no, emit=yes) ->
|
44 | Utils.fetch "#{Config.jira.url}/rest/api/2/issue/#{key}",
|
45 | method: "PUT"
|
46 | body: JSON.stringify
|
47 | fields:
|
48 | assignee:
|
49 | name: null
|
50 | .then ->
|
51 | Create = require "./create"
|
52 | Create.fromKey(key)
|
53 | .then (ticket) ->
|
54 | context.robot.emit "JiraTicketUnassigned", ticket, context, no if emit
|
55 |
|
56 | module.exports = Assign
|