1 | _ = require "underscore"
|
2 |
|
3 | Config = require "../config"
|
4 | Utils = require "../utils"
|
5 |
|
6 | class Transition
|
7 |
|
8 | @forTicketToState: (ticket, toState, msg, includeAttachment=no, emit=yes) ->
|
9 | type = _(Config.maps.transitions).find (type) -> type.name is toState
|
10 | transition = ticket.transitions.find (state) -> state.to.name.toLowerCase() is type.jira.toLowerCase()
|
11 | if transition
|
12 | Utils.fetch "#{Config.jira.url}/rest/api/2/issue/#{ticket.key}/transitions",
|
13 | method: "POST"
|
14 | body: JSON.stringify
|
15 | transition:
|
16 | id: transition.id
|
17 | .then ->
|
18 | Create = require "./create"
|
19 | Create.fromKey ticket.key
|
20 | .then (ticket) ->
|
21 | msg.robot.emit "JiraTicketTransitioned", ticket, transition, msg.message.room, includeAttachment if emit
|
22 | text: "<@#{msg.message.user.id}> transitioned this ticket to #{transition.to.name}"
|
23 | fallback: "@#{msg.message.user.name} transitioned this ticket to #{transition.to.name}"
|
24 | .catch (error) ->
|
25 | msg.robot.emit "JiraTicketTransitionFailed", error, msg.message.room if emit
|
26 | Promise.reject error
|
27 | else
|
28 | error = "<#{Config.jira.url}/browse/#{ticket.key}|#{ticket.key}> is a `#{ticket.fields.issuetype.name}` and does not support transitioning from `#{ticket.fields.status.name}` to `#{type.jira}`"
|
29 | msg.robot.emit "JiraTicketTransitionFailed", error, msg.message.room if emit
|
30 | Promise.reject error
|
31 |
|
32 | @forTicketKeyToState: (key, toState, msg, includeAttachment=no, emit=yes) ->
|
33 | Create = require "./create"
|
34 | Create.fromKey(key)
|
35 | .then (ticket) ->
|
36 | Transition.forTicketToState ticket, toState, msg, includeAttachment, emit
|
37 |
|
38 | module.exports = Transition
|