forked from codeclimate/codeclimate-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.rb
86 lines (72 loc) · 1.94 KB
/
service_test.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
#!/usr/bin/env ruby
#
# Ad-hoc script for sending the test event to service classes
#
# Usage:
#
# $ <SERVICE>_<CONFIG_ATTR_1>="..." \
# <SERVICE>_<CONFIG_ATTR_2>="..." \
# ... ... bundle exec ruby service_test.rb
#
# Example:
#
# $ SLACK_WEBHOOK_URL="http://..." bundle exec ruby service_test.rb
# $ GITHUBPULLREQUESTS_UPDATE_STATUS=false GITHUBPULLREQUESTS_OAUTH_TOKEN=06083a4a060d358ca709939b1f00645777661c44 bundle exec ruby service_test.rb
#
# Other Environment variables used:
#
# REPO_NAME Defaults to "App"
#
###
require "cc/services"
CC::Service.load_services
class WithResponseLogging
def initialize(invocation)
@invocation = invocation
end
def call
@invocation.call.tap { |r| p r }
end
end
class ServiceTest
def initialize(klass, *params)
@klass = klass
@params = params
end
def test(payload = {})
config = {}
puts "-" * 80
puts @klass
@params.each do |param|
if var = ENV[to_env_var(param)]
config[param] = var
else
puts " -> skipping"
return false
end
end
puts " -> testing"
puts " -> #{config.inspect}"
print " => "
test_service(@klass, config, payload)
end
private
def to_env_var(param)
"#{@klass.to_s.split("::").last}_#{param}".upcase
end
def test_service(klass, config, payload)
repo_name = ENV["REPO_NAME"] || "App"
service = klass.new(
config,
{ name: :test, repo_name: repo_name }.merge(payload),
)
CC::Service::Invocation.new(service) do |i|
i.wrap(WithResponseLogging)
end
end
end
ServiceTest.new(CC::Service::Slack, :webhook_url).test
ServiceTest.new(CC::Service::Flowdock, :api_token).test
ServiceTest.new(CC::Service::Jira, :username, :password, :domain, :project_id).test
ServiceTest.new(CC::Service::Asana, :api_key, :workspace_id, :project_id).test
ServiceTest.new(CC::Service::GitHubPullRequests, :oauth_token).test(github_slug: "codeclimate/codeclimate")