Commit 24a8dec1 authored by Rubén Dávila's avatar Rubén Dávila Committed by Eric Eastwood

Add backend for CI/CD "Repo by URL" projects

See https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/4567
parent f290e2ea
......@@ -43,7 +43,7 @@ class ProjectsController < Projects::ApplicationController
cookies[:issue_board_welcome_hidden] = { path: project_path(@project), value: nil, expires: Time.at(0) }
redirect_to(
project_path(@project),
project_path(@project, custom_import_params),
notice: _("Project '%{project_name}' was successfully created.") % { project_name: @project.name }
)
else
......@@ -105,7 +105,7 @@ class ProjectsController < Projects::ApplicationController
def show
if @project.import_in_progress?
redirect_to project_import_path(@project)
redirect_to project_import_path(@project, custom_import_params)
return
end
......@@ -361,6 +361,10 @@ class ProjectsController < Projects::ApplicationController
]
end
def custom_import_params
{}
end
def repo_exists?
project.repository_exists? && !project.empty_repo?
......
module EE
module ProjectsController
extend ::Gitlab::Utils::Override
def project_params_attributes
super + project_params_ee
end
......@@ -21,7 +23,17 @@ module EE
mirror_trigger_builds
mirror_user_id
external_authorization_classification_label
ci_cd_only
]
end
override :custom_import_params
def custom_import_params
custom_params = super
ci_cd_param = params.dig(:project, :ci_cd_only) || params[:ci_cd_only]
custom_params[:ci_cd_only] = ci_cd_param if ci_cd_param == 'true'
custom_params
end
end
end
......@@ -54,6 +54,7 @@ class License < ActiveRecord::Base
commit_committer_check
project_creation_level
external_authorization_service
ci_cd_projects
].freeze
EEU_FEATURES = EEP_FEATURES + %i[
......
......@@ -9,6 +9,7 @@ module EE
mirror = params.delete(:mirror)
mirror_user_id = params.delete(:mirror_user_id)
mirror_trigger_builds = params.delete(:mirror_trigger_builds)
ci_cd_only = params.delete(:ci_cd_only)
project = super do |project|
# Repository size limit comes as MB from the view
......@@ -22,6 +23,8 @@ module EE
end
if project&.persisted?
setup_ci_cd_project if ci_cd_only
log_geo_event(project)
log_audit_event(project)
end
......@@ -55,6 +58,12 @@ module EE
end
end
def setup_ci_cd_project
return unless ::License.feature_available?(:ci_cd_projects)
::Projects::SetupCiCd.new(project, current_user).execute
end
def log_audit_event(project)
::AuditEventService.new(
current_user,
......
module Projects
class SetupCiCd < BaseService
def execute
return if project.import_url.blank?
update_project
disable_project_features
end
private
def update_project
project.update_attributes(
container_registry_enabled: false,
mirror: true,
mirror_trigger_builds: true,
mirror_overwrites_diverged_branches: true,
only_mirror_protected_branches: false,
mirror_user_id: current_user.id
)
end
def disable_project_features
project.project_feature.update_attributes(
issues_access_level: ProjectFeature::DISABLED,
merge_requests_access_level: ProjectFeature::DISABLED,
wiki_access_level: ProjectFeature::DISABLED,
snippets_access_level: ProjectFeature::DISABLED
)
end
end
end
......@@ -9,6 +9,39 @@ describe Projects::CreateService, '#execute' do
}
end
context 'with a CI/CD only project' do
before do
opts.merge!(
ci_cd_only: true,
import_url: 'http://foo.com'
)
end
context 'when CI/CD projects feature is available' do
before do
stub_licensed_features(ci_cd_projects: true)
end
it 'calls the service to setup CI/CD on the project' do
expect(Projects::SetupCiCd).to receive_message_chain(:new, :execute)
create_project(user, opts)
end
end
context 'when CI/CD projects feature is not available' do
before do
stub_licensed_features(ci_cd_projects: false)
end
it "doesn't call the service to setup CI/CD on the project" do
expect(Projects::SetupCiCd).not_to receive(:new)
create_project(user, opts)
end
end
end
context 'repository_size_limit assignment as Bytes' do
let(:admin_user) { create(:user, admin: true) }
......
require 'spec_helper'
describe Projects::SetupCiCd do
let(:user) { create(:user) }
let(:project) { create(:project, creator: user, import_url: 'http://foo.com') }
subject do
described_class.new(project, project.creator)
end
it 'sets up pull mirroring on the project' do
subject.execute
expect(project.mirror).to be_truthy
expect(project.mirror_trigger_builds).to be_truthy
expect(project.mirror_user_id).to eq(user.id)
end
it 'disables some features' do
subject.execute
project_feature = project.project_feature
expect(project.container_registry_enabled).to be_falsey
expect(project_feature).not_to be_issues_enabled
expect(project_feature).not_to be_merge_requests_enabled
expect(project_feature).not_to be_wiki_enabled
expect(project_feature.snippets_access_level).to eq(ProjectFeature::DISABLED)
end
context 'when import_url is blank' do
before do
project.update_attribute(:import_url, nil)
end
it "doesn't update the project" do
expect(project).not_to receive(:update_project)
expect(project).not_to receive(:disable_project_features)
subject.execute
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment