/
manager.go
156 lines (127 loc) · 3.57 KB
/
manager.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"github.com/fatih/color"
)
// Manager is a type to encapsulate issue actions
type Manager struct {
URL string
}
func (m *Manager) list(status string, org string, repo string) {
url := m.URL + "/issues/search?status=" + status + "&org=" + org
if repo != "" {
url = url + "&repo=" + repo
}
resp, err := http.Get(url)
if err != nil {
color.Red("Couldn't connect to the server" + m.URL)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err := printIssuesList(body); err != nil {
m.printError(body)
}
}
func (m *Manager) details(issue string) {
req, err := http.NewRequest("GET", m.URL+"/issues/"+issue, nil)
req.Header.Add("X-AUTH-TOKEN", "token")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
color.Red("Couldn't connect to the server")
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
printIssueDetails(body)
}
func (m *Manager) move(issue string, status string) {
if status == "" {
m.printListOfPossibleStatuses()
} else {
var jsonStr = []byte(`{"status":"` + status + `"}`)
req, err := http.NewRequest("PUT", m.URL+"/issues/"+issue, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("X-AUTH-TOKEN", "token")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
color.Red("Couldn't connect to the server")
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
color.Green(string(body))
}
}
func (m *Manager) setup(org string, repo string) {
url := m.URL + "/setup?org=" + org + "&repo=" + repo
req, err := http.NewRequest("POST", url, nil)
req.Header.Add("X-AUTH-TOKEN", "token")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
color.Red("Couldn't connect to the server")
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
println(string(body))
}
func (m *Manager) create(title string, desc string, org string, repo string) {
url := m.URL + "/issues"
var jsonStr = []byte(`{"title":"` + title + `","body":"` + desc + `","org":"` + org + `","repo":"` + repo + `"}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Add("X-AUTH-TOKEN", "token")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
color.Red("Couldn't connect to the server")
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
printIssueDetails(body)
}
func (m *Manager) comment(issue string, text string) {
resp, err := http.PostForm(m.URL+"/issues/"+issue+"/comment",
url.Values{"body": {text}})
if err != nil {
color.Red("Couldn't connect to the server")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
color.Green(string(body))
}
func (m *Manager) printError(body []byte) {
e := Error{}
err := json.Unmarshal(body, &e)
if err != nil {
color.Red(err.Error())
}
color.Red(e.Message)
}
func (m *Manager) printListOfPossibleStatuses() {
req, err := http.NewRequest("GET", m.URL+"/statuses", nil)
req.Header.Add("X-AUTH-TOKEN", "token")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
color.Red("Couldn't connect to the server")
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
statuses := []string{}
json.Unmarshal(body, &statuses)
color.Yellow("You should provide a valid status to move your issue")
color.Yellow("valid statuses for this project are:")
for _, val := range statuses {
color.Yellow(" - " + val)
}
}