forked from bndr/gojenkins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
folder.go
62 lines (55 loc) · 1.29 KB
/
folder.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
package gojenkins
import (
"errors"
"strconv"
"strings"
)
type Folder struct {
Raw *FolderResponse
Jenkins *Jenkins
Base string
}
type FolderResponse struct {
Actions []generalObj
Description string `json:"description"`
DisplayName string `json:"displayName"`
Name string `json:"name"`
URL string `json:"url"`
Jobs []InnerJob `json:"jobs"`
PrimaryView *ViewData `json:"primaryView"`
Views []ViewData `json:"views"`
}
func (f *Folder) parentBase() string {
return f.Base[:strings.LastIndex(f.Base, "/job")]
}
func (f *Folder) GetName() string {
return f.Raw.Name
}
func (f *Folder) Create(name string) (*Folder, error) {
mode := "com.cloudbees.hudson.plugins.folder.Folder"
data := map[string]string{
"name": name,
"mode": mode,
"Submit": "OK",
"json": makeJson(map[string]string{
"name": name,
"mode": mode,
}),
}
r, err := f.Jenkins.Requester.Post(f.parentBase()+"/createItem", nil, f.Raw, data)
if err != nil {
return nil, err
}
if r.StatusCode == 200 {
f.Poll()
return f, nil
}
return nil, errors.New(strconv.Itoa(r.StatusCode))
}
func (f *Folder) Poll() (int, error) {
response, err := f.Jenkins.Requester.GetJSON(f.Base, f.Raw, nil)
if err != nil {
return 0, err
}
return response.StatusCode, nil
}