-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
hooks.rb
287 lines (273 loc) · 11.1 KB
/
hooks.rb
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# frozen_string_literal: true
module Octokit
class Client
# Methods for the Hooks API
module Hooks
# List repo hooks
#
# Requires authenticated client.
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository.
# @return [Array<Sawyer::Resource>] Array of hashes representing hooks.
# @see https://developer.github.com/v3/repos/hooks/#list-hooks
# @example
# @client.hooks('octokit/octokit.rb')
def hooks(repo, options = {})
paginate "#{Repository.path repo}/hooks", options
end
# Get single hook
#
# Requires authenticated client.
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository.
# @param id [Integer] Id of the hook to get.
# @return [Sawyer::Resource] Hash representing hook.
# @see https://developer.github.com/v3/repos/hooks/#get-single-hook
# @example
# @client.hook('octokit/octokit.rb', 100000)
def hook(repo, id, options = {})
get "#{Repository.path repo}/hooks/#{id}", options
end
# Create a hook
#
# Requires authenticated client.
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository.
# @param name [String] The name of the service that is being called. See
# {https://api.github.com/hooks Hooks} for the possible names.
# @param config [Hash] A Hash containing key/value pairs to provide
# settings for this hook. These settings vary between the services and
# are defined in the {https://github.com/github/github-services github-services} repo.
# @option options [Array<String>] :events ('["push"]') Determines what
# events the hook is triggered for.
# @option options [Boolean] :active Determines whether the hook is
# actually triggered on pushes.
# @return [Sawyer::Resource] Hook info for the new hook
# @see https://api.github.com/hooks
# @see https://github.com/github/github-services
# @see https://developer.github.com/v3/repos/hooks/#create-a-hook
# @example
# @client.create_hook(
# 'octokit/octokit.rb',
# 'web',
# {
# :url => 'http://something.com/webhook',
# :content_type => 'json'
# },
# {
# :events => ['push', 'pull_request'],
# :active => true
# }
# )
def create_hook(repo, name, config, options = {})
options = { name: name, config: config, events: ['push'], active: true }.merge(options)
post "#{Repository.path repo}/hooks", options
end
# Edit a hook
#
# Requires authenticated client.
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository.
# @param id [Integer] Id of the hook being updated.
# @param name [String] The name of the service that is being called. See
# {https://api.github.com/hooks Hooks} for the possible names.
# @param config [Hash] A Hash containing key/value pairs to provide
# settings for this hook. These settings vary between the services and
# are defined in the {https://github.com/github/github-services github-services} repo.
# @option options [Array<String>] :events ('["push"]') Determines what
# events the hook is triggered for.
# @option options [Array<String>] :add_events Determines a list of events
# to be added to the list of events that the Hook triggers for.
# @option options [Array<String>] :remove_events Determines a list of events
# to be removed from the list of events that the Hook triggers for.
# @option options [Boolean] :active Determines whether the hook is
# actually triggered on pushes.
# @return [Sawyer::Resource] Hook info for the updated hook
# @see https://api.github.com/hooks
# @see https://github.com/github/github-services
# @see https://developer.github.com/v3/repos/hooks/#edit-a-hook
# @example
# @client.edit_hook(
# 'octokit/octokit.rb',
# 100000,
# 'web',
# {
# :url => 'http://something.com/webhook',
# :content_type => 'json'
# },
# {
# :add_events => ['status'],
# :remove_events => ['pull_request'],
# :active => true
# }
# )
def edit_hook(repo, id, name, config, options = {})
options = { name: name, config: config }.merge(options)
patch "#{Repository.path repo}/hooks/#{id}", options
end
# Delete hook
#
# Requires authenticated client.
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository.
# @param id [Integer] Id of the hook to remove.
# @return [Boolean] True if hook removed, false otherwise.
# @see https://developer.github.com/v3/repos/hooks/#delete-a-hook
# @example
# @client.remove_hook('octokit/octokit.rb', 1000000)
def remove_hook(repo, id, options = {})
boolean_from_response :delete, "#{Repository.path repo}/hooks/#{id}", options
end
# Test hook
#
# Requires authenticated client.
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository.
# @param id [Integer] Id of the hook to test.
# @return [Boolean] Success
# @see https://developer.github.com/v3/repos/hooks/#test-a-push-hook
# @example
# @client.test_hook('octokit/octokit.rb', 1000000)
def test_hook(repo, id, options = {})
boolean_from_response :post, "#{Repository.path repo}/hooks/#{id}/tests", options
end
# Ping hook
#
# Requires authenticated client.
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository.
# @param id [Integer] Id of the hook to send a ping.
# @return [Boolean] Ping requested?
# @see https://developer.github.com/v3/repos/hooks/#ping-a-hook
# @example
# @client.ping_hook('octokit/octokit.rb', 1000000)
def ping_hook(repo, id, options = {})
boolean_from_response :post, "#{Repository.path repo}/hooks/#{id}/pings", options
end
# List org hooks
#
# Requires client authenticated as admin for the org.
#
# @param org [String, Integer] Organization GitHub login or id.
# @return [Array<Sawyer::Resource>] Array of hashes representing hooks.
# @see https://developer.github.com/v3/orgs/hooks/#list-hooks
# @example
# @client.org_hooks('octokit')
def org_hooks(org, options = {})
paginate "#{Organization.path org}/hooks", options
end
alias list_org_hooks org_hooks
# Get an org hook
#
# Requires client authenticated as admin for the org.
#
# @param org [String, Integer] Organization GitHub login or id.
# @param id [Integer] Id of the hook to get.
# @return [Sawyer::Resource] Hash representing hook.
# @see https://developer.github.com/v3/orgs/hooks/#get-single-hook
# @example
# @client.org_hook('octokit', 123)
def org_hook(org, id, options = {})
get "#{Organization.path org}/hooks/#{id}", options
end
# Create an org hook
#
# Requires client authenticated as admin for the org.
#
# @param org [String, Integer] Organization GitHub login or id.
# @param config [Hash] A Hash containing key/value pairs to provide
# settings for this hook.
# @option options [Array<String>] :events ('["push"]') Determines what
# events the hook is triggered for.
# @option options [Boolean] :active Determines whether the hook is
# actually triggered on pushes.
# @return [Sawyer::Resource] Hook info for the new hook
# @see https://api.github.com/hooks
# @see https://developer.github.com/v3/orgs/hooks/#create-a-hook
# @example
# @client.create_org_hook(
# 'octokit',
# {
# :url => 'http://something.com/webhook',
# :content_type => 'json'
# },
# {
# :events => ['push', 'pull_request'],
# :active => true
# }
# )
def create_org_hook(org, config, options = {})
options = { name: 'web', config: config }.merge(options)
post "#{Organization.path org}/hooks", options
end
# Update an org hook
#
# Requires client authenticated as admin for the org.
#
# @param org [String, Integer] Organization GitHub login or id.
# @param id [Integer] Id of the hook to update.
# @param config [Hash] A Hash containing key/value pairs to provide
# settings for this hook.
# @option options [Array<String>] :events ('["push"]') Determines what
# events the hook is triggered for.
# @option options [Boolean] :active Determines whether the hook is
# actually triggered on pushes.
# @return [Sawyer::Resource] Hook info for the new hook
# @see https://api.github.com/hooks
# @see https://developer.github.com/v3/orgs/hooks/#edit-a-hook
# @example
# @client.edit_org_hook(
# 'octokit',
# 123,
# {
# :url => 'http://something.com/webhook',
# :content_type => 'json'
# },
# {
# :events => ['push', 'pull_request'],
# :active => true
# }
# )
def edit_org_hook(org, id, config, options = {})
options = { config: config }.merge(options)
patch "#{Organization.path org}/hooks/#{id}", options
end
alias update_org_hook edit_org_hook
# Ping org hook
#
# Requires client authenticated as admin for the org.
#
# @param org [String, Integer] Organization GitHub login or id.
# @param id [Integer] Id of the hook to update.
# @return [Boolean] Success
# @see https://developer.github.com/v3/orgs/hooks/#ping-a-hook
# @example
# @client.ping_org_hook('octokit', 1000000)
def ping_org_hook(org, id, options = {})
boolean_from_response :post, "#{Organization.path org}/hooks/#{id}/pings", options
end
# Remove org hook
#
# Requires client authenticated as admin for the org.
#
# @param org [String, Integer] Organization GitHub login or id.
# @param id [Integer] Id of the hook to update.
# @return [Boolean] True if hook removed, false otherwise.
# @see https://developer.github.com/v3/orgs/hooks/#delete-a-hook
# @example
# @client.remove_org_hook('octokit', 1000000)
def remove_org_hook(org, id, options = {})
boolean_from_response :delete, "#{Organization.path org}/hooks/#{id}", options
end
# Parse payload string
#
# @param payload_string [String] The payload
# @return [Sawyer::Resource] The payload object
# @see https://developer.github.com/v3/activity/events/types/
def parse_payload(payload_string)
payload_hash = agent.class.decode payload_string
Sawyer::Resource.new agent, payload_hash
end
end
end
end