debian-mirror-gitlab/app/services/projects/update_service.rb

136 lines
4 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module Projects
class UpdateService < BaseService
2018-03-17 18:26:18 +05:30
include UpdateVisibilityLevel
2018-11-18 11:00:15 +05:30
ValidationError = Class.new(StandardError)
2016-08-24 12:49:21 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def execute
validate!
2014-09-02 18:07:02 +05:30
2018-03-27 19:54:05 +05:30
ensure_wiki_exists if enabling_wiki?
2018-10-15 14:42:47 +05:30
yield if block_given?
# If the block added errors, don't try to save the project
2018-11-18 11:00:15 +05:30
return update_failed! if project.errors.any?
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
if project.update(params.except(:default_branch))
after_update
2018-05-09 12:01:36 +05:30
2017-08-17 22:00:37 +05:30
success
else
2018-11-18 11:00:15 +05:30
update_failed!
2014-09-02 18:07:02 +05:30
end
2018-11-18 11:00:15 +05:30
rescue ValidationError => e
error(e.message)
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
def run_auto_devops_pipeline?
2018-11-08 19:23:39 +05:30
return false if project.repository.gitlab_ci_yml || !project.auto_devops&.previous_changes&.include?('enabled')
2017-09-10 17:25:29 +05:30
2018-11-20 20:47:30 +05:30
project.auto_devops_enabled?
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
private
2018-11-18 11:00:15 +05:30
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
def after_update
todos_features_changes = %w(
issues_access_level
merge_requests_access_level
repository_access_level
)
project_changed_feature_keys = project.project_feature.previous_changes.keys
if project.previous_changes.include?(:visibility_level) && project.private?
# don't enqueue immediately to prevent todos removal in case of a mistake
2019-01-03 12:48:30 +05:30
TodosDestroyer::ProjectPrivateWorker.perform_in(Todo::WAIT_FOR_DELETE, project.id)
2018-11-18 11:00:15 +05:30
elsif (project_changed_feature_keys & todos_features_changes).present?
2019-01-03 12:48:30 +05:30
TodosDestroyer::PrivateFeaturesWorker.perform_in(Todo::WAIT_FOR_DELETE, project.id)
2018-11-18 11:00:15 +05:30
end
if project.previous_changes.include?('path')
2019-03-02 22:35:43 +05:30
after_rename_service(project).execute
2018-11-18 11:00:15 +05:30
else
system_hook_service.execute_hooks_for(project, :update)
end
2018-12-05 23:21:45 +05:30
update_pages_config if changing_pages_related_config?
end
2019-03-02 22:35:43 +05:30
def after_rename_service(project)
# The path slug the project was using, before the rename took place.
path_before = project.previous_changes['path'].first
AfterRenameService.new(project, path_before: path_before, full_path_before: project.full_path_was)
end
2018-12-05 23:21:45 +05:30
def changing_pages_related_config?
changing_pages_https_only? || changing_pages_access_level?
2018-11-18 11:00:15 +05:30
end
def update_failed!
2018-10-15 14:42:47 +05:30
model_errors = project.errors.full_messages.to_sentence
error_message = model_errors.presence || 'Project could not be updated!'
error(error_message)
end
2017-09-10 17:25:29 +05:30
def renaming_project_with_container_registry_tags?
new_path = params[:path]
new_path && new_path != project.path &&
project.has_container_registry_tags?
end
def changing_default_branch?
new_branch = params[:default_branch]
2018-11-08 19:23:39 +05:30
new_branch && project.repository.exists? &&
new_branch != project.default_branch
2017-09-10 17:25:29 +05:30
end
2018-03-27 19:54:05 +05:30
def enabling_wiki?
2018-11-18 11:00:15 +05:30
return false if project.wiki_enabled?
2018-03-27 19:54:05 +05:30
params.dig(:project_feature_attributes, :wiki_access_level).to_i > ProjectFeature::DISABLED
end
2018-12-05 23:21:45 +05:30
def changing_pages_access_level?
params.dig(:project_feature_attributes, :pages_access_level)
end
2018-03-27 19:54:05 +05:30
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-05-09 12:01:36 +05:30
def update_pages_config
Projects::UpdatePagesConfigurationService.new(project).execute
end
def changing_pages_https_only?
project.previous_changes.include?(:pages_https_only)
end
2014-09-02 18:07:02 +05:30
end
end