/
sub_workflow_move.go
63 lines (52 loc) · 1.41 KB
/
sub_workflow_move.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"encoding/json"
"github.com/adriacidre/fsm"
"github.com/nats-io/nats"
"github.com/supu-io/messages"
)
// WFMove holds all related logic for event workflow.move
type WFMove struct{}
// Subscribe to workflow.move in order to move an issue to its
// new status and execute proper hooks
func (w *WFMove) Subscribe(nc *nats.Conn) {
e := ErrorMessage{}
nc.Subscribe("workflow.move", func(m *nats.Msg) {
input, err := w.mapInput(string(m.Data))
if err != nil {
e.Error = err.Error()
nc.Publish(m.Reply, e.toJSON())
return
}
i, err := w.executeTransition(input)
if err != nil {
e.Error = err.Error()
nc.Publish(m.Reply, e.toJSON())
return
}
nc.Publish(m.Reply, ToJSON(i))
})
}
// Executes a transition for a given input
func (w *WFMove) executeTransition(input *messages.UpdateIssue) (*messages.UpdateIssue, error) {
e := fsm.State(input.Status)
i := Issue{
ID: input.Issue.ID,
State: fsm.State(input.Issue.Status),
}
err := i.Apply(input.Status).Transition(e)
if err != nil {
return nil, err
}
Hook(input.Issue, input.Config, string(i.State))
input.Issue.Status = string(i.State)
return input, nil
}
// Maps the json input to an InputMove structure
func (w *WFMove) mapInput(body string) (*messages.UpdateIssue, error) {
input := messages.UpdateIssue{}
if err := json.Unmarshal([]byte(body), &input); err != nil {
return nil, err
}
return &input, nil
}