class CodeBuildNotifier::CurrentBuild

Attributes

author_email[R]

attrs from git info

author_name[R]

attrs from git info

build_id[R]
commit_hash[R]
commit_message_subject[R]

attrs from git info

committer_email[R]

attrs from git info

committer_name[R]

attrs from git info

git_repo_url[R]
previous_build[RW]
short_hash[R]

attrs from git info

source_version[R]
start_time[R]
status_code[R]
trigger[R]

Public Class Methods

new( build_id: ENV['CODEBUILD_BUILD_ID'], commit_hash: ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'], git_repo: ENV['CODEBUILD_SOURCE_REPO_URL'], head_ref: ENV['CODEBUILD_WEBHOOK_HEAD_REF'], start_time: ENV['CODEBUILD_START_TIME'], status_code: ENV['CODEBUILD_BUILD_SUCCEEDING'], source_version: ENV['CODEBUILD_SOURCE_VERSION'], trigger: ENV['CODEBUILD_WEBHOOK_TRIGGER'] ) click to toggle source

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

# File lib/codebuild-notifier/current_build.rb, line 30
def initialize(
  build_id: ENV['CODEBUILD_BUILD_ID'],
  commit_hash: ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'],
  git_repo: ENV['CODEBUILD_SOURCE_REPO_URL'],
  head_ref: ENV['CODEBUILD_WEBHOOK_HEAD_REF'],
  start_time: ENV['CODEBUILD_START_TIME'],
  status_code: ENV['CODEBUILD_BUILD_SUCCEEDING'],
  source_version: ENV['CODEBUILD_SOURCE_VERSION'],
  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/, '')
  @head_ref = head_ref
  @source_version = source_version
  @start_time = start_time || (Time.now.to_f * 1_000).to_i
  @status_code = status_code
  @trigger = trigger

  @short_hash, @author_name, @author_email,
    @committer_name, @committer_email,
    @commit_message_subject = git_info
end

Public Instance Methods

branch_name() click to toggle source

If launched via retry, the webhook head ref env var is blank, but if the previous build for this branch has been located, the branch_name of that build is the same as for this build

# File lib/codebuild-notifier/current_build.rb, line 58
def branch_name
  if launched_by_retry?
    previous_build&.branch_name
  else
    @head_ref.to_s.gsub(%r{^refs/heads/}, '')
  end
end
for_pr?() click to toggle source
# File lib/codebuild-notifier/current_build.rb, line 80
def for_pr?
  %r{^pr/}.match?(source_version.to_s)
end
history_fields() click to toggle source
# File lib/codebuild-notifier/current_build.rb, line 114
def history_fields
  {
    author_email: author_email,
    author_name: author_name,
    build_id: build_id,
    commit_hash: commit_hash,
    commit_subject: commit_message_subject,
    committer_email: committer_email,
    committer_name: committer_name,
    git_repo_url: git_repo_url,
    project_code: project_code,
    start_time: start_time,
    status: status
  }
end
launched_by_retry?() click to toggle source

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

# File lib/codebuild-notifier/current_build.rb, line 76
def launched_by_retry?
  trigger.to_s.empty?
end
project_code() click to toggle source
# File lib/codebuild-notifier/current_build.rb, line 70
def project_code
  @project_code ||= build_id.split(':').first
end
source_id() click to toggle source

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.

# File lib/codebuild-notifier/current_build.rb, line 92
def source_id
  # If launched via retry, trigger is blank, but if the previous
  # build for this branch has been located, the source_id of that
  # build is the same as for this build
  if launched_by_retry?
    previous_build&.source_id
  else
    "#{project_code}:#{trigger}"
  end
end
source_ref() click to toggle source
# File lib/codebuild-notifier/current_build.rb, line 103
def source_ref
  # If launched via retry, trigger is blank, but if the previous
  # build for this branch has been located, the source_ref of that
  # build is the same as for this build
  if launched_by_retry?
    previous_build&.source_ref
  else
    trigger
  end
end
status() click to toggle source
# File lib/codebuild-notifier/current_build.rb, line 66
def status
  status_code.to_s == '1' ? 'SUCCEEDED' : 'FAILED'
end

Private Instance Methods

git_info() click to toggle source
# File lib/codebuild-notifier/current_build.rb, line 130
        def git_info
  Git.current_commit
end