Class: TerraspaceCiGithub::Vars
- Defined in:
- lib/terraspace_ci_github/vars.rb
Constant Summary collapse
- COMMIT_PATTERN =
%r{Merge pull request #(\d) from (.*)}
Instance Method Summary collapse
- #branch_name ⇒ Object
- #branch_url ⇒ Object
- #build_id ⇒ Object
- #build_type ⇒ Object
- #build_url ⇒ Object
- #commit_message ⇒ Object
-
#data ⇒ Object
Hash of properties to store.
- #full_repo ⇒ Object
- #host ⇒ Object
-
#pr ⇒ Object
GitHub webhook JSON payload in file and path is set in GITHUB_EVENT_PATH.
- #pr_number ⇒ Object
- #pr_url ⇒ Object
- #sha ⇒ Object
Methods inherited from Base
Instance Method Details
#branch_name ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/terraspace_ci_github/vars.rb', line 91 def branch_name if build_type == "pull_request" pr.dig('pull_request','head','ref') else # push ENV['GITHUB_REF_NAME'] end end |
#branch_url ⇒ Object
29 30 31 |
# File 'lib/terraspace_ci_github/vars.rb', line 29 def branch_url "#{host}/#{full_repo}/tree/#{branch_name}" if branch_name end |
#build_id ⇒ Object
60 61 62 |
# File 'lib/terraspace_ci_github/vars.rb', line 60 def build_id ENV['GITHUB_RUN_ID'] end |
#build_type ⇒ Object
87 88 89 |
# File 'lib/terraspace_ci_github/vars.rb', line 87 def build_type ENV['GITHUB_EVENT_NAME'] end |
#build_url ⇒ Object
56 57 58 |
# File 'lib/terraspace_ci_github/vars.rb', line 56 def build_url "#{host}/#{full_repo}/actions/runs/#{build_id}" end |
#commit_message ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/terraspace_ci_github/vars.rb', line 66 def return unless github_token? resp = client.commit(full_repo, sha) resp['commit']['message'] rescue Octokit::Unauthorized => e puts "WARN: #{e.}. Error getting commit message. Please double check your github token" end |
#data ⇒ Object
Hash of properties to store
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/terraspace_ci_github/vars.rb', line 4 def data { build_system: "github", # required host: host, full_repo: full_repo, branch_name: branch_name, # urls pr_url: pr_url, build_url: build_url, branch_url: branch_url, # additional properties build_type: build_type, # required IE: pull_request or push pr_number: pr_number, # set when build_type=pull_request sha: sha, # additional properties commit_message: , build_id: build_id, build_number: ENV['GITHUB_RUN_NUMBER'], } end |
#full_repo ⇒ Object
75 76 77 |
# File 'lib/terraspace_ci_github/vars.rb', line 75 def full_repo ENV['GITHUB_REPOSITORY'] end |
#host ⇒ Object
25 26 27 |
# File 'lib/terraspace_ci_github/vars.rb', line 25 def host ENV['GITHUB_SERVER_URL'] || 'https://github.com' end |
#pr ⇒ Object
GitHub webhook JSON payload in file and path is set in GITHUB_EVENT_PATH
100 101 102 103 |
# File 'lib/terraspace_ci_github/vars.rb', line 100 def pr return {} unless ENV['GITHUB_EVENT_PATH'] JSON.load(IO.read(ENV['GITHUB_EVENT_PATH'])) end |
#pr_number ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/terraspace_ci_github/vars.rb', line 48 def pr_number if pr['number'] pr['number'] elsif md = .match(COMMIT_PATTERN) md[1] # number end end |
#pr_url ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/terraspace_ci_github/vars.rb', line 34 def pr_url if pr['number'] "#{host}/#{full_repo}/pull/#{pr['number']}" elsif md = .match(COMMIT_PATTERN) # git push commit has commit with PR info # IE: Merge pull request #4 from tongueroo/feature number = md[1] org_branch = md[2] org = org_branch.split('/').first repo = ENV['GITHUB_REPOSITORY'].split('/').last # IE: tongueroo/infra-ci "#{host}/#{org}/#{repo}/pull/#{number}" end end |
#sha ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/terraspace_ci_github/vars.rb', line 79 def sha if build_type == "pull_request" pr.dig('pull_request','head','sha') else # push ENV['GITHUB_SHA'] end end |