Read about our upcoming Code of Conduct on this issue

Commit 2a20a4f3 authored by Francisco Javier López's avatar Francisco Javier López Committed by Grzegorz Bizon
Browse files

Rename project's pipelines relation

parent b024c46c4a24
...@@ -22,7 +22,7 @@ def index ...@@ -22,7 +22,7 @@ def index
# Fetch branches for the specified mode # Fetch branches for the specified mode
fetch_branches_by_mode fetch_branches_by_mode
@refs_pipelines = @project.pipelines.latest_successful_for_refs(@branches.map(&:name)) @refs_pipelines = @project.ci_pipelines.latest_successful_for_refs(@branches.map(&:name))
@merged_branch_names = repository.merged_branch_names(@branches.map(&:name)) @merged_branch_names = repository.merged_branch_names(@branches.map(&:name))
# n+1: https://gitlab.com/gitlab-org/gitaly/issues/992 # n+1: https://gitlab.com/gitlab-org/gitaly/issues/992
......
...@@ -46,7 +46,7 @@ def index ...@@ -46,7 +46,7 @@ def index
end end
def new def new
@pipeline = project.pipelines.new(ref: @project.default_branch) @pipeline = project.all_pipelines.new(ref: @project.default_branch)
end end
def create def create
...@@ -142,9 +142,9 @@ def charts ...@@ -142,9 +142,9 @@ def charts
@charts[:pipeline_times] = Gitlab::Ci::Charts::PipelineTime.new(project) @charts[:pipeline_times] = Gitlab::Ci::Charts::PipelineTime.new(project)
@counts = {} @counts = {}
@counts[:total] = @project.pipelines.count(:all) @counts[:total] = @project.all_pipelines.count(:all)
@counts[:success] = @project.pipelines.success.count(:all) @counts[:success] = @project.all_pipelines.success.count(:all)
@counts[:failed] = @project.pipelines.failed.count(:all) @counts[:failed] = @project.all_pipelines.failed.count(:all)
end end
private private
...@@ -164,7 +164,7 @@ def create_params ...@@ -164,7 +164,7 @@ def create_params
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def pipeline def pipeline
@pipeline ||= project @pipeline ||= project
.pipelines .all_pipelines
.includes(user: :status) .includes(user: :status)
.find_by!(id: params[:id]) .find_by!(id: params[:id])
.present(current_user: current_user) .present(current_user: current_user)
......
...@@ -20,7 +20,7 @@ def index ...@@ -20,7 +20,7 @@ def index
@tags = Kaminari.paginate_array(@tags).page(params[:page]) @tags = Kaminari.paginate_array(@tags).page(params[:page])
tag_names = @tags.map(&:name) tag_names = @tags.map(&:name)
@tags_pipelines = @project.pipelines.latest_successful_for_refs(tag_names) @tags_pipelines = @project.ci_pipelines.latest_successful_for_refs(tag_names)
@releases = project.releases.where(tag: tag_names) @releases = project.releases.where(tag: tag_names)
respond_to do |format| respond_to do |format|
......
...@@ -8,7 +8,7 @@ class PipelinesFinder ...@@ -8,7 +8,7 @@ class PipelinesFinder
def initialize(project, current_user, params = {}) def initialize(project, current_user, params = {})
@project = project @project = project
@current_user = current_user @current_user = current_user
@pipelines = project.pipelines @pipelines = project.all_pipelines
@params = params @params = params
end end
......
...@@ -12,14 +12,14 @@ class Pipeline < ActiveRecord::Base ...@@ -12,14 +12,14 @@ class Pipeline < ActiveRecord::Base
include AtomicInternalId include AtomicInternalId
include EnumWithNil include EnumWithNil
belongs_to :project, inverse_of: :pipelines belongs_to :project, inverse_of: :all_pipelines
belongs_to :user belongs_to :user
belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline' belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
belongs_to :pipeline_schedule, class_name: 'Ci::PipelineSchedule' belongs_to :pipeline_schedule, class_name: 'Ci::PipelineSchedule'
belongs_to :merge_request, class_name: 'MergeRequest' belongs_to :merge_request, class_name: 'MergeRequest'
has_internal_id :iid, scope: :project, presence: false, init: ->(s) do has_internal_id :iid, scope: :project, presence: false, init: ->(s) do
s&.project&.pipelines&.maximum(:iid) || s&.project&.pipelines&.count s&.project&.all_pipelines&.maximum(:iid) || s&.project&.all_pipelines&.count
end end
has_many :stages, -> { order(position: :asc) }, inverse_of: :pipeline has_many :stages, -> { order(position: :asc) }, inverse_of: :pipeline
...@@ -174,6 +174,7 @@ class Pipeline < ActiveRecord::Base ...@@ -174,6 +174,7 @@ class Pipeline < ActiveRecord::Base
end end
scope :internal, -> { where(source: internal_sources) } scope :internal, -> { where(source: internal_sources) }
scope :ci_sources, -> { where(config_source: ci_sources_values) }
scope :sort_by_merge_request_pipelines, -> do scope :sort_by_merge_request_pipelines, -> do
sql = 'CASE ci_pipelines.source WHEN (?) THEN 0 ELSE 1 END, ci_pipelines.id DESC' sql = 'CASE ci_pipelines.source WHEN (?) THEN 0 ELSE 1 END, ci_pipelines.id DESC'
...@@ -271,6 +272,10 @@ def self.internal_sources ...@@ -271,6 +272,10 @@ def self.internal_sources
sources.reject { |source| source == "external" }.values sources.reject { |source| source == "external" }.values
end end
def self.ci_sources_values
config_sources.values_at(:repository_source, :auto_devops_source, :unknown_source)
end
def stages_count def stages_count
statuses.select(:stage).distinct.count statuses.select(:stage).distinct.count
end end
......
...@@ -298,7 +298,7 @@ def diff_refs ...@@ -298,7 +298,7 @@ def diff_refs
end end
def pipelines def pipelines
project.pipelines.where(sha: sha) project.ci_pipelines.where(sha: sha)
end end
def last_pipeline def last_pipeline
...@@ -312,7 +312,7 @@ def status(ref = nil) ...@@ -312,7 +312,7 @@ def status(ref = nil)
end end
def status_for_project(ref, pipeline_project) def status_for_project(ref, pipeline_project)
pipeline_project.pipelines.latest_status_per_commit(id, ref)[id] pipeline_project.ci_pipelines.latest_status_per_commit(id, ref)[id]
end end
def set_status_for_ref(ref, status) def set_status_for_ref(ref, status)
......
...@@ -24,7 +24,7 @@ def each(&block) ...@@ -24,7 +24,7 @@ def each(&block)
# Setting this status ahead of time removes the need for running a query for # Setting this status ahead of time removes the need for running a query for
# every commit we're displaying. # every commit we're displaying.
def with_pipeline_status def with_pipeline_status
statuses = project.pipelines.latest_status_per_commit(map(&:id), ref) statuses = project.ci_pipelines.latest_status_per_commit(map(&:id), ref)
each do |commit| each do |commit|
commit.set_status_for_ref(ref, statuses[commit.id]) commit.set_status_for_ref(ref, statuses[commit.id])
......
...@@ -1056,7 +1056,7 @@ def diverged_from_target_branch? ...@@ -1056,7 +1056,7 @@ def diverged_from_target_branch?
def all_pipelines(shas: all_commit_shas) def all_pipelines(shas: all_commit_shas)
return Ci::Pipeline.none unless source_project return Ci::Pipeline.none unless source_project
@all_pipelines ||= source_project.pipelines @all_pipelines ||= source_project.ci_pipelines
.where(sha: shas, ref: source_branch) .where(sha: shas, ref: source_branch)
.where(merge_request: [nil, self]) .where(merge_request: [nil, self])
.sort_by_merge_request_pipelines .sort_by_merge_request_pipelines
...@@ -1220,7 +1220,7 @@ def mergeable_with_quick_action?(current_user, autocomplete_precheck: false, las ...@@ -1220,7 +1220,7 @@ def mergeable_with_quick_action?(current_user, autocomplete_precheck: false, las
end end
def base_pipeline def base_pipeline
@base_pipeline ||= project.pipelines @base_pipeline ||= project.ci_pipelines
.order(id: :desc) .order(id: :desc)
.find_by(sha: diff_base_sha) .find_by(sha: diff_base_sha)
end end
......
...@@ -247,7 +247,17 @@ class Project < ActiveRecord::Base ...@@ -247,7 +247,17 @@ class Project < ActiveRecord::Base
has_many :container_repositories, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :container_repositories, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :commit_statuses has_many :commit_statuses
has_many :pipelines, class_name: 'Ci::Pipeline', inverse_of: :project # The relation :all_pipelines is intented to be used when we want to get the
# whole list of pipelines associated to the project
has_many :all_pipelines, class_name: 'Ci::Pipeline', inverse_of: :project
# The relation :ci_pipelines is intented to be used when we want to get only
# those pipeline which are directly related to CI. There are
# other pipelines, like webide ones, that we won't retrieve
# if we use this relation.
has_many :ci_pipelines,
-> { Feature.enabled?(:pipeline_ci_sources_only, default_enabled: true) ? ci_sources : all },
class_name: 'Ci::Pipeline',
inverse_of: :project
has_many :stages, class_name: 'Ci::Stage', inverse_of: :project has_many :stages, class_name: 'Ci::Stage', inverse_of: :project
# Ci::Build objects store data on the file system such as artifact files and # Ci::Build objects store data on the file system such as artifact files and
...@@ -621,7 +631,7 @@ def commits_by(oids:) ...@@ -621,7 +631,7 @@ def commits_by(oids:)
# ref can't be HEAD, can only be branch/tag name or SHA # ref can't be HEAD, can only be branch/tag name or SHA
def latest_successful_builds_for(ref = default_branch) def latest_successful_builds_for(ref = default_branch)
latest_pipeline = pipelines.latest_successful_for(ref) latest_pipeline = ci_pipelines.latest_successful_for(ref)
if latest_pipeline if latest_pipeline
latest_pipeline.builds.latest.with_artifacts_archive latest_pipeline.builds.latest.with_artifacts_archive
...@@ -1376,7 +1386,7 @@ def pipeline_for(ref, sha = nil) ...@@ -1376,7 +1386,7 @@ def pipeline_for(ref, sha = nil)
return unless sha return unless sha
pipelines.order(id: :desc).find_by(sha: sha, ref: ref) ci_pipelines.order(id: :desc).find_by(sha: sha, ref: ref)
end end
def latest_successful_pipeline_for_default_branch def latest_successful_pipeline_for_default_branch
...@@ -1385,12 +1395,12 @@ def latest_successful_pipeline_for_default_branch ...@@ -1385,12 +1395,12 @@ def latest_successful_pipeline_for_default_branch
end end
@latest_successful_pipeline_for_default_branch = @latest_successful_pipeline_for_default_branch =
pipelines.latest_successful_for(default_branch) ci_pipelines.latest_successful_for(default_branch)
end end
def latest_successful_pipeline_for(ref = nil) def latest_successful_pipeline_for(ref = nil)
if ref && ref != default_branch if ref && ref != default_branch
pipelines.latest_successful_for(ref) ci_pipelines.latest_successful_for(ref)
else else
latest_successful_pipeline_for_default_branch latest_successful_pipeline_for_default_branch
end end
......
...@@ -38,11 +38,11 @@ def execute(data, force: false) ...@@ -38,11 +38,11 @@ def execute(data, force: false)
end end
def can_test? def can_test?
project.pipelines.any? project.ci_pipelines.any?
end end
def test_data(project, user) def test_data(project, user)
data = Gitlab::DataBuilder::Pipeline.build(project.pipelines.last) data = Gitlab::DataBuilder::Pipeline.build(project.ci_pipelines.last)
data[:user] = user.hook_attrs data[:user] = user.hook_attrs
data data
end end
......
...@@ -78,7 +78,7 @@ def cancel_pending_pipelines ...@@ -78,7 +78,7 @@ def cancel_pending_pipelines
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def auto_cancelable_pipelines def auto_cancelable_pipelines
project.pipelines project.ci_pipelines
.where(ref: pipeline.ref) .where(ref: pipeline.ref)
.where.not(id: pipeline.id) .where.not(id: pipeline.id)
.where.not(sha: project.commit(pipeline.ref).try(:id)) .where.not(sha: project.commit(pipeline.ref).try(:id))
......
...@@ -34,7 +34,7 @@ def disable_auto_devops ...@@ -34,7 +34,7 @@ def disable_auto_devops
end end
def auto_devops_pipelines def auto_devops_pipelines
@auto_devops_pipelines ||= project.pipelines.auto_devops_source @auto_devops_pipelines ||= project.ci_pipelines.auto_devops_source
end end
end end
end end
......
...@@ -49,7 +49,7 @@ def job_events_data ...@@ -49,7 +49,7 @@ def job_events_data
end end
def pipeline_events_data def pipeline_events_data
pipeline = project.pipelines.first pipeline = project.ci_pipelines.first
throw(:validation_error, 'Ensure the project has CI pipelines.') unless pipeline.present? throw(:validation_error, 'Ensure the project has CI pipelines.') unless pipeline.present?
Gitlab::DataBuilder::Pipeline.build(pipeline) Gitlab::DataBuilder::Pipeline.build(pipeline)
......
...@@ -104,7 +104,7 @@ def create_merge_request_pipelines ...@@ -104,7 +104,7 @@ def create_merge_request_pipelines
def create_pipeline!(project, ref, commit) def create_pipeline!(project, ref, commit)
project.pipelines.create!(sha: commit.id, ref: ref, source: :push) project.ci_pipelines.create!(sha: commit.id, ref: ref, source: :push)
end end
def build_create!(pipeline, opts = {}) def build_create!(pipeline, opts = {})
......
...@@ -29,7 +29,7 @@ class CommitStatuses < Grape::API ...@@ -29,7 +29,7 @@ class CommitStatuses < Grape::API
not_found!('Commit') unless user_project.commit(params[:sha]) not_found!('Commit') unless user_project.commit(params[:sha])
pipelines = user_project.pipelines.where(sha: params[:sha]) pipelines = user_project.ci_pipelines.where(sha: params[:sha])
statuses = ::CommitStatus.where(pipeline: pipelines) statuses = ::CommitStatus.where(pipeline: pipelines)
statuses = statuses.latest unless to_boolean(params[:all]) statuses = statuses.latest unless to_boolean(params[:all])
statuses = statuses.where(ref: params[:ref]) if params[:ref].present? statuses = statuses.where(ref: params[:ref]) if params[:ref].present?
...@@ -75,7 +75,7 @@ class CommitStatuses < Grape::API ...@@ -75,7 +75,7 @@ class CommitStatuses < Grape::API
pipeline = @project.pipeline_for(ref, commit.sha) pipeline = @project.pipeline_for(ref, commit.sha)
unless pipeline unless pipeline
pipeline = @project.pipelines.create!( pipeline = @project.ci_pipelines.create!(
source: :external, source: :external,
sha: commit.sha, sha: commit.sha,
ref: ref, ref: ref,
......
...@@ -56,7 +56,7 @@ class Jobs < Grape::API ...@@ -56,7 +56,7 @@ class Jobs < Grape::API
end end
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
get ':id/pipelines/:pipeline_id/jobs' do get ':id/pipelines/:pipeline_id/jobs' do
pipeline = user_project.pipelines.find(params[:pipeline_id]) pipeline = user_project.ci_pipelines.find(params[:pipeline_id])
builds = pipeline.builds builds = pipeline.builds
builds = filter_builds(builds, params[:scope]) builds = filter_builds(builds, params[:scope])
builds = builds.preload(:job_artifacts_archive, :job_artifacts, project: [:namespace]) builds = builds.preload(:job_artifacts_archive, :job_artifacts, project: [:namespace])
......
...@@ -130,7 +130,7 @@ class Pipelines < Grape::API ...@@ -130,7 +130,7 @@ class Pipelines < Grape::API
helpers do helpers do
def pipeline def pipeline
@pipeline ||= user_project.pipelines.find(params[:pipeline_id]) @pipeline ||= user_project.ci_pipelines.find(params[:pipeline_id])
end end
end end
end end
......
...@@ -14,7 +14,7 @@ def initialize(project, ref, job = nil) ...@@ -14,7 +14,7 @@ def initialize(project, ref, job = nil)
@ref = ref @ref = ref
@job = job @job = job
@pipeline = @project.pipelines.latest_successful_for(@ref) @pipeline = @project.ci_pipelines.latest_successful_for(@ref)
end end
def entity def entity
......
...@@ -22,7 +22,7 @@ def entity ...@@ -22,7 +22,7 @@ def entity
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def status def status
@project.pipelines @project.ci_pipelines
.where(sha: @sha) .where(sha: @sha)
.latest_status(@ref) || 'unknown' .latest_status(@ref) || 'unknown'
end end
......
...@@ -54,7 +54,7 @@ def initialize(project) ...@@ -54,7 +54,7 @@ def initialize(project)
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def collect def collect
query = project.pipelines query = project.all_pipelines
.where("? > #{::Ci::Pipeline.table_name}.created_at AND #{::Ci::Pipeline.table_name}.created_at > ?", @to, @from) # rubocop:disable GitlabSecurity/SqlInjection .where("? > #{::Ci::Pipeline.table_name}.created_at AND #{::Ci::Pipeline.table_name}.created_at > ?", @to, @from) # rubocop:disable GitlabSecurity/SqlInjection
totals_count = grouped_count(query) totals_count = grouped_count(query)
...@@ -115,7 +115,7 @@ def initialize(*) ...@@ -115,7 +115,7 @@ def initialize(*)
class PipelineTime < Chart class PipelineTime < Chart
def collect def collect
commits = project.pipelines.last(30) commits = project.all_pipelines.last(30)
commits.each do |commit| commits.each do |commit|
@labels << commit.short_sha @labels << commit.short_sha
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment