2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-01-17 10:35:57 -05:00
|
|
|
module Projects
|
2015-03-07 14:47:06 -05:00
|
|
|
class UpdateService < BaseService
|
2017-09-02 02:59:24 -04:00
|
|
|
include UpdateVisibilityLevel
|
2019-04-09 11:38:58 -04:00
|
|
|
include ValidatesClassificationLabel
|
2017-09-02 02:59:24 -04:00
|
|
|
|
2018-08-03 10:34:28 -04:00
|
|
|
ValidationError = Class.new(StandardError)
|
2013-11-06 11:45:39 -05:00
|
|
|
|
2018-08-03 10:34:28 -04:00
|
|
|
def execute
|
2019-08-15 13:37:36 -04:00
|
|
|
remove_unallowed_params
|
2018-08-03 10:34:28 -04:00
|
|
|
validate!
|
2013-11-06 11:45:39 -05:00
|
|
|
|
2018-02-14 09:10:17 -05:00
|
|
|
ensure_wiki_exists if enabling_wiki?
|
|
|
|
|
2020-05-26 17:07:45 -04:00
|
|
|
if changing_repository_storage?
|
|
|
|
storage_move = project.repository_storage_moves.build(
|
|
|
|
source_storage_name: project.repository_storage,
|
|
|
|
destination_storage_name: params.delete(:repository_storage)
|
|
|
|
)
|
|
|
|
storage_move.schedule
|
2020-03-02 16:08:01 -05:00
|
|
|
end
|
|
|
|
|
2018-06-04 12:40:21 -04:00
|
|
|
yield if block_given?
|
|
|
|
|
2019-04-09 11:38:58 -04:00
|
|
|
validate_classification_label(project, :external_authorization_classification_label)
|
|
|
|
|
2018-06-05 09:56:29 -04:00
|
|
|
# If the block added errors, don't try to save the project
|
2018-08-03 10:34:28 -04:00
|
|
|
return update_failed! if project.errors.any?
|
2018-06-05 09:56:29 -04:00
|
|
|
|
2018-07-02 06:43:06 -04:00
|
|
|
if project.update(params.except(:default_branch))
|
2018-07-16 14:30:17 -04:00
|
|
|
after_update
|
2018-01-03 03:07:03 -05:00
|
|
|
|
2017-01-15 01:58:05 -05:00
|
|
|
success
|
|
|
|
else
|
2018-08-03 10:34:28 -04:00
|
|
|
update_failed!
|
2014-06-17 14:53:26 -04:00
|
|
|
end
|
2018-08-03 10:34:28 -04:00
|
|
|
rescue ValidationError => e
|
|
|
|
error(e.message)
|
2013-01-17 10:35:57 -05:00
|
|
|
end
|
2017-07-13 09:32:31 -04:00
|
|
|
|
2017-11-17 11:24:40 -05:00
|
|
|
def run_auto_devops_pipeline?
|
2018-06-05 07:32:46 -04:00
|
|
|
return false if project.repository.gitlab_ci_yml || !project.auto_devops&.previous_changes&.include?('enabled')
|
2017-12-04 06:27:21 -05:00
|
|
|
|
2018-08-16 09:24:25 -04:00
|
|
|
project.auto_devops_enabled?
|
2017-11-17 11:24:40 -05:00
|
|
|
end
|
|
|
|
|
2017-07-13 09:32:31 -04:00
|
|
|
private
|
|
|
|
|
2018-08-03 10:34:28 -04:00
|
|
|
def validate!
|
|
|
|
unless valid_visibility_level_change?(project, params[:visibility_level])
|
2019-04-15 08:25:48 -04:00
|
|
|
raise ValidationError.new(s_('UpdateProject|New visibility level not allowed!'))
|
2018-08-03 10:34:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if renaming_project_with_container_registry_tags?
|
2019-04-15 08:25:48 -04:00
|
|
|
raise ValidationError.new(s_('UpdateProject|Cannot rename project because it contains container registry tags!'))
|
2018-08-03 10:34:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if changing_default_branch?
|
2019-04-15 08:25:48 -04:00
|
|
|
raise ValidationError.new(s_("UpdateProject|Could not set the default branch")) unless project.change_head(params[:default_branch])
|
2018-08-03 10:34:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-15 13:37:36 -04:00
|
|
|
def remove_unallowed_params
|
|
|
|
params.delete(:emails_disabled) unless can?(current_user, :set_emails_disabled, project)
|
|
|
|
end
|
|
|
|
|
2018-07-16 14:30:17 -04:00
|
|
|
def after_update
|
2018-07-27 05:28:17 -04:00
|
|
|
todos_features_changes = %w(
|
|
|
|
issues_access_level
|
|
|
|
merge_requests_access_level
|
|
|
|
repository_access_level
|
|
|
|
)
|
|
|
|
project_changed_feature_keys = project.project_feature.previous_changes.keys
|
|
|
|
|
2019-11-28 10:06:57 -05:00
|
|
|
if project.visibility_level_previous_changes && project.private?
|
2018-07-16 14:30:17 -04:00
|
|
|
# don't enqueue immediately to prevent todos removal in case of a mistake
|
2019-05-21 15:20:27 -04:00
|
|
|
TodosDestroyer::ConfidentialIssueWorker.perform_in(Todo::WAIT_FOR_DELETE, nil, project.id)
|
2018-12-11 13:15:10 -05:00
|
|
|
TodosDestroyer::ProjectPrivateWorker.perform_in(Todo::WAIT_FOR_DELETE, project.id)
|
2018-07-27 05:28:17 -04:00
|
|
|
elsif (project_changed_feature_keys & todos_features_changes).present?
|
2018-12-11 13:15:10 -05:00
|
|
|
TodosDestroyer::PrivateFeaturesWorker.perform_in(Todo::WAIT_FOR_DELETE, project.id)
|
2018-07-16 14:30:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if project.previous_changes.include?('path')
|
2019-01-16 20:53:50 -05:00
|
|
|
after_rename_service(project).execute
|
2018-07-16 14:30:17 -04:00
|
|
|
else
|
|
|
|
system_hook_service.execute_hooks_for(project, :update)
|
|
|
|
end
|
|
|
|
|
2019-11-28 10:06:57 -05:00
|
|
|
if project.visibility_level_decreased? && project.unlink_forks_upon_visibility_decrease_enabled?
|
|
|
|
# It's a system-bounded operation, so no extra authorization check is required.
|
|
|
|
Projects::UnlinkForkService.new(project, current_user).execute
|
|
|
|
end
|
|
|
|
|
2018-10-05 09:41:11 -04:00
|
|
|
update_pages_config if changing_pages_related_config?
|
|
|
|
end
|
|
|
|
|
2019-01-16 20:53:50 -05:00
|
|
|
def after_rename_service(project)
|
2019-04-23 05:30:18 -04:00
|
|
|
AfterRenameService.new(project, path_before: project.path_before_last_save, full_path_before: project.full_path_before_last_save)
|
2019-01-16 20:53:50 -05:00
|
|
|
end
|
|
|
|
|
2018-10-05 09:41:11 -04:00
|
|
|
def changing_pages_related_config?
|
|
|
|
changing_pages_https_only? || changing_pages_access_level?
|
2018-07-16 14:30:17 -04:00
|
|
|
end
|
|
|
|
|
2018-08-03 10:34:28 -04:00
|
|
|
def update_failed!
|
2018-06-05 09:56:29 -04:00
|
|
|
model_errors = project.errors.full_messages.to_sentence
|
2019-04-15 08:25:48 -04:00
|
|
|
error_message = model_errors.presence || s_('UpdateProject|Project could not be updated!')
|
2018-06-05 09:56:29 -04:00
|
|
|
|
|
|
|
error(error_message)
|
|
|
|
end
|
|
|
|
|
2017-07-24 06:47:33 -04:00
|
|
|
def renaming_project_with_container_registry_tags?
|
|
|
|
new_path = params[:path]
|
|
|
|
|
|
|
|
new_path && new_path != project.path &&
|
|
|
|
project.has_container_registry_tags?
|
|
|
|
end
|
|
|
|
|
2017-07-13 09:32:31 -04:00
|
|
|
def changing_default_branch?
|
|
|
|
new_branch = params[:default_branch]
|
|
|
|
|
2018-05-23 13:18:55 -04:00
|
|
|
new_branch && project.repository.exists? &&
|
|
|
|
new_branch != project.default_branch
|
2017-07-13 09:32:31 -04:00
|
|
|
end
|
2018-02-14 09:10:17 -05:00
|
|
|
|
|
|
|
def enabling_wiki?
|
2018-08-03 10:34:28 -04:00
|
|
|
return false if project.wiki_enabled?
|
2018-02-14 09:10:17 -05:00
|
|
|
|
2018-03-08 10:03:53 -05:00
|
|
|
params.dig(:project_feature_attributes, :wiki_access_level).to_i > ProjectFeature::DISABLED
|
2018-02-14 09:10:17 -05:00
|
|
|
end
|
|
|
|
|
2018-10-05 09:41:11 -04:00
|
|
|
def changing_pages_access_level?
|
|
|
|
params.dig(:project_feature_attributes, :pages_access_level)
|
|
|
|
end
|
|
|
|
|
2018-02-14 09:10:17 -05:00
|
|
|
def ensure_wiki_exists
|
|
|
|
ProjectWiki.new(project, project.owner).wiki
|
|
|
|
rescue ProjectWiki::CouldNotCreateWikiError
|
|
|
|
log_error("Could not create wiki for #{project.full_name}")
|
2019-08-09 10:46:55 -04:00
|
|
|
Gitlab::Metrics.counter(:wiki_can_not_be_created_total, 'Counts the times we failed to create a wiki').increment
|
2018-02-14 09:10:17 -05:00
|
|
|
end
|
2018-01-03 03:07:03 -05:00
|
|
|
|
|
|
|
def update_pages_config
|
|
|
|
Projects::UpdatePagesConfigurationService.new(project).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def changing_pages_https_only?
|
|
|
|
project.previous_changes.include?(:pages_https_only)
|
|
|
|
end
|
2020-03-02 16:08:01 -05:00
|
|
|
|
2020-05-26 17:07:45 -04:00
|
|
|
def changing_repository_storage?
|
2020-03-02 16:08:01 -05:00
|
|
|
new_repository_storage = params[:repository_storage]
|
|
|
|
|
|
|
|
new_repository_storage && project.repository.exists? &&
|
2020-05-26 17:07:45 -04:00
|
|
|
project.repository_storage != new_repository_storage &&
|
2020-03-02 16:08:01 -05:00
|
|
|
can?(current_user, :change_repository_storage, project)
|
|
|
|
end
|
2013-01-17 10:35:57 -05:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Projects::UpdateService.prepend_if_ee('EE::Projects::UpdateService')
|