forked from DaoCloud/go-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.go
130 lines (111 loc) · 4.06 KB
/
webhook.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
123
124
125
126
127
128
129
130
package coding
import (
"fmt"
"strings"
"github.com/google/go-querystring/query"
)
type Hook struct {
DepotID int `json:"depot_id"`
HookURL string `json:"hook_url"`
Type int `json:"type"` // "type":1,
Token string `json:"token"` // "token":"",
Status int `json:"status"` // "status":0,
CreatedAt int64 `json:"created_at"` // "created_at":1431511538877,
UpdatedAt int64 `json:"updated_at"` // "updated_at":1431511538877,
SendType int `json:"send_type"` // "send_type":3,
ID int `json:"id"` // "id":174
}
type HookService struct {
client *Client
}
// webhook 列表 ( scope=project, project:depot )
// API: GET /api/user/{user_name}/project/{project_name}/git/hooks
func (s *HookService) ListHooks(owner, project string) ([]*Hook, error) {
req, err := s.client.NewRequest("GET",
fmt.Sprintf("/user/%s/project/%s/git/hooks", owner, project),
nil)
if err != nil {
return nil, err
}
out := struct {
Code int `json:"code"`
Hooks []*Hook `json:"data"`
}{}
_, err = s.client.Do(req, &out)
return out.Hooks, err
}
//获取 webhook ( scope=project, project:depot )
//API: GET /api/user/{user_name}/project/{project_name}/git/hook/{hook_id}
func (s *HookService) GetHook(owner, project string, hookID int) (*Hook, error) {
req, err := s.client.NewRequest("GET",
fmt.Sprintf("/user/%s/project/%s/git/hook/%d", owner, project, hookID),
nil)
if err != nil {
return nil, err
}
out := struct {
Code int `json:"code"`
Hook *Hook `json:"data"`
}{}
_, err = s.client.Do(req, &out)
return out.Hook, err
}
type HookForm struct {
HookURL string `url:"hook_url"` // hook_url string webhook 链接
Token string `url:"token"` // token string 自定义 webhook 秘钥
TypePush bool `url:"type_push"` // type_push boolean push代码 通知开关
TypeMR_PR bool `url:"type_mr_pr"` // type_mr_pr boolean MR/PR 通知开关
TypeTopic bool `url:"type_topic"` // type_topic boolean 发布讨论 通知开关
TypeMember bool `url:"type_member"` // type_member boolean 成员变动 通知开关
TypeComment bool `url:"type_comment"` // type_comment boolean 发表评论 通知开关
TypeDocument bool `url:"type_document"` // type_document boolean 文档 通知开关
TypeWatch bool `url:"type_watch"` // type_watch boolean 项目被关注 通知开关
TypeStar bool `url:"type_star"` // type_star boolean 项目被加星 通知开关
TypeTask bool `url:"type_task"` // type_task boolean 项目任务 通知开关
}
// 增加 webhook ( scope=project, project:depot )
// API: POST /api/user/{user_name}/project/{project_name}/git/hook/{hook_id}
func (s *HookService) CreateHook(owner, project string, hook *HookForm) error {
v, err := query.Values(hook)
if err != nil {
return err
}
req, err := s.client.NewRequest("POST",
fmt.Sprintf("/user/%s/project/%s/git/hook", owner, project),
strings.NewReader(v.Encode()))
if err != nil {
return err
}
req.Header.Add("content-type", "application/x-www-form-urlencoded")
_, err = s.client.Do(req, nil)
return err
}
// 编辑 webhook ( scope=project, project:depot )
// API: PUT /api/user/{user_name}/project/{project_name}/git/hook/{hook_id}
func (s *HookService) EditHook(owner, project string, hookID int, hook *HookForm) error {
v, err := query.Values(hook)
if err != nil {
return err
}
req, err := s.client.NewRequest("PUT",
fmt.Sprintf("/user/%s/project/%s/git/hook/%d", owner, project, hookID),
strings.NewReader(v.Encode()))
if err != nil {
return err
}
req.Header.Add("content-type", "application/x-www-form-urlencoded")
_, err = s.client.Do(req, nil)
return err
}
// 删除 webhook ( scope=project, project:depot )
// API: DELETE /api/user/{user_name}/project/{project_name}/git/hook
func (s *HookService) DeleteHook(owner, project string, hookID int) error {
req, err := s.client.NewRequest("DELETE",
fmt.Sprintf("/user/%s/project/%s/git/hook/%d", owner, project, hookID),
nil)
if err != nil {
return err
}
_, err = s.client.Do(req, nil)
return err
}