forked from octokit/octokit.rb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.rb
325 lines (267 loc) · 8.37 KB
/
helper.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
if RUBY_ENGINE == 'ruby'
require 'simplecov'
require 'coveralls'
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new [
SimpleCov::Formatter::HTMLFormatter,
Coveralls::SimpleCov::Formatter
]
SimpleCov.start
end
require 'json'
require 'octokit'
require 'rspec'
require 'webmock/rspec'
require 'base64'
require 'jwt'
WebMock.disable_net_connect!(:allow => 'coveralls.io')
RSpec.configure do |config|
config.raise_errors_for_deprecations!
config.before(:all) do
@test_repo = "#{test_github_login}/#{test_github_repository}"
@test_repo_id = test_github_repository_id
@test_org_repo = "#{test_github_org}/#{test_github_repository}"
end
end
require 'vcr'
VCR.configure do |c|
c.configure_rspec_metadata!
c.filter_sensitive_data("<GITHUB_LOGIN>") do
test_github_login
end
c.filter_sensitive_data("<GITHUB_PASSWORD>") do
test_github_password
end
c.filter_sensitive_data("<<ACCESS_TOKEN>>") do
test_github_token
end
c.filter_sensitive_data("<GITHUB_CLIENT_ID>") do
test_github_client_id
end
c.filter_sensitive_data("<GITHUB_CLIENT_SECRET>") do
test_github_client_secret
end
c.filter_sensitive_data("<<ENTERPRISE_GITHUB_LOGIN>>") do
test_github_enterprise_login
end
c.filter_sensitive_data("<<ENTERPRISE_ACCESS_TOKEN>>") do
test_github_enterprise_token
end
c.filter_sensitive_data("<<ENTERPRISE_MANAGEMENT_CONSOLE_PASSWORD>>") do
test_github_enterprise_management_console_password
end
c.filter_sensitive_data("<<ENTERPRISE_MANAGEMENT_CONSOLE_ENDPOINT>>") do
test_github_enterprise_management_console_endpoint
end
c.filter_sensitive_data("<<ENTERPRISE_HOSTNAME>>") do
test_github_enterprise_endpoint
end
c.define_cassette_placeholder("<GITHUB_TEST_REPOSITORY>") do
test_github_repository
end
c.define_cassette_placeholder("<GITHUB_TEST_ORGANIZATION>") do
test_github_org
end
c.define_cassette_placeholder("<GITHUB_TEST_ORG_TEAM_ID>") do
"10050505050000"
end
c.define_cassette_placeholder("<GITHUB_TEST_INTEGRATION>") do
test_github_integration
end
c.define_cassette_placeholder("<GITHUB_TEST_INTEGRATION_INSTALLATION>") do
test_github_integration_installation
end
c.before_http_request(:real?) do |request|
next if request.headers['X-Vcr-Test-Repo-Setup']
next unless request.uri.include? test_github_repository
options = {
:headers => {'X-Vcr-Test-Repo-Setup' => 'true'},
:auto_init => true
}
test_repo = "#{test_github_login}/#{test_github_repository}"
if !oauth_client.repository?(test_repo, options)
Octokit.octokit_warn "NOTICE: Creating #{test_repo} test repository."
oauth_client.create_repository(test_github_repository, options)
end
test_org_repo = "#{test_github_org}/#{test_github_repository}"
if !oauth_client.repository?(test_org_repo, options)
Octokit.octokit_warn "NOTICE: Creating #{test_org_repo} test repository."
options[:organization] = test_github_org
oauth_client.create_repository(test_github_repository, options)
end
end
c.ignore_request do |request|
!!request.headers['X-Vcr-Test-Repo-Setup']
end
c.default_cassette_options = {
:serialize_with => :json,
# TODO: Track down UTF-8 issue and remove
:preserve_exact_body_bytes => true,
:decode_compressed_response => true,
:record => ENV['TRAVIS'] ? :none : :once
}
c.cassette_library_dir = 'spec/cassettes'
c.hook_into :webmock
end
def delete_test_repo
begin
oauth_client.delete_repository @test_repo
rescue Octokit::NotFound
end
end
def test_github_login
ENV.fetch 'OCTOKIT_TEST_GITHUB_LOGIN', 'api-padawan'
end
def test_github_password
ENV.fetch 'OCTOKIT_TEST_GITHUB_PASSWORD', 'wow_such_password'
end
def test_github_token
ENV.fetch 'OCTOKIT_TEST_GITHUB_TOKEN', 'x' * 40
end
def test_github_client_id
ENV.fetch 'OCTOKIT_TEST_GITHUB_CLIENT_ID', 'x' * 21
end
def test_github_client_secret
ENV.fetch 'OCTOKIT_TEST_GITHUB_CLIENT_SECRET', 'x' * 40
end
def test_github_enterprise_login
ENV.fetch 'OCTOKIT_TEST_GITHUB_ENTERPRISE_LOGIN', 'crashoverride'
end
def test_github_enterprise_token
ENV.fetch 'OCTOKIT_TEST_GITHUB_ENTERPRISE_TOKEN', 'x' * 40
end
def test_github_enterprise_management_console_password
ENV.fetch 'OCTOKIT_TEST_GITHUB_ENTERPRISE_MANAGEMENT_CONSOLE_PASSWORD', 'Secretpa55'
end
def test_github_enterprise_management_console_endpoint
ENV.fetch 'OCTOKIT_TEST_GITHUB_ENTERPRISE_MANAGEMENT_CONSOLE_ENDPOINT', 'https://enterprise.github.dev:8443/'
end
def test_github_enterprise_endpoint
ENV.fetch 'OCTOKIT_TEST_GITHUB_ENTERPRISE_ENDPOINT', 'http://enterprise.github.dev/api/v3/'
end
def test_github_repository
ENV.fetch 'OCTOKIT_TEST_GITHUB_REPOSITORY', 'api-sandbox'
end
def test_github_repository_id
ENV.fetch 'OCTOKIT_TEST_GITHUB_REPOSITORY_ID', 20_974_780
end
def test_github_org
ENV.fetch 'OCTOKIT_TEST_GITHUB_ORGANIZATION', 'api-playground'
end
def test_github_integration
ENV.fetch 'OCTOKIT_TEST_GITHUB_INTEGRATION', 42
end
def test_github_integration_installation
ENV.fetch 'OCTOKIT_TEST_GITHUB_INTEGRATION_INSTALLATION', 37
end
def test_github_integration_pem_key
ENV.fetch 'OCTOKIT_TEST_INTEGRATION_PEM_KEY', "#{fixture_path}/fake_integration.private-key.pem"
end
def stub_delete(url)
stub_request(:delete, github_url(url))
end
def stub_get(url)
stub_request(:get, github_url(url))
end
def stub_head(url)
stub_request(:head, github_url(url))
end
def stub_patch(url)
stub_request(:patch, github_url(url))
end
def stub_post(url)
stub_request(:post, github_url(url))
end
def stub_put(url)
stub_request(:put, github_url(url))
end
def fixture_path
File.expand_path("../fixtures", __FILE__)
end
def fixture(file)
File.new(fixture_path + '/' + file)
end
def json_response(file)
{
:body => fixture(file),
:headers => {
:content_type => 'application/json; charset=utf-8'
}
}
end
def github_url(url)
return url if url =~ /^http/
url = File.join(Octokit.api_endpoint, url)
uri = Addressable::URI.parse(url)
uri.path.gsub!("v3//", "v3/")
uri.to_s
end
def github_enterprise_url(url)
test_github_enterprise_endpoint + url
end
def github_management_console_url(url)
test_github_enterprise_management_console_endpoint + url
end
def basic_github_url(path, options = {})
url = File.join(Octokit.api_endpoint, path)
uri = Addressable::URI.parse(url)
uri.path.gsub!("v3//", "v3/")
uri.user = options.fetch(:login, test_github_login)
uri.password = options.fetch(:password, test_github_password)
uri.to_s
end
def basic_auth_client(login = test_github_login, password = test_github_password )
client = Octokit.client
client.login = test_github_login
client.password = test_github_password
client
end
def oauth_client
Octokit::Client.new(:access_token => test_github_token)
end
def enterprise_admin_client
stack = Faraday::RackBuilder.new do |builder|
builder.request :multipart
builder.request :url_encoded
builder.adapter Faraday.default_adapter
end
client = Octokit::EnterpriseAdminClient.new \
:access_token => test_github_enterprise_token,
:connection_options => { :ssl => { :verify => false } }
client.configure do |c|
c.api_endpoint = test_github_enterprise_endpoint
c.middleware = stack
end
client
end
def enterprise_management_console_client
stack = Faraday::RackBuilder.new do |builder|
builder.request :multipart
builder.request :url_encoded
builder.adapter Faraday.default_adapter
end
client = Octokit::EnterpriseManagementConsoleClient.new \
:management_console_endpoint => test_github_enterprise_management_console_endpoint,
:management_console_password => test_github_enterprise_management_console_password,
:connection_options => { :ssl => { :verify => false } }
client.configure do |c|
c.middleware = stack
end
client
end
def new_jwt_token
private_pem = File.read(test_github_integration_pem_key)
private_key = OpenSSL::PKey::RSA.new(private_pem)
payload = {}.tap do |opts|
opts[:iat] = Time.now.to_i # Issued at time.
opts[:exp] = opts[:iat] + 600 # JWT expiration time is 10 minutes from issued time.
opts[:iss] = test_github_integration # Integration's GitHub identifier.
end
JWT.encode(payload, private_key, 'RS256')
end
def use_vcr_placeholder_for(text, replacement)
VCR.configure do |c|
c.define_cassette_placeholder(replacement) do
text
end
end
end