summaryrefslogtreecommitdiff
path: root/app/models/ci/commit.rb
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2016-03-31 19:51:28 +0200
committerKamil Trzcinski <ayufan@ayufan.eu>2016-04-11 23:32:54 +0200
commit5d69f5b46d475f34fb71dfb4e8b683e90897f1da (patch)
tree48f268cb483ecab1d8d7e042c60931596838b1a3 /app/models/ci/commit.rb
parent986b4a54ee159de56a7ebe51327887b49474813b (diff)
downloadgitlab-ce-5d69f5b46d475f34fb71dfb4e8b683e90897f1da.tar.gz
Use Ci::Commit as Pipeline
Diffstat (limited to 'app/models/ci/commit.rb')
-rw-r--r--app/models/ci/commit.rb124
1 files changed, 61 insertions, 63 deletions
diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb
index f4cf7034b14..70fe63877cb 100644
--- a/app/models/ci/commit.rb
+++ b/app/models/ci/commit.rb
@@ -19,6 +19,7 @@
module Ci
class Commit < ActiveRecord::Base
extend Ci::Model
+ include CiStatus
belongs_to :project, class_name: '::Project', foreign_key: :gl_project_id
has_many :statuses, class_name: 'CommitStatus'
@@ -28,12 +29,18 @@ module Ci
validates_presence_of :sha
validate :valid_commit_sha
+ # Make sure that status is saved
+ before_save :status
+ before_save :started_at
+ before_save :finished_at
+ before_save :duration
+
def self.truncate_sha(sha)
sha[0...8]
end
- def to_param
- sha
+ def stages
+ statuses.group(:stage).order(:stage_idx).pluck(:stage)
end
def project_id
@@ -68,15 +75,26 @@ module Ci
nil
end
- def stage
- running_or_pending = statuses.latest.running_or_pending.ordered
- running_or_pending.first.try(:stage)
+ def branch?
+ !tag?
+ end
+
+ def retryable?
+ builds.latest.any? do |build|
+ build.failed? || build.retryable?
+ end
+ end
+
+ def invalidate
+ status = nil
+ started_at = nil
+ finished_at = nil
end
- def create_builds(ref, tag, user, trigger_request = nil)
+ def create_builds(user, trigger_request = nil)
return unless config_processor
config_processor.stages.any? do |stage|
- CreateBuildsService.new.execute(self, stage, ref, tag, user, trigger_request, 'success').present?
+ CreateBuildsService.new(self).execute(stage, user, 'success', trigger_request).present?
end
end
@@ -84,7 +102,7 @@ module Ci
return unless config_processor
# don't create other builds if this one is retried
- latest_builds = builds.similar(build).latest
+ latest_builds = builds.latest
return unless latest_builds.exists?(build.id)
# get list of stages after this build
@@ -97,26 +115,12 @@ module Ci
# create builds for next stages based
next_stages.any? do |stage|
- CreateBuildsService.new.execute(self, stage, build.ref, build.tag, build.user, build.trigger_request, status).present?
+ CreateBuildsService.new(self).execute(stage, build.user, status, build.trigger_request).present?
end
end
- def refs
- statuses.order(:ref).pluck(:ref).uniq
- end
-
- def latest_statuses
- @latest_statuses ||= statuses.latest.to_a
- end
-
- def latest_statuses_for_ref(ref)
- latest_statuses.select { |status| status.ref == ref }
- end
-
- def matrix_builds(build = nil)
- matrix_builds = builds.latest.ordered
- matrix_builds = matrix_builds.similar(build) if build
- matrix_builds.to_a
+ def latest
+ statuses.latest
end
def retried
@@ -124,56 +128,23 @@ module Ci
end
def status
- if yaml_errors.present?
- return 'failed'
- end
-
- @status ||= Ci::Status.get_status(latest_statuses)
- end
-
- def pending?
- status == 'pending'
- end
-
- def running?
- status == 'running'
- end
-
- def success?
- status == 'success'
- end
-
- def failed?
- status == 'failed'
- end
-
- def canceled?
- status == 'canceled'
- end
-
- def active?
- running? || pending?
- end
-
- def complete?
- canceled? || success? || failed?
+ read_attribute(:status) || update_status
end
def duration
- duration_array = statuses.map(&:duration).compact
- duration_array.reduce(:+).to_i
+ read_attribute(:duration) || update_duration
end
def started_at
- @started_at ||= statuses.order('started_at ASC').first.try(:started_at)
+ read_attribute(:started_at) || update_started_at
end
def finished_at
- @finished_at ||= statuses.order('finished_at DESC').first.try(:finished_at)
+ read_attribute(:finished_at) || update_finished_at
end
def coverage
- coverage_array = latest_statuses.map(&:coverage).compact
+ coverage_array = latest.map(&:coverage).compact
if coverage_array.size >= 1
'%.2f' % (coverage_array.reduce(:+) / coverage_array.size)
end
@@ -191,6 +162,7 @@ module Ci
end
def ci_yaml_file
+ return nil if defined?(@ci_yaml_file)
@ci_yaml_file ||= begin
blob = project.repository.blob_at(sha, '.gitlab-ci.yml')
blob.load_all_data!(project.repository)
@@ -206,6 +178,32 @@ module Ci
private
+ def update_status
+ status =
+ if yaml_errors.present?
+ 'failed'
+ else
+ latest.status
+ end
+ end
+
+ def update_started_at
+ started_at =
+ statuses.order(:id).first.try(:started_at)
+ end
+
+ def update_finished_at
+ finished_at =
+ statuses.order(id: :desc).first.try(:finished_at)
+ end
+
+ def update_duration
+ duration = begin
+ duration_array = latest.map(&:duration).compact
+ duration_array.reduce(:+).to_i
+ end
+ end
+
def save_yaml_error(error)
return if self.yaml_errors?
self.yaml_errors = error