1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "time"
12 )
13
14
15
16
17
18
19
20
21
22
23 type WebHookPayload = PushEvent
24
25
26
27
28
29 type WebHookCommit = HeadCommit
30
31
32
33
34
35
36 type WebHookAuthor = CommitAuthor
37
38
39 type Hook struct {
40 CreatedAt *time.Time `json:"created_at,omitempty"`
41 UpdatedAt *time.Time `json:"updated_at,omitempty"`
42 URL *string `json:"url,omitempty"`
43 ID *int64 `json:"id,omitempty"`
44 Type *string `json:"type,omitempty"`
45 Name *string `json:"name,omitempty"`
46 TestURL *string `json:"test_url,omitempty"`
47 PingURL *string `json:"ping_url,omitempty"`
48 LastResponse map[string]interface{} `json:"last_response,omitempty"`
49
50
51
52 Config map[string]interface{} `json:"config,omitempty"`
53 Events []string `json:"events,omitempty"`
54 Active *bool `json:"active,omitempty"`
55 }
56
57 func (h Hook) String() string {
58 return Stringify(h)
59 }
60
61
62
63
64
65
66 type createHookRequest struct {
67
68 Name string `json:"name"`
69 Config map[string]interface{} `json:"config,omitempty"`
70 Events []string `json:"events,omitempty"`
71 Active *bool `json:"active,omitempty"`
72 }
73
74
75
76
77
78
79
80
81 func (s *RepositoriesService) CreateHook(ctx context.Context, owner, repo string, hook *Hook) (*Hook, *Response, error) {
82 u := fmt.Sprintf("repos/%v/%v/hooks", owner, repo)
83
84 hookReq := &createHookRequest{
85 Name: "web",
86 Events: hook.Events,
87 Active: hook.Active,
88 Config: hook.Config,
89 }
90
91 req, err := s.client.NewRequest("POST", u, hookReq)
92 if err != nil {
93 return nil, nil, err
94 }
95
96 h := new(Hook)
97 resp, err := s.client.Do(ctx, req, h)
98 if err != nil {
99 return nil, resp, err
100 }
101
102 return h, resp, nil
103 }
104
105
106
107
108 func (s *RepositoriesService) ListHooks(ctx context.Context, owner, repo string, opts *ListOptions) ([]*Hook, *Response, error) {
109 u := fmt.Sprintf("repos/%v/%v/hooks", owner, repo)
110 u, err := addOptions(u, opts)
111 if err != nil {
112 return nil, nil, err
113 }
114
115 req, err := s.client.NewRequest("GET", u, nil)
116 if err != nil {
117 return nil, nil, err
118 }
119
120 var hooks []*Hook
121 resp, err := s.client.Do(ctx, req, &hooks)
122 if err != nil {
123 return nil, resp, err
124 }
125
126 return hooks, resp, nil
127 }
128
129
130
131
132 func (s *RepositoriesService) GetHook(ctx context.Context, owner, repo string, id int64) (*Hook, *Response, error) {
133 u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
134 req, err := s.client.NewRequest("GET", u, nil)
135 if err != nil {
136 return nil, nil, err
137 }
138 h := new(Hook)
139 resp, err := s.client.Do(ctx, req, h)
140 if err != nil {
141 return nil, resp, err
142 }
143
144 return h, resp, nil
145 }
146
147
148
149
150 func (s *RepositoriesService) EditHook(ctx context.Context, owner, repo string, id int64, hook *Hook) (*Hook, *Response, error) {
151 u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
152 req, err := s.client.NewRequest("PATCH", u, hook)
153 if err != nil {
154 return nil, nil, err
155 }
156 h := new(Hook)
157 resp, err := s.client.Do(ctx, req, h)
158 if err != nil {
159 return nil, resp, err
160 }
161
162 return h, resp, nil
163 }
164
165
166
167
168 func (s *RepositoriesService) DeleteHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
169 u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
170 req, err := s.client.NewRequest("DELETE", u, nil)
171 if err != nil {
172 return nil, err
173 }
174 return s.client.Do(ctx, req, nil)
175 }
176
177
178
179
180 func (s *RepositoriesService) PingHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
181 u := fmt.Sprintf("repos/%v/%v/hooks/%d/pings", owner, repo, id)
182 req, err := s.client.NewRequest("POST", u, nil)
183 if err != nil {
184 return nil, err
185 }
186 return s.client.Do(ctx, req, nil)
187 }
188
189
190
191
192 func (s *RepositoriesService) TestHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
193 u := fmt.Sprintf("repos/%v/%v/hooks/%d/tests", owner, repo, id)
194 req, err := s.client.NewRequest("POST", u, nil)
195 if err != nil {
196 return nil, err
197 }
198 return s.client.Do(ctx, req, nil)
199 }
200
View as plain text