2018-07-17 16:50:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-01-17 15:35:57 +00:00
|
|
|
module Projects
|
2015-03-07 19:47:06 +00:00
|
|
|
class UpdateService < BaseService
|
2017-09-02 06:59:24 +00:00
|
|
|
include UpdateVisibilityLevel
|
|
|
|
|
2018-08-03 14:34:28 +00:00
|
|
|
ValidationError = Class.new(StandardError)
|
2013-11-06 16:45:39 +00:00
|
|
|
|
2018-08-03 14:34:28 +00:00
|
|
|
def execute
|
|
|
|
validate!
|
2013-11-06 16:45:39 +00:00
|
|
|
|
2018-02-14 14:10:17 +00:00
|
|
|
ensure_wiki_exists if enabling_wiki?
|
|
|
|
|
2018-06-04 16:40:21 +00:00
|
|
|
yield if block_given?
|
|
|
|
|
2018-06-05 13:56:29 +00:00
|
|
|
# If the block added errors, don't try to save the project
|
2018-08-03 14:34:28 +00:00
|
|
|
return update_failed! if project.errors.any?
|
2018-06-05 13:56:29 +00:00
|
|
|
|
2018-07-02 10:43:06 +00:00
|
|
|
if project.update(params.except(:default_branch))
|
2018-07-16 18:30:17 +00:00
|
|
|
after_update
|
2018-01-03 08:07:03 +00:00
|
|
|
|
2017-01-15 06:58:05 +00:00
|
|
|
success
|
|
|
|
else
|
2018-08-03 14:34:28 +00:00
|
|
|
update_failed!
|
2014-06-17 18:53:26 +00:00
|
|
|
end
|
2018-08-03 14:34:28 +00:00
|
|
|
rescue ValidationError => e
|
|
|
|
error(e.message)
|
2013-01-17 15:35:57 +00:00
|
|
|
end
|
2017-07-13 13:32:31 +00:00
|
|
|
|
2017-11-17 16:24:40 +00:00
|
|
|
def run_auto_devops_pipeline?
|
2018-06-05 11:32:46 +00:00
|
|
|
return false if project.repository.gitlab_ci_yml || !project.auto_devops&.previous_changes&.include?('enabled')
|
2017-12-04 11:27:21 +00:00
|
|
|
|
2018-02-02 18:39:55 +00:00
|
|
|
project.auto_devops.enabled? || (project.auto_devops.enabled.nil? && Gitlab::CurrentSettings.auto_devops_enabled?)
|
2017-11-17 16:24:40 +00:00
|
|
|
end
|
|
|
|
|
2017-07-13 13:32:31 +00:00
|
|
|
private
|
|
|
|
|
2018-08-03 14:34:28 +00:00
|
|
|
def validate!
|
|
|
|
unless valid_visibility_level_change?(project, params[:visibility_level])
|
|
|
|
raise ValidationError.new('New visibility level not allowed!')
|
|
|
|
end
|
|
|
|
|
|
|
|
if renaming_project_with_container_registry_tags?
|
|
|
|
raise ValidationError.new('Cannot rename project because it contains container registry tags!')
|
|
|
|
end
|
|
|
|
|
|
|
|
if changing_default_branch?
|
|
|
|
raise ValidationError.new("Could not set the default branch") unless project.change_head(params[:default_branch])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-16 18:30:17 +00:00
|
|
|
def after_update
|
2018-07-27 09:28:17 +00: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
|
|
|
|
|
2018-07-16 18:30:17 +00:00
|
|
|
if project.previous_changes.include?(:visibility_level) && project.private?
|
|
|
|
# don't enqueue immediately to prevent todos removal in case of a mistake
|
|
|
|
TodosDestroyer::ProjectPrivateWorker.perform_in(1.hour, project.id)
|
2018-07-27 09:28:17 +00:00
|
|
|
elsif (project_changed_feature_keys & todos_features_changes).present?
|
|
|
|
TodosDestroyer::PrivateFeaturesWorker.perform_in(1.hour, project.id)
|
2018-07-16 18:30:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if project.previous_changes.include?('path')
|
|
|
|
project.rename_repo
|
|
|
|
else
|
|
|
|
system_hook_service.execute_hooks_for(project, :update)
|
|
|
|
end
|
|
|
|
|
|
|
|
update_pages_config if changing_pages_https_only?
|
|
|
|
end
|
|
|
|
|
2018-08-03 14:34:28 +00:00
|
|
|
def update_failed!
|
2018-06-05 13:56:29 +00:00
|
|
|
model_errors = project.errors.full_messages.to_sentence
|
|
|
|
error_message = model_errors.presence || 'Project could not be updated!'
|
|
|
|
|
|
|
|
error(error_message)
|
|
|
|
end
|
|
|
|
|
2017-07-24 10:47:33 +00: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 13:32:31 +00:00
|
|
|
def changing_default_branch?
|
|
|
|
new_branch = params[:default_branch]
|
|
|
|
|
2018-05-23 17:18:55 +00:00
|
|
|
new_branch && project.repository.exists? &&
|
|
|
|
new_branch != project.default_branch
|
2017-07-13 13:32:31 +00:00
|
|
|
end
|
2018-02-14 14:10:17 +00:00
|
|
|
|
|
|
|
def enabling_wiki?
|
2018-08-03 14:34:28 +00:00
|
|
|
return false if project.wiki_enabled?
|
2018-02-14 14:10:17 +00:00
|
|
|
|
2018-03-08 15:03:53 +00:00
|
|
|
params.dig(:project_feature_attributes, :wiki_access_level).to_i > ProjectFeature::DISABLED
|
2018-02-14 14:10:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_wiki_exists
|
|
|
|
ProjectWiki.new(project, project.owner).wiki
|
|
|
|
rescue ProjectWiki::CouldNotCreateWikiError
|
|
|
|
log_error("Could not create wiki for #{project.full_name}")
|
|
|
|
Gitlab::Metrics.counter(:wiki_can_not_be_created_total, 'Counts the times we failed to create a wiki')
|
|
|
|
end
|
2018-01-03 08:07:03 +00:00
|
|
|
|
|
|
|
def update_pages_config
|
|
|
|
Projects::UpdatePagesConfigurationService.new(project).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def changing_pages_https_only?
|
|
|
|
project.previous_changes.include?(:pages_https_only)
|
|
|
|
end
|
2013-01-17 15:35:57 +00:00
|
|
|
end
|
|
|
|
end
|