project_approval_rules_helpers.rb 3.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
# frozen_string_literal: true

module API
  module Helpers
    module ProjectApprovalRulesHelpers
      extend Grape::API::Helpers

      params :create_project_approval_rule do
        requires :name, type: String, desc: 'The name of the approval rule'
        requires :approvals_required, type: Integer, desc: 'The number of required approvals for this rule'
        optional :rule_type, type: String, desc: 'The type of approval rule'
Stan Hu's avatar
Stan Hu committed
12 13 14
        optional :users, as: :user_ids, type: Array[Integer], coerce_with: ::API::Validations::Types::CommaSeparatedToIntegerArray.coerce, desc: 'The user ids for this rule'
        optional :groups, as: :group_ids, type: Array[Integer], coerce_with: ::API::Validations::Types::CommaSeparatedToIntegerArray.coerce, desc: 'The group ids for this rule'
        optional :protected_branch_ids, type: Array[Integer], coerce_with: ::API::Validations::Types::CommaSeparatedToIntegerArray.coerce, desc: 'The protected branch ids for this rule'
15
        optional :scanners, type: Array[String], desc: 'The security scanners to be considered by the approval rule'
16 17 18 19 20 21
      end

      params :update_project_approval_rule do
        requires :approval_rule_id, type: Integer, desc: 'The ID of an approval_rule'
        optional :name, type: String, desc: 'The name of the approval rule'
        optional :approvals_required, type: Integer, desc: 'The number of required approvals for this rule'
Stan Hu's avatar
Stan Hu committed
22 23 24
        optional :users, as: :user_ids, type: Array[Integer], coerce_with: ::API::Validations::Types::CommaSeparatedToIntegerArray.coerce, desc: 'The user ids for this rule'
        optional :groups, as: :group_ids, type: Array[Integer], coerce_with: ::API::Validations::Types::CommaSeparatedToIntegerArray.coerce, desc: 'The group ids for this rule'
        optional :protected_branch_ids, type: Array[Integer], coerce_with: ::API::Validations::Types::CommaSeparatedToIntegerArray.coerce, desc: 'The protected branch ids for this rule'
25
        optional :remove_hidden_groups, type: Boolean, desc: 'Whether hidden groups should be removed'
26
        optional :scanners, type: Array[String], desc: 'The security scanners to be considered by the approval rule'
27 28 29 30 31 32
      end

      params :delete_project_approval_rule do
        requires :approval_rule_id, type: Integer, desc: 'The ID of an approval_rule'
      end

33 34 35
      def authorize_read_project_approval_rule!
        return if can?(current_user, :admin_project, user_project)

36 37 38 39 40 41 42 43 44 45 46
        authorize! :create_merge_request_in, user_project
      end

      def create_project_approval_rule(present_with:)
        authorize! :admin_project, user_project

        result = ::ApprovalRules::CreateService.new(user_project, current_user, declared_params(include_missing: false)).execute

        if result[:status] == :success
          present result[:rule], with: present_with, current_user: current_user
        else
47
          render_api_error!(result[:message], result[:http_status] || 400)
48 49 50 51 52 53 54 55 56 57 58 59 60
        end
      end

      def update_project_approval_rule(present_with:)
        authorize! :admin_project, user_project

        params = declared_params(include_missing: false)
        approval_rule = user_project.approval_rules.find(params.delete(:approval_rule_id))
        result = ::ApprovalRules::UpdateService.new(approval_rule, current_user, params).execute

        if result[:status] == :success
          present result[:rule], with: present_with, current_user: current_user
        else
61
          render_api_error!(result[:message], result[:http_status] || 400)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        end
      end

      def destroy_project_approval_rule
        authorize! :admin_project, user_project

        approval_rule = user_project.approval_rules.find(params[:approval_rule_id])

        destroy_conditionally!(approval_rule) do |rule|
          ::ApprovalRules::ProjectRuleDestroyService.new(rule).execute
        end
      end
    end
  end
end