-
Notifications
You must be signed in to change notification settings - Fork 3
/
jiraclient.go
158 lines (144 loc) · 3.4 KB
/
jiraclient.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
157
158
package jkl
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"strings"
"time"
)
var j, _ = cookiejar.New(nil)
var httpClient *JiraClient
type JiraClient struct {
*http.Client
jiraRoot string
}
func init() {
x := false
Verbose = &x
}
func NewJiraClient(jiraRoot string) *JiraClient {
jc := &JiraClient{
&http.Client{
Jar: j,
},
jiraRoot,
}
if jc.jiraRoot == "" {
jc.jiraRoot = os.Getenv("JIRA_ROOT")
}
if cookiefile := os.Getenv("JIRA_COOKIEFILE"); cookiefile != "" {
makeNewFile := false
f, err := os.Open(cookiefile)
server := jc.jiraRoot + "rest/gadget/1.0/login"
u, _ := url.Parse(server)
if err != nil {
makeNewFile = true
} else {
if stat, err := f.Stat(); err == nil {
if time.Now().Sub(stat.ModTime()).Minutes() > 60 {
makeNewFile = true
} else {
var cookies []*http.Cookie
dec := json.NewDecoder(f)
dec.Decode(&cookies)
u, _ = url.Parse(jc.jiraRoot)
jc.Jar.SetCookies(u, cookies)
}
}
f.Close()
}
if makeNewFile {
f, err = os.Create(cookiefile)
if err != nil {
panic(err)
}
http.DefaultClient.Jar = j
form := url.Values{}
form.Add("os_username", os.Getenv("JIRA_USER"))
form.Add("os_password", os.Getenv("JIRA_PASSWORD"))
req, _ := http.NewRequest("POST", server, strings.NewReader(form.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode >= 400 {
fmt.Println(resp.Header)
fmt.Println(resp.Status)
fmt.Println(err)
}
b := bytes.NewBuffer(nil)
enc := json.NewEncoder(b)
enc.Encode(j.Cookies(u))
io.Copy(f, b)
f.Close()
}
}
if *Verbose {
fmt.Println("Jira root:", jc.jiraRoot)
}
return jc
}
func (j *JiraClient) DoLess(req *http.Request) (*http.Response, error) {
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json, text/plain, text/html")
return j.DoEvenLess(req)
}
func (j *JiraClient) DoEvenLess(req *http.Request) (*http.Response, error) {
var err error
if os.Getenv("JIRA_COOKIEFILE") == "" {
req.SetBasicAuth(os.Getenv("JIRA_USER"), os.Getenv("JIRA_PASSWORD"))
}
resp, err := j.Client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
fmt.Println("Status code:", resp.StatusCode)
if *Verbose {
fmt.Println("Headers:")
fmt.Println(resp.Header)
}
fmt.Println("Response:")
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
fmt.Println(string(b))
return nil, errors.New("Some http error happened.")
}
return resp, nil
}
func (j *JiraClient) Do(req *http.Request) (*http.Response, error) {
var err error
req.URL, err = url.Parse(j.jiraRoot + "rest/" + req.URL.RequestURI())
if err != nil {
return nil, err
}
return j.DoLess(req)
}
func (j *JiraClient) Put(path string, payload io.Reader) (*http.Response, error) {
req, err := http.NewRequest("PUT", path, payload)
if err != nil {
return nil, err
}
return j.Do(req)
}
func (j *JiraClient) Post(path string, payload io.Reader) (*http.Response, error) {
req, err := http.NewRequest("POST", path, payload)
if err != nil {
return nil, err
}
return j.Do(req)
}
func (j *JiraClient) Get(path string) (*http.Response, error) {
req, err := http.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
return j.Do(req)
}