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',
  # Note: order is important, this must come last because the regex is a multiline match.
  'COMMIT_MESSAGE:%B'
].freeze

Class Method Summary collapse

Class Method Details

._commit_shaObject



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
    # Pull Request Builder Plugin OR Git Plugin.
    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_urlObject



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_outputObject



127
128
129
130
# File 'lib/percy/client/environment.rb', line 127

def self._raw_branch_output
  # Discover from local git repo branch name.
  `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')  # "git show" format uses %n for newlines.
  output = `git show --quiet #{commit_sha} --format="#{format}" 2> /dev/null`.strip
  return if $?.to_i != 0
  output
end

.branchObject

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
    # Note: this is very unfortunately necessary because Travis does not expose the head
    # branch, only the targeted 'master' branch in TRAVIS_BRANCH, with no way to get the
    # actual head branch of the PR. We create a fake branch name so that Percy does not
    # mistake this PR as a new master build.
    # https://github.com/travis-ci/travis-ci/issues/1633#issuecomment-194749671
    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

.commitHash

not be found.

Returns:

  • (Hash)

    All commit data from the current commit. Might be empty if commit data could



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

  # Use the specified SHA or, if not given, the parsed SHA at HEAD.
  commit_sha = _commit_sha || output && output.match(/COMMIT_SHA:(.*)/)[1]

  # If not running in a git repo, allow nils for certain commit attributes.
  extract_or_nil = lambda { |regex| (output && output.match(regex) || [])[1] }
  data = {
    # The only required attribute:
    branch: branch,
    # An optional but important attribute:
    sha: commit_sha,

    # Optional attributes:
    message: extract_or_nil.call(/COMMIT_MESSAGE:(.*)/m),
    committed_at: extract_or_nil.call(/COMMITTED_DATE:(.*)/),
    # These GIT_ environment vars are from the Jenkins Git Plugin, but could be
    # used generically. This behavior may change in the future.
    author_name: extract_or_nil.call(/AUTHOR_NAME:(.*)/) || ENV['GIT_AUTHOR_NAME'],
    author_email: extract_or_nil.call(/AUTHOR_EMAIL:(.*)/)  || ENV['GIT_AUTHOR_EMAIL'],
    committer_name: extract_or_nil.call(/COMMITTER_NAME:(.*)/) || ENV['GIT_COMMITTER_NAME'],
    committer_email: extract_or_nil.call(/COMMITTER_EMAIL:(.*)/) || ENV['GIT_COMMITTER_EMAIL'],
  }
end

.current_ciObject



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']  # Pull Request Builder plugin.
  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_nonceObject

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_shardsObject



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
    # Support for https://github.com/ArturT/knapsack
    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_numberObject



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
    # GitHub Pull Request Builder plugin.
    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
    # Unfortunately, codeship always returns 'false' for CI_PULL_REQUEST. For now, return nil.
  when :drone
    ENV['CI_PULL_REQUEST']
  when :semaphore
    ENV['PULL_REQUEST_NUMBER']
  end
end

.repoObject



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']  # Deprecated.

  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_branchObject

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