2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-12-25 04:48:26 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Releases::CreateService do
|
2018-12-25 04:48:26 -05:00
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:tag_name) { project.repository.tag_names.first }
|
|
|
|
let(:tag_sha) { project.repository.find_tag(tag_name).dereferenced_target.sha }
|
|
|
|
let(:name) { 'Bionic Beaver' }
|
|
|
|
let(:description) { 'Awesome release!' }
|
|
|
|
let(:params) { { tag: tag_name, name: name, description: description, ref: ref } }
|
|
|
|
let(:ref) { nil }
|
|
|
|
let(:service) { described_class.new(project, user, params) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
shared_examples 'a successful release creation' do
|
|
|
|
it 'creates a new release' do
|
2020-05-19 14:08:11 -04:00
|
|
|
expected_job_count = MailScheduler::NotificationServiceWorker.jobs.size + 1
|
|
|
|
|
2020-11-06 19:08:58 -05:00
|
|
|
expect_next_instance_of(Release) do |release|
|
|
|
|
expect(release)
|
|
|
|
.to receive(:execute_hooks)
|
|
|
|
.with('create')
|
|
|
|
end
|
|
|
|
|
2018-12-25 04:48:26 -05:00
|
|
|
result = service.execute
|
2019-04-09 02:52:15 -04:00
|
|
|
|
|
|
|
expect(project.releases.count).to eq(1)
|
2018-12-25 04:48:26 -05:00
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
expect(result[:tag]).not_to be_nil
|
|
|
|
expect(result[:release]).not_to be_nil
|
|
|
|
expect(result[:release].description).to eq(description)
|
|
|
|
expect(result[:release].name).to eq(name)
|
|
|
|
expect(result[:release].author).to eq(user)
|
|
|
|
expect(result[:release].sha).to eq(tag_sha)
|
2020-05-19 14:08:11 -04:00
|
|
|
expect(MailScheduler::NotificationServiceWorker.jobs.size).to eq(expected_job_count)
|
2018-12-25 04:48:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a successful release creation'
|
|
|
|
|
|
|
|
context 'when the tag does not exist' do
|
|
|
|
let(:tag_name) { 'non-exist-tag' }
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ref is provided' do
|
|
|
|
let(:ref) { 'master' }
|
|
|
|
let(:tag_name) { 'foobar' }
|
|
|
|
|
|
|
|
it_behaves_like 'a successful release creation'
|
|
|
|
|
|
|
|
it 'creates a tag if the tag does not exist' do
|
|
|
|
expect(project.repository.ref_exists?("refs/tags/#{tag_name}")).to be_falsey
|
|
|
|
|
|
|
|
result = service.execute
|
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
expect(result[:tag]).not_to be_nil
|
|
|
|
expect(result[:release]).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'there already exists a release on a tag' do
|
|
|
|
let!(:release) do
|
|
|
|
create(:release, project: project, tag: tag_name, description: description)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error and does not update the release' do
|
|
|
|
result = service.execute
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(project.releases.find_by(tag: tag_name).description).to eq(description)
|
|
|
|
end
|
|
|
|
end
|
2019-09-03 05:38:59 -04:00
|
|
|
|
|
|
|
context 'when a passed-in milestone does not exist for this project' do
|
|
|
|
it 'raises an error saying the milestone is inexistent' do
|
2019-09-16 11:06:26 -04:00
|
|
|
inexistent_milestone_tag = 'v111.0'
|
|
|
|
service = described_class.new(project, user, params.merge!({ milestones: [inexistent_milestone_tag] }))
|
2019-09-03 05:38:59 -04:00
|
|
|
result = service.execute
|
2019-09-16 11:06:26 -04:00
|
|
|
|
2019-09-03 05:38:59 -04:00
|
|
|
expect(result[:status]).to eq(:error)
|
2019-09-16 11:06:26 -04:00
|
|
|
expect(result[:message]).to eq("Milestone(s) not found: #{inexistent_milestone_tag}")
|
2019-09-03 05:38:59 -04:00
|
|
|
end
|
|
|
|
end
|
2018-12-25 04:48:26 -05:00
|
|
|
end
|
2019-04-09 02:52:15 -04:00
|
|
|
|
|
|
|
describe '#find_or_build_release' do
|
|
|
|
it 'does not save the built release' do
|
|
|
|
service.find_or_build_release
|
|
|
|
|
|
|
|
expect(project.releases.count).to eq(0)
|
|
|
|
end
|
2019-09-03 05:38:59 -04:00
|
|
|
|
|
|
|
context 'when existing milestone is passed in' do
|
|
|
|
let(:title) { 'v1.0' }
|
|
|
|
let(:milestone) { create(:milestone, :active, project: project, title: title) }
|
2019-09-16 11:06:26 -04:00
|
|
|
let(:params_with_milestone) { params.merge!({ milestones: [title] }) }
|
|
|
|
let(:service) { described_class.new(milestone.project, user, params_with_milestone) }
|
2019-09-03 05:38:59 -04:00
|
|
|
|
|
|
|
it 'creates a release and ties this milestone to it' do
|
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(project.releases.count).to eq(1)
|
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
|
|
|
|
release = project.releases.last
|
|
|
|
|
2019-09-16 11:06:26 -04:00
|
|
|
expect(release.milestones).to match_array([milestone])
|
2019-09-03 05:38:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when another release was previously created with that same milestone linked' do
|
|
|
|
it 'also creates another release tied to that same milestone' do
|
2019-09-16 11:06:26 -04:00
|
|
|
other_release = create(:release, milestones: [milestone], project: project, tag: 'v1.0')
|
2019-09-03 05:38:59 -04:00
|
|
|
service.execute
|
|
|
|
release = project.releases.last
|
|
|
|
|
2019-09-16 11:06:26 -04:00
|
|
|
expect(release.milestones).to match_array([milestone])
|
|
|
|
expect(other_release.milestones).to match_array([milestone])
|
2019-09-03 05:38:59 -04:00
|
|
|
expect(release.id).not_to eq(other_release.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-16 11:06:26 -04:00
|
|
|
context 'when multiple existing milestone titles are passed in' do
|
|
|
|
let(:title_1) { 'v1.0' }
|
|
|
|
let(:title_2) { 'v1.0-rc' }
|
|
|
|
let!(:milestone_1) { create(:milestone, :active, project: project, title: title_1) }
|
|
|
|
let!(:milestone_2) { create(:milestone, :active, project: project, title: title_2) }
|
|
|
|
let!(:params_with_milestones) { params.merge!({ milestones: [title_1, title_2] }) }
|
|
|
|
|
|
|
|
it 'creates a release and ties it to these milestones' do
|
|
|
|
described_class.new(project, user, params_with_milestones).execute
|
|
|
|
release = project.releases.last
|
|
|
|
|
|
|
|
expect(release.milestones.map(&:title)).to include(title_1, title_2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-26 04:14:20 -05:00
|
|
|
context 'when multiple milestone titles are passed in but one of them does not exist' do
|
2019-09-16 11:06:26 -04:00
|
|
|
let(:title) { 'v1.0' }
|
|
|
|
let(:inexistent_title) { 'v111.0' }
|
|
|
|
let!(:milestone) { create(:milestone, :active, project: project, title: title) }
|
|
|
|
let!(:params_with_milestones) { params.merge!({ milestones: [title, inexistent_title] }) }
|
|
|
|
let(:service) { described_class.new(milestone.project, user, params_with_milestones) }
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq("Milestone(s) not found: #{inexistent_title}")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create any release' do
|
|
|
|
expect do
|
|
|
|
service.execute
|
|
|
|
end.not_to change(Release, :count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-18 04:09:02 -05:00
|
|
|
context 'no milestone association behavior' do
|
|
|
|
let(:title_1) { 'v1.0' }
|
|
|
|
let(:title_2) { 'v1.0-rc' }
|
|
|
|
let!(:milestone_1) { create(:milestone, :active, project: project, title: title_1) }
|
|
|
|
let!(:milestone_2) { create(:milestone, :active, project: project, title: title_2) }
|
2019-09-16 11:06:26 -04:00
|
|
|
|
2020-11-18 04:09:02 -05:00
|
|
|
context 'when no milestones parameter is passed' do
|
|
|
|
it 'creates a release without a milestone tied to it' do
|
|
|
|
expect(service.param_for_milestone_titles_provided?).to be_falsey
|
2019-09-16 11:06:26 -04:00
|
|
|
|
2020-11-18 04:09:02 -05:00
|
|
|
service.execute
|
|
|
|
release = project.releases.last
|
|
|
|
|
|
|
|
expect(release.milestones).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create any new MilestoneRelease object' do
|
|
|
|
expect { service.execute }.not_to change { MilestoneRelease.count }
|
|
|
|
end
|
2019-09-03 05:38:59 -04:00
|
|
|
end
|
|
|
|
|
2020-11-18 04:09:02 -05:00
|
|
|
context 'when an empty array is passed as the milestones parameter' do
|
|
|
|
it 'creates a release without a milestone tied to it' do
|
|
|
|
service = described_class.new(project, user, params.merge!({ milestones: [] }))
|
|
|
|
service.execute
|
|
|
|
release = project.releases.last
|
|
|
|
|
|
|
|
expect(release.milestones).to be_empty
|
|
|
|
end
|
2019-09-03 05:38:59 -04:00
|
|
|
end
|
|
|
|
|
2020-11-18 04:09:02 -05:00
|
|
|
context 'when nil is passed as the milestones parameter' do
|
|
|
|
it 'creates a release without a milestone tied to it' do
|
|
|
|
expect(service.param_for_milestone_titles_provided?).to be_falsey
|
2019-09-16 11:06:26 -04:00
|
|
|
|
2020-11-18 04:09:02 -05:00
|
|
|
service = described_class.new(project, user, params.merge!({ milestones: nil }))
|
|
|
|
service.execute
|
|
|
|
release = project.releases.last
|
|
|
|
|
|
|
|
expect(release.milestones).to be_empty
|
|
|
|
end
|
2019-09-03 05:38:59 -04:00
|
|
|
end
|
|
|
|
end
|
2019-04-09 02:52:15 -04:00
|
|
|
end
|
2020-05-21 20:08:07 -04:00
|
|
|
|
|
|
|
context 'Evidence collection' do
|
2020-06-16 11:08:32 -04:00
|
|
|
let(:sha) { project.repository.commit('master').sha }
|
2020-05-21 20:08:07 -04:00
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
name: 'New release',
|
|
|
|
ref: 'master',
|
|
|
|
tag: 'v0.1',
|
|
|
|
description: 'Super nice release',
|
|
|
|
released_at: released_at
|
|
|
|
}.compact
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2020-05-21 20:08:07 -04:00
|
|
|
let(:last_release) { project.releases.last }
|
|
|
|
|
|
|
|
around do |example|
|
2020-08-31 11:10:41 -04:00
|
|
|
freeze_time { example.run }
|
2020-05-21 20:08:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
subject { service.execute }
|
|
|
|
|
|
|
|
context 'historical release' do
|
|
|
|
let(:released_at) { 3.weeks.ago }
|
|
|
|
|
|
|
|
it 'does not execute CreateEvidenceWorker' do
|
2020-11-18 16:09:32 -05:00
|
|
|
expect { subject }.not_to change(Releases::CreateEvidenceWorker.jobs, :size)
|
2020-05-21 20:08:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create an Evidence object', :sidekiq_inline do
|
|
|
|
expect { subject }.not_to change(Releases::Evidence, :count)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is a historical release' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(last_release.historical_release?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not an upcoming release' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(last_release.upcoming_release?).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
shared_examples 'uses the right pipeline for evidence' do
|
|
|
|
it 'creates evidence without pipeline if it does not exist', :sidekiq_inline do
|
|
|
|
expect_next_instance_of(Releases::CreateEvidenceService, anything, pipeline: nil) do |service|
|
|
|
|
expect(service).to receive(:execute).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { subject }.to change(Releases::Evidence, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the last pipeline for evidence', :sidekiq_inline do
|
|
|
|
create(:ci_empty_pipeline, sha: sha, project: project) # old pipeline
|
|
|
|
pipeline = create(:ci_empty_pipeline, sha: sha, project: project)
|
|
|
|
|
|
|
|
expect_next_instance_of(Releases::CreateEvidenceService, anything, pipeline: pipeline) do |service|
|
|
|
|
expect(service).to receive(:execute).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { subject }.to change(Releases::Evidence, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when old evidence_pipeline is passed to service' do
|
|
|
|
let!(:old_pipeline) { create(:ci_empty_pipeline, sha: sha, project: project) }
|
|
|
|
let!(:new_pipeline) { create(:ci_empty_pipeline, sha: sha, project: project) }
|
|
|
|
let(:params) do
|
|
|
|
super().merge(
|
|
|
|
evidence_pipeline: old_pipeline
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the old pipeline for evidence', :sidekiq_inline do
|
|
|
|
expect_next_instance_of(Releases::CreateEvidenceService, anything, pipeline: old_pipeline) do |service|
|
|
|
|
expect(service).to receive(:execute).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { subject }.to change(Releases::Evidence, :count).by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'pipeline is still being used for evidence if new pipeline is being created for tag', :sidekiq_inline do
|
|
|
|
pipeline = create(:ci_empty_pipeline, sha: sha, project: project)
|
|
|
|
|
|
|
|
expect(project.repository).to receive(:add_tag).and_wrap_original do |m, *args|
|
|
|
|
create(:ci_empty_pipeline, sha: sha, project: project)
|
|
|
|
m.call(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect_next_instance_of(Releases::CreateEvidenceService, anything, pipeline: pipeline) do |service|
|
|
|
|
expect(service).to receive(:execute).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { subject }.to change(Releases::Evidence, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the last pipeline for evidence when tag is already created', :sidekiq_inline do
|
|
|
|
Tags::CreateService.new(project, user).execute('v0.1', 'master', nil)
|
|
|
|
|
|
|
|
expect(project.repository.find_tag('v0.1')).to be_present
|
|
|
|
|
|
|
|
create(:ci_empty_pipeline, sha: sha, project: project) # old pipeline
|
|
|
|
pipeline = create(:ci_empty_pipeline, sha: sha, project: project)
|
|
|
|
|
|
|
|
expect_next_instance_of(Releases::CreateEvidenceService, anything, pipeline: pipeline) do |service|
|
|
|
|
expect(service).to receive(:execute).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { subject }.to change(Releases::Evidence, :count).by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-21 20:08:07 -04:00
|
|
|
context 'immediate release' do
|
|
|
|
let(:released_at) { nil }
|
|
|
|
|
|
|
|
it 'sets `released_at` to the current dttm' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(last_release.updated_at).to be_like_time(Time.current)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'queues CreateEvidenceWorker' do
|
2020-11-18 16:09:32 -05:00
|
|
|
expect { subject }.to change(Releases::CreateEvidenceWorker.jobs, :size).by(1)
|
2020-05-21 20:08:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates Evidence', :sidekiq_inline do
|
|
|
|
expect { subject }.to change(Releases::Evidence, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not a historical release' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(last_release.historical_release?).to be_falsy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not an upcoming release' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(last_release.upcoming_release?).to be_falsy
|
|
|
|
end
|
2020-06-16 11:08:32 -04:00
|
|
|
|
|
|
|
include_examples 'uses the right pipeline for evidence'
|
2020-05-21 20:08:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'upcoming release' do
|
|
|
|
let(:released_at) { 1.day.from_now }
|
|
|
|
|
2020-11-18 16:09:32 -05:00
|
|
|
it 'does not execute CreateEvidenceWorker' do
|
|
|
|
expect { subject }.not_to change(Releases::CreateEvidenceWorker.jobs, :size)
|
2020-05-21 20:08:07 -04:00
|
|
|
end
|
|
|
|
|
2020-11-18 16:09:32 -05:00
|
|
|
it 'does not create an Evidence object', :sidekiq_inline do
|
|
|
|
expect { subject }.not_to change(Releases::Evidence, :count)
|
2020-05-21 20:08:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not a historical release' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(last_release.historical_release?).to be_falsy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is an upcoming release' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(last_release.upcoming_release?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-25 04:48:26 -05:00
|
|
|
end
|