Read about our upcoming Code of Conduct on this issue

repository_controller.rb 2.69 KB
Newer Older
1
2
3
module Projects
  module Settings
    class RepositoryController < Projects::ApplicationController
4
      before_action :authorize_admin_project!
5
      before_action :remote_mirror, only: [:show]
6

7
      def show
8
9
10
11
12
13
        render_show
      end

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

14
        if @new_deploy_token.persisted?
Mayra Cabrera's avatar
Mayra Cabrera committed
15
          flash.now[:notice] = s_('DeployTokens|Your new project deploy token has been created.')
16
17
18
19
20
21
22
23
        end

        render_show
      end

      private

      def render_show
24
        @deploy_keys = DeployKeysPresenter.new(@project, current_user: current_user)
25
        @deploy_tokens = @project.deploy_tokens.active
26

27
        define_deploy_token
28
        define_protected_refs
29
        remote_mirror
30

31
32
        render 'show'
      end
33

34
35
      def define_protected_refs
        @protected_branches = @project.protected_branches.order(:name).page(params[:page])
36
        @protected_tags = @project.protected_tags.order(:name).page(params[:page])
37
        @protected_branch = @project.protected_branches.new
38
        @protected_tag = @project.protected_tags.new
39
40
41
42

        @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
43
        load_gon_index
44
      end
45

46
47
48
49
50
51
      def remote_mirror
        return unless project.feature_available?(:repository_mirrors)

        @remote_mirror = project.remote_mirrors.first_or_initialize
      end

52
53
      def access_levels_options
        {
54
55
56
          create_access_levels: levels_for_dropdown,
          push_access_levels: levels_for_dropdown,
          merge_access_levels: levels_for_dropdown
57
58
59
        }
      end

60
61
      def levels_for_dropdown
        roles = ProtectedRefAccess::HUMAN_ACCESS_LEVELS.map do |id, text|
62
63
64
65
66
          { id: id, text: text, before_divider: true }
        end
        { roles: roles }
      end

67
68
      def protectable_tags_for_dropdown
        { open_tags: ProtectableDropdown.new(@project, :tags).hash }
69
70
      end

71
72
      def protectable_branches_for_dropdown
        { open_branches: ProtectableDropdown.new(@project, :branches).hash }
73
74
      end

75
      def load_gon_index
76
77
        gon.push(protectable_tags_for_dropdown)
        gon.push(protectable_branches_for_dropdown)
78
        gon.push(access_levels_options)
79
      end
80
81

      def define_deploy_token
82
83
84
85
86
        @new_deploy_token ||= DeployToken.new
      end

      def deploy_token_params
        params.require(:deploy_token).permit(:name, :expires_at, :read_repository, :read_registry)
87
      end
88
89
90
    end
  end
end