github.com/bndr/gojenkins@v1.1.0/folder.go (about) 1 // Copyright 2015 Vadim Kravcenko 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package gojenkins 16 17 import ( 18 "context" 19 "errors" 20 "strconv" 21 "strings" 22 ) 23 24 type Folder struct { 25 Raw *FolderResponse 26 Jenkins *Jenkins 27 Base string 28 } 29 30 type FolderResponse struct { 31 Actions []generalObj 32 Description string `json:"description"` 33 DisplayName string `json:"displayName"` 34 Name string `json:"name"` 35 URL string `json:"url"` 36 Jobs []InnerJob `json:"jobs"` 37 PrimaryView *ViewData `json:"primaryView"` 38 Views []ViewData `json:"views"` 39 } 40 41 func (f *Folder) parentBase() string { 42 return f.Base[:strings.LastIndex(f.Base, "/job")] 43 } 44 45 func (f *Folder) GetName() string { 46 return f.Raw.Name 47 } 48 49 func (f *Folder) Create(ctx context.Context, name string) (*Folder, error) { 50 mode := "com.cloudbees.hudson.plugins.folder.Folder" 51 data := map[string]string{ 52 "name": name, 53 "mode": mode, 54 "Submit": "OK", 55 "json": makeJson(map[string]string{ 56 "name": name, 57 "mode": mode, 58 }), 59 } 60 r, err := f.Jenkins.Requester.Post(ctx, f.parentBase()+"/createItem", nil, f.Raw, data) 61 if err != nil { 62 return nil, err 63 } 64 if r.StatusCode == 200 { 65 f.Poll(ctx) 66 return f, nil 67 } 68 return nil, errors.New(strconv.Itoa(r.StatusCode)) 69 } 70 71 func (f *Folder) Poll(ctx context.Context) (int, error) { 72 response, err := f.Jenkins.Requester.GetJSON(ctx, f.Base, f.Raw, nil) 73 if err != nil { 74 return 0, err 75 } 76 return response.StatusCode, nil 77 }