Class: CodeBuildNotifier::CurrentBuild

Inherits:
Object
  • Object
show all
Defined in:
lib/codebuild-notifier/current_build.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(build_id: ENV['CODEBUILD_BUILD_ID'], commit_hash: ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'], git_repo: ENV['CODEBUILD_SOURCE_REPO_URL'], status_code: ENV['CODEBUILD_BUILD_SUCCEEDING'], trigger: ENV['CODEBUILD_WEBHOOK_TRIGGER']) ⇒ CurrentBuild

Default values are extracted from CODEBUILD_* ENV vars present in each CodeBuild # job container.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/codebuild-notifier/current_build.rb', line 24

def initialize(
  build_id: ENV['CODEBUILD_BUILD_ID'],
  commit_hash: ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'],
  git_repo: ENV['CODEBUILD_SOURCE_REPO_URL'],
  status_code: ENV['CODEBUILD_BUILD_SUCCEEDING'],
  trigger: ENV['CODEBUILD_WEBHOOK_TRIGGER']
)
  @build_id = build_id
  @commit_hash = commit_hash
  # Handle repos specified with and without optional .git suffix.
  @git_repo_url = git_repo.to_s.gsub(/\.git\z/, '')
  @status_code = status_code
  @trigger = trigger
end

Instance Attribute Details

#build_idObject (readonly)

Returns the value of attribute build_id.



20
21
22
# File 'lib/codebuild-notifier/current_build.rb', line 20

def build_id
  @build_id
end

#commit_hashObject (readonly)

Returns the value of attribute commit_hash.



20
21
22
# File 'lib/codebuild-notifier/current_build.rb', line 20

def commit_hash
  @commit_hash
end

#git_repo_urlObject (readonly)

Returns the value of attribute git_repo_url.



20
21
22
# File 'lib/codebuild-notifier/current_build.rb', line 20

def git_repo_url
  @git_repo_url
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



20
21
22
# File 'lib/codebuild-notifier/current_build.rb', line 20

def status_code
  @status_code
end

#triggerObject (readonly)

Returns the value of attribute trigger.



20
21
22
# File 'lib/codebuild-notifier/current_build.rb', line 20

def trigger
  @trigger
end

Instance Method Details

#for_pr?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/codebuild-notifier/current_build.rb', line 53

def for_pr?
  %r{^pr/}.match?(trigger.to_s)
end

#launched_by_retry?Boolean

If trigger is empty, this build was launched using the Retry command from the console or api.

Returns:

  • (Boolean)


49
50
51
# File 'lib/codebuild-notifier/current_build.rb', line 49

def launched_by_retry?
  trigger.to_s.empty?
end

#project_codeObject



43
44
45
# File 'lib/codebuild-notifier/current_build.rb', line 43

def project_code
  @project_code ||= build_id.split(':').first
end

#source_idObject

source_id, the primary key, is a composite of project_code and trigger. e.g.:

my-app_ruby2-4:branch/master
my-app_ruby2-3:pr/4056

project_code forms part of the key to support having repos with multiple projects, for example, with different buildspec files for different ruby versions, or for rspec vs cucumber.



65
66
67
# File 'lib/codebuild-notifier/current_build.rb', line 65

def source_id
  "#{project_code}:#{trigger}"
end

#statusObject



39
40
41
# File 'lib/codebuild-notifier/current_build.rb', line 39

def status
  status_code.to_s == '1' ? 'SUCCEEDED' : 'FAILED'
end