Module: Percy::Client::Environment

Defined in:
lib/percy/client/environment.rb

Defined Under Namespace

Classes: BranchNotFoundError, 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

.branchObject

The name of the target branch that the build will be compared against.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/percy/client/environment.rb', line 59

def self.branch
  return ENV['PERCY_BRANCH'] if ENV['PERCY_BRANCH']

  result = case current_ci
  when :jenkins
    ENV['ghprbTargetBranch']
  when :travis
    ENV['TRAVIS_BRANCH']
  when :circle
    ENV['CIRCLE_BRANCH']
  when :codeship
    ENV['CI_BRANCH']
  else
    # Discover from current git repo branch name.
    `git rev-parse --abbrev-ref HEAD`.strip
  end
  if result == ''
    raise Percy::Client::Environment::BranchNotFoundError.new('No target branch found.')
  end
  result
end

.commitObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/percy/client/environment.rb', line 43

def self.commit
  format = GIT_FORMAT_LINES.join('%n')  # "git show" format uses %n for newlines.
  output = `git show --quiet #{commit_sha} --format="#{format}"`.strip
  data = {
    sha: output.match(/COMMIT_SHA:(.*)/)[1],
    branch: branch,
    committed_at: output.match(/COMMITTED_DATE:(.*)/)[1],
    author_name: output.match(/AUTHOR_NAME:(.*)/)[1],
    author_email: output.match(/AUTHOR_EMAIL:(.*)/)[1],
    committer_name: output.match(/COMMITTER_NAME:(.*)/)[1],
    committer_email: output.match(/COMMITTER_EMAIL:(.*)/)[1],
    message: output.match(/COMMIT_MESSAGE:(.*)/m)[1],
  }
end

.commit_shaObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/percy/client/environment.rb', line 26

def self.commit_sha
  return ENV['PERCY_COMMIT'] if ENV['PERCY_COMMIT']

  case current_ci
  when :jenkins
    ENV['ghprbActualCommit']
  when :travis
    ENV['TRAVIS_COMMIT']
  when :circle
    ENV['CIRCLE_SHA1']
  when :codeship
    ENV['CI_COMMIT_ID']
  else
    'HEAD'
  end
end

.current_ciObject



19
20
21
22
23
24
# File 'lib/percy/client/environment.rb', line 19

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'
end

.pull_request_numberObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/percy/client/environment.rb', line 99

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.
  end
end

.repoObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/percy/client/environment.rb', line 81

def self.repo
  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']}"
  else
    origin_url = `git config --get remote.origin.url`
    if origin_url == ''
      raise Percy::Client::Environment::RepoNotFoundError.new('No local git repository found.')
    end
    match = origin_url.match(Regexp.new('[:/]([^/]+\/[^/]+)\.git'))
    match[1]
  end
end