This repository has been archived by the owner on Jan 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathci-bundle-update.rb
159 lines (130 loc) · 3.73 KB
/
ci-bundle-update.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
require 'circleci'
require 'httparty'
module CiBundleUpdate
class Base
def initialize(token, exec_days, raw_extra_build_parameters)
@exec_days = exec_days
@raw_extra_build_parameters = raw_extra_build_parameters
post_initialize(token, exec_days)
end
def build
raise NotImplementedError
end
private
attr_reader :exec_days, :raw_extra_build_parameters
def post_initialize(_token, _exec_days)
nil
end
def extra_build_parameters
raise NotImplementedError
end
def skip?
exec_days &&
! exec_days.split(',').include?(Time.now.strftime('%a'))
end
end
class CircleCi < Base
def build(username, reponame, branch)
if skip?
puts "This build was skipped for $EXEC_DAYS (#{exec_days})"
return
end
response = ::CircleCi::Project.new(username, reponame).build_branch(
branch,
{},
build_parameters: { 'BUNDLE_UPDATE' => true }.merge(extra_build_parameters)
)
if response.success?
puts "This build was accepted"
else
puts "This build was not accepted for #{response.body}"
end
end
private
def post_initialize(token, _exec_days)
::CircleCi.configure do |config|
config.token = token
end
end
def extra_build_parameters
@extra_build_parameters ||=
begin
ary = (raw_extra_build_parameters || "")
.split(',')
.map{|e| e.split('=')}
.flatten
Hash[*ary]
end
end
end
class Wercker < Base
include HTTParty
base_uri 'https://app.wercker.com/api/v3'
def build(username, reponame, branch)
if skip?
puts "This build was skipped for $EXEC_DAYS (#{exec_days})"
return
end
application = get_application(username, reponame)
build_pipeline = get_build_pipeline(application['id'], branch)
raise 'build pipeline not found.' unless build_pipeline
run_build(build_pipeline['id'], branch)
end
private
def post_initialize(token, _exec_days)
@headers = {
Authorization: "Bearer #{token}",
'Content-type': 'application/json',
}
end
def get_application(username, reponame)
self.class.get("/applications/#{username}/#{reponame}", headers: @headers).parsed_response
end
def get_build_pipeline(application_id, branch)
query = {
applicationId: application_id,
branch: branch,
}
runs = self.class.get('/runs', query: query, headers: @headers).parsed_response
run = runs.find { |run| run['pipeline']['name'] == 'build' }
run['pipeline'] if run
end
def run_build(pipeline_id, branch)
body = {
pipelineId: pipeline_id,
branch: branch,
envVars: [
{ key: 'BUNDLE_UPDATE', value: 'true' },
*extra_build_parameters
],
}
self.class.post('/runs', body: body.to_json, headers: @headers)
end
def extra_build_parameters
@extra_build_parameters ||=
(raw_extra_build_parameters || "")
.split(',')
.map{|e| e.split('=')}
.map{|k,v| {key: k, value: v}}
end
end
end
if $0 == __FILE__
require 'optparse'
options = ARGV.getopts(nil, 'ci:circle_ci')
case options['ci']
when 'circle_ci'
ci_bundle_update = CiBundleUpdate::CircleCi.new(
ENV['CIRCLECI_TOKEN'],
ENV['EXEC_DAYS'],
ENV['EXTRA_BUILD_PARAMETERS']
)
when 'wercker'
ci_bundle_update = CiBundleUpdate::Wercker.new(
ENV['WERCKER_TOKEN'],
ENV['EXEC_DAYS'],
ENV['EXTRA_BUILD_PARAMETERS']
)
end
ci_bundle_update.build(ENV['GITHUB_USERNAME'], ENV['GITHUB_REPONAME'], ENV['BRANCH'])
end