2015-10-06 10:01:16 +00:00
|
|
|
class CommitStatus < ActiveRecord::Base
|
2016-08-25 02:55:32 +00:00
|
|
|
include HasStatus
|
2016-06-13 11:34:36 +00:00
|
|
|
include Importable
|
2016-03-31 17:51:28 +00:00
|
|
|
|
2015-10-06 10:01:16 +00:00
|
|
|
self.table_name = 'ci_builds'
|
|
|
|
|
2015-12-04 11:55:23 +00:00
|
|
|
belongs_to :project, class_name: '::Project', foreign_key: :gl_project_id
|
2016-08-11 13:22:35 +00:00
|
|
|
belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
|
2015-10-06 10:01:16 +00:00
|
|
|
belongs_to :user
|
|
|
|
|
2016-06-21 12:43:37 +00:00
|
|
|
delegate :commit, to: :pipeline
|
|
|
|
|
2016-06-13 11:34:36 +00:00
|
|
|
validates :pipeline, presence: true, unless: :importing?
|
2015-10-06 10:01:16 +00:00
|
|
|
|
|
|
|
validates_presence_of :name
|
|
|
|
|
2015-10-12 10:15:48 +00:00
|
|
|
alias_attribute :author, :user
|
|
|
|
|
2016-07-20 17:10:08 +00:00
|
|
|
scope :latest, -> do
|
|
|
|
max_id = unscope(:select).select("max(#{quoted_table_name}.id)")
|
|
|
|
|
|
|
|
where(id: max_id.group(:name, :commit_id))
|
|
|
|
end
|
2016-08-10 14:45:30 +00:00
|
|
|
|
2016-05-16 21:34:42 +00:00
|
|
|
scope :retried, -> { where.not(id: latest) }
|
2016-05-09 21:58:53 +00:00
|
|
|
scope :ordered, -> { order(:name) }
|
2016-04-12 17:57:54 +00:00
|
|
|
scope :ignored, -> { where(allow_failure: true, status: [:failed, :canceled]) }
|
2016-08-23 09:27:22 +00:00
|
|
|
scope :latest_ci_stages, -> { latest.ordered.includes(project: :namespace) }
|
|
|
|
scope :retried_ci_stages, -> { retried.ordered.includes(project: :namespace) }
|
2015-10-06 10:01:16 +00:00
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
state_machine :status do
|
2016-08-12 11:57:58 +00:00
|
|
|
event :enqueue do
|
2016-08-11 13:22:35 +00:00
|
|
|
transition [:created, :skipped] => :pending
|
2016-07-18 12:57:24 +00:00
|
|
|
end
|
|
|
|
|
2016-08-18 21:36:54 +00:00
|
|
|
event :process do
|
|
|
|
transition skipped: :created
|
|
|
|
end
|
|
|
|
|
2015-10-06 10:01:16 +00:00
|
|
|
event :run do
|
|
|
|
transition pending: :running
|
|
|
|
end
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
event :skip do
|
|
|
|
transition [:created, :pending] => :skipped
|
|
|
|
end
|
|
|
|
|
2015-10-06 10:01:16 +00:00
|
|
|
event :drop do
|
2016-08-11 13:22:35 +00:00
|
|
|
transition [:created, :pending, :running] => :failed
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
event :success do
|
2016-08-11 13:22:35 +00:00
|
|
|
transition [:created, :pending, :running] => :success
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
event :cancel do
|
2016-08-11 13:22:35 +00:00
|
|
|
transition [:created, :pending, :running] => :canceled
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
after_transition created: [:pending, :running] do |commit_status|
|
|
|
|
commit_status.update_attributes queued_at: Time.now
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
after_transition [:created, :pending] => :running do |commit_status|
|
2016-02-19 16:38:47 +00:00
|
|
|
commit_status.update_attributes started_at: Time.now
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2016-02-19 16:38:47 +00:00
|
|
|
after_transition any => [:success, :failed, :canceled] do |commit_status|
|
|
|
|
commit_status.update_attributes finished_at: Time.now
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
# We use around_transition to process pipeline on next stages as soon as possible, before the `after_*` is executed
|
|
|
|
around_transition any => [:success, :failed, :canceled] do |commit_status, block|
|
|
|
|
block.call
|
|
|
|
|
2016-08-11 18:54:02 +00:00
|
|
|
commit_status.pipeline.try(:process!)
|
2016-08-11 13:22:35 +00:00
|
|
|
end
|
2016-08-11 16:37:36 +00:00
|
|
|
|
2016-08-11 18:54:02 +00:00
|
|
|
after_transition do |commit_status, transition|
|
2016-08-12 09:36:51 +00:00
|
|
|
commit_status.pipeline.try(:build_updated) unless transition.loopback?
|
2016-08-11 16:37:36 +00:00
|
|
|
end
|
2016-08-11 20:54:25 +00:00
|
|
|
|
|
|
|
after_transition [:created, :pending, :running] => :success do |commit_status|
|
2016-06-02 14:19:18 +00:00
|
|
|
MergeRequests::MergeWhenBuildSucceedsService.new(commit_status.pipeline.project, nil).trigger(commit_status)
|
2015-11-02 16:27:38 +00:00
|
|
|
end
|
2016-03-08 18:22:50 +00:00
|
|
|
|
|
|
|
after_transition any => :failed do |commit_status|
|
2016-06-02 14:19:18 +00:00
|
|
|
MergeRequests::AddTodoWhenBuildFailsService.new(commit_status.pipeline.project, nil).execute(commit_status)
|
2016-03-08 18:22:50 +00:00
|
|
|
end
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2016-06-03 11:34:38 +00:00
|
|
|
delegate :sha, :short_sha, to: :pipeline
|
2016-04-16 19:46:26 +00:00
|
|
|
|
|
|
|
def before_sha
|
2016-06-03 11:09:49 +00:00
|
|
|
pipeline.before_sha || Gitlab::Git::BLANK_SHA
|
2016-04-16 19:46:26 +00:00
|
|
|
end
|
2015-10-06 10:01:16 +00:00
|
|
|
|
2016-04-12 17:57:54 +00:00
|
|
|
def self.stages
|
2016-05-09 23:38:25 +00:00
|
|
|
# We group by stage name, but order stages by theirs' index
|
2016-05-14 19:44:35 +00:00
|
|
|
unscoped.from(all, :sg).group('stage').order('max(stage_idx)', 'stage').pluck('sg.stage')
|
2016-04-12 17:57:54 +00:00
|
|
|
end
|
|
|
|
|
2016-05-14 19:44:35 +00:00
|
|
|
def self.stages_status
|
|
|
|
# We execute subquery for each stage to calculate a stage status
|
|
|
|
statuses = unscoped.from(all, :sg).group('stage').pluck('sg.stage', all.where('stage=sg.stage').status_sql)
|
|
|
|
statuses.inject({}) do |h, k|
|
|
|
|
h[k.first] = k.last
|
|
|
|
h
|
|
|
|
end
|
2016-04-12 17:57:54 +00:00
|
|
|
end
|
|
|
|
|
2016-02-19 18:58:43 +00:00
|
|
|
def ignored?
|
2016-03-17 14:54:56 +00:00
|
|
|
allow_failure? && (failed? || canceled?)
|
2016-02-19 18:58:43 +00:00
|
|
|
end
|
|
|
|
|
2015-10-06 10:01:16 +00:00
|
|
|
def duration
|
2016-08-10 14:45:30 +00:00
|
|
|
calculate_duration
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
2015-10-12 13:45:46 +00:00
|
|
|
|
2016-03-09 15:24:02 +00:00
|
|
|
def stuck?
|
2015-10-12 19:12:31 +00:00
|
|
|
false
|
|
|
|
end
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|