Read about our upcoming Code of Conduct on this issue

update_service.rb 2.44 KB
Newer Older
1
module Projects
2
  class UpdateService < BaseService
3
4
    include UpdateVisibilityLevel

5
    def execute
Michael Kozono's avatar
Michael Kozono committed
6
      unless valid_visibility_level_change?(project, params[:visibility_level])
7
        return error('New visibility level not allowed!')
Tomasz Maczukin's avatar
Tomasz Maczukin committed
8
9
      end

10
      if renaming_project_with_container_registry_tags?
11
12
        return error('Cannot rename project because it contains container registry tags!')
      end
13

14
      if changing_default_branch?
15
        return error("Could not set the default branch") unless project.change_head(params[:default_branch])
16
17
      end

18
19
      ensure_wiki_exists if enabling_wiki?

20
      if project.update_attributes(params.except(:default_branch))
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
21
22
        if project.previous_changes.include?('path')
          project.rename_repo
23
24
        else
          system_hook_service.execute_hooks_for(project, :update)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
25
        end
26

Rob Watson's avatar
Rob Watson committed
27
28
        update_pages_config if changing_pages_https_only?

29
30
        success
      else
31
32
33
34
        model_errors = project.errors.full_messages.to_sentence
        error_message = model_errors.presence || 'Project could not be updated!'

        error(error_message)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
35
      end
36
    end
37

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

41
      project.auto_devops.enabled? || (project.auto_devops.enabled.nil? && Gitlab::CurrentSettings.auto_devops_enabled?)
42
43
    end

44
45
    private

46
47
48
49
50
51
52
    def renaming_project_with_container_registry_tags?
      new_path = params[:path]

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

53
54
55
56
57
58
    def changing_default_branch?
      new_branch = params[:default_branch]

      project.repository.exists? &&
        new_branch && new_branch != project.default_branch
    end
59
60
61
62

    def enabling_wiki?
      return false if @project.wiki_enabled?

63
      params.dig(:project_feature_attributes, :wiki_access_level).to_i > ProjectFeature::DISABLED
64
65
66
67
68
69
70
71
    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
Rob Watson's avatar
Rob Watson committed
72
73
74
75
76
77
78
79

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

    def changing_pages_https_only?
      project.previous_changes.include?(:pages_https_only)
    end
80
81
  end
end