UNPKG

3.45 kBtext/coffeescriptView Raw
1_ = require "underscore"
2
3Assign = require "./assign"
4Config = require "../config"
5Ticket = require "./ticket"
6Transition = require "./transition"
7User = require "./user"
8Utils = require "../utils"
9
10class Create
11 @fromJSON: (json) ->
12 Utils.fetch "#{Config.jira.url}/rest/api/2/issue",
13 method: "POST"
14 body: JSON.stringify json
15
16 @with: (project, type, summary, msg, fields, emit=yes) ->
17 toState = null
18 assignee = null
19 room = Utils.JiraBot.adapter.getRoomName msg
20
21 if msg.message.user.profile?.email?
22 user = User.withEmail(msg.message.user.profile.email)
23 else
24 user = Promise.resolve()
25
26 user.then (reporter) ->
27 { summary, description, toState, assignee, labels, priority } = Utils.extract.all summary
28 labels.unshift room
29
30 issue =
31 fields:
32 project: key: project
33 summary: summary
34 description: ""
35 issuetype: name: type
36
37 _(issue.fields).extend fields if fields
38 issue.fields.labels = _(issue.fields.labels).union labels
39 issue.fields.description += """
40 #{(if description then description + "\n\n" else "")}
41 Reported by #{msg.message.user.name} in ##{room} on #{msg.robot.adapterName}
42 #{Utils.JiraBot.adapter.getPermalink msg}
43 """
44 issue.fields.reporter = reporter if reporter
45 issue.fields.priority = id: priority.id if priority
46 Create.fromJSON issue
47 .then (json) ->
48 Create.fromKey(json.key)
49 .then (ticket) ->
50 Promise.all([
51 Transition.forTicketToState ticket, toState, msg, no, no if toState
52 Assign.forTicketToPerson ticket, assignee, msg, no, no if assignee
53 ticket
54 ])
55 .catch (error) ->
56 Utils.robot.logger.error error
57 [ undefined, text:error, ticket]
58 .then (results) ->
59 [ transition, assignee, ticket ] = results
60 roomProject = Config.maps.projects[room]
61 if emit
62 Utils.robot.emit "JiraTicketCreated",
63 ticket: ticket
64 room: msg.message.room
65 transition: transition
66 assignee: assignee
67 unless emit and roomProject is project
68 Utils.robot.emit "JiraTicketCreatedElsewhere",
69 ticket: ticket
70 room: msg.message.room
71 user: msg.message.user
72 transition: transition
73 assignee: assignee
74 ticket
75 .catch (error) ->
76 msg.robot.logger.error error.stack
77 .catch (error) ->
78 Utils.robot.logger.error error.stack
79 Utils.robot.emit "JiraTicketCreationFailed", error, msg.message.room if emit
80 Promise.reject error
81
82 @fromKey: (key) ->
83 key = key.trim().toUpperCase()
84 params =
85 expand: Config.jira.expand
86 fields: Config.jira.fields
87
88 Utils.fetch("#{Config.jira.url}/rest/api/2/issue/#{key}#{Utils.buildQueryString params}")
89 .then (json) ->
90 Promise.all [
91 json,
92 Utils.fetch "#{Config.jira.url}/rest/api/2/issue/#{json.key}/watchers"
93 ]
94 .then (jsons) ->
95 new Ticket _(jsons[0]).extend _(jsons[1]).pick("watchers")
96
97 @subtaskFromKeyWith: (key, summary, msg, emit=yes) ->
98 Create.fromKey(key)
99 .then (parent) ->
100 Create.with parent.fields.project.key, "Sub-task", summary, msg,
101 parent: key: parent.key
102 labels: parent.fields.labels or []
103 description: "Sub-task of #{key}\n\n"
104 , emit
105
106module.exports = Create