From ccb9beeed0e418ef4dea201b3507bd2f4a14b4a2 Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Tue, 18 Apr 2017 21:26:56 +0200 Subject: Properly expire cache for **all** MRs of a pipeline Turn ExpirePipelineCacheService into Worker so it can fetch all the merge requests for which the pipeline runs or did run against. --- spec/models/ci/pipeline_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'spec/models/ci') diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index d7d6a75d38d..42c0791fba1 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -376,8 +376,8 @@ describe Ci::Pipeline, models: true do end describe 'pipeline caching' do - it 'executes ExpirePipelinesCacheService' do - expect_any_instance_of(Ci::ExpirePipelineCacheService).to receive(:execute).with(pipeline) + it 'performs ExpirePipelinesCacheWorker' do + expect(ExpirePipelineCacheWorker).to receive(:perform_async).with(pipeline.id) pipeline.cancel end -- cgit v1.2.1 From c623c41c2f917e0773a6e3f0b6c78b027ca846f7 Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Thu, 20 Apr 2017 08:44:01 +0200 Subject: Use a better performing query to find all MRs for pipeline And add some specs. --- spec/models/ci/pipeline_spec.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'spec/models/ci') diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 42c0791fba1..be14fdca81c 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -1037,6 +1037,30 @@ describe Ci::Pipeline, models: true do end end + describe "#all_merge_requests" do + let(:project) { create(:project, :repository) } + let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master', sha: project.repository.commit('master').id) } + + it "returns merge requests whose `diff_head_sha` matches the pipeline's SHA" do + merge_request = create(:merge_request, source_project: project, source_branch: pipeline.ref) + + expect(pipeline.all_merge_requests).to eq([merge_request]) + end + + it "returns merge requests whose source branch matches the pipeline's source branch" do + pipeline = create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master', sha: project.repository.commit('master^').id) + merge_request = create(:merge_request, source_project: project, source_branch: pipeline.ref) + + expect(pipeline.all_merge_requests).to eq([merge_request]) + end + + it "doesn't return merge requests whose source branch doesn't match the pipeline's ref" do + create(:merge_request, source_project: project, source_branch: 'feature', target_branch: 'master') + + expect(pipeline.all_merge_requests).to be_empty + end + end + describe '#stuck?' do before do create(:ci_build, :pending, pipeline: pipeline) -- cgit v1.2.1 From 14642e3c28ae40d2ecf409c327e29e6c3add63fa Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 21 Apr 2017 17:39:19 +0200 Subject: Refactor ExpirePipelineCacheWorker#perform Make it gracefully handle unexisting pipelines and refactor iterating all the merge request paths. --- spec/models/ci/pipeline_spec.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'spec/models/ci') diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index be14fdca81c..8b9b51bcf2f 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -1014,11 +1014,12 @@ describe Ci::Pipeline, models: true do end describe "#merge_requests" do - let(:project) { create(:project, :repository) } - let(:pipeline) { FactoryGirl.create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master', sha: project.repository.commit('master').id) } + let(:project) { create(:empty_project) } + let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master', sha: 'a288a022a53a5a944fae87bcec6efc87b7061808') } it "returns merge requests whose `diff_head_sha` matches the pipeline's SHA" do merge_request = create(:merge_request, source_project: project, source_branch: pipeline.ref) + allow_any_instance_of(MergeRequest).to receive(:diff_head_sha) { 'a288a022a53a5a944fae87bcec6efc87b7061808' } expect(pipeline.merge_requests).to eq([merge_request]) end @@ -1038,23 +1039,24 @@ describe Ci::Pipeline, models: true do end describe "#all_merge_requests" do - let(:project) { create(:project, :repository) } - let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master', sha: project.repository.commit('master').id) } + let(:project) { create(:empty_project) } + let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master', sha: 'a288a022a53a5a944fae87bcec6efc87b7061808') } - it "returns merge requests whose `diff_head_sha` matches the pipeline's SHA" do + it "returns merge request if pipeline runs on `diff_head_sha`" do merge_request = create(:merge_request, source_project: project, source_branch: pipeline.ref) + allow_any_instance_of(MergeRequest).to receive(:diff_head_sha) { 'a288a022a53a5a944fae87bcec6efc87b7061808' } expect(pipeline.all_merge_requests).to eq([merge_request]) end - it "returns merge requests whose source branch matches the pipeline's source branch" do - pipeline = create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master', sha: project.repository.commit('master^').id) + it "returns merge request if pipeline runs any commit of the `source_branch`" do merge_request = create(:merge_request, source_project: project, source_branch: pipeline.ref) + allow_any_instance_of(MergeRequest).to receive(:diff_head_sha) { '97de212e80737a608d939f648d959671fb0a0142b' } expect(pipeline.all_merge_requests).to eq([merge_request]) end - it "doesn't return merge requests whose source branch doesn't match the pipeline's ref" do + it "doesn't return merge request if pipeline runs on a different `source_branch`" do create(:merge_request, source_project: project, source_branch: 'feature', target_branch: 'master') expect(pipeline.all_merge_requests).to be_empty -- cgit v1.2.1 From 956624688dec0d63024a424accc6a52b7bf04927 Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Mon, 24 Apr 2017 10:18:37 +0200 Subject: Simplify specs and remove unnecessary attributes --- spec/models/ci/pipeline_spec.rb | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to 'spec/models/ci') diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 8b9b51bcf2f..8e1695e0c70 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -1040,23 +1040,15 @@ describe Ci::Pipeline, models: true do describe "#all_merge_requests" do let(:project) { create(:empty_project) } - let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master', sha: 'a288a022a53a5a944fae87bcec6efc87b7061808') } - - it "returns merge request if pipeline runs on `diff_head_sha`" do - merge_request = create(:merge_request, source_project: project, source_branch: pipeline.ref) - allow_any_instance_of(MergeRequest).to receive(:diff_head_sha) { 'a288a022a53a5a944fae87bcec6efc87b7061808' } + let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master') } - expect(pipeline.all_merge_requests).to eq([merge_request]) - end - - it "returns merge request if pipeline runs any commit of the `source_branch`" do + it "returns all merge requests having the same source branch" do merge_request = create(:merge_request, source_project: project, source_branch: pipeline.ref) - allow_any_instance_of(MergeRequest).to receive(:diff_head_sha) { '97de212e80737a608d939f648d959671fb0a0142b' } expect(pipeline.all_merge_requests).to eq([merge_request]) end - it "doesn't return merge request if pipeline runs on a different `source_branch`" do + it "doesn't return merge requests having a different source branch" do create(:merge_request, source_project: project, source_branch: 'feature', target_branch: 'master') expect(pipeline.all_merge_requests).to be_empty -- cgit v1.2.1