Class: TerraspaceCiGithub::Vars

Inherits:
Base
  • Object
show all
Defined in:
lib/terraspace_ci_github/vars.rb

Instance Method Summary collapse

Methods inherited from Base

#client, #github_token?

Instance Method Details

#branch_nameObject



66
67
68
69
70
71
72
# File 'lib/terraspace_ci_github/vars.rb', line 66

def branch_name
  if build_type == "pull_request"
    pr.dig('pull_request','head','ref')
  else # push
    ENV['GITHUB_REF_NAME']
  end
end

#build_idObject



36
37
38
# File 'lib/terraspace_ci_github/vars.rb', line 36

def build_id
  ENV['GITHUB_RUN_ID']
end

#build_typeObject



62
63
64
# File 'lib/terraspace_ci_github/vars.rb', line 62

def build_type
  ENV['GITHUB_EVENT_NAME']
end

#build_urlObject



32
33
34
# File 'lib/terraspace_ci_github/vars.rb', line 32

def build_url
  "#{host}/#{full_repo}/actions/runs/#{build_id}"
end

#commit_messageObject



42
43
44
45
46
47
48
# File 'lib/terraspace_ci_github/vars.rb', line 42

def commit_message
  return unless github_token?
  resp = client.commit(full_repo, sha)
  resp['commit']['message']
rescue Octokit::Unauthorized => e
  puts "WARN: #{e.message}. Error getting commit message. Please double check your github token"
end

#dataObject

Hash of properties to store



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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,
    # 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: commit_message,
    build_id: build_id,
    build_number: ENV['GITHUB_RUN_NUMBER'],
  }
end

#full_repoObject



50
51
52
# File 'lib/terraspace_ci_github/vars.rb', line 50

def full_repo
  ENV['GITHUB_REPOSITORY']
end

#hostObject



24
25
26
# File 'lib/terraspace_ci_github/vars.rb', line 24

def host
  ENV['GITHUB_SERVER_URL'] || 'https://github.com'
end

#prObject

GitHub webhook JSON payload in file and path is set in GITHUB_EVENT_PATH



75
76
77
78
# File 'lib/terraspace_ci_github/vars.rb', line 75

def pr
  return {} unless ENV['GITHUB_EVENT_PATH']
  JSON.load(IO.read(ENV['GITHUB_EVENT_PATH']))
end

#pr_urlObject



28
29
30
# File 'lib/terraspace_ci_github/vars.rb', line 28

def pr_url
  "#{host}/#{full_repo}/pull/#{pr['number']}" if pr['number']
end

#shaObject



54
55
56
57
58
59
60
# File 'lib/terraspace_ci_github/vars.rb', line 54

def sha
  if build_type == "pull_request"
    pr.dig('pull_request','head','sha')
  else # push
    ENV['GITHUB_SHA']
  end
end