2019-09-30 05:06:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-21 08:26:47 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-01-25 19:08:57 -05:00
|
|
|
RSpec.describe Ci::PipelineEntity do
|
2019-03-01 05:05:04 -05:00
|
|
|
include Gitlab::Routing
|
|
|
|
|
2020-02-24 22:08:49 -05:00
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
2021-04-14 11:09:04 -04:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
let(:request) { double('request', current_user: user) }
|
|
|
|
let(:entity) { described_class.represent(pipeline, request: request) }
|
2016-12-21 08:26:47 -05:00
|
|
|
|
|
|
|
describe '#as_json' do
|
|
|
|
subject { entity.as_json }
|
|
|
|
|
|
|
|
context 'when pipeline is empty' do
|
|
|
|
let(:pipeline) { create(:ci_empty_pipeline) }
|
|
|
|
|
|
|
|
it 'contains required fields' do
|
2021-08-03 11:10:03 -04:00
|
|
|
expect(subject).to include :id, :iid, :user, :path, :coverage, :source
|
2016-12-21 08:26:47 -05:00
|
|
|
expect(subject).to include :ref, :commit
|
|
|
|
expect(subject).to include :updated_at, :created_at
|
|
|
|
end
|
|
|
|
|
2018-05-11 10:17:03 -04:00
|
|
|
it 'excludes coverage data when disabled' do
|
|
|
|
entity = described_class
|
|
|
|
.represent(pipeline, request: request, disable_coverage: true)
|
|
|
|
|
|
|
|
expect(entity.as_json).not_to include(:coverage)
|
|
|
|
end
|
|
|
|
|
2016-12-21 08:26:47 -05:00
|
|
|
it 'contains details' do
|
|
|
|
expect(subject).to include :details
|
|
|
|
expect(subject[:details])
|
2019-08-28 03:51:26 -04:00
|
|
|
.to include :duration, :finished_at, :name
|
2018-04-05 17:04:42 -04:00
|
|
|
expect(subject[:details][:status]).to include :icon, :favicon, :text, :label, :tooltip
|
2016-12-21 08:26:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains flags' do
|
|
|
|
expect(subject).to include :flags
|
|
|
|
expect(subject[:flags])
|
2019-05-06 04:08:42 -04:00
|
|
|
.to include :stuck, :auto_devops, :yaml_errors,
|
|
|
|
:retryable, :cancelable, :merge_request
|
2016-12-21 08:26:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
context 'when default branch not protected' do
|
2016-12-21 08:26:47 -05:00
|
|
|
before do
|
2021-01-22 13:09:10 -05:00
|
|
|
stub_not_protect_default_branch
|
2016-12-21 08:26:47 -05:00
|
|
|
end
|
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
context 'when pipeline is retryable' do
|
|
|
|
let_it_be(:pipeline) do
|
|
|
|
create(:ci_pipeline, status: :success, project: project)
|
2019-05-06 11:36:02 -04:00
|
|
|
end
|
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2021-01-22 13:09:10 -05:00
|
|
|
create(:ci_build, :failed, pipeline: pipeline)
|
2016-12-21 08:26:47 -05:00
|
|
|
end
|
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
it 'does not serialize stage builds' do
|
|
|
|
subject.with_indifferent_access.dig(:details, :stages, 0).tap do |stage|
|
|
|
|
expect(stage).not_to include(:groups, :latest_statuses, :retries)
|
|
|
|
end
|
2016-12-21 08:26:47 -05:00
|
|
|
end
|
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
context 'user has ability to retry pipeline' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
2016-12-21 08:26:47 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
it 'contains retry path' do
|
|
|
|
expect(subject[:retry_path]).to be_present
|
|
|
|
end
|
|
|
|
end
|
2016-12-21 08:26:47 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
context 'user does not have ability to retry pipeline' do
|
|
|
|
it 'does not contain retry path' do
|
|
|
|
expect(subject).not_to have_key(:retry_path)
|
|
|
|
end
|
|
|
|
end
|
2016-12-21 08:26:47 -05:00
|
|
|
end
|
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
context 'when pipeline is cancelable' do
|
|
|
|
let_it_be(:pipeline) do
|
|
|
|
create(:ci_pipeline, status: :running, project: project)
|
2019-05-06 11:36:02 -04:00
|
|
|
end
|
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2021-01-22 13:09:10 -05:00
|
|
|
create(:ci_build, :pending, pipeline: pipeline)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2016-12-21 08:26:47 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
it 'does not serialize stage builds' do
|
|
|
|
subject.with_indifferent_access.dig(:details, :stages, 0).tap do |stage|
|
|
|
|
expect(stage).not_to include(:groups, :latest_statuses, :retries)
|
|
|
|
end
|
2016-12-21 08:26:47 -05:00
|
|
|
end
|
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
context 'user has ability to cancel pipeline' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains cancel path' do
|
|
|
|
expect(subject[:cancel_path]).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'user does not have ability to cancel pipeline' do
|
|
|
|
it 'does not contain cancel path' do
|
|
|
|
expect(subject).not_to have_key(:cancel_path)
|
|
|
|
end
|
2016-12-21 08:26:47 -05:00
|
|
|
end
|
2020-01-08 07:07:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'delete path' do
|
|
|
|
context 'user has ability to delete pipeline' do
|
|
|
|
let(:project) { create(:project, namespace: user.namespace) }
|
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project) }
|
|
|
|
|
|
|
|
it 'contains delete path' do
|
|
|
|
expect(subject[:delete_path]).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'user does not have ability to delete pipeline' do
|
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project) }
|
|
|
|
|
|
|
|
it 'does not contain delete path' do
|
|
|
|
expect(subject).not_to have_key(:delete_path)
|
|
|
|
end
|
2016-12-21 08:26:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-20 06:44:31 -05:00
|
|
|
context 'when pipeline ref is empty' do
|
|
|
|
let(:pipeline) { create(:ci_empty_pipeline) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(pipeline).to receive(:ref).and_return(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not generate branch path' do
|
|
|
|
expect(subject[:ref][:path]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2017-10-03 07:18:59 -04:00
|
|
|
|
|
|
|
context 'when pipeline has a failure reason set' do
|
|
|
|
let(:pipeline) { create(:ci_empty_pipeline) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
pipeline.drop!(:config_error)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a correct failure reason' do
|
|
|
|
expect(subject[:failure_reason])
|
2021-05-03 14:10:17 -04:00
|
|
|
.to eq 'The pipeline failed due to an error on the CI/CD configuration file.'
|
2017-10-03 07:18:59 -04:00
|
|
|
end
|
|
|
|
end
|
2019-03-01 05:05:04 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
context 'when request has a project' do
|
|
|
|
before do
|
|
|
|
allow(request).to receive(:project).and_return(project)
|
2019-03-01 05:05:04 -05:00
|
|
|
end
|
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
context 'when pipeline is detached merge request pipeline' do
|
|
|
|
let_it_be(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) }
|
2021-04-14 11:09:04 -04:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
let(:project) { merge_request.target_project }
|
|
|
|
let(:pipeline) { merge_request.pipelines_for_merge_request.first }
|
2019-04-02 03:07:11 -04:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
it 'makes detached flag true' do
|
|
|
|
expect(subject[:flags][:detached_merge_request_pipeline]).to be_truthy
|
2019-03-01 05:05:04 -05:00
|
|
|
end
|
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
it 'does not expose source sha and target sha' do
|
|
|
|
expect(subject[:source_sha]).to be_nil
|
|
|
|
expect(subject[:target_sha]).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a developer' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has merge request information' do
|
|
|
|
expect(subject[:merge_request][:iid]).to eq(merge_request.iid)
|
2019-03-01 05:05:04 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
expect(project_merge_request_path(project, merge_request))
|
|
|
|
.to include(subject[:merge_request][:path])
|
2019-03-01 05:05:04 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
expect(subject[:merge_request][:title]).to eq(merge_request.title)
|
2019-03-01 05:05:04 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
expect(subject[:merge_request][:source_branch])
|
|
|
|
.to eq(merge_request.source_branch)
|
2019-03-01 05:05:04 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
expect(project_commits_path(project, merge_request.source_branch))
|
|
|
|
.to include(subject[:merge_request][:source_branch_path])
|
2019-03-01 05:05:04 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
expect(subject[:merge_request][:target_branch])
|
|
|
|
.to eq(merge_request.target_branch)
|
2019-03-01 05:05:04 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
expect(project_commits_path(project, merge_request.target_branch))
|
|
|
|
.to include(subject[:merge_request][:target_branch_path])
|
|
|
|
end
|
2019-03-01 05:05:04 -05:00
|
|
|
end
|
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
context 'when user is an external user' do
|
|
|
|
it 'has no merge request information' do
|
|
|
|
expect(subject[:merge_request]).to be_nil
|
|
|
|
end
|
2019-03-01 05:05:04 -05:00
|
|
|
end
|
|
|
|
end
|
2019-03-07 02:33:13 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
context 'when pipeline is merge request pipeline' do
|
|
|
|
let_it_be(:merge_request) { create(:merge_request, :with_merge_request_pipeline, merge_sha: 'abc') }
|
2021-04-14 11:09:04 -04:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
let(:project) { merge_request.target_project }
|
|
|
|
let(:pipeline) { merge_request.pipelines_for_merge_request.first }
|
2019-03-07 02:33:13 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
it 'makes detached flag false' do
|
|
|
|
expect(subject[:flags][:detached_merge_request_pipeline]).to be_falsy
|
|
|
|
end
|
2019-03-07 02:33:13 -05:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
it 'makes atached flag true' do
|
2022-02-23 19:18:13 -05:00
|
|
|
expect(subject[:flags][:merge_request_pipeline]).to be true
|
|
|
|
expect(subject[:flags][:merge_request]).to be true
|
2021-01-22 13:09:10 -05:00
|
|
|
end
|
2019-04-02 03:07:11 -04:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
it 'exposes source sha and target sha' do
|
|
|
|
expect(subject[:source_sha]).to be_present
|
|
|
|
expect(subject[:target_sha]).to be_present
|
|
|
|
end
|
2019-08-28 03:51:26 -04:00
|
|
|
|
2021-01-22 13:09:10 -05:00
|
|
|
it 'exposes merge request event type' do
|
|
|
|
expect(subject[:merge_request_event_type]).to be_present
|
|
|
|
end
|
2019-08-28 03:51:26 -04:00
|
|
|
end
|
2019-03-07 02:33:13 -05:00
|
|
|
end
|
2019-11-29 04:06:31 -05:00
|
|
|
|
|
|
|
context 'when pipeline has failed builds' do
|
2021-05-18 11:10:46 -04:00
|
|
|
let_it_be(:pipeline) { create(:ci_pipeline, project: project, user: user) }
|
2019-11-29 04:06:31 -05:00
|
|
|
let_it_be(:build) { create(:ci_build, :success, pipeline: pipeline) }
|
|
|
|
let_it_be(:failed_1) { create(:ci_build, :failed, pipeline: pipeline) }
|
|
|
|
let_it_be(:failed_2) { create(:ci_build, :failed, pipeline: pipeline) }
|
|
|
|
|
|
|
|
context 'when the user can retry the pipeline' do
|
2021-05-18 11:10:46 -04:00
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
2019-11-29 04:06:31 -05:00
|
|
|
|
2021-05-18 11:10:46 -04:00
|
|
|
it 'exposes these failed builds' do
|
2019-11-29 04:06:31 -05:00
|
|
|
expect(subject[:failed_builds].map { |b| b[:id] }).to contain_exactly(failed_1.id, failed_2.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user cannot retry the pipeline' do
|
|
|
|
it 'is nil' do
|
|
|
|
expect(subject[:failed_builds]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-12-06 13:14:09 -05:00
|
|
|
|
|
|
|
context 'when pipeline has coverage' do
|
|
|
|
let_it_be(:pipeline) { create(:ci_pipeline, project: project, user: user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(pipeline).to receive(:coverage).and_return(35.0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'exposes the coverage' do
|
|
|
|
expect(subject[:coverage]).to eq('35.00')
|
|
|
|
end
|
|
|
|
end
|
2016-12-21 08:26:47 -05:00
|
|
|
end
|
|
|
|
end
|