Module: PactBroker::Client::Git
- Defined in:
- lib/pact_broker/client/git.rb
Constant Summary collapse
- COMMAND =
'git rev-parse --abbrev-ref HEAD'.freeze
- COMMIT_COMMAND =
'git rev-parse HEAD'.freeze
- BRANCH_ENV_VAR_NAMES =
%w{GITHUB_HEAD_REF GITHUB_REF BUILDKITE_BRANCH CIRCLE_BRANCH TRAVIS_BRANCH GIT_BRANCH GIT_LOCAL_BRANCH APPVEYOR_REPO_BRANCH CI_COMMIT_REF_NAME BITBUCKET_BRANCH BUILD_SOURCEBRANCHNAME CIRRUS_BRANCH}.freeze
- COMMIT_ENV_VAR_NAMES =
%w{GITHUB_SHA BUILDKITE_COMMIT CIRCLE_SHA1 TRAVIS_COMMIT GIT_COMMIT APPVEYOR_REPO_COMMIT CI_COMMIT_ID BITBUCKET_COMMIT BUILD_SOURCEVERSION CIRRUS_CHANGE_IN_REPO}
- BUILD_URL_ENV_VAR_NAMES =
%w{BUILDKITE_BUILD_URL CIRCLE_BUILD_URL TRAVIS_BUILD_WEB_URL BUILD_URL }
Class Method Summary collapse
- .branch(options) ⇒ Object
- .branch_from_git_command(raise_error) ⇒ Object
-
.build_url ⇒ Object
This does not belong in the Git module.
- .commit(options) ⇒ Object
- .commit_from_git_command(raise_error) ⇒ Object
- .execute_and_parse_command(raise_error) ⇒ Object
- .execute_git_command ⇒ Object
- .execute_git_commit_command(raise_error) ⇒ Object
- .find_branch_from_env_var_ending_with_branch ⇒ Object
- .find_branch_from_known_env_vars ⇒ Object
-
.find_commit_from_env_vars ⇒ Object
private.
- .github_build_url ⇒ Object
- .validate_branch_names(branch_names) ⇒ Object
- .value_from_env_var(env_var_name) ⇒ Object
Class Method Details
.branch(options) ⇒ Object
37 38 39 |
# File 'lib/pact_broker/client/git.rb', line 37 def self.branch() find_branch_from_known_env_vars || find_branch_from_env_var_ending_with_branch || branch_from_git_command([:raise_error]) end |
.branch_from_git_command(raise_error) ⇒ Object
78 79 80 81 82 |
# File 'lib/pact_broker/client/git.rb', line 78 def self.branch_from_git_command(raise_error) branch_names = execute_and_parse_command(raise_error) validate_branch_names(branch_names) if raise_error branch_names.size == 1 ? branch_names[0] : nil end |
.build_url ⇒ Object
This does not belong in the Git module. TODO move it.
43 44 45 |
# File 'lib/pact_broker/client/git.rb', line 43 def self.build_url github_build_url || BUILD_URL_ENV_VAR_NAMES.collect{ | name | value_from_env_var(name) }.compact.first end |
.commit(options) ⇒ Object
33 34 35 |
# File 'lib/pact_broker/client/git.rb', line 33 def self.commit() find_commit_from_env_vars || commit_from_git_command([:raise_error]) end |
.commit_from_git_command(raise_error) ⇒ Object
84 85 86 |
# File 'lib/pact_broker/client/git.rb', line 84 def self.commit_from_git_command(raise_error) execute_git_commit_command(raise_error) end |
.execute_and_parse_command(raise_error) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/pact_broker/client/git.rb', line 113 def self.execute_and_parse_command(raise_error) execute_git_command .split("\n") .collect(&:strip) .reject(&:empty?) .collect(&:split) .collect(&:first) .collect{ |line| line.gsub(/^origin\//, '') } .reject{ |line| line == "HEAD" } rescue StandardError => e if raise_error raise PactBroker::Client::Error, "Could not determine current git branch using command `#{COMMAND}`. #{e.class} #{e.}" else return [] end end |
.execute_git_command ⇒ Object
98 99 100 |
# File 'lib/pact_broker/client/git.rb', line 98 def self.execute_git_command `#{COMMAND}` end |
.execute_git_commit_command(raise_error) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/pact_broker/client/git.rb', line 102 def self.execute_git_commit_command(raise_error) `#{COMMIT_COMMAND}` rescue StandardError => e if raise_error raise PactBroker::Client::Error, "Could not determine current git commit using command `#{COMMIT_COMMAND}`. #{e.class} #{e.}" else return nil end end |
.find_branch_from_env_var_ending_with_branch ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/pact_broker/client/git.rb', line 58 def self.find_branch_from_env_var_ending_with_branch values = ENV.keys .select{ |env_var_name| env_var_name.end_with?("_BRANCH") } .collect{ |env_var_name| value_from_env_var(env_var_name) }.compact if values.size == 1 values.first else nil end end |
.find_branch_from_known_env_vars ⇒ Object
53 54 55 56 |
# File 'lib/pact_broker/client/git.rb', line 53 def self.find_branch_from_known_env_vars val = BRANCH_ENV_VAR_NAMES.collect { |env_var_name| value_from_env_var(env_var_name) }.compact.first val.gsub(%r{^refs/heads/}, "") if val end |
.find_commit_from_env_vars ⇒ Object
private
49 50 51 |
# File 'lib/pact_broker/client/git.rb', line 49 def self.find_commit_from_env_vars COMMIT_ENV_VAR_NAMES.collect { |env_var_name| value_from_env_var(env_var_name) }.compact.first end |
.github_build_url ⇒ Object
130 131 132 133 134 135 |
# File 'lib/pact_broker/client/git.rb', line 130 def self.github_build_url parts = %w{GITHUB_SERVER_URL GITHUB_REPOSITORY GITHUB_RUN_ID}.collect{ | name | value_from_env_var(name) } if parts.all? [parts[0], parts[1], "actions", "runs", parts[2]].join("/") end end |
.validate_branch_names(branch_names) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/pact_broker/client/git.rb', line 88 def self.validate_branch_names(branch_names) if branch_names.size == 0 raise PactBroker::Client::Error, "Command `#{COMMAND}` didn't return anything that could be identified as the current branch." end if branch_names.size > 1 raise PactBroker::Client::Error, "Command `#{COMMAND}` returned multiple branches: #{branch_names.join(", ")}. You will need to get the branch name another way." end end |
.value_from_env_var(env_var_name) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/pact_broker/client/git.rb', line 69 def self.value_from_env_var(env_var_name) val = ENV[env_var_name] if val && val.strip.size > 0 val else nil end end |