1 | _ = require "underscore"
|
2 |
|
3 | Config = require "../config"
|
4 | Create = require "./create"
|
5 | Utils = require "../utils"
|
6 |
|
7 | class Clone
|
8 | @fromTicketKeyToProject: (key, project, channel, context, emit=yes) ->
|
9 | original = null
|
10 | cloned = null
|
11 | Create.fromKey(key)
|
12 | .then (issue) ->
|
13 | original = issue
|
14 | Utils.fetch("#{Config.jira.url}/rest/api/2/project/#{project}")
|
15 | .then (json) ->
|
16 | issueTypes = _(json.issueTypes).reject (it) -> it.name is "Sub-task"
|
17 | issueType = Utils.fuzzyFind issue.fields.issuetype.name, issueTypes, ['name']
|
18 | issueType = issueTypes[0] unless issueType
|
19 | Promise.reject "Unable to find a suitable issue type in #{project} that matches with #{original.fields.issuetype.name}" unless issueType
|
20 |
|
21 | Create.fromJSON
|
22 | fields:
|
23 | project:
|
24 | key: project
|
25 | summary: original.fields.summary
|
26 | labels: original.fields.labels
|
27 | description: """
|
28 | #{original.fields.description}
|
29 |
|
30 | Cloned from #{key}
|
31 | """
|
32 | issuetype: name: issueType.name
|
33 | .then (json) ->
|
34 | Create.fromKey(json.key)
|
35 | .then (ticket) ->
|
36 | cloned = ticket
|
37 | room = Utils.JiraBot.adapter.getRoomName context
|
38 | Utils.fetch "#{Config.jira.url}/rest/api/2/issueLink",
|
39 | method: "POST"
|
40 | body: JSON.stringify
|
41 | type:
|
42 | name: "Cloners"
|
43 | inwardIssue:
|
44 | key: original.key
|
45 | outwardIssue:
|
46 | key: cloned.key
|
47 | comment:
|
48 | body: """
|
49 | Cloned by #{context.message.user.name} in ##{room} on #{context.robot.adapterName}
|
50 | #{Utils.JiraBot.adapter.getPermalink context}
|
51 | """
|
52 | .then ->
|
53 | if emit
|
54 | Utils.robot.emit "JiraTicketCreated", context, ticket: cloned
|
55 | Utils.robot.emit "JiraTicketCloned", cloned, channel, key, context if context.message.room isnt channel
|
56 | .catch (error) ->
|
57 | Utils.robot.emit "JiraTicketCloneFailed", error, key, context if emit
|
58 |
|
59 | module.exports = Clone
|