1 | _ = require "underscore"
|
2 |
|
3 | Assign = require "./assign"
|
4 | Config = require "../config"
|
5 | Ticket = require "./ticket"
|
6 | Transition = require "./transition"
|
7 | User = require "./user"
|
8 | Utils = require "../utils"
|
9 |
|
10 | class 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, context, fields, emit=yes) ->
|
17 | toState = null
|
18 | assignee = null
|
19 | room = Utils.JiraBot.adapter.getRoomName context
|
20 |
|
21 | if context.message.user.profile?.email?
|
22 | user = User.withEmail(context.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 #{context.message.user.name} in ##{room} on #{context.robot.adapterName}
|
42 | #{Utils.JiraBot.adapter.getPermalink context}
|
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, context, no, no if toState
|
52 | Assign.forTicketToPerson ticket, assignee, context, 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", context,
|
63 | ticket: ticket
|
64 | transition: transition
|
65 | assignee: assignee
|
66 | unless emit and roomProject is project
|
67 | Utils.robot.emit "JiraTicketCreatedElsewhere", context,
|
68 | ticket: ticket
|
69 | transition: transition
|
70 | assignee: assignee
|
71 | ticket
|
72 | .catch (error) ->
|
73 | context.robot.logger.error error.stack
|
74 | .catch (error) ->
|
75 | Utils.robot.logger.error error.stack
|
76 | Utils.robot.emit "JiraTicketCreationFailed", error, context if emit
|
77 | Promise.reject error
|
78 |
|
79 | @fromKey: (key) ->
|
80 | key = key.trim().toUpperCase()
|
81 | params =
|
82 | expand: Config.jira.expand
|
83 | fields: Config.jira.fields
|
84 |
|
85 | Utils.fetch("#{Config.jira.url}/rest/api/2/issue/#{key}#{Utils.buildQueryString params}")
|
86 | .then (json) ->
|
87 | Promise.all [
|
88 | json,
|
89 | Utils.fetch "#{Config.jira.url}/rest/api/2/issue/#{json.key}/watchers"
|
90 | ]
|
91 | .then (jsons) ->
|
92 | new Ticket _(jsons[0]).extend _(jsons[1]).pick("watchers")
|
93 |
|
94 | @subtaskFromKeyWith: (key, summary, context, emit=yes) ->
|
95 | Create.fromKey(key)
|
96 | .then (parent) ->
|
97 | Create.with parent.fields.project.key, "Sub-task", summary, context,
|
98 | parent: key: parent.key
|
99 | labels: parent.fields.labels or []
|
100 | description: "Sub-task of #{key}\n\n"
|
101 | , emit
|
102 |
|
103 | module.exports = Create
|