From a266752821116e4736c493ad865e5ba6e1821c0d Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 6 Oct 2015 11:59:55 +0200 Subject: Latest builds always include builds with unique name and unique ref --- app/models/ci/build.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models/ci') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index f35224916ed..3c92710968c 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -47,7 +47,7 @@ module Ci scope :failed, ->() { where(status: "failed") } scope :unstarted, ->() { where(runner_id: nil) } scope :running_or_pending, ->() { where(status:[:running, :pending]) } - scope :latest, ->() { where(id: unscope(:select).select('max(id)').group(:name)).order(stage_idx: :asc) } + scope :latest, ->() { where(id: unscope(:select).select('max(id)').group(:name, :ref)).order(stage_idx: :asc) } scope :ignore_failures, ->() { where(allow_failure: false) } scope :for_ref, ->(ref) { where(ref: ref) } scope :similar, ->(build) { where(ref: build.ref, tag: build.tag, trigger_request_id: build.trigger_request_id) } -- cgit v1.2.1 From 065fe557f7c1ec8a520b7c18c83fa062f90d1443 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 6 Oct 2015 12:00:15 +0200 Subject: Fixed failure reading .gitlab-ci.yml --- app/models/ci/commit.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models/ci') diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index 46370034f9a..fde754a92a1 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -211,7 +211,7 @@ module Ci end def ci_yaml_file - gl_project.repository.blob_at(sha, '.gitlab-ci.yml') + gl_project.repository.blob_at(sha, '.gitlab-ci.yml').data rescue nil end -- cgit v1.2.1 From 82b6a17ca70a20015e654b4a0fdb8aec6d244f95 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 6 Oct 2015 21:41:37 +0200 Subject: Fix ci build routing and few tests Signed-off-by: Dmitriy Zaporozhets --- app/models/ci/build.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models/ci') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 3c92710968c..5d17f4418ed 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -144,7 +144,7 @@ module Ci state :canceled, value: 'canceled' end - delegate :sha, :short_sha, :project, + delegate :sha, :short_sha, :project, :gl_project, to: :commit, prefix: false def before_sha -- cgit v1.2.1 From 914cfbd2f154ed3154a7dc3cee3309713eea786f Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 6 Oct 2015 12:01:16 +0200 Subject: Implement Commit Status API --- app/models/ci/build.rb | 87 +++++++++--------------------------------------- app/models/ci/commit.rb | 88 +++++++++++++++++++++++-------------------------- 2 files changed, 56 insertions(+), 119 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 5d17f4418ed..41ce522b2ff 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -24,32 +24,19 @@ # module Ci - class Build < ActiveRecord::Base - extend Ci::Model - + class Build < CommitStatus LAZY_ATTRIBUTES = ['trace'] - belongs_to :commit, class_name: 'Ci::Commit' belongs_to :runner, class_name: 'Ci::Runner' belongs_to :trigger_request, class_name: 'Ci::TriggerRequest' - belongs_to :user serialize :options - validates :commit, presence: true - validates :status, presence: true validates :coverage, numericality: true, allow_blank: true validates_presence_of :ref - scope :running, ->() { where(status: "running") } - scope :pending, ->() { where(status: "pending") } - scope :success, ->() { where(status: "success") } - scope :failed, ->() { where(status: "failed") } scope :unstarted, ->() { where(runner_id: nil) } - scope :running_or_pending, ->() { where(status:[:running, :pending]) } - scope :latest, ->() { where(id: unscope(:select).select('max(id)').group(:name, :ref)).order(stage_idx: :asc) } scope :ignore_failures, ->() { where(allow_failure: false) } - scope :for_ref, ->(ref) { where(ref: ref) } scope :similar, ->(build) { where(ref: build.ref, tag: build.tag, trigger_request_id: build.trigger_request_id) } acts_as_taggable @@ -74,13 +61,14 @@ module Ci def create_from(build) new_build = build.dup - new_build.status = :pending + new_build.status = 'pending' new_build.runner_id = nil + new_build.trigger_request_id = nil new_build.save end def retry(build) - new_build = Ci::Build.new(status: :pending) + new_build = Ci::Build.new(status: 'pending') new_build.ref = build.ref new_build.tag = build.tag new_build.options = build.options @@ -98,28 +86,7 @@ module Ci end state_machine :status, initial: :pending do - event :run do - transition pending: :running - end - - event :drop do - transition running: :failed - end - - event :success do - transition running: :success - end - - event :cancel do - transition [:pending, :running] => :canceled - end - - after_transition pending: :running do |build, transition| - build.update_attributes started_at: Time.now - end - after_transition any => [:success, :failed, :canceled] do |build, transition| - build.update_attributes finished_at: Time.now project = build.project if project.web_hooks? @@ -136,19 +103,10 @@ module Ci build.update_coverage end end - - state :pending, value: 'pending' - state :running, value: 'running' - state :failed, value: 'failed' - state :success, value: 'success' - state :canceled, value: 'canceled' end - delegate :sha, :short_sha, :project, :gl_project, - to: :commit, prefix: false - - def before_sha - Gitlab::Git::BLANK_SHA + def ignored? + failed? && allow_failure? end def trace_html @@ -156,22 +114,6 @@ module Ci html || '' end - def started? - !pending? && !canceled? && started_at - end - - def active? - running? || pending? - end - - def complete? - canceled? || success? || failed? - end - - def ignored? - failed? && allow_failure? - end - def timeout project.timeout end @@ -180,14 +122,6 @@ module Ci yaml_variables + project_variables + trigger_variables end - def duration - if started_at && finished_at - finished_at - started_at - elsif started_at - Time.now - started_at - end - end - def project commit.project end @@ -278,6 +212,15 @@ module Ci "#{dir_to_trace}/#{id}.log" end + def description + name + end + + def target_url + Gitlab::Application.routes.url_helpers. + namespace_project_build_url(gl_project.namespace, gl_project, self) + end + private def yaml_variables diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index fde754a92a1..042a68681bb 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -20,7 +20,8 @@ module Ci extend Ci::Model belongs_to :gl_project, class_name: '::Project', foreign_key: :gl_project_id - has_many :builds, dependent: :destroy, class_name: 'Ci::Build' + has_many :statuses, dependent: :destroy, class_name: 'CommitStatus' + has_many :builds, class_name: 'Ci::Build' has_many :trigger_requests, dependent: :destroy, class_name: 'Ci::TriggerRequest' validates_presence_of :sha @@ -81,12 +82,11 @@ module Ci end def stage - running_or_pending = builds_without_retry.running_or_pending - running_or_pending.limit(1).pluck(:stage).first + running_or_pending = statuses.latest.running_or_pending + running_or_pending.first.try(:stage) end def create_builds(ref, tag, user, trigger_request = nil) - return if skip_ci? && trigger_request.blank? return unless config_processor config_processor.stages.any? do |stage| CreateBuildsService.new.execute(self, stage, ref, tag, user, trigger_request).present? @@ -94,7 +94,6 @@ module Ci end def create_next_builds(ref, tag, user, trigger_request) - return if skip_ci? && trigger_request.blank? return unless config_processor stages = builds.where(ref: ref, tag: tag, trigger_request: trigger_request).group_by(&:stage) @@ -107,39 +106,47 @@ module Ci end def refs - builds.group(:ref).pluck(:ref) + statuses.pluck(:ref).compact.uniq end - def last_ref - builds.latest.first.try(:ref) - end - - def builds_without_retry - builds.latest + def statuses_for_ref(ref = nil) + if ref + statuses.for_ref(ref) + else + statuses + end end - def builds_without_retry_for_ref(ref) - builds.for_ref(ref).latest + def builds_without_retry(ref = nil) + if ref + builds.for_ref(ref).latest + else + builds.latest + end end - def retried_builds - @retried_builds ||= (builds.order(id: :desc) - builds_without_retry) + def retried + @retried ||= (statuses.order(id: :desc) - statuses.latest) end - def status - if skip_ci? - return 'skipped' - elsif yaml_errors.present? + def status(ref = nil) + if yaml_errors.present? return 'failed' - elsif builds.none? + end + + latest_statuses = statuses.latest.to_a + latest_statuses.reject! { |status| status.try(&:allow_failure?) } + latest_statuses.select! { |status| status.ref == nil || status.ref == ref } if ref + + if latest_statuses.none? return 'skipped' - elsif success? + elsif latest_statuses.all?(&:success?) 'success' - elsif pending? + elsif latest_statuses.all?(&:pending?) 'pending' - elsif running? + elsif latest_statuses.any?(&:running?) || latest_statuses.any?(&:pending?) 'running' - elsif canceled? + elsif latest_statuses.all?(&:canceled?) 'canceled' else 'failed' @@ -147,21 +154,15 @@ module Ci end def pending? - builds_without_retry.all? do |build| - build.pending? - end + status == 'pending' end def running? - builds_without_retry.any? do |build| - build.running? || build.pending? - end + status == 'running' end def success? - builds_without_retry.all? do |build| - build.success? || build.ignored? - end + status == 'success' end def failed? @@ -169,21 +170,15 @@ module Ci end def canceled? - builds_without_retry.all? do |build| - build.canceled? - end - end - - def duration - @duration ||= builds_without_retry.select(&:duration).sum(&:duration).to_i + status == 'canceled' end - def duration_for_ref(ref) - builds_without_retry_for_ref(ref).select(&:duration).sum(&:duration).to_i + def duration(ref = nil) + statuses_for_ref(ref).latest.select(&:duration).sum(&:duration).to_i end def finished_at - @finished_at ||= builds.order('finished_at DESC').first.try(:finished_at) + @finished_at ||= statuses.order('finished_at DESC').first.try(:finished_at) end def coverage @@ -195,8 +190,8 @@ module Ci end end - def matrix_for_ref?(ref) - builds_without_retry_for_ref(ref).pluck(:id).size > 1 + def matrix?(ref) + builds_without_retry(ref).pluck(:id).size > 1 end def config_processor @@ -217,7 +212,6 @@ module Ci end def skip_ci? - return false if builds.any? git_commit_message =~ /(\[ci skip\])/ if git_commit_message end -- cgit v1.2.1 From 0aefeeb882b40d740b80f15ac6610a88a340d30b Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 12 Oct 2015 12:16:00 +0200 Subject: Add Commit Status documentation --- app/models/ci/commit.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models/ci') diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index 042a68681bb..623ff619c49 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -136,7 +136,7 @@ module Ci latest_statuses = statuses.latest.to_a latest_statuses.reject! { |status| status.try(&:allow_failure?) } - latest_statuses.select! { |status| status.ref == nil || status.ref == ref } if ref + latest_statuses.select! { |status| status.ref.nil? || status.ref == ref } if ref if latest_statuses.none? return 'skipped' -- cgit v1.2.1 From 69c04498ef0a49186a667fd44383b9b43690a91e Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 12 Oct 2015 15:45:46 +0200 Subject: Small bug fixes --- app/models/ci/build.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 41ce522b2ff..cb6a1015210 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -212,15 +212,23 @@ module Ci "#{dir_to_trace}/#{id}.log" end - def description - name - end - def target_url Gitlab::Application.routes.url_helpers. namespace_project_build_url(gl_project.namespace, gl_project, self) end + def cancel_url + if active? + cancel_namespace_project_build_path(gl_project.namespace, gl_project, self, return_to: request.original_url) + end + end + + def retry_url + if commands.present? + cancel_namespace_project_build_path(gl_project.namespace, gl_project, self, return_to: request.original_url) + end + end + private def yaml_variables -- cgit v1.2.1 From 789fe7b4899fb97c48ad363c5ecb1969d78d1536 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 12 Oct 2015 16:32:58 +0200 Subject: Update rendering --- app/models/ci/commit.rb | 66 ++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 34 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index 623ff619c49..201b6f62b86 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -106,50 +106,47 @@ module Ci end def refs - statuses.pluck(:ref).compact.uniq + statuses.order(:ref).pluck(:ref).uniq end - def statuses_for_ref(ref = nil) - if ref - statuses.for_ref(ref) - else - statuses - end + def latest_statuses + @latest_statuses ||= statuses.latest.to_a end - def builds_without_retry(ref = nil) - if ref - builds.for_ref(ref).latest - else - builds.latest - end + def builds_without_retry + @builds_without_retry ||= builds.latest.to_a + end + + def builds_without_retry_for_ref(ref) + builds_without_retry.select { |build| build.ref == ref } end def retried @retried ||= (statuses.order(id: :desc) - statuses.latest) end - def status(ref = nil) + def status if yaml_errors.present? return 'failed' end - latest_statuses = statuses.latest.to_a - latest_statuses.reject! { |status| status.try(&:allow_failure?) } - latest_statuses.select! { |status| status.ref.nil? || status.ref == ref } if ref - - if latest_statuses.none? - return 'skipped' - elsif latest_statuses.all?(&:success?) - 'success' - elsif latest_statuses.all?(&:pending?) - 'pending' - elsif latest_statuses.any?(&:running?) || latest_statuses.any?(&:pending?) - 'running' - elsif latest_statuses.all?(&:canceled?) - 'canceled' - else - 'failed' + @status ||= begin + latest = latest_statuses + latest.reject! { |status| status.try(&:allow_failure?) } + + if latest.none? + 'skipped' + elsif latest.all?(&:success?) + 'success' + elsif latest.all?(&:pending?) + 'pending' + elsif latest.any?(&:running?) || latest.any?(&:pending?) + 'running' + elsif latest.all?(&:canceled?) + 'canceled' + else + 'failed' + end end end @@ -173,8 +170,9 @@ module Ci status == 'canceled' end - def duration(ref = nil) - statuses_for_ref(ref).latest.select(&:duration).sum(&:duration).to_i + def duration + duration_array = latest_statuses.map(&:duration).compact + duration_array.reduce(:+).to_i end def finished_at @@ -190,8 +188,8 @@ module Ci end end - def matrix?(ref) - builds_without_retry(ref).pluck(:id).size > 1 + def matrix_for_ref?(ref) + builds_without_retry_for_ref(ref).size > 1 end def config_processor -- cgit v1.2.1 From 5cd504ed331dd23d17709285237945fb867978a8 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 12 Oct 2015 16:39:08 +0200 Subject: Rename builds_without_retry to latest_builds --- app/models/ci/commit.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index 201b6f62b86..296890c5e7c 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -48,7 +48,7 @@ module Ci end def retry - builds_without_retry.each do |build| + latest_builds.each do |build| Ci::Build.retry(build) end end @@ -113,12 +113,12 @@ module Ci @latest_statuses ||= statuses.latest.to_a end - def builds_without_retry - @builds_without_retry ||= builds.latest.to_a + def latest_builds + @latest_builds ||= builds.latest.to_a end - def builds_without_retry_for_ref(ref) - builds_without_retry.select { |build| build.ref == ref } + def latest_builds_for_ref(ref) + latest_builds.select { |build| build.ref == ref } end def retried @@ -181,7 +181,7 @@ module Ci def coverage if project.coverage_enabled? - coverage_array = builds_without_retry.map(&:coverage).compact + coverage_array = latest_builds.map(&:coverage).compact if coverage_array.size >= 1 '%.2f' % (coverage_array.reduce(:+) / coverage_array.size) end @@ -189,7 +189,7 @@ module Ci end def matrix_for_ref?(ref) - builds_without_retry_for_ref(ref).size > 1 + latest_builds_for_ref(ref).size > 1 end def config_processor -- cgit v1.2.1 From daca1c6511c3a09d5f0c33a8c4d29487e668afc2 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 12 Oct 2015 21:35:52 +0200 Subject: Fix broken tests --- app/models/ci/commit.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index 296890c5e7c..f6fc4645947 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -82,7 +82,7 @@ module Ci end def stage - running_or_pending = statuses.latest.running_or_pending + running_or_pending = statuses.latest.running_or_pending.ordered running_or_pending.first.try(:stage) end @@ -189,7 +189,7 @@ module Ci end def matrix_for_ref?(ref) - latest_builds_for_ref(ref).size > 1 + builds_without_retry_for_ref(ref).size > 1 end def config_processor -- cgit v1.2.1 From 766da5fa7bdded8a333a758d7b172533ade5a934 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 12 Oct 2015 22:34:59 +0200 Subject: Fix broken matrix_for_ref? Signed-off-by: Kamil Trzcinski --- app/models/ci/commit.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models/ci') diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index f6fc4645947..68864edfbbf 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -189,7 +189,7 @@ module Ci end def matrix_for_ref?(ref) - builds_without_retry_for_ref(ref).size > 1 + latest_builds_for_ref(ref).size > 1 end def config_processor -- cgit v1.2.1 From e7cc554cc181cbb850f89af26e64a9ab56116f28 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 13 Oct 2015 11:50:37 +0200 Subject: Fix retry and cancel URLs --- app/models/ci/build.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index cb6a1015210..f8c731a7bf7 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -219,13 +219,15 @@ module Ci def cancel_url if active? - cancel_namespace_project_build_path(gl_project.namespace, gl_project, self, return_to: request.original_url) + Gitlab::Application.routes.url_helpers. + cancel_namespace_project_build_path(gl_project.namespace, gl_project, self, return_to: request.original_url) end end def retry_url if commands.present? - cancel_namespace_project_build_path(gl_project.namespace, gl_project, self, return_to: request.original_url) + Gitlab::Application.routes.url_helpers. + cancel_namespace_project_build_path(gl_project.namespace, gl_project, self, return_to: request.original_url) end end -- cgit v1.2.1 From 33c9d6e45c0800fd5a312af2c286826764fd7589 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 13 Oct 2015 16:51:13 +0200 Subject: Fix retry and cancel for build --- app/models/ci/build.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index f8c731a7bf7..cfafbf6786e 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -220,14 +220,14 @@ module Ci def cancel_url if active? Gitlab::Application.routes.url_helpers. - cancel_namespace_project_build_path(gl_project.namespace, gl_project, self, return_to: request.original_url) + cancel_namespace_project_build_path(gl_project.namespace, gl_project, self) end end def retry_url if commands.present? Gitlab::Application.routes.url_helpers. - cancel_namespace_project_build_path(gl_project.namespace, gl_project, self, return_to: request.original_url) + retry_namespace_project_build_path(gl_project.namespace, gl_project, self) end end -- cgit v1.2.1 From 7b5ab3ded5e6f5d88f04802d5a71a9ae94d20f92 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 12 Oct 2015 17:06:53 +0200 Subject: Added CI_BUILD_TAG, _STAGE, _NAME and _TRIGGERED to CI builds --- app/models/ci/build.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'app/models/ci') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index cfafbf6786e..481440e869f 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -119,7 +119,7 @@ module Ci end def variables - yaml_variables + project_variables + trigger_variables + predefined_variables + yaml_variables + project_variables + trigger_variables end def project @@ -258,5 +258,14 @@ module Ci [] end end + + def predefined_variables + variables = [] + variables << { key: :CI_BUILD_TAG, value: ref, public: true } if tag + variables << { key: :CI_BUILD_NAME, value: name, public: true } + variables << { key: :CI_BUILD_STAGE, value: stage, public: true } + variables << { key: :CI_BUILD_TRIGGERED, value: 'true', public: true } if trigger_request + variables + end end end -- cgit v1.2.1 From 58074195198e715045dc5280aed9515297fe7ad3 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 13 Oct 2015 17:00:55 +0200 Subject: Use tag? instead of tag to indicate that this is boolean --- app/models/ci/build.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models/ci') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 481440e869f..23aed6d9952 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -261,7 +261,7 @@ module Ci def predefined_variables variables = [] - variables << { key: :CI_BUILD_TAG, value: ref, public: true } if tag + variables << { key: :CI_BUILD_TAG, value: ref, public: true } if tag? variables << { key: :CI_BUILD_NAME, value: name, public: true } variables << { key: :CI_BUILD_STAGE, value: stage, public: true } variables << { key: :CI_BUILD_TRIGGERED, value: 'true', public: true } if trigger_request -- cgit v1.2.1 From 7af4f5215e28927830cbc74d383cdfeb9e4ef587 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 12 Oct 2015 21:12:31 +0200 Subject: Show warning if build doesn't have runners with specified tags or runners didn't connect recently Slightly refactor runner status detection: moving it to Runner class Signed-off-by: Kamil Trzcinski --- app/models/ci/build.rb | 12 ++++++++++++ app/models/ci/project.rb | 6 +++--- app/models/ci/runner.rb | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) (limited to 'app/models/ci') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index cfafbf6786e..2c7cad46b00 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -231,6 +231,18 @@ module Ci end end + def can_be_served?(runner) + (tag_list - runner.tag_list).empty? + end + + def any_runners_online? + project.any_runners? { |runner| runner.active? && runner.online? && can_be_served?(runner) } + end + + def show_warning? + pending? && !any_runners_online? + end + private def yaml_variables diff --git a/app/models/ci/project.rb b/app/models/ci/project.rb index 88ba933a434..ef28353a30c 100644 --- a/app/models/ci/project.rb +++ b/app/models/ci/project.rb @@ -115,12 +115,12 @@ module Ci web_url end - def any_runners? - if runners.active.any? + def any_runners?(&block) + if runners.active.any?(&block) return true end - shared_runners_enabled && Ci::Runner.shared.active.any? + shared_runners_enabled && Ci::Runner.shared.active.any?(&block) end def set_default_values diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb index 6838ccfaaab..02a3e9db1fa 100644 --- a/app/models/ci/runner.rb +++ b/app/models/ci/runner.rb @@ -20,6 +20,8 @@ module Ci class Runner < ActiveRecord::Base extend Ci::Model + + LAST_CONTACT_TIME = 5.minutes.ago has_many :builds, class_name: 'Ci::Build' has_many :runner_projects, dependent: :destroy, class_name: 'Ci::RunnerProject' @@ -33,6 +35,7 @@ module Ci scope :shared, ->() { where(is_shared: true) } scope :active, ->() { where(active: true) } scope :paused, ->() { where(active: false) } + scope :online, ->() { where('contacted_at > ?', LAST_CONTACT_TIME) } acts_as_taggable @@ -65,6 +68,20 @@ module Ci is_shared end + def online? + contacted_at && contacted_at > LAST_CONTACT_TIME + end + + def status + if contacted_at.nil? + :not_connected + elsif active? + online? ? :online : :offline + else + :paused + end + end + def belongs_to_one_project? runner_projects.count == 1 end -- cgit v1.2.1