Commit 2ccee716 authored by Felipe Artur's avatar Felipe Artur

Small code improvements and add migration spec

parent d9bebd89
......@@ -119,9 +119,8 @@ module Ci
end
def update_merge_requests_head_pipeline
merge_requests = MergeRequest.where(source_branch: @pipeline.ref, source_project: @pipeline.project)
merge_requests.update_all(head_pipeline_id: @pipeline.id) if merge_requests.any?
MergeRequest.where(source_branch: @pipeline.ref, source_project: @pipeline.project).
update_all(head_pipeline_id: @pipeline.id)
end
def error(message, save: false)
......
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170506185517) do
ActiveRecord::Schema.define(version: 20170508170547) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -114,7 +114,6 @@ ActiveRecord::Schema.define(version: 20170506185517) do
t.string "plantuml_url"
t.boolean "plantuml_enabled"
t.integer "terminal_max_session_time", default: 0, null: false
t.string "default_artifacts_expire_in", default: "0", null: false
t.integer "unique_ips_limit_per_user"
t.integer "unique_ips_limit_time_window"
t.boolean "unique_ips_limit_enabled", default: false, null: false
......
......@@ -9,10 +9,6 @@ feature 'Cycle Analytics', feature: true, js: true do
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) }
before do
allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue])
end
context 'as an allowed user' do
context 'when project is new' do
before do
......@@ -36,8 +32,10 @@ feature 'Cycle Analytics', feature: true, js: true do
context "when there's cycle analytics data" do
before do
allow_any_instance_of(Gitlab::ReferenceExtractor).to receive(:issues).and_return([issue])
mr.update(head_pipeline: pipeline)
project.add_master(user)
create_cycle
deploy_master
......
......@@ -103,6 +103,7 @@ pipelines:
- manual_actions
- artifacts
- pipeline_schedule
- merge_requests
statuses:
- project
- pipeline
......
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20170508170547_add_head_pipeline_for_each_merge_request.rb')
describe AddHeadPipelineForEachMergeRequest do
let(:migration) { described_class.new }
let!(:project) { create(:empty_project) }
let!(:forked_project_link) { create(:forked_project_link, forked_from_project: project) }
let!(:other_project) { forked_project_link.forked_to_project }
let!(:pipeline_1) { create(:ci_pipeline, project: project, ref: "branch_1") }
let!(:pipeline_2) { create(:ci_pipeline, project: other_project, ref: "branch_1") }
let!(:pipeline_3) { create(:ci_pipeline, project: other_project, ref: "branch_1") }
let!(:pipeline_4) { create(:ci_pipeline, project: project, ref: "branch_2") }
let!(:mr_1) { create(:merge_request, source_project: project, target_project: project, source_branch: "branch_1", target_branch: "target_1") }
let!(:mr_2) { create(:merge_request, source_project: other_project, target_project: project, source_branch: "branch_1", target_branch: "target_2") }
let!(:mr_3) { create(:merge_request, source_project: project, target_project: project, source_branch: "branch_2", target_branch: "master") }
let!(:mr_4) { create(:merge_request, source_project: project, target_project: project, source_branch: "branch_3", target_branch: "master") }
context "#up" do
context "when source_project and source_branch of pipeline are the same of merge request" do
it "sets head_pipeline_id of given merge requests" do
migration.up
expect(mr_1.reload.head_pipeline_id).to eq(pipeline_1.id)
expect(mr_2.reload.head_pipeline_id).to eq(pipeline_3.id)
expect(mr_3.reload.head_pipeline_id).to eq(pipeline_4.id)
expect(mr_4.reload.head_pipeline_id).to be_nil
end
end
end
end
......@@ -760,8 +760,7 @@ describe MergeRequest, models: true do
describe '#head_pipeline' do
describe 'when the source project exists' do
it 'returns the latest pipeline' do
sha = "123abc"
pipeline = create(:ci_empty_pipeline, project: subject.source_project, ref: 'master', status: 'running', sha: sha)
pipeline = create(:ci_empty_pipeline, project: subject.source_project, ref: 'master', status: 'running', sha: "123abc")
subject.update(head_pipeline: pipeline)
expect(subject.head_pipeline).to eq(pipeline)
......
......@@ -34,16 +34,40 @@ describe Ci::CreatePipelineService, services: true do
it { expect(pipeline).to have_attributes(status: 'pending') }
it { expect(pipeline.builds.first).to be_kind_of(Ci::Build) }
it 'updates head pipeline of each merge request' do
merge_request_1 = create(:merge_request, source_branch: 'master', target_branch: "branch_1", source_project: project)
merge_request_2 = create(:merge_request, source_branch: 'master', target_branch: "branch_2", source_project: project)
merge_request_3 = create(:merge_request, source_branch: 'other_branch', target_branch: "branch_2", source_project: project)
context '#update_merge_requests_head_pipeline' do
it 'updates head pipeline of each merge request' do
merge_request_1 = create(:merge_request, source_branch: 'master', target_branch: "branch_1", source_project: project)
merge_request_2 = create(:merge_request, source_branch: 'master', target_branch: "branch_2", source_project: project)
head_pipeline = pipeline
head_pipeline = pipeline
expect(merge_request_1.reload.head_pipeline).to eq(head_pipeline)
expect(merge_request_2.reload.head_pipeline).to eq(head_pipeline)
expect(merge_request_3.reload.head_pipeline).to be_nil
expect(merge_request_1.reload.head_pipeline).to eq(head_pipeline)
expect(merge_request_2.reload.head_pipeline).to eq(head_pipeline)
end
context 'when there is no pipeline for source branch' do
it "does not update merge request head pipeline" do
merge_request = create(:merge_request, source_branch: 'other_branch', target_branch: "branch_1", source_project: project)
head_pipeline = pipeline
expect(merge_request.reload.head_pipeline).not_to eq(head_pipeline)
end
end
context 'when merge request target project is different from source project' do
let!(:target_project) { create(:empty_project) }
let!(:forked_project_link) { create(:forked_project_link, forked_to_project: project, forked_from_project: target_project) }
it 'updates head pipeline for merge request' do
merge_request =
create(:merge_request, source_branch: 'master', target_branch: "branch_1", source_project: project, target_project: target_project)
head_pipeline = pipeline
expect(merge_request.reload.head_pipeline).to eq(head_pipeline)
end
end
end
context 'auto-cancel enabled' do
......
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