update_service.rb 4.4 KB
Newer Older
1
2
# frozen_string_literal: true

3
module Projects
4
  class UpdateService < BaseService
5
    include UpdateVisibilityLevel
6
    include ValidatesClassificationLabel
7

8
    ValidationError = Class.new(StandardError)
9

10
    # rubocop: disable CodeReuse/ActiveRecord
11
    def execute
12
      remove_unallowed_params
13
      validate!
14

15
16
      ensure_wiki_exists if enabling_wiki?

17
18
      yield if block_given?

19
20
      validate_classification_label(project, :external_authorization_classification_label)

21
      # If the block added errors, don't try to save the project
22
      return update_failed! if project.errors.any?
23

Lin Jen-Shin's avatar
Lin Jen-Shin committed
24
      if project.update(params.except(:default_branch))
25
        after_update
Rob Watson's avatar
Rob Watson committed
26

27
28
        success
      else
29
        update_failed!
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
30
      end
31
32
    rescue ValidationError => e
      error(e.message)
33
    end
34
    # rubocop: enable CodeReuse/ActiveRecord
35

36
    def run_auto_devops_pipeline?
37
      return false if project.repository.gitlab_ci_yml || !project.auto_devops&.previous_changes&.include?('enabled')
38

39
      project.auto_devops_enabled?
40
41
    end

42
43
    private

44
45
    def validate!
      unless valid_visibility_level_change?(project, params[:visibility_level])
46
        raise ValidationError.new(s_('UpdateProject|New visibility level not allowed!'))
47
48
49
      end

      if renaming_project_with_container_registry_tags?
50
        raise ValidationError.new(s_('UpdateProject|Cannot rename project because it contains container registry tags!'))
51
52
53
      end

      if changing_default_branch?
54
        raise ValidationError.new(s_("UpdateProject|Could not set the default branch")) unless project.change_head(params[:default_branch])
55
56
57
      end
    end

58
59
60
61
    def remove_unallowed_params
      params.delete(:emails_disabled) unless can?(current_user, :set_emails_disabled, project)
    end

62
    def after_update
63
64
65
66
67
68
69
      todos_features_changes = %w(
        issues_access_level
        merge_requests_access_level
        repository_access_level
      )
      project_changed_feature_keys = project.project_feature.previous_changes.keys

70
71
      if project.previous_changes.include?(:visibility_level) && project.private?
        # don't enqueue immediately to prevent todos removal in case of a mistake
72
        TodosDestroyer::ConfidentialIssueWorker.perform_in(Todo::WAIT_FOR_DELETE, nil, project.id)
73
        TodosDestroyer::ProjectPrivateWorker.perform_in(Todo::WAIT_FOR_DELETE, project.id)
74
      elsif (project_changed_feature_keys & todos_features_changes).present?
75
        TodosDestroyer::PrivateFeaturesWorker.perform_in(Todo::WAIT_FOR_DELETE, project.id)
76
77
78
      end

      if project.previous_changes.include?('path')
79
        after_rename_service(project).execute
80
81
82
83
      else
        system_hook_service.execute_hooks_for(project, :update)
      end

84
85
86
      update_pages_config if changing_pages_related_config?
    end

87
    def after_rename_service(project)
88
      AfterRenameService.new(project, path_before: project.path_before_last_save, full_path_before: project.full_path_before_last_save)
89
90
    end

91
92
    def changing_pages_related_config?
      changing_pages_https_only? || changing_pages_access_level?
93
94
    end

95
    def update_failed!
96
      model_errors = project.errors.full_messages.to_sentence
97
      error_message = model_errors.presence || s_('UpdateProject|Project could not be updated!')
98
99
100
101

      error(error_message)
    end

102
103
104
105
106
107
108
    def renaming_project_with_container_registry_tags?
      new_path = params[:path]

      new_path && new_path != project.path &&
        project.has_container_registry_tags?
    end

109
110
111
    def changing_default_branch?
      new_branch = params[:default_branch]

112
113
      new_branch && project.repository.exists? &&
        new_branch != project.default_branch
114
    end
115
116

    def enabling_wiki?
117
      return false if project.wiki_enabled?
118

119
      params.dig(:project_feature_attributes, :wiki_access_level).to_i > ProjectFeature::DISABLED
120
121
    end

122
123
124
125
    def changing_pages_access_level?
      params.dig(:project_feature_attributes, :pages_access_level)
    end

126
127
128
129
    def ensure_wiki_exists
      ProjectWiki.new(project, project.owner).wiki
    rescue ProjectWiki::CouldNotCreateWikiError
      log_error("Could not create wiki for #{project.full_name}")
130
      Gitlab::Metrics.counter(:wiki_can_not_be_created_total, 'Counts the times we failed to create a wiki').increment
131
    end
Rob Watson's avatar
Rob Watson committed
132
133
134
135
136
137
138
139

    def update_pages_config
      Projects::UpdatePagesConfigurationService.new(project).execute
    end

    def changing_pages_https_only?
      project.previous_changes.include?(:pages_https_only)
    end
140
141
  end
end