Read about our upcoming Code of Conduct on this issue

This instance was upgraded to Heptapod 0.28.0 today

repository_controller.rb 3.5 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
5
module Projects
  module Settings
    class RepositoryController < Projects::ApplicationController
6
      before_action :authorize_admin_project!
7
      before_action :remote_mirror, only: [:show]
8
      before_action :check_cleanup_feature_flag!, only: :cleanup
9

10
      def show
11
12
13
14
15
16
        render_show
      end

      def create_deploy_token
        @new_deploy_token = DeployTokens::CreateService.new(@project, current_user, deploy_token_params).execute

17
        if @new_deploy_token.persisted?
18
          flash.now[:notice] = s_('DeployTokens|Your new project deploy token has been created.')
19
20
        end

21
        render_show
22
23
      end

24
25
26
27
28
29
30
31
32
33
34
35
36
37
      def cleanup
        cleanup_params = params.require(:project).permit(:bfg_object_map)
        result = Projects::UpdateService.new(project, current_user, cleanup_params).execute

        if result[:status] == :success
          RepositoryCleanupWorker.perform_async(project.id, current_user.id)
          flash[:notice] = _('Repository cleanup has started. You will receive an email once the cleanup operation is complete.')
        else
          flash[:alert] = _('Failed to upload object map file')
        end

        redirect_to project_settings_repository_path(project)
      end

38
39
      private

40
41
42
43
      def check_cleanup_feature_flag!
        render_404 unless ::Feature.enabled?(:project_cleanup, project)
      end

44
      def render_show
45
        @deploy_keys = DeployKeysPresenter.new(@project, current_user: current_user)
46
        @deploy_tokens = @project.deploy_tokens.active
47

48
        define_deploy_token
49
        define_protected_refs
50
        remote_mirror
51

52
53
        render 'show'
      end
54

55
      # rubocop: disable CodeReuse/ActiveRecord
56
57
      def define_protected_refs
        @protected_branches = @project.protected_branches.order(:name).page(params[:page])
58
        @protected_tags = @project.protected_tags.order(:name).page(params[:page])
59
        @protected_branch = @project.protected_branches.new
60
        @protected_tag = @project.protected_tags.new
61
62
63
64

        @protected_branches_count = @protected_branches.reduce(0) { |sum, branch| sum + branch.matching(@project.repository.branches).size }
        @protected_tags_count = @protected_tags.reduce(0) { |sum, tag| sum + tag.matching(@project.repository.tags).size }

Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
65
        load_gon_index
66
      end
67
      # rubocop: enable CodeReuse/ActiveRecord
68

69
70
71
72
      def remote_mirror
        @remote_mirror = project.remote_mirrors.first_or_initialize
      end

73
74
      def access_levels_options
        {
75
76
77
          create_access_levels: levels_for_dropdown,
          push_access_levels: levels_for_dropdown,
          merge_access_levels: levels_for_dropdown
78
79
80
        }
      end

81
82
      def levels_for_dropdown
        roles = ProtectedRefAccess::HUMAN_ACCESS_LEVELS.map do |id, text|
83
84
85
86
87
          { id: id, text: text, before_divider: true }
        end
        { roles: roles }
      end

88
89
      def protectable_tags_for_dropdown
        { open_tags: ProtectableDropdown.new(@project, :tags).hash }
90
91
      end

92
93
      def protectable_branches_for_dropdown
        { open_branches: ProtectableDropdown.new(@project, :branches).hash }
94
95
      end

96
      def load_gon_index
97
98
        gon.push(protectable_tags_for_dropdown)
        gon.push(protectable_branches_for_dropdown)
99
        gon.push(access_levels_options)
100
      end
101
102

      def define_deploy_token
103
104
105
106
107
        @new_deploy_token ||= DeployToken.new
      end

      def deploy_token_params
        params.require(:deploy_token).permit(:name, :expires_at, :read_repository, :read_registry)
108
      end
109
110
111
    end
  end
end