diff options
27 files changed, 169 insertions, 84 deletions
diff --git a/app/models/deployment.rb b/app/models/deployment.rb index 994e69912b6..101f18f3886 100644 --- a/app/models/deployment.rb +++ b/app/models/deployment.rb @@ -23,6 +23,8 @@ class Deployment < ApplicationRecord validates :sha, presence: true validates :ref, presence: true + validate :valid_sha, on: :create + validate :valid_ref, on: :create delegate :name, to: :environment, prefix: true @@ -234,6 +236,18 @@ class Deployment < ApplicationRecord end end + def valid_sha + return if project&.commit(sha) + + errors.add(:sha, _('The commit does not exist')) + end + + def valid_ref + return if project&.commit(ref) + + errors.add(:ref, _('The branch or tag does not exist')) + end + private def ref_path diff --git a/changelogs/unreleased/deployment-validate-sha.yml b/changelogs/unreleased/deployment-validate-sha.yml new file mode 100644 index 00000000000..93e6faf1313 --- /dev/null +++ b/changelogs/unreleased/deployment-validate-sha.yml @@ -0,0 +1,5 @@ +--- +title: Validate deployment SHAs and refs +merge_request: +author: +type: fixed diff --git a/locale/gitlab.pot b/locale/gitlab.pot index a2e1c4eeea6..c83fc685a62 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -17849,6 +17849,9 @@ msgstr "" msgid "The branch for this project has no active pipeline configuration." msgstr "" +msgid "The branch or tag does not exist" +msgstr "" + msgid "The character highlighter helps you keep the subject line to %{titleLength} characters and wrap the body at %{bodyLength} so they are readable in git." msgstr "" @@ -17858,6 +17861,9 @@ msgstr "" msgid "The collection of events added to the data gathered for that stage." msgstr "" +msgid "The commit does not exist" +msgstr "" + msgid "The configuration status of the table below only applies to the default branch and is based on the %{linkStart}latest pipeline%{linkEnd}. Once you've configured a scan for the default branch, any subsequent feature branch you create will include the scan." msgstr "" diff --git a/spec/controllers/projects/deployments_controller_spec.rb b/spec/controllers/projects/deployments_controller_spec.rb index 66112c95742..b360319c6b1 100644 --- a/spec/controllers/projects/deployments_controller_spec.rb +++ b/spec/controllers/projects/deployments_controller_spec.rb @@ -6,7 +6,7 @@ describe Projects::DeploymentsController do include ApiHelpers let(:user) { create(:user) } - let(:project) { create(:project) } + let(:project) { create(:project, :repository) } let(:environment) { create(:environment, name: 'production', project: project) } before do diff --git a/spec/controllers/projects/serverless/functions_controller_spec.rb b/spec/controllers/projects/serverless/functions_controller_spec.rb index 33d66f4ac5a..7e98ded88a9 100644 --- a/spec/controllers/projects/serverless/functions_controller_spec.rb +++ b/spec/controllers/projects/serverless/functions_controller_spec.rb @@ -7,9 +7,9 @@ describe Projects::Serverless::FunctionsController do include ReactiveCachingHelpers let(:user) { create(:user) } - let(:cluster) { create(:cluster, :project, :provided_by_gcp) } + let(:project) { create(:project, :repository) } + let(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) } let(:service) { cluster.platform_kubernetes } - let(:project) { cluster.project } let(:environment) { create(:environment, project: project) } let!(:deployment) { create(:deployment, :success, environment: environment, cluster: cluster) } let(:knative_services_finder) { environment.knative_services_finder } diff --git a/spec/features/merge_request/user_sees_deployment_widget_spec.rb b/spec/features/merge_request/user_sees_deployment_widget_spec.rb index 3743ef0f25d..99c9e9dc501 100644 --- a/spec/features/merge_request/user_sees_deployment_widget_spec.rb +++ b/spec/features/merge_request/user_sees_deployment_widget_spec.rb @@ -33,10 +33,10 @@ describe 'Merge request > User sees deployment widget', :js do end context 'when a user created a new merge request with the same SHA' do - let(:pipeline2) { create(:ci_pipeline, sha: sha, project: project, ref: 'new-patch-1') } + let(:pipeline2) { create(:ci_pipeline, sha: sha, project: project, ref: 'video') } let(:build2) { create(:ci_build, :success, pipeline: pipeline2) } let(:environment2) { create(:environment, project: project) } - let!(:deployment2) { create(:deployment, environment: environment2, sha: sha, ref: 'new-patch-1', deployable: build2) } + let!(:deployment2) { create(:deployment, environment: environment2, sha: sha, ref: 'video', deployable: build2) } it 'displays one environment which is related to the pipeline' do visit project_merge_request_path(project, merge_request) diff --git a/spec/features/projects/environments/environment_metrics_spec.rb b/spec/features/projects/environments/environment_metrics_spec.rb index c027b776d67..d34db5e15cc 100644 --- a/spec/features/projects/environments/environment_metrics_spec.rb +++ b/spec/features/projects/environments/environment_metrics_spec.rb @@ -6,7 +6,7 @@ describe 'Environment > Metrics' do include PrometheusHelpers let(:user) { create(:user) } - let(:project) { create(:prometheus_project) } + let(:project) { create(:prometheus_project, :repository) } let(:pipeline) { create(:ci_pipeline, project: project) } let(:build) { create(:ci_build, pipeline: pipeline) } let(:environment) { create(:environment, project: project) } diff --git a/spec/features/projects/environments/environment_spec.rb b/spec/features/projects/environments/environment_spec.rb index 3eab13cb820..55c6aed19e0 100644 --- a/spec/features/projects/environments/environment_spec.rb +++ b/spec/features/projects/environments/environment_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'Environment' do - let(:project) { create(:project) } + let(:project) { create(:project, :repository) } let(:user) { create(:user) } let(:role) { :developer } @@ -17,6 +17,7 @@ describe 'Environment' do let!(:permissions) { } let!(:deployment) { } let!(:action) { } + let!(:cluster) { } before do visit_environment(environment) @@ -94,19 +95,10 @@ describe 'Environment' do it 'does show build name' do expect(page).to have_link("#{build.name} (##{build.id})") - expect(page).not_to have_link('Re-deploy') - expect(page).not_to have_terminal_button end - context 'when user has ability to re-deploy' do - let(:permissions) do - create(:protected_branch, :developers_can_merge, - name: build.ref, project: project) - end - - it 'does show re-deploy' do - expect(page).to have_link('Re-deploy') - end + it 'shows the re-deploy button' do + expect(page).to have_button('Re-deploy to environment') end context 'with manual action' do @@ -141,6 +133,11 @@ describe 'Environment' do end context 'when user has no ability to trigger a deployment' do + let(:permissions) do + create(:protected_branch, :no_one_can_merge, + name: action.ref, project: project) + end + it 'does not show a play button' do expect(page).not_to have_link(action.name) end @@ -158,8 +155,9 @@ describe 'Environment' do context 'with terminal' do context 'when user configured kubernetes from CI/CD > Clusters' do - let!(:cluster) { create(:cluster, :project, :provided_by_gcp) } - let(:project) { cluster.project } + let!(:cluster) do + create(:cluster, :project, :provided_by_gcp, projects: [project]) + end context 'for project maintainer' do let(:role) { :maintainer } @@ -228,6 +226,11 @@ describe 'Environment' do end context 'when user has no ability to stop environment' do + let(:permissions) do + create(:protected_branch, :no_one_can_merge, + name: action.ref, project: project) + end + it 'does not allow to stop environment' do expect(page).not_to have_button('Stop') end diff --git a/spec/features/projects/serverless/functions_spec.rb b/spec/features/projects/serverless/functions_spec.rb index e82e5b81021..c661ceb8eda 100644 --- a/spec/features/projects/serverless/functions_spec.rb +++ b/spec/features/projects/serverless/functions_spec.rb @@ -6,7 +6,7 @@ describe 'Functions', :js do include KubernetesHelpers include ReactiveCachingHelpers - let(:project) { create(:project) } + let(:project) { create(:project, :repository) } let(:user) { create(:user) } before do @@ -36,9 +36,8 @@ describe 'Functions', :js do end context 'when the user has a cluster and knative installed and visits the serverless page' do - let(:cluster) { create(:cluster, :project, :provided_by_gcp) } + let(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) } let(:service) { cluster.platform_kubernetes } - let(:project) { cluster.project } let(:environment) { create(:environment, project: project) } let!(:deployment) { create(:deployment, :success, cluster: cluster, environment: environment) } let(:knative_services_finder) { environment.knative_services_finder } diff --git a/spec/finders/clusters/knative_services_finder_spec.rb b/spec/finders/clusters/knative_services_finder_spec.rb index 7ad64cc3bca..57dbead7921 100644 --- a/spec/finders/clusters/knative_services_finder_spec.rb +++ b/spec/finders/clusters/knative_services_finder_spec.rb @@ -6,9 +6,9 @@ describe Clusters::KnativeServicesFinder do include KubernetesHelpers include ReactiveCachingHelpers - let(:cluster) { create(:cluster, :project, :provided_by_gcp) } + let(:project) { create(:project, :repository) } + let(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) } let(:service) { environment.deployment_platform } - let(:project) { cluster.cluster_project.project } let(:environment) { create(:environment, project: project) } let!(:deployment) { create(:deployment, :success, environment: environment, cluster: cluster) } let(:namespace) do diff --git a/spec/finders/deployments_finder_spec.rb b/spec/finders/deployments_finder_spec.rb index be35a705b0d..2dcbd5ba8ea 100644 --- a/spec/finders/deployments_finder_spec.rb +++ b/spec/finders/deployments_finder_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' describe DeploymentsFinder do subject { described_class.new(project, params).execute } - let(:project) { create(:project, :public, :repository) } + let(:project) { create(:project, :public, :test_repo) } let(:params) { {} } describe "#execute" do @@ -34,7 +34,7 @@ describe DeploymentsFinder do let!(:deployment_1) { create(:deployment, :success, project: project, iid: 11, ref: 'master', created_at: 2.days.ago, updated_at: Time.now) } let!(:deployment_2) { create(:deployment, :success, project: project, iid: 12, ref: 'feature', created_at: 1.day.ago, updated_at: 2.hours.ago) } - let!(:deployment_3) { create(:deployment, :success, project: project, iid: 8, ref: 'patch', created_at: Time.now, updated_at: 1.hour.ago) } + let!(:deployment_3) { create(:deployment, :success, project: project, iid: 8, ref: 'video', created_at: Time.now, updated_at: 1.hour.ago) } where(:order_by, :sort, :ordered_deployments) do 'created_at' | 'asc' | [:deployment_1, :deployment_2, :deployment_3] diff --git a/spec/finders/projects/serverless/functions_finder_spec.rb b/spec/finders/projects/serverless/functions_finder_spec.rb index 589e4000d46..dcf1b2045ab 100644 --- a/spec/finders/projects/serverless/functions_finder_spec.rb +++ b/spec/finders/projects/serverless/functions_finder_spec.rb @@ -8,9 +8,9 @@ describe Projects::Serverless::FunctionsFinder do include ReactiveCachingHelpers let(:user) { create(:user) } - let(:cluster) { create(:cluster, :project, :provided_by_gcp) } + let(:project) { create(:project, :repository) } + let(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) } let(:service) { cluster.platform_kubernetes } - let(:project) { cluster.project } let(:environment) { create(:environment, project: project) } let!(:deployment) { create(:deployment, :success, environment: environment, cluster: cluster) } let(:knative_services_finder) { environment.knative_services_finder } diff --git a/spec/lib/gitlab/ci/pipeline/seed/deployment_spec.rb b/spec/lib/gitlab/ci/pipeline/seed/deployment_spec.rb index 4e63f60ea6b..90f4b06cea0 100644 --- a/spec/lib/gitlab/ci/pipeline/seed/deployment_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/seed/deployment_spec.rb @@ -3,8 +3,13 @@ require 'spec_helper' describe Gitlab::Ci::Pipeline::Seed::Deployment do - let_it_be(:project) { create(:project) } - let(:job) { build(:ci_build, project: project) } + let_it_be(:project) { create(:project, :repository) } + let(:pipeline) do + create(:ci_pipeline, project: project, + sha: 'b83d6e391c22777fca1ed3012fce84f633d7fed0') + end + + let(:job) { build(:ci_build, project: project, pipeline: pipeline) } let(:seed) { described_class.new(job) } let(:attributes) { {} } diff --git a/spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb b/spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb index 4bdc57c8c04..15edc649702 100644 --- a/spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb +++ b/spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb @@ -8,7 +8,8 @@ describe Gitlab::Prometheus::Queries::AdditionalMetricsDeploymentQuery do end include_examples 'additional metrics query' do - let(:deployment) { create(:deployment, environment: environment) } + let(:project) { create(:project, :repository) } + let(:deployment) { create(:deployment, environment: environment, project: project) } let(:query_params) { [deployment.id] } it 'queries using specific time' do diff --git a/spec/lib/gitlab/slash_commands/command_spec.rb b/spec/lib/gitlab/slash_commands/command_spec.rb index 73b93589fac..9849cf78b2f 100644 --- a/spec/lib/gitlab/slash_commands/command_spec.rb +++ b/spec/lib/gitlab/slash_commands/command_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Gitlab::SlashCommands::Command do - let(:project) { create(:project) } + let(:project) { create(:project, :repository) } let(:user) { create(:user) } let(:chat_name) { double(:chat_name, user: user) } diff --git a/spec/lib/gitlab/slash_commands/deploy_spec.rb b/spec/lib/gitlab/slash_commands/deploy_spec.rb index 93a724d8e12..fb9969800a2 100644 --- a/spec/lib/gitlab/slash_commands/deploy_spec.rb +++ b/spec/lib/gitlab/slash_commands/deploy_spec.rb @@ -4,7 +4,7 @@ require 'spec_helper' describe Gitlab::SlashCommands::Deploy do describe '#execute' do - let(:project) { create(:project) } + let(:project) { create(:project, :repository) } let(:user) { create(:user) } let(:chat_name) { double(:chat_name, user: user) } let(:regex_match) { described_class.match('deploy staging to production') } diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index d813a88c809..ee8e9805c49 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -1054,7 +1054,7 @@ describe Ci::Build do end describe 'state transition as a deployable' do - let!(:build) { create(:ci_build, :with_deployment, :start_review_app) } + let!(:build) { create(:ci_build, :with_deployment, :start_review_app, project: project, pipeline: pipeline) } let(:deployment) { build.deployment } let(:environment) { deployment.environment } @@ -3907,7 +3907,7 @@ describe Ci::Build do end context 'when build is a last deployment' do - let(:build) { create(:ci_build, :success, environment: 'production') } + let(:build) { create(:ci_build, :success, environment: 'production', pipeline: pipeline, project: project) } let(:environment) { create(:environment, name: 'production', project: build.project) } let!(:deployment) { create(:deployment, :success, environment: environment, project: environment.project, deployable: build) } @@ -3915,7 +3915,7 @@ describe Ci::Build do end context 'when there is a newer build with deployment' do - let(:build) { create(:ci_build, :success, environment: 'production') } + let(:build) { create(:ci_build, :success, environment: 'production', pipeline: pipeline, project: project) } let(:environment) { create(:environment, name: 'production', project: build.project) } let!(:deployment) { create(:deployment, :success, environment: environment, project: environment.project, deployable: build) } let!(:last_deployment) { create(:deployment, :success, environment: environment, project: environment.project) } @@ -3924,7 +3924,7 @@ describe Ci::Build do end context 'when build with deployment has failed' do - let(:build) { create(:ci_build, :failed, environment: 'production') } + let(:build) { create(:ci_build, :failed, environment: 'production', pipeline: pipeline, project: project) } let(:environment) { create(:environment, name: 'production', project: build.project) } let!(:deployment) { create(:deployment, :success, environment: environment, project: environment.project, deployable: build) } @@ -3932,7 +3932,7 @@ describe Ci::Build do end context 'when build with deployment is running' do - let(:build) { create(:ci_build, environment: 'production') } + let(:build) { create(:ci_build, environment: 'production', pipeline: pipeline, project: project) } let(:environment) { create(:environment, name: 'production', project: build.project) } let!(:deployment) { create(:deployment, :success, environment: environment, project: environment.project, deployable: build) } diff --git a/spec/models/deployment_spec.rb b/spec/models/deployment_spec.rb index 33e4cd34aa5..15745441698 100644 --- a/spec/models/deployment_spec.rb +++ b/spec/models/deployment_spec.rb @@ -403,40 +403,37 @@ describe Deployment do describe '#previous_environment_deployment' do it 'returns the previous deployment of the same environment' do - deploy1 = create(:deployment, :success, ref: 'v1.0.0') + deploy1 = create(:deployment, :success) deploy2 = create( :deployment, :success, project: deploy1.project, - environment: deploy1.environment, - ref: 'v1.0.1' + environment: deploy1.environment ) expect(deploy2.previous_environment_deployment).to eq(deploy1) end it 'ignores deployments that were not successful' do - deploy1 = create(:deployment, :failed, ref: 'v1.0.0') + deploy1 = create(:deployment, :failed) deploy2 = create( :deployment, :success, project: deploy1.project, - environment: deploy1.environment, - ref: 'v1.0.1' + environment: deploy1.environment ) expect(deploy2.previous_environment_deployment).to be_nil end it 'ignores deployments for different environments' do - deploy1 = create(:deployment, :success, ref: 'v1.0.0') + deploy1 = create(:deployment, :success) preprod = create(:environment, project: deploy1.project, name: 'preprod') deploy2 = create( :deployment, :success, project: deploy1.project, - environment: preprod, - ref: 'v1.0.1' + environment: preprod ) expect(deploy2.previous_environment_deployment).to be_nil @@ -499,4 +496,36 @@ describe Deployment do end end end + + describe '#valid_sha' do + it 'does not add errors for a valid SHA' do + project = create(:project, :repository) + deploy = build(:deployment, project: project) + + expect(deploy).to be_valid + end + + it 'adds an error for an invalid SHA' do + deploy = build(:deployment, sha: 'foo') + + expect(deploy).not_to be_valid + expect(deploy.errors[:sha]).not_to be_empty + end + end + + describe '#valid_ref' do + it 'does not add errors for a valid ref' do + project = create(:project, :repository) + deploy = build(:deployment, project: project) + + expect(deploy).to be_valid + end + + it 'adds an error for an invalid ref' do + deploy = build(:deployment, ref: 'does-not-exist') + + expect(deploy).not_to be_valid + expect(deploy.errors[:ref]).not_to be_empty + end + end end diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb index e37a7e85b75..fe1517e76d3 100644 --- a/spec/models/environment_spec.rb +++ b/spec/models/environment_spec.rb @@ -7,7 +7,7 @@ describe Environment, :use_clean_rails_memory_store_caching do using RSpec::Parameterized::TableSyntax include RepoHelpers - let(:project) { create(:project, :stubbed_repository) } + let(:project) { create(:project, :repository) } subject(:environment) { create(:environment, project: project) } @@ -29,7 +29,6 @@ describe Environment, :use_clean_rails_memory_store_caching do it { is_expected.to validate_length_of(:external_url).is_at_most(255) } describe '.order_by_last_deployed_at' do - let(:project) { create(:project, :repository) } let!(:environment1) { create(:environment, project: project) } let!(:environment2) { create(:environment, project: project) } let!(:environment3) { create(:environment, project: project) } @@ -139,8 +138,8 @@ describe Environment, :use_clean_rails_memory_store_caching do describe '.with_deployment' do subject { described_class.with_deployment(sha) } - let(:environment) { create(:environment) } - let(:sha) { RepoHelpers.sample_commit.id } + let(:environment) { create(:environment, project: project) } + let(:sha) { 'b83d6e391c22777fca1ed3012fce84f633d7fed0' } context 'when deployment has the specified sha' do let!(:deployment) { create(:deployment, environment: environment, sha: sha) } @@ -149,7 +148,7 @@ describe Environment, :use_clean_rails_memory_store_caching do end context 'when deployment does not have the specified sha' do - let!(:deployment) { create(:deployment, environment: environment, sha: 'abc') } + let!(:deployment) { create(:deployment, environment: environment, sha: 'ddd0f15ae83993f5cb66a927a28673882e99100b') } it { is_expected.to be_empty } end @@ -158,7 +157,7 @@ describe Environment, :use_clean_rails_memory_store_caching do describe '#folder_name' do context 'when it is inside a folder' do subject(:environment) do - create(:environment, name: 'staging/review-1') + create(:environment, name: 'staging/review-1', project: project) end it 'returns a top-level folder name' do @@ -672,8 +671,7 @@ describe Environment, :use_clean_rails_memory_store_caching do context 'when the environment is available' do context 'with a deployment service' do context 'when user configured kubernetes from CI/CD > Clusters' do - let!(:cluster) { create(:cluster, :project, :provided_by_gcp) } - let(:project) { cluster.project } + let!(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) } context 'with deployment' do let!(:deployment) { create(:deployment, :success, environment: environment) } @@ -794,10 +792,9 @@ describe Environment, :use_clean_rails_memory_store_caching do end describe '#calculate_reactive_cache' do - let(:cluster) { create(:cluster, :project, :provided_by_user) } - let(:project) { cluster.project } - let(:environment) { create(:environment, project: project) } - let!(:deployment) { create(:deployment, :success, environment: environment) } + let!(:cluster) { create(:cluster, :project, :provided_by_user, projects: [project]) } + let!(:environment) { create(:environment, project: project) } + let!(:deployment) { create(:deployment, :success, environment: environment, project: project) } subject { environment.calculate_reactive_cache } @@ -830,7 +827,7 @@ describe Environment, :use_clean_rails_memory_store_caching do context 'when the environment is available' do context 'with a deployment service' do - let(:project) { create(:prometheus_project) } + let(:project) { create(:prometheus_project, :repository) } context 'and a deployment' do let!(:deployment) { create(:deployment, environment: environment) } diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 31dc0134410..169201a9e90 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -5107,7 +5107,7 @@ describe Project do describe '.deployments' do subject { project.deployments } - let(:project) { create(:project) } + let(:project) { create(:project, :repository) } before do allow_any_instance_of(Deployment).to receive(:create_ref) diff --git a/spec/requests/api/deployments_spec.rb b/spec/requests/api/deployments_spec.rb index 3dc8e5749d4..96b14339469 100644 --- a/spec/requests/api/deployments_spec.rb +++ b/spec/requests/api/deployments_spec.rb @@ -11,10 +11,10 @@ describe API::Deployments do end describe 'GET /projects/:id/deployments' do - let(:project) { create(:project) } + let(:project) { create(:project, :repository) } let!(:deployment_1) { create(:deployment, :success, project: project, iid: 11, ref: 'master', created_at: Time.now, updated_at: Time.now) } - let!(:deployment_2) { create(:deployment, :success, project: project, iid: 12, ref: 'feature', created_at: 1.day.ago, updated_at: 2.hours.ago) } - let!(:deployment_3) { create(:deployment, :success, project: project, iid: 8, ref: 'patch', created_at: 2.days.ago, updated_at: 1.hour.ago) } + let!(:deployment_2) { create(:deployment, :success, project: project, iid: 12, ref: 'master', created_at: 1.day.ago, updated_at: 2.hours.ago) } + let!(:deployment_3) { create(:deployment, :success, project: project, iid: 8, ref: 'master', created_at: 2.days.ago, updated_at: 1.hour.ago) } context 'as member of the project' do it 'returns projects deployments sorted by id asc' do @@ -345,7 +345,7 @@ describe API::Deployments do context 'prevent N + 1 queries' do context 'when the endpoint returns multiple records' do - let(:project) { create(:project) } + let(:project) { create(:project, :repository) } def create_record create(:deployment, :success, project: project) @@ -372,9 +372,11 @@ describe API::Deployments do end it 'does not increase the query count' do - expect { create_record }.not_to change { request_with_query_count } + 10.times { create_record } + + expect { trigger_request }.not_to be_n_plus_1_query - expect(json_response.size).to eq(2) + expect(json_response.size).to eq(11) end end end diff --git a/spec/requests/api/environments_spec.rb b/spec/requests/api/environments_spec.rb index aa273e97209..bdb0ef44038 100644 --- a/spec/requests/api/environments_spec.rb +++ b/spec/requests/api/environments_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' describe API::Environments do let(:user) { create(:user) } let(:non_member) { create(:user) } - let(:project) { create(:project, :private, namespace: user.namespace) } + let(:project) { create(:project, :private, :repository, namespace: user.namespace) } let!(:environment) { create(:environment, project: project) } before do diff --git a/spec/serializers/deployment_entity_spec.rb b/spec/serializers/deployment_entity_spec.rb index 2a57ea51b39..7abe74fae8f 100644 --- a/spec/serializers/deployment_entity_spec.rb +++ b/spec/serializers/deployment_entity_spec.rb @@ -6,7 +6,7 @@ describe DeploymentEntity do let(:user) { developer } let(:developer) { create(:user) } let(:reporter) { create(:user) } - let(:project) { create(:project) } + let(:project) { create(:project, :repository) } let(:request) { double('request') } let(:deployment) { create(:deployment, deployable: build, project: project) } let(:build) { create(:ci_build, :manual, pipeline: pipeline) } diff --git a/spec/services/ci/retry_build_service_spec.rb b/spec/services/ci/retry_build_service_spec.rb index 76fe6f53a11..706649c1a8b 100644 --- a/spec/services/ci/retry_build_service_spec.rb +++ b/spec/services/ci/retry_build_service_spec.rb @@ -4,8 +4,11 @@ require 'spec_helper' describe Ci::RetryBuildService do set(:user) { create(:user) } - set(:project) { create(:project) } - set(:pipeline) { create(:ci_pipeline, project: project) } + set(:project) { create(:project, :repository) } + set(:pipeline) do + create(:ci_pipeline, project: project, + sha: 'b83d6e391c22777fca1ed3012fce84f633d7fed0') + end let(:stage) do create(:ci_stage_entity, project: project, @@ -207,7 +210,8 @@ describe Ci::RetryBuildService do context 'when build with deployment is retried' do let!(:build) do - create(:ci_build, :with_deployment, :deploy_to_production, pipeline: pipeline, stage_id: stage.id) + create(:ci_build, :with_deployment, :deploy_to_production, + pipeline: pipeline, stage_id: stage.id, project: project) end it 'creates a new deployment' do diff --git a/spec/services/deployments/after_create_service_spec.rb b/spec/services/deployments/after_create_service_spec.rb index 4ca96658db0..14b6dd4a74e 100644 --- a/spec/services/deployments/after_create_service_spec.rb +++ b/spec/services/deployments/after_create_service_spec.rb @@ -6,10 +6,18 @@ describe Deployments::AfterCreateService do let(:user) { create(:user) } let(:project) { create(:project, :repository) } let(:options) { { name: 'production' } } + let(:pipeline) do + create( + :ci_pipeline, + sha: 'b83d6e391c22777fca1ed3012fce84f633d7fed0', + project: project + ) + end let(:job) do create(:ci_build, :with_deployment, + pipeline: pipeline, ref: 'master', tag: false, environment: 'production', @@ -139,6 +147,7 @@ describe Deployments::AfterCreateService do let(:job) do create(:ci_build, :with_deployment, + pipeline: pipeline, ref: 'master', environment: 'production', project: project, @@ -152,6 +161,7 @@ describe Deployments::AfterCreateService do let(:job) do create(:ci_build, :with_deployment, + pipeline: pipeline, ref: 'master', environment: 'prod-slug', project: project, @@ -165,6 +175,7 @@ describe Deployments::AfterCreateService do let(:job) do create(:ci_build, :with_deployment, + pipeline: pipeline, yaml_variables: [{ key: :APP_HOST, value: 'host' }], environment: 'production', project: project, @@ -175,7 +186,7 @@ describe Deployments::AfterCreateService do end context 'when yaml environment does not have url' do - let(:job) { create(:ci_build, :with_deployment, environment: 'staging', project: project) } + let(:job) { create(:ci_build, :with_deployment, pipeline: pipeline, environment: 'staging', project: project) } it 'returns the external_url from persisted environment' do is_expected.to be_nil @@ -202,6 +213,7 @@ describe Deployments::AfterCreateService do let(:job) do create(:ci_build, :with_deployment, + pipeline: pipeline, ref: 'master', tag: false, environment: 'staging', diff --git a/spec/services/deployments/link_merge_requests_service_spec.rb b/spec/services/deployments/link_merge_requests_service_spec.rb index ba069658dfd..a0fb3b0abb0 100644 --- a/spec/services/deployments/link_merge_requests_service_spec.rb +++ b/spec/services/deployments/link_merge_requests_service_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' describe Deployments::LinkMergeRequestsService do + let(:project) { create(:project, :repository) } + describe '#execute' do context 'when the deployment did not succeed' do it 'does nothing' do @@ -16,20 +18,29 @@ describe Deployments::LinkMergeRequestsService do context 'when there is a previous deployment' do it 'links all merge requests merged since the previous deployment' do - deploy1 = create(:deployment, :success, sha: 'foo') + deploy1 = create( + :deployment, + :success, + project: project, + sha: '7975be0116940bf2ad4321f79d02a55c5f7779aa' + ) + deploy2 = create( :deployment, :success, - sha: 'bar', project: deploy1.project, - environment: deploy1.environment + environment: deploy1.environment, + sha: 'ddd0f15ae83993f5cb66a927a28673882e99100b' ) service = described_class.new(deploy2) expect(service) .to receive(:link_merge_requests_for_range) - .with('foo', 'bar') + .with( + '7975be0116940bf2ad4321f79d02a55c5f7779aa', + 'ddd0f15ae83993f5cb66a927a28673882e99100b' + ) service.execute end @@ -37,7 +48,7 @@ describe Deployments::LinkMergeRequestsService do context 'when there are no previous deployments' do it 'links all merged merge requests' do - deploy = create(:deployment, :success) + deploy = create(:deployment, :success, project: project) service = described_class.new(deploy) expect(service).to receive(:link_all_merged_merge_requests) @@ -49,7 +60,6 @@ describe Deployments::LinkMergeRequestsService do describe '#link_merge_requests_for_range' do it 'links merge requests' do - project = create(:project, :repository) environment = create(:environment, project: project) deploy = create(:deployment, :success, project: project, environment: environment) @@ -81,7 +91,6 @@ describe Deployments::LinkMergeRequestsService do describe '#link_all_merged_merge_requests' do it 'links all merged merge requests targeting the deployed branch' do - project = create(:project, :repository) environment = create(:environment, project: project) deploy = create(:deployment, :success, project: project, environment: environment) diff --git a/spec/support/prometheus/additional_metrics_shared_examples.rb b/spec/support/prometheus/additional_metrics_shared_examples.rb index 4e006edb7da..3a5909cd908 100644 --- a/spec/support/prometheus/additional_metrics_shared_examples.rb +++ b/spec/support/prometheus/additional_metrics_shared_examples.rb @@ -14,7 +14,7 @@ RSpec.shared_examples 'additional metrics query' do let(:client) { double('prometheus_client') } let(:query_result) { described_class.new(client).query(*query_params) } - let(:project) { create(:project) } + let(:project) { create(:project, :repository) } let(:environment) { create(:environment, slug: 'environment-slug', project: project) } before do @@ -47,8 +47,7 @@ RSpec.shared_examples 'additional metrics query' do describe 'project has Kubernetes service' do context 'when user configured kubernetes from CI/CD > Clusters' do - let!(:cluster) { create(:cluster, :project, :provided_by_gcp) } - let(:project) { cluster.project } + let!(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) } let(:environment) { create(:environment, slug: 'environment-slug', project: project) } let(:kube_namespace) { environment.deployment_namespace } |