Skip to content

Draft: Implement BranchRules::CreateApprovalProjectRuleService

What does this MR do and why?

Implement BranchRules::CreateApprovalProjectRuleService

This service takes name approvals_required user_ids group_ids and creates a regular ApprovalProjectRule for the branch rule.

  • Regular branch rules will create an approval rule for the branch rule's protected_branch.
  • All branches rules will create an approval rule with no protected_branch with applies_to_all_protected_branches: false. -All protected branches rules will create an approval rule with no protected_branch with applies_to_all_protected_branches: true.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

Before After

How to set up and validate locally

You'll need to be a member of the Flightjs/flight project, if you are not then add yourself to that project first or use a project that you are a member of.

  1. Open the graphql explorer http://gdk.test:3000/-/graphql-explorer
  2. Create a branch rule
mutation {
  branchRuleCreate(input: { name: "test", projectPath: "Flightjs/flight" }) {
    errors
    branchRule {
      id
    }
  }
}
  1. Alternatively find the global ID of an existing branch rule using the project query (if this fails you can also create a protected branch through the project settings > repository > protected branches
query {
  project(fullPath: "Flightjs/flight") {
    branchRules {
      nodes {
        id
        name
      }
    }
  }
}
  1. Open a rails console
bin/rails console
current_user = User.find_by_username("your_username")
branch_rule = GlobalID::Locator.locate(id) # id from the branch rule you created in step 2 or found in step 3
project = branch_rule.project
user_ids = project.users.without_bots.ids
group_ids = [project.group.id]
params = { name: 'test rule', approvals_required: 2, user_ids: user_ids, group_ids: group_ids }
response = BranchRules::CreateApprovalProjectRuleService.new(branch_rule, current_user, params).execute
# Tests
response.success? # should be true
approval_project_rule = response[:approval_project_rule] # should be the created rule
response[:errors] # should be nil
approval_project_rule.name == params[:name]
approval_project_rule.required_approvals == params[:required_approvals]
approval_project_rule.user_ids.difference(user_ids).none?
approval_project_rule.group_ids.difference(group_ids).none?
branch_rule.approval_project_rules.include?(approval_project_rule)
Edited by Joe Woodward

Merge request reports