Module: Percy::Client::Environment
- Defined in:
- lib/percy/client/environment.rb
Defined Under Namespace
Classes: Error, RepoNotFoundError
Constant Summary
collapse
- GIT_FORMAT_LINES =
[
'COMMIT_SHA:%H',
'AUTHOR_NAME:%an',
'AUTHOR_EMAIL:%ae',
'COMMITTER_NAME:%an',
'COMMITTER_EMAIL:%ae',
'COMMITTED_DATE:%ai',
'COMMIT_MESSAGE:%B'
].freeze
Class Method Summary
collapse
Class Method Details
._commit_sha ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/percy/client/environment.rb', line 57
def self._commit_sha
return ENV['PERCY_COMMIT'] if ENV['PERCY_COMMIT']
case current_ci
when :jenkins
ENV['ghprbActualCommit'] || ENV['GIT_COMMIT']
when :travis
ENV['TRAVIS_COMMIT']
when :circle
ENV['CIRCLE_SHA1']
when :codeship
ENV['CI_COMMIT_ID']
when :drone
ENV['DRONE_COMMIT']
when :semaphore
ENV['REVISION']
end
end
|
._get_origin_url ⇒ Object
220
221
222
|
# File 'lib/percy/client/environment.rb', line 220
def self._get_origin_url
`git config --get remote.origin.url`
end
|
._raw_branch_output ⇒ Object
127
128
129
130
|
# File 'lib/percy/client/environment.rb', line 127
def self._raw_branch_output
`git rev-parse --abbrev-ref HEAD 2> /dev/null`.strip
end
|
._raw_commit_output(commit_sha) ⇒ Object
78
79
80
81
82
83
|
# File 'lib/percy/client/environment.rb', line 78
def self._raw_commit_output(commit_sha)
format = GIT_FORMAT_LINES.join('%n') output = `git show --quiet #{commit_sha} --format="#{format}" 2> /dev/null`.strip
return if $?.to_i != 0
output
end
|
.branch ⇒ Object
The name of the current branch.
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
|
# File 'lib/percy/client/environment.rb', line 86
def self.branch
return ENV['PERCY_BRANCH'] if ENV['PERCY_BRANCH']
result = case current_ci
when :jenkins
ENV['ghprbTargetBranch']
when :travis
if pull_request_number && ENV['TRAVIS_BRANCH'] == 'master'
"github-pr-#{pull_request_number}"
else
ENV['TRAVIS_BRANCH']
end
when :circle
ENV['CIRCLE_BRANCH']
when :codeship
ENV['CI_BRANCH']
when :drone
ENV['DRONE_BRANCH']
when :semaphore
ENV['BRANCH_NAME']
else
_raw_branch_output
end
if result == ''
STDERR.puts '[percy] Warning: not in a git repo, setting PERCY_BRANCH to "master".'
result = 'master'
end
result
end
|
.commit ⇒ Hash
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
|
# File 'lib/percy/client/environment.rb', line 29
def self.commit
output = _raw_commit_output(_commit_sha) if _commit_sha
output = _raw_commit_output('HEAD') if !output
commit_sha = _commit_sha || output && output.match(/COMMIT_SHA:(.*)/)[1]
= lambda { |regex| (output && output.match(regex) || [])[1] }
data = {
branch: branch,
sha: commit_sha,
message: .call(/COMMIT_MESSAGE:(.*)/m),
committed_at: .call(/COMMITTED_DATE:(.*)/),
author_name: .call(/AUTHOR_NAME:(.*)/) || ENV['GIT_AUTHOR_NAME'],
author_email: .call(/AUTHOR_EMAIL:(.*)/) || ENV['GIT_AUTHOR_EMAIL'],
committer_name: .call(/COMMITTER_NAME:(.*)/) || ENV['GIT_COMMITTER_NAME'],
committer_email: .call(/COMMITTER_EMAIL:(.*)/) || ENV['GIT_COMMITTER_EMAIL'],
}
end
|
.current_ci ⇒ Object
18
19
20
21
22
23
24
25
|
# File 'lib/percy/client/environment.rb', line 18
def self.current_ci
return :travis if ENV['TRAVIS_BUILD_ID']
return :jenkins if ENV['JENKINS_URL'] && ENV['ghprbPullId'] return :circle if ENV['CIRCLECI']
return :codeship if ENV['CI_NAME'] && ENV['CI_NAME'] == 'codeship'
return :drone if ENV['DRONE'] == 'true'
return :semaphore if ENV['SEMAPHORE'] == 'true'
end
|
.parallel_nonce ⇒ Object
A nonce which will be the same for all nodes of a parallel build, used to identify shards of the same CI build. This is usually just the CI environment build ID.
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/percy/client/environment.rb', line 185
def self.parallel_nonce
return ENV['PERCY_PARALLEL_NONCE'] if ENV['PERCY_PARALLEL_NONCE']
case current_ci
when :travis
ENV['TRAVIS_BUILD_NUMBER']
when :circle
ENV['CIRCLE_BUILD_NUM']
when :codeship
ENV['CI_BUILD_NUMBER']
when :semaphore
ENV['SEMAPHORE_BUILD_NUMBER']
end
end
|
.parallel_total_shards ⇒ Object
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/percy/client/environment.rb', line 200
def self.parallel_total_shards
return Integer(ENV['PERCY_PARALLEL_TOTAL']) if ENV['PERCY_PARALLEL_TOTAL']
case current_ci
when :circle
var = 'CIRCLE_NODE_TOTAL'
Integer(ENV[var]) if ENV[var] && !ENV[var].empty?
when :travis
var = 'CI_NODE_TOTAL'
Integer(ENV[var]) if ENV[var] && !ENV[var].empty?
when :codeship
var = 'CI_NODE_TOTAL'
Integer(ENV[var]) if ENV[var] && !ENV[var].empty?
when :semaphore
Integer(ENV['SEMAPHORE_THREAD_COUNT'])
end
end
|
.pull_request_number ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/percy/client/environment.rb', line 161
def self.pull_request_number
return ENV['PERCY_PULL_REQUEST'] if ENV['PERCY_PULL_REQUEST']
case current_ci
when :jenkins
ENV['ghprbPullId']
when :travis
ENV['TRAVIS_PULL_REQUEST'] if ENV['TRAVIS_PULL_REQUEST'] != 'false'
when :circle
if ENV['CI_PULL_REQUESTS'] && ENV['CI_PULL_REQUESTS'] != ''
ENV['CI_PULL_REQUESTS'].split('/')[-1]
end
when :codeship
when :drone
ENV['CI_PULL_REQUEST']
when :semaphore
ENV['PULL_REQUEST_NUMBER']
end
end
|
.repo ⇒ Object
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
|
# File 'lib/percy/client/environment.rb', line 133
def self.repo
return ENV['PERCY_PROJECT'] if ENV['PERCY_PROJECT']
return ENV['PERCY_REPO_SLUG'] if ENV['PERCY_REPO_SLUG']
case current_ci
when :travis
ENV['TRAVIS_REPO_SLUG']
when :circle
"#{ENV['CIRCLE_PROJECT_USERNAME']}/#{ENV['CIRCLE_PROJECT_REPONAME']}"
when :semaphore
ENV['SEMAPHORE_REPO_SLUG']
else
origin_url = _get_origin_url.strip
if origin_url == ''
raise Percy::Client::Environment::RepoNotFoundError.new(
'No local git repository found. ' +
'You can manually set PERCY_PROJECT to fix this. See https://percy.io/docs')
end
match = origin_url.match(Regexp.new('[:/]([^/]+\/[^/]+?)(\.git)?\Z'))
if !match
raise Percy::Client::Environment::RepoNotFoundError.new(
"Could not determine repository name from URL: #{origin_url.inspect}\n" +
"You can manually set PERCY_PROJECT to fix this. See https://percy.io/docs")
end
match[1]
end
end
|
.target_branch ⇒ Object
The target branch to compare against (if unset, Percy will pick master).
122
123
124
|
# File 'lib/percy/client/environment.rb', line 122
def self.target_branch
return ENV['PERCY_TARGET_BRANCH'] if ENV['PERCY_TARGET_BRANCH']
end
|