factor-connector-github 0.0.1
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.
- data/lib/factor/connector/github.rb +236 -0
- metadata +78 -0
@@ -0,0 +1,236 @@
|
|
1
|
+
require 'factor-connector-api'
|
2
|
+
require 'github_api'
|
3
|
+
|
4
|
+
github_events = [
|
5
|
+
'commit_comment',
|
6
|
+
'create',
|
7
|
+
'delete',
|
8
|
+
'deployment',
|
9
|
+
'deployment_status',
|
10
|
+
'download',
|
11
|
+
'fork',
|
12
|
+
'gollum',
|
13
|
+
'issue_comment',
|
14
|
+
'issues',
|
15
|
+
'member',
|
16
|
+
'page_build',
|
17
|
+
'public',
|
18
|
+
'pull_request',
|
19
|
+
'pull_request_review_comment',
|
20
|
+
'push',
|
21
|
+
'release',
|
22
|
+
'team_add',
|
23
|
+
'watch']
|
24
|
+
|
25
|
+
Factor::Connector.service 'github' do
|
26
|
+
github_events.each do |github_event|
|
27
|
+
listener github_event do
|
28
|
+
start do |params|
|
29
|
+
api_key = params['api_key']
|
30
|
+
username = params['username']
|
31
|
+
repo = params['repo']
|
32
|
+
branch = params['branch']
|
33
|
+
|
34
|
+
if repo
|
35
|
+
username, repo = repo.split('/') if repo.include?('/') && !username
|
36
|
+
repo, branch = repo.split('#') if repo.include?('#') && !branch
|
37
|
+
branch ||= 'master'
|
38
|
+
end
|
39
|
+
|
40
|
+
fail 'API Key is required' unless api_key
|
41
|
+
fail 'Username is required' unless username
|
42
|
+
fail 'Repo is required' unless repo
|
43
|
+
|
44
|
+
info 'connecting to Github'
|
45
|
+
begin
|
46
|
+
github = Github.new oauth_token: api_key
|
47
|
+
rescue => ex
|
48
|
+
fail 'Failed to connect to github. Try re-activating Github service.', exception: ex
|
49
|
+
end
|
50
|
+
|
51
|
+
hook_url = web_hook id: 'post_receive' do
|
52
|
+
start do |_listener_start_params, hook_data, _req, _res|
|
53
|
+
hook_branch = hook_data['ref'].split('/')[-1] if hook_data['ref']
|
54
|
+
|
55
|
+
if hook_data['zen']
|
56
|
+
info "Received ping for hook '#{hook_data['hook_id']}'."
|
57
|
+
warn 'Not triggering a workflow since this is not a push.'
|
58
|
+
elsif hook_branch != branch && github_event == 'push'
|
59
|
+
warn 'Incorrect branch on hook'
|
60
|
+
warn "expected: '#{branch}', got: '#{hook_branch}'"
|
61
|
+
else
|
62
|
+
access_query = "access_token=#{api_key}"
|
63
|
+
|
64
|
+
info 'Getting the Archive URL of the repo'
|
65
|
+
begin
|
66
|
+
repo_reference = {
|
67
|
+
user: username,
|
68
|
+
repo: repo
|
69
|
+
}
|
70
|
+
github_repo = github.repos.get(repo_reference)
|
71
|
+
archive_url_template = github_repo.archive_url
|
72
|
+
uri_string = archive_url_template
|
73
|
+
.sub('{archive_format}', 'zipball')
|
74
|
+
.sub('{/ref}', "/#{branch}")
|
75
|
+
download_ref_uri = URI(uri_string)
|
76
|
+
rescue => ex
|
77
|
+
fail 'Failed to get archive URL from Github', exception: ex
|
78
|
+
end
|
79
|
+
|
80
|
+
info "Downloading the repo from Github (#{download_ref_uri})"
|
81
|
+
begin
|
82
|
+
client = Net::HTTP.new(
|
83
|
+
download_ref_uri.host,
|
84
|
+
download_ref_uri.port)
|
85
|
+
client.use_ssl = true
|
86
|
+
download_uri = "#{download_ref_uri.path}?#{access_query}"
|
87
|
+
response = client.get(download_uri)
|
88
|
+
location = response['location']
|
89
|
+
trailing = location.include?('?') ? '&' : '?'
|
90
|
+
content_uri = "#{location}#{trailing}#{access_query}"
|
91
|
+
hook_data['content'] = URI(content_uri)
|
92
|
+
rescue => ex
|
93
|
+
fail 'Failed to download the repo from Github', exception: ex
|
94
|
+
end
|
95
|
+
|
96
|
+
start_workflow hook_data
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
info 'Checking for existing hook'
|
102
|
+
begin
|
103
|
+
hook = github.repos.hooks.list(username, repo).find do |h|
|
104
|
+
h['config'] && h['config']['url'] && h['config']['url'] == hook_url
|
105
|
+
end
|
106
|
+
rescue => ex
|
107
|
+
fail "Couldn't get list of existing hooks. Check username/repo."
|
108
|
+
end
|
109
|
+
|
110
|
+
unless hook
|
111
|
+
info "Creating hook to '#{hook_url}' on #{username}/#{repo}."
|
112
|
+
begin
|
113
|
+
github_config = {
|
114
|
+
'url' => hook_url,
|
115
|
+
'content_type' => 'json'
|
116
|
+
}
|
117
|
+
github_settings = {
|
118
|
+
'name' => 'web',
|
119
|
+
'active' => true,
|
120
|
+
'config' => github_config,
|
121
|
+
'events' => github_event
|
122
|
+
}
|
123
|
+
repo_hooks = github.repos.hooks
|
124
|
+
hook = repo_hooks.create(username, repo, github_settings)
|
125
|
+
rescue => ex
|
126
|
+
fail 'Hook creation in Github failed', exception: ex
|
127
|
+
end
|
128
|
+
info "Created hook with id '#{hook.id}'"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
stop do |params|
|
133
|
+
api_key = params['api_key']
|
134
|
+
username = params['username']
|
135
|
+
repo = params['repo']
|
136
|
+
branch = params['branch']
|
137
|
+
|
138
|
+
if repo
|
139
|
+
username, repo = repo.split('/') if repo.include?('/') && !username
|
140
|
+
repo, branch = repo.split('#') if repo.include?('#') && !branch
|
141
|
+
branch ||= 'master'
|
142
|
+
end
|
143
|
+
|
144
|
+
fail 'API Key is required' unless api_key
|
145
|
+
fail 'Username is required' unless username
|
146
|
+
fail 'Repo is required' unless repo
|
147
|
+
|
148
|
+
hook_url = get_web_hook('post_receive')
|
149
|
+
|
150
|
+
info 'Connecting to Github'
|
151
|
+
begin
|
152
|
+
github = Github.new oauth_token: api_key
|
153
|
+
rescue
|
154
|
+
fail 'Connection failed. Try re-activating Github in the sevices page.'
|
155
|
+
end
|
156
|
+
|
157
|
+
info 'Pulling up the hook info from Github'
|
158
|
+
begin
|
159
|
+
hooks = github.repos.hooks.list(username, repo)
|
160
|
+
hook = hooks.find do |h|
|
161
|
+
h['config'] && h['config']['url'] && h['config']['url'] == hook_url
|
162
|
+
end
|
163
|
+
rescue
|
164
|
+
fail 'Getting info about the hook from Github failed'
|
165
|
+
end
|
166
|
+
|
167
|
+
fail "Hook wasn't found." unless hook
|
168
|
+
|
169
|
+
info 'Deleting hook'
|
170
|
+
begin
|
171
|
+
github.repos.hooks.delete username, repo, hook.id
|
172
|
+
rescue
|
173
|
+
fail 'Deleting hook failed'
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
action 'download' do |params|
|
180
|
+
api_key = params['api_key']
|
181
|
+
username = params['username']
|
182
|
+
repo = params['repo']
|
183
|
+
branch = params['branch']
|
184
|
+
|
185
|
+
if repo
|
186
|
+
username, repo = repo.split('/') if repo.include?('/') && !username
|
187
|
+
repo, branch = repo.split('#') if repo.include?('#') && !branch
|
188
|
+
branch ||= 'master'
|
189
|
+
end
|
190
|
+
|
191
|
+
fail 'Repo must be defined' unless repo
|
192
|
+
fail 'API Key must be defined' unless api_key
|
193
|
+
fail 'Username must be define' unless username
|
194
|
+
|
195
|
+
info 'Connecting to Github'
|
196
|
+
begin
|
197
|
+
github = Github.new oauth_token: api_key
|
198
|
+
rescue
|
199
|
+
fail 'Failed to connect to github. Try re-activating Github service.'
|
200
|
+
end
|
201
|
+
|
202
|
+
info 'Getting the Archive URL of the repo'
|
203
|
+
begin
|
204
|
+
repo_reference = {
|
205
|
+
user: username,
|
206
|
+
repo: repo
|
207
|
+
}
|
208
|
+
github_repo = github.repos.get(repo_reference)
|
209
|
+
archive_url_template = github_repo.archive_url
|
210
|
+
uri_string = archive_url_template
|
211
|
+
.sub('{archive_format}', 'zipball')
|
212
|
+
.sub('{/ref}', "/#{branch}")
|
213
|
+
download_ref_uri = URI(uri_string)
|
214
|
+
rescue => ex
|
215
|
+
fail 'Failed to get archive URL from Github', exception: ex
|
216
|
+
end
|
217
|
+
|
218
|
+
info 'Downloading the repo from Github'
|
219
|
+
begin
|
220
|
+
client = Net::HTTP.new(
|
221
|
+
download_ref_uri.host,
|
222
|
+
download_ref_uri.port)
|
223
|
+
client.use_ssl = true
|
224
|
+
access_query = "access_token=#{api_key}"
|
225
|
+
response = client.get("#{download_ref_uri.path}?#{access_query}")
|
226
|
+
uri_connector = response['location'].include?('?') ? '&' : '?'
|
227
|
+
response_data = {
|
228
|
+
content: "#{response['location']}#{uri_connector}#{access_query}"
|
229
|
+
}
|
230
|
+
rescue => ex
|
231
|
+
fail 'Failed to download the repo from Github', exception: ex
|
232
|
+
end
|
233
|
+
|
234
|
+
action_callback response_data
|
235
|
+
end
|
236
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: factor-connector-github
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Maciej Skierkowski
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-09-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: github_api
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.11.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.11.3
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: factor-connector-api
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.0.1
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.0.1
|
46
|
+
description:
|
47
|
+
email:
|
48
|
+
- maciej@factor.io
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- lib/factor/connector/github.rb
|
54
|
+
homepage: https://factor.io
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.8.25
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: BitBalloon Factor.io Connector
|
78
|
+
test_files: []
|