lib/codebuild-notifier/current_build.rb in codebuild-notifier-0.3.2 vs lib/codebuild-notifier/current_build.rb in codebuild-notifier-1.0.0
- old
+ new
@@ -15,30 +15,43 @@
# You should have received a copy of the GNU General Public License
# along with codebuild-notifier. If not, see <http://www.gnu.org/licenses/>.
module CodeBuildNotifier
class CurrentBuild
- attr_reader :build_id, :commit_hash, :git_repo_url, :status_code, :trigger
attr_accessor :previous_build
+ attr_reader :build_id, :commit_hash, :git_repo_url, :source_version,
+ :start_time, :status_code, :trigger
+ # attrs from git info
+ attr_reader :author_email, :author_name, :committer_email, :committer_name,
+ :commit_message_subject, :short_hash
+
# Default values are extracted from CODEBUILD_* ENV vars present in each
# CodeBuild # job container.
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
# 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
@@ -63,11 +76,11 @@
def launched_by_retry?
trigger.to_s.empty?
end
def for_pr?
- %r{^pr/}.match?(trigger.to_s)
+ %r{^pr/}.match?(source_version.to_s)
end
# source_id, the primary key, is a composite of project_code and
# trigger.
# e.g.:
@@ -94,8 +107,28 @@
if launched_by_retry?
previous_build&.source_ref
else
trigger
end
+ end
+
+ 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
+
+ private def git_info
+ Git.current_commit
end
end
end