Class: TerraspaceCiAzure::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/terraspace_ci_azure/interface.rb

Instance Method Summary collapse

Instance Method Details

#base_project_urlObject

removes the user@ part IE: BUILD_REPOSITORY_URI=[email protected]/tongueroo/infra-project/_git/infra-ci



57
58
59
60
61
# File 'lib/terraspace_ci_azure/interface.rb', line 57

def base_project_url
  uri = URI(ENV['BUILD_REPOSITORY_URI'])
  base_path = uri.path.split('/')[0..2].join('/')
  "#{uri.scheme}://#{uri.host}#{base_path}"
end

#base_repo_urlObject



63
64
65
66
# File 'lib/terraspace_ci_azure/interface.rb', line 63

def base_repo_url
  uri = URI(ENV['BUILD_REPOSITORY_URI'])
  "#{uri.scheme}://#{uri.host}#{uri.path}"
end

#branch_nameObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/terraspace_ci_azure/interface.rb', line 28

def branch_name
  if pr_number
    message = ENV['BUILD_SOURCEVERSIONMESSAGE']
    md = message.match(/Merge pull request \d+ from (.*) into (.*)/)
    if md
      # IE: BUILD_SOURCEVERSIONMESSAGE=Merge pull request 2 from feature into main
      # Its a bit weird but with azure repos with check policy trigger
      md[1]
    else # GitHub and Bitbucket PR has actual branch though
      # IE: SYSTEM_PULLREQUEST_SOURCEBRANCH=feature
      message
    end
  else # push
    ENV['BUILD_SOURCEBRANCHNAME']
  end
end

#branch_urlObject



110
111
112
# File 'lib/terraspace_ci_azure/interface.rb', line 110

def branch_url
  "#{base_repo_url}?version=GB#{branch_name}"
end

#build_idObject



85
86
87
# File 'lib/terraspace_ci_azure/interface.rb', line 85

def build_id
  ENV['BUILD_BUILDID']
end

#build_typeObject



76
77
78
# File 'lib/terraspace_ci_azure/interface.rb', line 76

def build_type
  ENV['SYSTEM_PULLREQUEST_PULLREQUESTID'] ? 'pull_request' : 'push'
end

#build_urlObject



99
100
101
102
# File 'lib/terraspace_ci_azure/interface.rb', line 99

def build_url
  return unless build_id
  "#{base_project_url}/_build/results?buildId=#{build_id}&view=results"
end

#commit_urlObject



105
106
107
# File 'lib/terraspace_ci_azure/interface.rb', line 105

def commit_url
  "#{base_repo_url}/commit/#{sha}"
end

#full_repoObject



69
70
71
72
73
74
# File 'lib/terraspace_ci_azure/interface.rb', line 69

def full_repo
  uri = URI(ENV['BUILD_REPOSITORY_URI'])
  org = uri.path.split('/')[1] # since there's a leading /
  repo = ENV['BUILD_REPOSITORY_NAME'] # tongueroo
  "#{org}/#{repo}"
end

#hostObject



50
51
52
53
# File 'lib/terraspace_ci_azure/interface.rb', line 50

def host
  uri = URI(ENV['BUILD_REPOSITORY_URI'])
  "#{uri.scheme}://#{uri.host}"
end

#pr_numberObject

IE: SYSTEM_PULLREQUEST_PULLREQUESTID=2



81
82
83
# File 'lib/terraspace_ci_azure/interface.rb', line 81

def pr_number
  ENV['SYSTEM_PULLREQUEST_PULLREQUESTID']
end

#pr_urlObject



91
92
93
94
95
# File 'lib/terraspace_ci_azure/interface.rb', line 91

def pr_url
  return unless pr_number
  uri = URI(ENV['BUILD_REPOSITORY_URI'])
  "#{base_repo_url}/pullrequest/#{pr_number}"
end

#shaObject



45
46
47
# File 'lib/terraspace_ci_azure/interface.rb', line 45

def sha
  ENV['BUILD_SOURCEVERSION']
end

#varsObject

Interface method. Returns Hash of properties.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/terraspace_ci_azure/interface.rb', line 6

def vars
  {
    build_system: "azure",
    host: host,
    full_repo: full_repo,
    branch_name: branch_name,
    # urls
    pr_url: pr_url,
    build_url: build_url,
    branch_url: branch_url,
    commit_url: commit_url,
    # additional properties
    build_type: build_type,
    pr_number: pr_number,
    sha: sha,
    # additional properties
    # commit_message: ENV['REPLACE_ME'],
    build_id: build_id,
    build_number: ENV['BUILD_BUILDNUMBER'], # IE: BUILD_BUILDNUMBER=20220715.12
  }
end