-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove.go
More file actions
108 lines (91 loc) · 1.96 KB
/
move.go
File metadata and controls
108 lines (91 loc) · 1.96 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"bytes"
"encoding/json"
"errors"
"log"
"net/http"
"github.com/supu-io/messages"
"github.com/supu-io/payload"
)
// Move ...
func move(m *messages.UpdateIssue) error {
t := getTransition(m)
if t == nil {
return errors.New("Invalid transition")
}
callHooks(m, t)
return nil
}
func getTransition(m *messages.UpdateIssue) *messages.Transition {
from := m.Issue.Status
to := m.Status
for _, t := range m.Workflow.Transitions {
if t.From == from && t.To == to {
return &t
}
}
return nil
}
func callHooks(m *messages.UpdateIssue, t *messages.Transition) {
p := getPayload(m)
for _, h := range t.Hooks {
hook(h, p)
}
}
func hook(hook messages.Hook, p string) {
req, err := http.NewRequest("POST", hook.URL, bytes.NewBuffer([]byte(p)))
// TODO: We will need at some point to support tokens
// req.Header.Add("X-AUTH-TOKEN", "token")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
_, err = client.Do(req)
if err != nil {
log.Println("Couldn't connect to the server")
}
}
func getPayload(m *messages.UpdateIssue) string {
from := m.Issue.Status
to := m.Status
c := m.Config
i := m.Issue
g := payload.Github{Token: &c.Github.Token}
conf := payload.Config{Github: &g}
t := payload.Transition{From: &from, To: &to}
s := getAllStatus(m.Workflow)
issue := payload.Issue{ID: &i.ID}
p := payload.Payload{
Config: &conf,
Transition: &t,
Status: &s,
Issue: &issue,
}
body, err := json.Marshal(p)
if err != nil {
log.Println(err)
return ""
}
return string(body)
}
func getAllStatus(w messages.Workflow) []string {
status := []string{}
for _, t := range w.Transitions {
addFrom := true
addTo := true
for _, s := range status {
if s == t.To {
addTo = false
}
if s == t.From {
addFrom = false
}
}
if addFrom == true {
status = append(status, string(t.From))
}
if addTo == true {
status = append(status, string(t.To))
}
}
return status
}