This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
forked from yosida95/golang-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins.go
122 lines (95 loc) · 2.49 KB
/
jenkins.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
package gojenkins
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
type Auth struct {
Username string
ApiToken string
}
type Jenkins struct {
auth *Auth
baseUrl string
}
type Options struct {
useApiEndPoint bool
}
type Crumb struct {
Crumb string `json:"crumb"`
CrumbRequestField string `json:"crumbRequestField"`
}
var DefaultOptions = Options{}
func init() {
DefaultOptions.useApiEndPoint = true
}
func NewJenkins(auth *Auth, baseUrl string) *Jenkins {
return &Jenkins{
auth: auth,
baseUrl: baseUrl,
}
}
func (jenkins *Jenkins) buildUrl(path string, params url.Values, options Options) (requestUrl string) {
requestUrl = jenkins.baseUrl + path
// Not the most efficient, but it is easier to read
if options.useApiEndPoint {
requestUrl = requestUrl + "/api/json"
}
if params != nil {
queryString := params.Encode()
if queryString != "" {
requestUrl = requestUrl + "?" + queryString
}
}
return
}
func (jenkins *Jenkins) sendRequest(req *http.Request) (*http.Response, error) {
/* For Crumb on CSRF Protection, we need to obtain the crumb header needed in
* a post request.
*/
crb := Crumb{}
jenkins.get("/crumbIssuer", nil, &crb, Options{useApiEndPoint: true})
if crb.Crumb != "" && crb.CrumbRequestField != "" {
req.Header.Set(crb.CrumbRequestField, crb.Crumb)
}
req.SetBasicAuth(jenkins.auth.Username, jenkins.auth.ApiToken)
return http.DefaultClient.Do(req)
}
func (jenkins *Jenkins) parseResponse(resp *http.Response, body interface{}) (err error) {
defer resp.Body.Close()
if body == nil {
return
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return json.Unmarshal(data, body)
}
func (jenkins *Jenkins) get(path string, params url.Values, body interface{}, options Options) (err error) {
requestUrl := jenkins.buildUrl(path, params, options)
req, err := http.NewRequest("GET", requestUrl, nil)
// always required basic auth on both GET/POST
req.SetBasicAuth(jenkins.auth.Username, jenkins.auth.ApiToken)
if err != nil {
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
return jenkins.parseResponse(resp, body)
}
func (jenkins *Jenkins) post(path string, params url.Values, body interface{}, options Options) (err error) {
requestUrl := jenkins.buildUrl(path, params, options)
req, err := http.NewRequest("POST", requestUrl, nil)
if err != nil {
return
}
resp, err := jenkins.sendRequest(req)
if err != nil {
return
}
return jenkins.parseResponse(resp, body)
}