github-web-hooks-receiver 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,75 @@
1
+ to: global-to@example.com
2
+ error_to: error@example.com
3
+ exception_notifier:
4
+ subject_label: "[git-utils]"
5
+ sender: sender@example.com
6
+ add_html: false
7
+ domains:
8
+ github.com:
9
+ add_html: true
10
+ owners:
11
+ clear-code:
12
+ to: commit@clear-code.com
13
+ ranguba:
14
+ to:
15
+ - groonga-commit@rubyforge.org
16
+ - commit@clear-code.com
17
+ repositories:
18
+ examples:
19
+ to: null@example.com
20
+ groonga:
21
+ to: groonga-commit@lists.sourceforge.jp
22
+ gitlab.example.net:
23
+ owners:
24
+ clear-code:
25
+ add_html: true
26
+ to:
27
+ - commit@example.net
28
+ support:
29
+ to: support@example.net
30
+ from: support+null@example.net
31
+ gitlab.example.org:
32
+ owners:
33
+ clear-code:
34
+ to:
35
+ - commit@example.org
36
+ from: null@example.org
37
+ repositories:
38
+ test-project1:
39
+ add_html: true
40
+ to: commit+test-project1@example.org
41
+ test-project2:
42
+ add_html: false
43
+ to: commit+test-project2@example.org
44
+ support:
45
+ to: support@example.org
46
+ from: null+support@example.org
47
+ ghe.example.com:
48
+ owners:
49
+ clear-code:
50
+ to:
51
+ - commit@example.com
52
+ from: null@example.com
53
+ repositories:
54
+ test-project1:
55
+ to: commit+test-project1@example.com
56
+ test-project2:
57
+ to: commit+test-project2@example.com
58
+ support:
59
+ to: support@example.com
60
+ from: null+support@example.com
61
+ ghe.example.co.jp:
62
+ add_html: true
63
+ owners:
64
+ clear-code:
65
+ to:
66
+ - commit@example.co.jp
67
+ from: null@example.co.jp
68
+ repositories:
69
+ test-project1:
70
+ to: commit+test-project1@example.co.jp
71
+ test-project2:
72
+ to: commit+test-project2@example.co.jp
73
+ support:
74
+ to: support@example.co.jp
75
+ from: null+support@example.co.jp
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+
5
+ tmp_dir = File.join(File.dirname(__FILE__), "..", "tmp")
6
+ result_path = File.join(tmp_dir, "commit-email-result.yaml")
7
+
8
+ if File.exist?(result_path)
9
+ result = YAML.load_file(result_path)
10
+ else
11
+ result = []
12
+ end
13
+
14
+ lines = []
15
+ $stdin.each_line do |line|
16
+ lines << line
17
+ end
18
+ result << {"argv" => ARGV, "lines" => lines}
19
+
20
+ File.open(result_path, "w") do |_result|
21
+ _result.print(result.to_yaml)
22
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "fileutils"
4
+
5
+ FileUtils.mkdir_p(ARGV.last)
@@ -0,0 +1,50 @@
1
+ # Copyright (C) 2010-2013 Kouhei Sutou <kou@clear-code.com>
2
+ #
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ require "github-web-hooks-receiver"
17
+
18
+ require "webrick/httpstatus"
19
+
20
+ module GitHubWebHooksReceiverTestUtils
21
+ include Capybara::DSL
22
+
23
+ private
24
+ def assert_response(code)
25
+ assert_equal(resolve_status(code), resolve_status(status_code))
26
+ end
27
+
28
+ def resolve_status(code_or_message)
29
+ messages = WEBrick::HTTPStatus::StatusMessage
30
+ if code_or_message.is_a?(String)
31
+ message = code_or_message
32
+ [(messages.find {|key, value| value == message} || [])[0],
33
+ message]
34
+ else
35
+ code = code_or_message
36
+ [code, messages[code]]
37
+ end
38
+ end
39
+
40
+ class LocalRepository < GitHubWebHooksReceiver::Repository
41
+ private
42
+ def fixtures_dir
43
+ @options[:fixtures_dir]
44
+ end
45
+
46
+ def repository_uri
47
+ File.join(fixtures_dir, "#{@name}.git")
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,345 @@
1
+ # Copyright (C) 2013 Kenji Okimoto <okimoto@clear-code.com>
2
+ #
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ require "yaml"
17
+
18
+ class MultiSiteReceiverTest < Test::Unit::TestCase
19
+ include GitHubWebHooksReceiverTestUtils
20
+
21
+ def setup
22
+ test_dir = File.dirname(__FILE__)
23
+ @fixtures_dir = File.join(test_dir, "fixtures")
24
+ @tmp_dir = File.join(test_dir, "tmp")
25
+ FileUtils.mkdir_p(@tmp_dir)
26
+ Capybara.app = app
27
+ end
28
+
29
+ def teardown
30
+ FileUtils.rm_rf(@tmp_dir)
31
+ end
32
+
33
+ def app
34
+ options = YAML.load_file(File.join(@fixtures_dir, "config-multi-site.yaml"))
35
+ options[:base_dir] = @tmp_dir
36
+ options[:fixtures_dir] = @fixtures_dir
37
+ options[:commit_email] = File.join(@fixtures_dir, "mock-commit-email.rb")
38
+ options[:git] = File.join(@fixtures_dir, "stub-git.rb")
39
+ GitHubWebHooksReceiver::App.new(options)
40
+ end
41
+
42
+ class GitHubTest < self
43
+ data("github.com/clear-code/git-utils" => {
44
+ :params => {
45
+ :domain => "github.com",
46
+ :owner_name => "clear-code",
47
+ :repository_name => "git-utils"
48
+ },
49
+ :expected => {
50
+ :add_html => true,
51
+ :from => nil,
52
+ :sender => "sender@example.com",
53
+ :error_to => "error@example.com",
54
+ :to => "commit@clear-code.com",
55
+ }
56
+ },
57
+ "github.com/ranguba/rroonga" => {
58
+ :params => {
59
+ :domain => "github.com",
60
+ :owner_name => "ranguba",
61
+ :repository_name => "rroonga"
62
+ },
63
+ :expected => {
64
+ :add_html => true,
65
+ :from => nil,
66
+ :sender => "sender@example.com",
67
+ :error_to => "error@example.com",
68
+ :to => ["groonga-commit@rubyforge.org", "commit@clear-code.com"],
69
+ }
70
+ },
71
+ "github.com/ranguba/examples" => {
72
+ :params => {
73
+ :domain => "github.com",
74
+ :owner_name => "ranguba",
75
+ :repository_name => "examples"
76
+ },
77
+ :expected => {
78
+ :add_html => true,
79
+ :from => nil,
80
+ :sender => "sender@example.com",
81
+ :error_to => "error@example.com",
82
+ :to => "null@example.com",
83
+ }
84
+ },
85
+ "ghe.example.com/clea-code/git-utils" => {
86
+ :params => {
87
+ :domain => "ghe.example.com",
88
+ :owner_name => "clear-code",
89
+ :repository_name => "git-utils"
90
+ },
91
+ :expected => {
92
+ :add_html => false,
93
+ :from => "null@example.com",
94
+ :sender => "sender@example.com",
95
+ :error_to => "error@example.com",
96
+ :to => "commit@example.com",
97
+ }
98
+ },
99
+ "ghe.example.com/clea-code/test-project1" => {
100
+ :params => {
101
+ :domain => "ghe.example.com",
102
+ :owner_name => "clear-code",
103
+ :repository_name => "test-project1"
104
+ },
105
+ :expected => {
106
+ :add_html => false,
107
+ :from => "null@example.com",
108
+ :sender => "sender@example.com",
109
+ :error_to => "error@example.com",
110
+ :to => "commit+test-project1@example.com",
111
+ }
112
+ },
113
+ "ghe.example.com/clea-code/test-project2" => {
114
+ :params => {
115
+ :domain => "ghe.example.com",
116
+ :owner_name => "clear-code",
117
+ :repository_name => "test-project2"
118
+ },
119
+ :expected => {
120
+ :add_html => false,
121
+ :from => "null@example.com",
122
+ :sender => "sender@example.com",
123
+ :error_to => "error@example.com",
124
+ :to => "commit+test-project2@example.com",
125
+ }
126
+ },
127
+ "ghe.example.co.jp/clea-code/git-utils" => {
128
+ :params => {
129
+ :domain => "ghe.example.co.jp",
130
+ :owner_name => "clear-code",
131
+ :repository_name => "git-utils"
132
+ },
133
+ :expected => {
134
+ :add_html => true,
135
+ :from => "null@example.co.jp",
136
+ :sender => "sender@example.com",
137
+ :error_to => "error@example.com",
138
+ :to => "commit@example.co.jp",
139
+ }
140
+ },
141
+ "ghe.example.co.jp/clea-code/test-project1" => {
142
+ :params => {
143
+ :domain => "ghe.example.co.jp",
144
+ :owner_name => "clear-code",
145
+ :repository_name => "test-project1"
146
+ },
147
+ :expected => {
148
+ :add_html => true,
149
+ :from => "null@example.co.jp",
150
+ :sender => "sender@example.com",
151
+ :error_to => "error@example.com",
152
+ :to => "commit+test-project1@example.co.jp",
153
+ }
154
+ },
155
+ "ghe.example.co.jp/clea-code/test-project2" => {
156
+ :params => {
157
+ :domain => "ghe.example.co.jp",
158
+ :owner_name => "clear-code",
159
+ :repository_name => "test-project2"
160
+ },
161
+ :expected => {
162
+ :add_html => true,
163
+ :from => "null@example.co.jp",
164
+ :sender => "sender@example.com",
165
+ :error_to => "error@example.com",
166
+ :to => "commit+test-project2@example.co.jp",
167
+ }
168
+ },
169
+ )
170
+ def test_post(data)
171
+ domain = data[:params][:domain]
172
+ owner_name = data[:params][:owner_name]
173
+ repository_name = data[:params][:repository_name]
174
+ gitlab_project_uri = "https://#{domain}/#{owner_name}/#{repository_name}"
175
+ repository_uri = "git@#{domain}:#{owner_name}/#{repository_name}.git"
176
+ repository_mirror_path = mirror_path(domain, owner_name, repository_name)
177
+ add_html = data[:expected][:add_html]
178
+ from = data[:expected][:from]
179
+ sender = data[:expected][:sender]
180
+ error_to = data[:expected][:error_to]
181
+ to = data[:expected][:to]
182
+ assert_false(File.exist?(repository_mirror_path))
183
+ before = "0f2be32a3671360a323f1dee64c757bc9fc44998"
184
+ after = "c7bf92799225d67788be7c42ea4f504a47708390"
185
+ reference = "refs/heads/master"
186
+ expected_argv = [
187
+ "--repository", repository_mirror_path,
188
+ "--max-size", "1M",
189
+ "--repository-browser", "github",
190
+ "--github-user", owner_name,
191
+ "--github-repository", repository_name,
192
+ "--name", "#{owner_name}/#{repository_name}",
193
+ ]
194
+ expected_argv.push("--from", from) if from
195
+ expected_argv.push("--sender", sender)
196
+ expected_argv.push("--add-html") if add_html
197
+ expected_argv.push("--error-to", error_to)
198
+ expected_argv.push(*to)
199
+ post_payload(:repository => {
200
+ :homepage => gitlab_project_uri,
201
+ :url => repository_uri,
202
+ :name => repository_name,
203
+ :owner => {
204
+ :name => owner_name,
205
+ }
206
+ },
207
+ :before => before,
208
+ :after => after,
209
+ :ref => reference)
210
+ assert_response("OK")
211
+ assert_true(File.exist?(repository_mirror_path), repository_mirror_path)
212
+ result = YAML.load_file(File.join(@tmp_dir, "commit-email-result.yaml"))
213
+ assert_equal([{
214
+ "argv" => expected_argv,
215
+ "lines" => ["#{before} #{after} #{reference}\n"],
216
+ }],
217
+ result)
218
+ end
219
+ end
220
+
221
+ class GitLabTest < self
222
+ data("gitlab.example.com/ranguba/rroonga" => {
223
+ :params => {
224
+ :domain => "gitlab.example.org",
225
+ :owner_name => "ranguba",
226
+ :repository_name => "rroonga"
227
+ },
228
+ :expected => {
229
+ :sender => "sender@example.com",
230
+ :error_to => "error@example.com",
231
+ :to => "global-to@example.com",
232
+ }
233
+ },
234
+ "gitlab.example.net/clear-code/git-utils" => {
235
+ :params => {
236
+ :domain => "gitlab.example.net",
237
+ :owner_name => "clear-code",
238
+ :repository_name => "git-utils"
239
+ },
240
+ :expected => {
241
+ :add_html => true,
242
+ :sender => "sender@example.com",
243
+ :error_to => "error@example.com",
244
+ :to => "commit@example.net",
245
+ }
246
+ },
247
+ "gitlab.example.net/support/git-utils" => {
248
+ :params => {
249
+ :domain => "gitlab.example.net",
250
+ :owner_name => "support",
251
+ :repository_name => "git-utils"
252
+ },
253
+ :expected => {
254
+ :add_html => false,
255
+ :from => "support+null@example.net",
256
+ :sender => "sender@example.com",
257
+ :error_to => "error@example.com",
258
+ :to => "support@example.net",
259
+ }
260
+ },
261
+ "gitlab.example.org/clear-code/test-project1" => {
262
+ :params => {
263
+ :domain => "gitlab.example.org",
264
+ :owner_name => "clear-code",
265
+ :repository_name => "test-project1"
266
+ },
267
+ :expected => {
268
+ :add_html => true,
269
+ :from => "null@example.org",
270
+ :sender => "sender@example.com",
271
+ :error_to => "error@example.com",
272
+ :to => "commit+test-project1@example.org",
273
+ }
274
+ },
275
+ "gitlab.example.org/clear-code/test-project2" => {
276
+ :params => {
277
+ :domain => "gitlab.example.org",
278
+ :owner_name => "clear-code",
279
+ :repository_name => "test-project2"
280
+ },
281
+ :expected => {
282
+ :add_html => false,
283
+ :from => "null@example.org",
284
+ :sender => "sender@example.com",
285
+ :error_to => "error@example.com",
286
+ :to => "commit+test-project2@example.org",
287
+ }
288
+ })
289
+ def test_post(data)
290
+ domain = data[:params][:domain]
291
+ owner_name = data[:params][:owner_name]
292
+ repository_name = data[:params][:repository_name]
293
+ gitlab_project_uri = "https://#{domain}/#{owner_name}/#{repository_name}"
294
+ repository_uri = "git@#{domain}:#{owner_name}/#{repository_name}.git"
295
+ repository_mirror_path = mirror_path(domain, owner_name, repository_name)
296
+ add_html = data[:expected][:add_html]
297
+ from = data[:expected][:from]
298
+ sender = data[:expected][:sender]
299
+ error_to = data[:expected][:error_to]
300
+ to = data[:expected][:to]
301
+ assert_false(File.exist?(repository_mirror_path))
302
+ before = "0f2be32a3671360a323f1dee64c757bc9fc44998"
303
+ after = "c7bf92799225d67788be7c42ea4f504a47708390"
304
+ reference = "refs/heads/master"
305
+ expected_argv = [
306
+ "--repository", repository_mirror_path,
307
+ "--max-size", "1M",
308
+ "--repository-browser", "gitlab",
309
+ "--gitlab-project-uri", gitlab_project_uri
310
+ ]
311
+ expected_argv.push("--from", from) if from
312
+ expected_argv.push("--sender", sender)
313
+ expected_argv.push("--add-html") if add_html
314
+ expected_argv.push("--error-to", error_to)
315
+ expected_argv.push(to)
316
+ post_payload(:repository => {
317
+ :homepage => gitlab_project_uri,
318
+ :url => repository_uri,
319
+ :name => repository_name,
320
+ },
321
+ :before => before,
322
+ :after => after,
323
+ :ref => reference,
324
+ :user_name => "jojo")
325
+ assert_response("OK")
326
+ assert_true(File.exist?(repository_mirror_path), repository_mirror_path)
327
+ result = YAML.load_file(File.join(@tmp_dir, "commit-email-result.yaml"))
328
+ assert_equal([{
329
+ "argv" => expected_argv,
330
+ "lines" => ["#{before} #{after} #{reference}\n"],
331
+ }],
332
+ result)
333
+ end
334
+ end
335
+
336
+ private
337
+
338
+ def post_payload(payload)
339
+ page.driver.post("/", :payload => JSON.generate(payload))
340
+ end
341
+
342
+ def mirror_path(*components)
343
+ File.join(@tmp_dir, "mirrors", *components)
344
+ end
345
+ end