Commit d9bebd89 authored by Felipe Artur's avatar Felipe Artur

Fix specs 2

parent 1bf2dacf
...@@ -17,6 +17,9 @@ module Ci ...@@ -17,6 +17,9 @@ module Ci
has_many :statuses, class_name: 'CommitStatus', foreign_key: :commit_id has_many :statuses, class_name: 'CommitStatus', foreign_key: :commit_id
has_many :builds, foreign_key: :commit_id has_many :builds, foreign_key: :commit_id
has_many :trigger_requests, dependent: :destroy, foreign_key: :commit_id has_many :trigger_requests, dependent: :destroy, foreign_key: :commit_id
# Merge requests for which the current pipeline is running against
# the merge request's latest commit.
has_many :merge_requests, foreign_key: "head_pipeline_id" has_many :merge_requests, foreign_key: "head_pipeline_id"
has_many :pending_builds, -> { pending }, foreign_key: :commit_id, class_name: 'Ci::Build' has_many :pending_builds, -> { pending }, foreign_key: :commit_id, class_name: 'Ci::Build'
...@@ -382,14 +385,6 @@ module Ci ...@@ -382,14 +385,6 @@ module Ci
project.execute_services(data, :pipeline_hooks) project.execute_services(data, :pipeline_hooks)
end end
# Merge requests for which the current pipeline is running against
# the merge request's latest commit.
def merge_requests
@merge_requests ||= project.merge_requests
.where(source_branch: self.ref)
.select { |merge_request| merge_request.head_pipeline.try(:id) == self.id }
end
# All the merge requests for which the current pipeline runs/ran against # All the merge requests for which the current pipeline runs/ran against
def all_merge_requests def all_merge_requests
@all_merge_requests ||= project.merge_requests.where(source_branch: ref) @all_merge_requests ||= project.merge_requests.where(source_branch: ref)
......
class AddHeadPipelineForEachMergeRequest < ActiveRecord::Migration class AddHeadPipelineForEachMergeRequest < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false DOWNTIME = false
class Pipeline < ActiveRecord::Base def up
self.table_name = "ci_pipelines" disable_statement_timeout
def self.last_per_branch pipelines = Arel::Table.new(:ci_pipelines)
select('ref, MAX(id) as head_id, project_id').group(:ref).group(:project_id) merge_requests = Arel::Table.new(:merge_requests)
end
end
class MergeRequest < ActiveRecord::Base; end head_id = pipelines.
project(Arel::Nodes::NamedFunction.new('max', [pipelines[:id]])).
from(pipelines).
where(pipelines[:ref].eq(merge_requests[:source_branch])).
where(pipelines[:project_id].eq(merge_requests[:source_project_id]))
def up sub_query = Arel::Nodes::SqlLiteral.new(Arel::Nodes::Grouping.new(head_id).to_sql)
Pipeline.last_per_branch.each do |pipeline|
mrs = MergeRequest.where(source_branch: pipeline.ref, source_project_id: pipeline.project_id) update_column_in_batches(:merge_requests, :head_pipeline_id, sub_query)
mrs.update_all(head_pipeline_id: pipeline.head_id)
end
end end
def down def down
......
...@@ -1200,7 +1200,10 @@ describe Projects::MergeRequestsController do ...@@ -1200,7 +1200,10 @@ describe Projects::MergeRequestsController do
let(:status) { pipeline.detailed_status(double('user')) } let(:status) { pipeline.detailed_status(double('user')) }
before { get_pipeline_status } before do
merge_request.update(head_pipeline: pipeline)
get_pipeline_status
end
it 'return a detailed head_pipeline status in json' do it 'return a detailed head_pipeline status in json' do
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
......
...@@ -9,14 +9,17 @@ feature 'Cycle Analytics', feature: true, js: true do ...@@ -9,14 +9,17 @@ feature 'Cycle Analytics', feature: true, js: true do
let(:mr) { create_merge_request_closing_issue(issue, commit_message: "References #{issue.to_reference}") } let(:mr) { create_merge_request_closing_issue(issue, commit_message: "References #{issue.to_reference}") }
let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: mr.source_branch, sha: mr.source_branch_sha) } let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: mr.source_branch, sha: mr.source_branch_sha) }
before { mr.update(head_pipeline: pipeline) } before do
allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue])
end
context 'as an allowed user' do context 'as an allowed user' do
context 'when project is new' do context 'when project is new' do
before do before do
project.add_master(user) project.add_master(user)
mr.update(head_pipeline_id: pipeline.id)
login_as(user) login_as(user)
visit namespace_project_cycle_analytics_path(project.namespace, project) visit namespace_project_cycle_analytics_path(project.namespace, project)
wait_for_ajax wait_for_ajax
end end
...@@ -33,8 +36,8 @@ feature 'Cycle Analytics', feature: true, js: true do ...@@ -33,8 +36,8 @@ feature 'Cycle Analytics', feature: true, js: true do
context "when there's cycle analytics data" do context "when there's cycle analytics data" do
before do before do
project.team << [user, :master] mr.update(head_pipeline: pipeline)
project.add_master(user)
create_cycle create_cycle
deploy_master deploy_master
...@@ -87,7 +90,7 @@ feature 'Cycle Analytics', feature: true, js: true do ...@@ -87,7 +90,7 @@ feature 'Cycle Analytics', feature: true, js: true do
context "as a guest" do context "as a guest" do
before do before do
project.team << [guest, :guest] project.add_guest(guest)
allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue]) allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue])
create_cycle create_cycle
......
...@@ -89,7 +89,7 @@ describe 'Merge request', :feature, :js do ...@@ -89,7 +89,7 @@ describe 'Merge request', :feature, :js do
statuses: [commit_status]) statuses: [commit_status])
create(:ci_build, :pending, pipeline: pipeline) create(:ci_build, :pending, pipeline: pipeline)
merge_request.update(head_pipeline_id: pipeline.id) merge_request.update(head_pipeline: pipeline)
visit namespace_project_merge_request_path(project.namespace, project, merge_request) visit namespace_project_merge_request_path(project.namespace, project, merge_request)
end end
...@@ -111,7 +111,7 @@ describe 'Merge request', :feature, :js do ...@@ -111,7 +111,7 @@ describe 'Merge request', :feature, :js do
status: :manual status: :manual
) )
merge_request.update(head_pipeline_id: pipeline.id) merge_request.update(head_pipeline: pipeline)
visit namespace_project_merge_request_path(project.namespace, visit namespace_project_merge_request_path(project.namespace,
project, project,
...@@ -136,7 +136,7 @@ describe 'Merge request', :feature, :js do ...@@ -136,7 +136,7 @@ describe 'Merge request', :feature, :js do
statuses: [commit_status]) statuses: [commit_status])
create(:ci_build, :pending, pipeline: pipeline) create(:ci_build, :pending, pipeline: pipeline)
merge_request.update(head_pipeline_id: pipeline.id) merge_request.update(head_pipeline: pipeline)
visit namespace_project_merge_request_path(project.namespace, project, merge_request) visit namespace_project_merge_request_path(project.namespace, project, merge_request)
end end
......
...@@ -130,7 +130,7 @@ describe 'cycle analytics events' do ...@@ -130,7 +130,7 @@ describe 'cycle analytics events' do
end end
before do before do
merge_request.update(head_pipeline_id: pipeline.id) merge_request.update(head_pipeline: pipeline)
create(:ci_build, pipeline: pipeline, status: :success, author: user) create(:ci_build, pipeline: pipeline, status: :success, author: user)
create(:ci_build, pipeline: pipeline, status: :success, author: user) create(:ci_build, pipeline: pipeline, status: :success, author: user)
...@@ -228,7 +228,7 @@ describe 'cycle analytics events' do ...@@ -228,7 +228,7 @@ describe 'cycle analytics events' do
end end
before do before do
merge_request.update(head_pipeline_id: pipeline.id) merge_request.update(head_pipeline: pipeline)
create(:ci_build, pipeline: pipeline, status: :success, author: user) create(:ci_build, pipeline: pipeline, status: :success, author: user)
create(:ci_build, pipeline: pipeline, status: :success, author: user) create(:ci_build, pipeline: pipeline, status: :success, author: user)
......
...@@ -1045,7 +1045,7 @@ describe Ci::Pipeline, models: true do ...@@ -1045,7 +1045,7 @@ describe Ci::Pipeline, models: true do
it "returns merge requests whose `diff_head_sha` matches the pipeline's SHA" do it "returns merge requests whose `diff_head_sha` matches the pipeline's SHA" do
allow_any_instance_of(MergeRequest).to receive(:diff_head_sha) { 'a288a022a53a5a944fae87bcec6efc87b7061808' } allow_any_instance_of(MergeRequest).to receive(:diff_head_sha) { 'a288a022a53a5a944fae87bcec6efc87b7061808' }
merge_request = create(:merge_request, source_project: project, head_pipeline: pipeline, , source_branch: pipeline.ref) merge_request = create(:merge_request, source_project: project, head_pipeline: pipeline, source_branch: pipeline.ref)
expect(pipeline.merge_requests).to eq([merge_request]) expect(pipeline.merge_requests).to eq([merge_request])
end end
......
...@@ -1507,6 +1507,8 @@ describe MergeRequest, models: true do ...@@ -1507,6 +1507,8 @@ describe MergeRequest, models: true do
status: status) status: status)
merge_request.update(head_pipeline: pipeline) merge_request.update(head_pipeline: pipeline)
pipeline
end end
let(:project) { create(:project, :public, :repository, only_allow_merge_if_pipeline_succeeds: true) } let(:project) { create(:project, :public, :repository, only_allow_merge_if_pipeline_succeeds: true) }
...@@ -1592,7 +1594,7 @@ describe MergeRequest, models: true do ...@@ -1592,7 +1594,7 @@ describe MergeRequest, models: true do
context 'with running pipeline' do context 'with running pipeline' do
before do before do
merge_request.update(head_pipeline: create_pipeline(:running)) create_pipeline(:running)
end end
it 'is mergeable' do it 'is mergeable' do
......
...@@ -128,7 +128,7 @@ describe MergeRequests::MergeWhenPipelineSucceedsService do ...@@ -128,7 +128,7 @@ describe MergeRequests::MergeWhenPipelineSucceedsService do
sha: mr_conflict.diff_head_sha, status: 'success') sha: mr_conflict.diff_head_sha, status: 'success')
end end
before { mr_conflict.update(head_pipeline_id: conflict_pipeline.id) } before { mr_conflict.update(head_pipeline: conflict_pipeline) }
it 'does not merge the merge request' do it 'does not merge the merge request' do
expect(MergeWorker).not_to receive(:perform_async) expect(MergeWorker).not_to receive(:perform_async)
......
...@@ -179,7 +179,7 @@ describe MergeRequests::UpdateService, services: true do ...@@ -179,7 +179,7 @@ describe MergeRequests::UpdateService, services: true do
ref: merge_request.source_branch, ref: merge_request.source_branch,
sha: merge_request.diff_head_sha) sha: merge_request.diff_head_sha)
merge_request.update(head_pipeline_id: pipeline.id) merge_request.update(head_pipeline: pipeline)
expect(MergeRequests::MergeWhenPipelineSucceedsService).to receive(:new).with(project, user). expect(MergeRequests::MergeWhenPipelineSucceedsService).to receive(:new).with(project, user).
and_return(service_mock) and_return(service_mock)
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
describe PipelineMetricsWorker do describe PipelineMetricsWorker do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: pipeline.ref, head_pipeline_id: pipeline.id) } let!(:merge_request) { create(:merge_request, source_project: project, source_branch: pipeline.ref, head_pipeline: pipeline) }
let(:pipeline) do let(:pipeline) do
create(:ci_empty_pipeline, create(:ci_empty_pipeline,
......
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