Class: TerraspaceCiGithub::Vars

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

Constant Summary collapse

COMMIT_PATTERN =
%r{Merge pull request #(\d) from (.*)}

Instance Method Summary collapse

Methods inherited from Base

#client, #github_token?

Instance Method Details

#branch_nameObject



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_urlObject



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_idObject



60
61
62
# File 'lib/terraspace_ci_github/vars.rb', line 60

def build_id
  ENV['GITHUB_RUN_ID']
end

#build_typeObject



87
88
89
# File 'lib/terraspace_ci_github/vars.rb', line 87

def build_type
  ENV['GITHUB_EVENT_NAME']
end

#build_urlObject



56
57
58
# File 'lib/terraspace_ci_github/vars.rb', line 56

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

#commit_messageObject



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

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
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: commit_message,
    build_id: build_id,
    build_number: ENV['GITHUB_RUN_NUMBER'],
  }
end

#full_repoObject



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

def full_repo
  ENV['GITHUB_REPOSITORY']
end

#hostObject



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

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



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_numberObject



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 = commit_message.match(COMMIT_PATTERN)
    md[1] # number
  end
end

#pr_urlObject



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 = commit_message.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

#shaObject



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