git.branch-protection.create

Add a branch protection rule

POST /git/api/repositories/{repoId}/branch-protectionIAM git:ManageBranchProtectionChanges stateDestructive

Request syntax

POST /git/api/repositories/{repoId}/branch-protection
{
  "branchPattern": "string",
  "requirePullRequest": "boolean",
  "requiredApprovals": "number",
  "dismissStaleApprovals": "boolean",
  "requireCodeOwnerReview": "boolean",
  "requireStatusChecks": "boolean",
  "requiredChecks": "array of string",
  "blockForcePush": "boolean",
  "blockDeletion": "boolean",
  "requireMergeQueue": "boolean",
  "mergeQueueMethod": "enum",
  "mergeQueueBuildConcurrency": "number",
  "mergeQueueMinGroupSize": "number",
  "mergeQueueMaxGroupSize": "number",
  "mergeQueueWaitMinutes": "number",
  "mergeQueueCheckTimeoutMinutes": "number",
  "mergeQueueOnlyNonFailing": "boolean"
}

Path parameters

  • repoIdRequired

    string

    Constraints: uuid

Request body

  • branchPatternRequired

    string

    The branch name or glob the rule covers, e.g. main or release/*

    Constraints: 1-200 chars

  • requirePullRequestOptional

    boolean

    Changes land only through a pull request; direct pushes are refused (true on a new rule)

  • requiredApprovalsOptional

    number

    Approvals a pull request needs before it can merge (0 on a new rule)

    Constraints: integer, 0-10

  • dismissStaleApprovalsOptional

    boolean

    A push to the pull request dismisses the approvals given before it (false on a new rule)

  • requireCodeOwnerReviewOptional

    boolean

    Every changed file CODEOWNERS assigns needs an approval from one of its owners

  • requireStatusChecksOptional

    boolean

    Required checks must pass before a pull request can merge (false on a new rule)

  • requiredChecksOptional

    array of string

    Workflow or status names that must pass; empty means every workflow a pull request to the branch triggers

    Constraints: 1-200 chars

  • blockForcePushOptional

    boolean

    Refuse pushes that rewrite the branch history (true on a new rule)

  • blockDeletionOptional

    boolean

    Refuse deleting the branch (true on a new rule)

  • requireMergeQueueOptional

    boolean

    The branch moves only through its merge queue; direct pushes and the merge button are refused (false on a new rule)

  • mergeQueueMethodOptional

    enum

    How the queue merges each pull request (merge on a new rule)

    One of: merge, squash, rebase

  • mergeQueueBuildConcurrencyOptional

    number

    How many queued pull requests may be building at once (5 on a new rule)

    Constraints: integer, 1-100

  • mergeQueueMinGroupSizeOptional

    number

    Fewest pull requests the queue waits for before it builds, up to the wait time (1 on a new rule)

    Constraints: integer, 1-100

  • mergeQueueMaxGroupSizeOptional

    number

    Most pull requests landed by one fast-forward (5 on a new rule)

    Constraints: integer, 1-100

  • mergeQueueWaitMinutesOptional

    number

    Minutes to wait for the minimum group size before building anyway (5 on a new rule)

    Constraints: integer, 0-360

  • mergeQueueCheckTimeoutMinutesOptional

    number

    Minutes a queue build may wait for its checks before it fails (60 on a new rule)

    Constraints: integer, 1-360

  • mergeQueueOnlyNonFailingOptional

    boolean

    Land only pull requests whose own build passed; off, a failed build still lands if a later build containing it passes (true on a new rule)

Response

201 with the rule, every setting filled in (the defaults for those not sent). 400 when the minimum group size exceeds the maximum.

A JSON response is wrapped as { "success": true, "data": … }; the CLI prints the whole body with --json.

Fields of data

  • idAlways

    string

    The rule's id
  • orgIdAlways

    string

    Your organization
  • repoIdAlways

    string

    The repository the record belongs to
  • branchPatternAlways

    string

    The branch name or glob the rule covers
  • requirePullRequestAlways

    boolean

    Changes must land through a pull request
  • requiredApprovalsAlways

    number

    Approvals a pull request needs to merge
  • dismissStaleApprovalsAlways

    boolean

    A new push withdraws earlier approvals
  • requireCodeOwnerReviewAlways

    boolean

    Every changed file CODEOWNERS assigns needs an approval from one of its owners
  • requireStatusChecksAlways

    boolean

    Required checks must pass before a merge
  • requiredChecksAlways

    array of string

    Checks that must pass, by workflow or status context name; empty means every workflow a pull request triggers
  • blockForcePushAlways

    boolean

    Pushes that rewrite the branch history are refused
  • blockDeletionAlways

    boolean

    Deleting the branch is refused
  • requireMergeQueueAlways

    boolean

    The branch moves only through the merge queue; direct pushes and merges are refused
  • mergeQueueMethodAlways

    one of merge, squash, rebase

    How the queue merges each pull request
  • mergeQueueBuildConcurrencyAlways

    number

    How many queued pull requests may be building at once
  • mergeQueueMinGroupSizeAlways

    number

    Fewest pull requests to wait for before the first build starts
  • mergeQueueMaxGroupSizeAlways

    number

    Most pull requests landed by one fast-forward
  • mergeQueueWaitMinutesAlways

    number

    Minutes to wait for the minimum group size before building anyway
  • mergeQueueCheckTimeoutMinutesAlways

    number

    Minutes a queue build may wait for its checks before it fails
  • mergeQueueOnlyNonFailingAlways

    boolean

    Only pull requests whose own build passed land; off lets a later passing build carry them
  • createdAtAlways

    string

    When the rule was added (ISO 8601)
  • updatedAtAlways

    string

    When the rule was last changed (ISO 8601)

Checked against the handlers: every route test validates their answers against this schema.

Errors

  • 400The request does not pass the schema above. The body is { "error": …, "issues": […] }, one issue per field.
  • 401No credentials, or an access key that is unknown, revoked or expired.
  • 403The caller's policies do not allow git:ManageBranchProtection on this resource. The body names the action and the resource, and the refusal is recorded in the audit log.
  • 404The resource does not exist in your organization.

Examples

The required parameters only, with example values. Replace id and the other placeholders with your own.

dcloud git branch-protection create repoId --branch-pattern example

Generated from the API description, the same one the API checks requests against and the CLI is built from. MCP tool: dcloud_git_branch-protection_create.