/
hook.go
94 lines (76 loc) · 2.11 KB
/
hook.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
package sdk
import (
"encoding/json"
"fmt"
"net/http"
)
// Hook used to link a git repository to a given pipeline
type Hook struct {
ID int64 `json:"id"`
UID string `json:"uid"`
Pipeline Pipeline `json:"pipeline"`
ApplicationID int64 `json:"application_id"`
Kind string `json:"kind"`
Host string `json:"host"`
Project string `json:"project"`
Repository string `json:"repository"`
Enabled bool `json:"enabled"`
Link string `json:"link"`
}
// AddHook creates a new hook between a pipeline and a repository
func AddHook(a *Application, p *Pipeline, host string, project string, repository string) (*Hook, error) {
h := Hook{
Pipeline: *p,
ApplicationID: a.ID,
Kind: "stash",
Host: host,
Project: project,
Repository: repository,
}
data, err := json.Marshal(h)
if err != nil {
return nil, err
}
uri := fmt.Sprintf("/project/%s/application/%s/pipeline/%s/hook", p.ProjectKey, a.Name, p.Name)
data, code, err := Request("POST", uri, data)
if err != nil {
return nil, err
}
if code != http.StatusOK {
return nil, fmt.Errorf("HTTP %d", code)
}
err = json.Unmarshal(data, &h)
if err != nil {
return nil, err
}
return &h, nil
}
// GetHooks lists all hooks related to a pipeline
func GetHooks(project, application, pipeline string) ([]Hook, error) {
var hooks []Hook
uri := fmt.Sprintf("/project/%s/application/%s/pipeline/%s/hook", project, application, pipeline)
data, code, err := Request("GET", uri, nil)
if err != nil {
return nil, err
}
if code > 300 {
return nil, fmt.Errorf("HTTP %d", code)
}
err = json.Unmarshal(data, &hooks)
if err != nil {
return nil, err
}
return hooks, nil
}
// DeleteHook remove a hook previously created
func DeleteHook(project, application, pipeline string, id int64) error {
uri := fmt.Sprintf("/project/%s/application/%s/pipeline/%s/hook/%d", project, application, pipeline, id)
_, code, err := Request("DELETE", uri, nil)
if err != nil {
return err
}
if code >= 300 {
return fmt.Errorf("HTTP %d", code)
}
return nil
}