2015-10-24 18:46:33 +05:30
|
|
|
class CommitStatus < ActiveRecord::Base
|
2016-09-29 09:46:39 +05:30
|
|
|
include HasStatus
|
2016-06-22 15:30:34 +05:30
|
|
|
include Importable
|
2016-11-03 12:29:30 +05:30
|
|
|
include AfterCommitQueue
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
self.table_name = 'ci_builds'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
belongs_to :user
|
2017-08-17 22:00:37 +05:30
|
|
|
belongs_to :project
|
2016-09-13 17:45:13 +05:30
|
|
|
belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
|
2017-08-17 22:00:37 +05:30
|
|
|
belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
|
2015-10-24 18:46:33 +05:30
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
delegate :commit, to: :pipeline
|
2017-08-17 22:00:37 +05:30
|
|
|
delegate :sha, :short_sha, to: :pipeline
|
2016-06-22 15:30:34 +05:30
|
|
|
|
|
|
|
validates :pipeline, presence: true, unless: :importing?
|
2017-09-10 17:25:29 +05:30
|
|
|
validates :name, presence: true, unless: :importing?
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
alias_attribute :author, :user
|
2018-03-17 18:26:18 +05:30
|
|
|
alias_attribute :pipeline_id, :commit_id
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
scope :failed_but_allowed, -> do
|
|
|
|
where(allow_failure: true, status: [:failed, :canceled])
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :exclude_ignored, -> do
|
2017-08-17 22:00:37 +05:30
|
|
|
# We want to ignore failed but allowed to fail jobs.
|
|
|
|
#
|
|
|
|
# TODO, we also skip ignored optional manual actions.
|
2016-11-03 12:29:30 +05:30
|
|
|
where("allow_failure = ? OR status IN (?)",
|
2017-08-17 22:00:37 +05:30
|
|
|
false, all_state_names - [:failed, :canceled, :manual])
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
scope :latest, -> { where(retried: [false, nil]) }
|
|
|
|
scope :retried, -> { where(retried: true) }
|
|
|
|
scope :ordered, -> { order(:name) }
|
|
|
|
scope :latest_ordered, -> { latest.ordered.includes(project: :namespace) }
|
|
|
|
scope :retried_ordered, -> { retried.ordered.includes(project: :namespace) }
|
|
|
|
scope :after_stage, -> (index) { where('stage_idx > ?', index) }
|
2016-01-19 16:12:03 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
enum failure_reason: {
|
|
|
|
unknown_failure: nil,
|
|
|
|
script_failure: 1,
|
|
|
|
api_failure: 2,
|
|
|
|
stuck_or_timeout_failure: 3,
|
|
|
|
runner_system_failure: 4,
|
|
|
|
missing_dependency_failure: 5
|
|
|
|
}
|
|
|
|
|
|
|
|
##
|
|
|
|
# We still create some CommitStatuses outside of CreatePipelineService.
|
|
|
|
#
|
|
|
|
# These are pages deployments and external statuses.
|
|
|
|
#
|
|
|
|
before_create unless: :importing? do
|
|
|
|
Ci::EnsureStageService.new(project, user).execute(self) do |stage|
|
|
|
|
self.run_after_commit { StageUpdateWorker.perform_async(stage.id) }
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
state_machine :status do
|
2016-09-13 17:45:13 +05:30
|
|
|
event :process do
|
2017-08-17 22:00:37 +05:30
|
|
|
transition [:skipped, :manual] => :created
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
event :enqueue do
|
|
|
|
transition [:created, :skipped, :manual] => :pending
|
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
event :run do
|
|
|
|
transition pending: :running
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
event :skip do
|
|
|
|
transition [:created, :pending] => :skipped
|
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
event :drop do
|
2016-09-13 17:45:13 +05:30
|
|
|
transition [:created, :pending, :running] => :failed
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
event :success do
|
2016-09-13 17:45:13 +05:30
|
|
|
transition [:created, :pending, :running] => :success
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
event :cancel do
|
2017-08-17 22:00:37 +05:30
|
|
|
transition [:created, :pending, :running, :manual] => :canceled
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
before_transition created: [:pending, :running] do |commit_status|
|
|
|
|
commit_status.queued_at = Time.now
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
before_transition [:created, :pending] => :running do |commit_status|
|
|
|
|
commit_status.started_at = Time.now
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
before_transition any => [:success, :failed, :canceled] do |commit_status|
|
|
|
|
commit_status.finished_at = Time.now
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
before_transition any => :failed do |commit_status, transition|
|
|
|
|
failure_reason = transition.args.first
|
|
|
|
commit_status.failure_reason = failure_reason
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
after_transition do |commit_status, transition|
|
2018-03-17 18:26:18 +05:30
|
|
|
next unless commit_status.project
|
2016-11-03 12:29:30 +05:30
|
|
|
next if transition.loopback?
|
|
|
|
|
|
|
|
commit_status.run_after_commit do
|
2018-03-17 18:26:18 +05:30
|
|
|
if pipeline_id
|
2017-08-17 22:00:37 +05:30
|
|
|
if complete? || manual?
|
2018-03-17 18:26:18 +05:30
|
|
|
PipelineProcessWorker.perform_async(pipeline_id)
|
2016-11-03 12:29:30 +05:30
|
|
|
else
|
2018-03-17 18:26:18 +05:30
|
|
|
PipelineUpdateWorker.perform_async(pipeline_id)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
StageUpdateWorker.perform_async(stage_id)
|
|
|
|
ExpireJobCacheWorker.perform_async(id)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
after_transition any => :failed do |commit_status|
|
2018-03-17 18:26:18 +05:30
|
|
|
next unless commit_status.project
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
commit_status.run_after_commit do
|
|
|
|
MergeRequests::AddTodoWhenBuildFailsService
|
2018-03-17 18:26:18 +05:30
|
|
|
.new(project, nil).execute(self)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def locking_enabled?
|
|
|
|
status_changed?
|
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
def before_sha
|
2016-06-16 23:09:34 +05:30
|
|
|
pipeline.before_sha || Gitlab::Git::BLANK_SHA
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def group_name
|
2018-03-27 19:54:05 +05:30
|
|
|
name.to_s.gsub(%r{\d+[\.\s:/\\]+\d+\s*}, '').strip
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def failed_but_allowed?
|
2016-06-02 11:05:42 +05:30
|
|
|
allow_failure? && (failed? || canceled?)
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def duration
|
|
|
|
calculate_duration
|
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def playable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
# To be overriden when inherrited from
|
|
|
|
def retryable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# To be overriden when inherrited from
|
|
|
|
def cancelable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def stuck?
|
|
|
|
false
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def has_trace?
|
2015-10-24 18:46:33 +05:30
|
|
|
false
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def auto_canceled?
|
|
|
|
canceled? && auto_canceled_by_id?
|
|
|
|
end
|
|
|
|
|
|
|
|
def detailed_status(current_user)
|
|
|
|
Gitlab::Ci::Status::Factory
|
|
|
|
.new(self, current_user)
|
|
|
|
.fabricate!
|
|
|
|
end
|
|
|
|
|
|
|
|
def sortable_name
|
2017-09-10 17:25:29 +05:30
|
|
|
name.to_s.split(/(\d+)/).map do |v|
|
2017-08-17 22:00:37 +05:30
|
|
|
v =~ /\d+/ ? v.to_i : v
|
|
|
|
end
|
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|