Skip to content

Commit bddadc8

Browse files
committed
Added support for Drone CI service API calls
1 parent 0436cb2 commit bddadc8

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

services.go

+113
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package gitlab
1919
import (
2020
"fmt"
2121
"net/url"
22+
"time"
2223
)
2324

2425
// ServicesService handles communication with the services related methods of
@@ -29,6 +30,19 @@ type ServicesService struct {
2930
client *Client
3031
}
3132

33+
type Service struct {
34+
ID *int `json:"id"`
35+
Title *string `json:"title"`
36+
CreatedAt *time.Time `json:"created_at"`
37+
UpdatedAt *time.Time `json:"created_at"`
38+
Active *bool `json:"active"`
39+
PushEvents *bool `json:"push_events"`
40+
IssuesEvents *bool `json:"issues_events"`
41+
MergeRequestsEvents *bool `json:"merge_requests_events"`
42+
TagPushEvents *bool `json:"tag_push_events"`
43+
NoteEvents *bool `json:"note_events"`
44+
}
45+
3246
// SetGitLabCIServiceOptions represents the available SetGitLabCIService()
3347
// options.
3448
//
@@ -148,3 +162,102 @@ func (s *ServicesService) DeleteHipChatService(pid interface{}) (*Response, erro
148162

149163
return resp, err
150164
}
165+
166+
// SetDroneCIServiceOptions represents the available SetDroneCIService()
167+
// options.
168+
//
169+
// GitLab API docs:
170+
// http://doc.gitlab.com/ce/api/services.html#createedit-drone-ci-service
171+
type SetDroneCIServiceOptions struct {
172+
Token string `url:"token"`
173+
DroneURL string `url:"drone_url"`
174+
EnableSSLVerification string `url:"enable_ssl_verification,omitempty"`
175+
}
176+
177+
// SetDroneCIService sets Drone CI service for a project.
178+
//
179+
// GitLab API docs:
180+
// http://doc.gitlab.com/ce/api/services.html#createedit-drone-ci-service
181+
func (s *ServicesService) SetDroneCIService(
182+
pid interface{},
183+
opt *SetDroneCIServiceOptions) (*Response, error) {
184+
project, err := parseID(pid)
185+
if err != nil {
186+
return nil, err
187+
}
188+
u := fmt.Sprintf("projects/%s/services/drone-ci", url.QueryEscape(project))
189+
190+
req, err := s.client.NewRequest("PUT", u, opt)
191+
if err != nil {
192+
return nil, err
193+
}
194+
195+
resp, err := s.client.Do(req, nil)
196+
if err != nil {
197+
return resp, err
198+
}
199+
200+
return resp, err
201+
}
202+
203+
// DeleteDroneCIService deletes Drone CI service settings for a project.
204+
//
205+
// GitLab API docs:
206+
// http://doc.gitlab.com/ce/api/services.html#delete-drone-ci-service
207+
func (s *ServicesService) DeleteDroneCIService(pid interface{}) (*Response, error) {
208+
project, err := parseID(pid)
209+
if err != nil {
210+
return nil, err
211+
}
212+
u := fmt.Sprintf("projects/%s/services/drone-ci", url.QueryEscape(project))
213+
214+
req, err := s.client.NewRequest("DELETE", u, nil)
215+
if err != nil {
216+
return nil, err
217+
}
218+
219+
resp, err := s.client.Do(req, nil)
220+
if err != nil {
221+
return resp, err
222+
}
223+
224+
return resp, err
225+
}
226+
227+
// DroneCIServiceProperties represents Drone CI specific properties.
228+
type DroneCIServiceProperties struct {
229+
Token *string `url:"token"`
230+
DroneURL *string `url:"drone_url"`
231+
EnableSSLVerification *string `url:"enable_ssl_verification"`
232+
}
233+
234+
// DroneCIService represents Drone CI service settings.
235+
type DroneCIService struct {
236+
Service
237+
Properties *DroneCIServiceProperties `json:"properties"`
238+
}
239+
240+
// GetDroneCIService gets Drone CI service settings for a project.
241+
//
242+
// GitLab API docs:
243+
// http://doc.gitlab.com/ce/api/services.html#get-drone-ci-service-settings
244+
func (s *ServicesService) GetDroneCIService(pid interface{}) (*DroneCIService, *Response, error) {
245+
project, err := parseID(pid)
246+
if err != nil {
247+
return nil, nil, err
248+
}
249+
u := fmt.Sprintf("projects/%s/services/drone-ci", url.QueryEscape(project))
250+
251+
req, err := s.client.NewRequest("GET", u, nil)
252+
if err != nil {
253+
return nil, nil, err
254+
}
255+
256+
opt := new(DroneCIService)
257+
resp, err := s.client.Do(req, opt)
258+
if err != nil {
259+
return nil, resp, err
260+
}
261+
262+
return opt, resp, err
263+
}

services_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"reflect"
7+
"testing"
8+
)
9+
10+
func TestSetDroneCIService(t *testing.T) {
11+
mux, server, client := setup()
12+
defer teardown(server)
13+
14+
mux.HandleFunc("/projects/1/services/drone-ci", func(w http.ResponseWriter, r *http.Request) {
15+
testMethod(t, r, "PUT")
16+
testFormValues(t, r, values{
17+
"token": "t",
18+
"drone_url": "u",
19+
"enable_ssl_verification": "true",
20+
})
21+
})
22+
23+
opt := &SetDroneCIServiceOptions{"t", "u", "true"}
24+
_, err := client.Services.SetDroneCIService(1, opt)
25+
26+
if err != nil {
27+
t.Fatalf("Services.SetDroneCIService returns an error: %v", err)
28+
}
29+
}
30+
31+
func TestDeleteDroneCIService(t *testing.T) {
32+
mux, server, client := setup()
33+
defer teardown(server)
34+
35+
mux.HandleFunc("/projects/1/services/drone-ci", func(w http.ResponseWriter, r *http.Request) {
36+
testMethod(t, r, "DELETE")
37+
})
38+
39+
_, err := client.Services.DeleteDroneCIService(1)
40+
41+
if err != nil {
42+
t.Fatalf("Services.DeleteDroneCIService returns an error: %v", err)
43+
}
44+
}
45+
46+
func TestGetDroneCIService(t *testing.T) {
47+
mux, server, client := setup()
48+
defer teardown(server)
49+
50+
mux.HandleFunc("/projects/1/services/drone-ci", func(w http.ResponseWriter, r *http.Request) {
51+
testMethod(t, r, "GET")
52+
fmt.Fprint(w, `{"id":1}`)
53+
})
54+
want := &DroneCIService{Service: Service{ID: Int(1)}}
55+
56+
service, _, err := client.Services.GetDroneCIService(1)
57+
58+
if err != nil {
59+
t.Fatalf("Services.GetDroneCIService returns an error: %v", err)
60+
}
61+
62+
if !reflect.DeepEqual(want, service) {
63+
t.Errorf("Services.GetDroneCIService returned %+v, want %+v", service, want)
64+
}
65+
}

0 commit comments

Comments
 (0)