2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Deployments::LinkMergeRequestsService do
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
|
|
|
|
# * ddd0f15 Merge branch 'po-fix-test-env-path' into 'master'
|
|
|
|
# |\
|
|
|
|
# | * 2d1db52 Correct test_env.rb path for adding branch
|
|
|
|
# |/
|
|
|
|
# * 1e292f8 Merge branch 'cherry-pick-ce369011' into 'master'
|
|
|
|
# |\
|
|
|
|
# | * c1c67ab Add file with a _flattable_ path
|
|
|
|
# |/
|
|
|
|
# * 7975be0 Merge branch 'rd-add-file-larger-than-1-mb' into 'master'
|
|
|
|
let_it_be(:first_deployment_sha) { '7975be0116940bf2ad4321f79d02a55c5f7779aa' }
|
|
|
|
let_it_be(:mr1_merge_commit_sha) { '1e292f8fedd741b75372e19097c76d327140c312' }
|
|
|
|
let_it_be(:mr2_merge_commit_sha) { 'ddd0f15ae83993f5cb66a927a28673882e99100b' }
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
describe '#execute' do
|
2020-03-13 15:44:24 +05:30
|
|
|
context 'when the deployment is for a review environment' do
|
2019-12-26 22:10:19 +05:30
|
|
|
it 'does nothing' do
|
2020-03-13 15:44:24 +05:30
|
|
|
environment =
|
|
|
|
create(:environment, environment_type: 'review', name: 'review/foo')
|
|
|
|
|
|
|
|
deploy = create(:deployment, :success, environment: environment)
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
expect(deploy).not_to receive(:link_merge_requests)
|
|
|
|
|
|
|
|
described_class.new(deploy).execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
context 'when the deployment is for one of the production environments' do
|
|
|
|
it 'links merge requests' do
|
|
|
|
environment =
|
|
|
|
create(:environment, environment_type: 'production', name: 'production/gcp')
|
|
|
|
|
|
|
|
deploy = create(:deployment, :success, environment: environment)
|
|
|
|
|
|
|
|
expect(deploy).to receive(:link_merge_requests).once
|
|
|
|
|
|
|
|
described_class.new(deploy).execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
context 'when the deployment failed' do
|
|
|
|
it 'does nothing' do
|
|
|
|
environment = create(:environment, name: 'foo')
|
|
|
|
deploy = create(:deployment, :failed, environment: environment)
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
expect(deploy).not_to receive(:link_merge_requests)
|
|
|
|
|
|
|
|
described_class.new(deploy).execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a previous deployment' do
|
|
|
|
it 'links all merge requests merged since the previous deployment' do
|
2020-03-13 15:44:24 +05:30
|
|
|
deploy1 = create(
|
|
|
|
:deployment,
|
|
|
|
:success,
|
|
|
|
project: project,
|
|
|
|
sha: first_deployment_sha
|
|
|
|
)
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
deploy2 = create(
|
|
|
|
:deployment,
|
|
|
|
:success,
|
|
|
|
project: deploy1.project,
|
2020-03-13 15:44:24 +05:30
|
|
|
environment: deploy1.environment,
|
|
|
|
sha: mr2_merge_commit_sha
|
2019-12-26 22:10:19 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
service = described_class.new(deploy2)
|
|
|
|
|
|
|
|
expect(service)
|
|
|
|
.to receive(:link_merge_requests_for_range)
|
2020-03-13 15:44:24 +05:30
|
|
|
.with(first_deployment_sha, mr2_merge_commit_sha)
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are no previous deployments' do
|
|
|
|
it 'links all merged merge requests' do
|
2020-03-13 15:44:24 +05:30
|
|
|
deploy = create(:deployment, :success, project: project)
|
2019-12-26 22:10:19 +05:30
|
|
|
service = described_class.new(deploy)
|
|
|
|
|
|
|
|
expect(service).to receive(:link_all_merged_merge_requests)
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#link_merge_requests_for_range' do
|
|
|
|
it 'links merge requests' do
|
|
|
|
environment = create(:environment, project: project)
|
|
|
|
deploy =
|
|
|
|
create(:deployment, :success, project: project, environment: environment)
|
|
|
|
|
|
|
|
mr1 = create(
|
|
|
|
:merge_request,
|
|
|
|
:merged,
|
2020-03-13 15:44:24 +05:30
|
|
|
merge_commit_sha: mr1_merge_commit_sha,
|
2019-12-26 22:10:19 +05:30
|
|
|
source_project: project,
|
|
|
|
target_project: project
|
|
|
|
)
|
|
|
|
|
|
|
|
mr2 = create(
|
|
|
|
:merge_request,
|
|
|
|
:merged,
|
2020-03-13 15:44:24 +05:30
|
|
|
merge_commit_sha: mr2_merge_commit_sha,
|
2019-12-26 22:10:19 +05:30
|
|
|
source_project: project,
|
|
|
|
target_project: project
|
|
|
|
)
|
|
|
|
|
|
|
|
described_class.new(deploy).link_merge_requests_for_range(
|
2020-03-13 15:44:24 +05:30
|
|
|
first_deployment_sha,
|
|
|
|
mr2_merge_commit_sha
|
2019-12-26 22:10:19 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
expect(deploy.merge_requests).to include(mr1, mr2)
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
it 'links picked merge requests' do
|
|
|
|
environment = create(:environment, project: project)
|
|
|
|
deploy =
|
|
|
|
create(:deployment, :success, project: project, environment: environment)
|
|
|
|
|
|
|
|
picked_mr = create(
|
|
|
|
:merge_request,
|
|
|
|
:merged,
|
|
|
|
merge_commit_sha: '123abc',
|
|
|
|
source_project: project,
|
|
|
|
target_project: project
|
|
|
|
)
|
|
|
|
|
|
|
|
mr1 = create(
|
|
|
|
:merge_request,
|
|
|
|
:merged,
|
|
|
|
merge_commit_sha: mr1_merge_commit_sha,
|
|
|
|
source_project: project,
|
|
|
|
target_project: project
|
|
|
|
)
|
|
|
|
|
|
|
|
# mr1 includes c1c67abba which is a cherry-pick of the fake picked_mr merge request
|
|
|
|
create(:track_mr_picking_note, noteable: picked_mr, project: project, commit_id: 'c1c67abbaf91f624347bb3ae96eabe3a1b742478')
|
|
|
|
|
|
|
|
described_class.new(deploy).link_merge_requests_for_range(
|
|
|
|
first_deployment_sha,
|
|
|
|
mr1_merge_commit_sha
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(deploy.merge_requests).to include(mr1, picked_mr)
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it "doesn't link the same merge_request twice" do
|
|
|
|
create(:merge_request, :merged, merge_commit_sha: mr1_merge_commit_sha,
|
|
|
|
source_project: project)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
picked_mr = create(:merge_request, :merged, merge_commit_sha: '123abc',
|
|
|
|
source_project: project)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
# the first MR includes c1c67abba which is a cherry-pick of the fake picked_mr merge request
|
|
|
|
create(:track_mr_picking_note, noteable: picked_mr, project: project, commit_id: 'c1c67abbaf91f624347bb3ae96eabe3a1b742478')
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
environment = create(:environment, project: project)
|
|
|
|
old_deploy =
|
|
|
|
create(:deployment, :success, project: project, environment: environment)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
# manually linking all the MRs to the old_deploy
|
|
|
|
old_deploy.link_merge_requests(project.merge_requests)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
deploy =
|
|
|
|
create(:deployment, :success, project: project, environment: environment)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
described_class.new(deploy).link_merge_requests_for_range(
|
|
|
|
first_deployment_sha,
|
|
|
|
mr1_merge_commit_sha
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(deploy.merge_requests).to be_empty
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#link_all_merged_merge_requests' do
|
|
|
|
it 'links all merged merge requests targeting the deployed branch' do
|
|
|
|
environment = create(:environment, project: project)
|
|
|
|
deploy =
|
|
|
|
create(:deployment, :success, project: project, environment: environment)
|
|
|
|
|
|
|
|
mr1 = create(
|
|
|
|
:merge_request,
|
|
|
|
:merged,
|
|
|
|
source_project: project,
|
|
|
|
target_project: project,
|
|
|
|
source_branch: 'source1',
|
|
|
|
target_branch: deploy.ref
|
|
|
|
)
|
|
|
|
|
|
|
|
mr2 = create(
|
|
|
|
:merge_request,
|
|
|
|
:merged,
|
|
|
|
source_project: project,
|
|
|
|
target_project: project,
|
|
|
|
source_branch: 'source2',
|
|
|
|
target_branch: deploy.ref
|
|
|
|
)
|
|
|
|
|
|
|
|
mr3 = create(
|
|
|
|
:merge_request,
|
|
|
|
:merged,
|
|
|
|
source_project: project,
|
|
|
|
target_project: project,
|
|
|
|
target_branch: 'foo'
|
|
|
|
)
|
|
|
|
|
|
|
|
described_class.new(deploy).link_all_merged_merge_requests
|
|
|
|
|
|
|
|
expect(deploy.merge_requests).to include(mr1, mr2)
|
|
|
|
expect(deploy.merge_requests).not_to include(mr3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|