Module: Cucumber::CiEnvironment

Extended by:
VariableExpression
Defined in:
lib/cucumber/ci_environment.rb,
lib/cucumber/ci_environment/variable_expression.rb

Defined Under Namespace

Modules: VariableExpression

Constant Summary collapse

CI_ENVIRONMENTS_PATH =
File.join(File.dirname(__FILE__), 'ci_environment/CiEnvironments.json')

Class Method Summary collapse

Methods included from VariableExpression

evaluate, get_value

Class Method Details

.detect(ci_environment, env) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cucumber/ci_environment.rb', line 24

def detect(ci_environment, env)
  url = evaluate(ci_environment['url'], env)
  return nil if url.nil?

  result = {
    name: ci_environment['name'],
    url: url,
    buildNumber: evaluate(ci_environment['buildNumber'], env)
  }

  detected_git = detect_git(ci_environment, env)
  result[:git] = detected_git if detected_git
  result
end

.detect_ci_environment(env) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/cucumber/ci_environment.rb', line 14

def detect_ci_environment(env)
  ci_environments = JSON.parse(File.read(CI_ENVIRONMENTS_PATH))
  ci_environments.each do |ci_environment|
    detected = detect(ci_environment, env)
    return detected unless detected.nil?
  end

  nil
end

.detect_git(ci_environment, env) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cucumber/ci_environment.rb', line 39

def detect_git(ci_environment, env)
  revision = detect_revision(ci_environment, env)
  return nil if revision.nil?

  remote = evaluate(ci_environment['git']['remote'], env)
  return nil if remote.nil?

  git_info = {
    remote: remove_userinfo_from_url(remote),
    revision: revision
  }

  tag = evaluate(ci_environment['git']['tag'], env)
  branch = evaluate(ci_environment['git']['branch'], env)
  git_info[:tag] = tag if tag
  git_info[:branch] = branch if branch
  git_info
end

.detect_revision(ci_environment, env) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/cucumber/ci_environment.rb', line 58

def detect_revision(ci_environment, env)
  return evaluate(ci_environment['git']['revision'], env) unless env['GITHUB_EVENT_NAME'] == 'pull_request'

  raise StandardError('GITHUB_EVENT_PATH not set') unless env['GITHUB_EVENT_PATH']

  event = JSON.parse(File.read(env['GITHUB_EVENT_PATH']))
  event.dig('pull_request', 'head', 'sha').tap do |revision|
    raise StandardError("Could not find .pull_request.head.sha in GITHUB_EVENT_PATH:\n#{JSON.pretty_generate(event)}") if revision.nil?
  end
end

.remove_userinfo_from_url(value) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cucumber/ci_environment.rb', line 69

def remove_userinfo_from_url(value)
  return nil if value.nil?

  begin
    uri = URI(value)
    uri.userinfo = ''
    uri.to_s
  rescue StandardError
    value
  end
end