UNPKG

2.1 kBtext/coffeescriptView Raw
1Config = require "../config"
2User = require "./user"
3Utils = require "../utils"
4
5class Assign
6
7 @forTicketToPerson: (ticket, person, msg, includeAttachment=no, emit=yes) ->
8 person = if person is "me" then msg.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", msg.message.user.profile.email
25 Utils.cache.put "#{ticket.key}:Assigned", msg.message.user.profile.email
26 msg.robot.emit "JiraTicketAssigned", ticket, chatUser, msg.message.room, 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 msg.robot.emit "JiraTicketAssignmentFailed", error, msg.message.room if emit
31 Promise.reject error
32 else
33 error = "Cannot find chat user `#{person}`"
34 msg.robot.emit "JiraTicketAssignmentFailed", error, msg.message.room if emit
35 Promise.reject error
36
37 @forTicketKeyToPerson: (key, person, msg, includeAttachment=no, emit=yes) ->
38 Create = require "./create"
39 Create.fromKey(key)
40 .then (ticket) ->
41 Assign.forTicketToPerson ticket, person, msg, includeAttachment, emit
42
43 @forTicketKeyToUnassigned: (key, msg, 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 msg.robot.emit "JiraTicketUnassigned", ticket, msg.message.room, no if emit
55
56module.exports = Assign