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'], 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']) ⇒ CurrentBuild

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Attribute Details

#author_emailObject (readonly)

attrs from git info



25
26
27
# File 'lib/codebuild-notifier/current_build.rb', line 25

def author_email
  @author_email
end

#author_nameObject (readonly)

attrs from git info



25
26
27
# File 'lib/codebuild-notifier/current_build.rb', line 25

def author_name
  @author_name
end

#build_idObject (readonly)

Returns the value of attribute build_id.



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

def build_id
  @build_id
end

#commit_hashObject (readonly)

Returns the value of attribute commit_hash.



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

def commit_hash
  @commit_hash
end

#commit_message_subjectObject (readonly)

attrs from git info



25
26
27
# File 'lib/codebuild-notifier/current_build.rb', line 25

def commit_message_subject
  @commit_message_subject
end

#committer_emailObject (readonly)

attrs from git info



25
26
27
# File 'lib/codebuild-notifier/current_build.rb', line 25

def committer_email
  @committer_email
end

#committer_nameObject (readonly)

attrs from git info



25
26
27
# File 'lib/codebuild-notifier/current_build.rb', line 25

def committer_name
  @committer_name
end

#git_repo_urlObject (readonly)

Returns the value of attribute git_repo_url.



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

def git_repo_url
  @git_repo_url
end

#previous_buildObject

Returns the value of attribute previous_build.



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

def previous_build
  @previous_build
end

#short_hashObject (readonly)

attrs from git info



25
26
27
# File 'lib/codebuild-notifier/current_build.rb', line 25

def short_hash
  @short_hash
end

#source_versionObject (readonly)

Returns the value of attribute source_version.



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

def source_version
  @source_version
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



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

def status_code
  @status_code
end

#triggerObject (readonly)

Returns the value of attribute trigger.



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

def trigger
  @trigger
end

Instance Method Details

#branch_nameObject

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



58
59
60
61
62
63
64
# 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?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/codebuild-notifier/current_build.rb', line 80

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

#history_fieldsObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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?Boolean

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

Returns:

  • (Boolean)


76
77
78
# File 'lib/codebuild-notifier/current_build.rb', line 76

def launched_by_retry?
  trigger.to_s.empty?
end

#project_codeObject



70
71
72
# File 'lib/codebuild-notifier/current_build.rb', line 70

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.



92
93
94
95
96
97
98
99
100
101
# 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_refObject



103
104
105
106
107
108
109
110
111
112
# 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

#statusObject



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

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