repository_controller.rb 2.62 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
      def remote_mirror
        @remote_mirror = project.remote_mirrors.first_or_initialize
      end

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

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

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

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

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

      def define_deploy_token
80
81
82
83
84
        @new_deploy_token ||= DeployToken.new
      end

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