Skip to main content
The REST API is now versioned. For more information, see "About API versioning."

规则

使用 REST API 管理组织的规则集。 组织规则集控制人员如何与组织存储库中的选定分支和标记进行交互。

Get all organization repository rulesets

Works with GitHub Apps

Get all the repository rulesets for an organization.

Parameters for "Get all organization repository rulesets"

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
org string 必须

The organization name. The name is not case sensitive.

查询参数
名称, 类型, 说明
per_page integer

The number of results per page (max 100).

默认: 30

page integer

Page number of the results to fetch.

默认: 1

HTTP response status codes for "Get all organization repository rulesets"

Status codeDescription
200

OK

404

Resource not found

500

Internal Error

Code samples for "Get all organization repository rulesets"

get/orgs/{org}/rulesets
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/rulesets

Response

Status: 200
[ { "id": 21, "name": "super cool ruleset", "source_type": "Organization", "source": "my-org", "enforcement": "enabled", "node_id": "RRS_lACkVXNlcgQB", "_links": { "self": { "href": "https://api.github.com/orgs/my-org/rulesets/21" }, "html": { "href": "https://github.com/organizations/my-org/settings/rules/21" } }, "created_at": "2023-07-15T08:43:03Z", "updated_at": "2023-08-23T16:29:47Z" }, { "id": 432, "name": "Another ruleset", "source_type": "Organization", "source": "my-org", "enforcement": "enabled", "node_id": "RRS_lACkVXNlcgQQ", "_links": { "self": { "href": "https://api.github.com/orgs/my-org/rulesets/432" }, "html": { "href": "https://github.com/organizations/my-org/settings/rules/432" } }, "created_at": "2023-08-15T08:43:03Z", "updated_at": "2023-09-23T16:29:47Z" } ]

Create an organization repository ruleset

Works with GitHub Apps

Create a repository ruleset for an organization.

Parameters for "Create an organization repository ruleset"

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
org string 必须

The organization name. The name is not case sensitive.

正文参数
名称, 类型, 说明
name string 必须

The name of the ruleset.

target string

The target of the ruleset.

可以是以下选项之一: branch, tag

enforcement string 必须

The enforcement level of the ruleset. evaluate allows admins to test rules before enforcing them. Admins can view insights on the Rule Insights page (evaluate is only available with GitHub Enterprise).

可以是以下选项之一: disabled, active, evaluate

bypass_actors array of objects

The actors that can bypass the rules in this ruleset

名称, 类型, 说明
actor_id integer 必须

The ID of the actor that can bypass a ruleset. If actor_type is OrganizationAdmin, this should be 1.

actor_type string 必须

The type of actor that can bypass a ruleset

可以是以下选项之一: RepositoryRole, Team, Integration, OrganizationAdmin

bypass_mode string 必须

When the specified actor can bypass the ruleset. pull_request means that an actor can only bypass rules on pull requests.

可以是以下选项之一: always, pull_request

conditions object

Conditions for an organization ruleset. The conditions object should contain both repository_name and ref_name properties or both repository_id and ref_name properties.

名称, 类型, 说明
repository_name_and_ref_name object 必须

Conditions to target repositories by name and refs by name

名称, 类型, 说明
ref_name object
名称, 类型, 说明
include array of strings

Array of ref names or patterns to include. One of these patterns must match for the condition to pass. Also accepts ~DEFAULT_BRANCH to include the default branch or ~ALL to include all branches.

exclude array of strings

Array of ref names or patterns to exclude. The condition will not pass if any of these patterns match.

repository_name object 必须
名称, 类型, 说明
include array of strings

Array of repository names or patterns to include. One of these patterns must match for the condition to pass. Also accepts ~ALL to include all repositories.

exclude array of strings

Array of repository names or patterns to exclude. The condition will not pass if any of these patterns match.

protected boolean

Whether renaming of target repositories is prevented.

repository_id_and_ref_name object 必须

Conditions to target repositories by id and refs by name

名称, 类型, 说明
ref_name object
名称, 类型, 说明
include array of strings

Array of ref names or patterns to include. One of these patterns must match for the condition to pass. Also accepts ~DEFAULT_BRANCH to include the default branch or ~ALL to include all branches.

exclude array of strings

Array of ref names or patterns to exclude. The condition will not pass if any of these patterns match.

repository_id object 必须
名称, 类型, 说明
repository_ids array of integers

The repository IDs that the ruleset applies to. One of these IDs must match for the condition to pass.

rules array of objects

An array of rules within the ruleset.

名称, 类型, 说明
creation object 必须

Only allow users with bypass permission to create matching refs.

名称, 类型, 说明
type string 必须

: creation

update object 必须

Only allow users with bypass permission to update matching refs.

名称, 类型, 说明
type string 必须

: update

parameters object
名称, 类型, 说明
update_allows_fetch_and_merge boolean 必须

Branch can pull changes from its upstream repository

deletion object 必须

Only allow users with bypass permissions to delete matching refs.

名称, 类型, 说明
type string 必须

: deletion

required_linear_history object 必须

Prevent merge commits from being pushed to matching refs.

名称, 类型, 说明
type string 必须

: required_linear_history

required_deployments object 必须

Choose which environments must be successfully deployed to before refs can be pushed into a ref that matches this rule.

名称, 类型, 说明
type string 必须

: required_deployments

parameters object
名称, 类型, 说明
required_deployment_environments array of strings 必须

The environments that must be successfully deployed to before branches can be merged.

required_signatures object 必须

Commits pushed to matching refs must have verified signatures.

名称, 类型, 说明
type string 必须

: required_signatures

pull_request object 必须

Require all commits be made to a non-target branch and submitted via a pull request before they can be merged.

名称, 类型, 说明
type string 必须

: pull_request

parameters object
名称, 类型, 说明
dismiss_stale_reviews_on_push boolean 必须

New, reviewable commits pushed will dismiss previous pull request review approvals.

require_code_owner_review boolean 必须

Require an approving review in pull requests that modify files that have a designated code owner.

require_last_push_approval boolean 必须

Whether the most recent reviewable push must be approved by someone other than the person who pushed it.

required_approving_review_count integer 必须

The number of approving reviews that are required before a pull request can be merged.

required_review_thread_resolution boolean 必须

All conversations on code must be resolved before a pull request can be merged.

required_status_checks object 必须

Choose which status checks must pass before the ref is updated. When enabled, commits must first be pushed to another ref where the checks pass.

名称, 类型, 说明
type string 必须

: required_status_checks

parameters object
名称, 类型, 说明
required_status_checks array of objects 必须

Status checks that are required.

名称, 类型, 说明
context string 必须

The status check context name that must be present on the commit.

integration_id integer

The optional integration ID that this status check must originate from.

strict_required_status_checks_policy boolean 必须

Whether pull requests targeting a matching branch must be tested with the latest code. This setting will not take effect unless at least one status check is enabled.

non_fast_forward object 必须

Prevent users with push access from force pushing to refs.

名称, 类型, 说明
type string 必须

: non_fast_forward

commit_message_pattern object 必须

Parameters to be used for the commit_message_pattern rule

名称, 类型, 说明
type string 必须

: commit_message_pattern

parameters object
名称, 类型, 说明
name string

How this rule will appear to users.

negate boolean

If true, the rule will fail if the pattern matches.

operator string 必须

The operator to use for matching.

可以是以下选项之一: starts_with, ends_with, contains, regex

pattern string 必须

The pattern to match with.

commit_author_email_pattern object 必须

Parameters to be used for the commit_author_email_pattern rule

名称, 类型, 说明
type string 必须

: commit_author_email_pattern

parameters object
名称, 类型, 说明
name string

How this rule will appear to users.

negate boolean

If true, the rule will fail if the pattern matches.

operator string 必须

The operator to use for matching.

可以是以下选项之一: starts_with, ends_with, contains, regex

pattern string 必须

The pattern to match with.

committer_email_pattern object 必须

Parameters to be used for the committer_email_pattern rule

名称, 类型, 说明
type string 必须

: committer_email_pattern

parameters object
名称, 类型, 说明
name string

How this rule will appear to users.

negate boolean

If true, the rule will fail if the pattern matches.

operator string 必须

The operator to use for matching.

可以是以下选项之一: starts_with, ends_with, contains, regex

pattern string 必须

The pattern to match with.

branch_name_pattern object 必须

Parameters to be used for the branch_name_pattern rule

名称, 类型, 说明
type string 必须

: branch_name_pattern

parameters object
名称, 类型, 说明
name string

How this rule will appear to users.

negate boolean

If true, the rule will fail if the pattern matches.

operator string 必须

The operator to use for matching.

可以是以下选项之一: starts_with, ends_with, contains, regex

pattern string 必须

The pattern to match with.

tag_name_pattern object 必须

Parameters to be used for the tag_name_pattern rule

名称, 类型, 说明
type string 必须

: tag_name_pattern

parameters object
名称, 类型, 说明
name string

How this rule will appear to users.

negate boolean

If true, the rule will fail if the pattern matches.

operator string 必须

The operator to use for matching.

可以是以下选项之一: starts_with, ends_with, contains, regex

pattern string 必须

The pattern to match with.

workflows object 必须

Require all changes made to a targeted branch to pass the specified workflows before they can be merged.

名称, 类型, 说明
type string 必须

: workflows

parameters object
名称, 类型, 说明
workflows array of objects 必须

Workflows that must pass for this rule to pass.

名称, 类型, 说明
path string 必须

The path to the workflow file

ref string

The ref (branch or tag) of the workflow file to use

repository_id integer 必须

The ID of the repository where the workflow is defined

sha string

The commit SHA of the workflow file to use

HTTP response status codes for "Create an organization repository ruleset"

Status codeDescription
201

Created

404

Resource not found

500

Internal Error

Code samples for "Create an organization repository ruleset"

post/orgs/{org}/rulesets
curl -L \ -X POST \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/rulesets \ -d '{"name":"super cool ruleset","target":"branch","enforcement":"active","bypass_actors":[{"actor_id":234,"actor_type":"Team","bypass_mode":"always"}],"conditions":{"ref_name":{"include":["refs/heads/main","refs/heads/master"],"exclude":["refs/heads/dev*"]},"repository_name":{"include":["important_repository","another_important_repository"],"exclude":["unimportant_repository"],"protected":true}},"rules":[{"type":"commit_author_email_pattern","parameters":{"operator":"contains","pattern":"github"}}]}'

Response

Status: 201
{ "id": 21, "name": "super cool ruleset", "target": "branch", "source_type": "Organization", "source": "my-org", "enforcement": "active", "bypass_actors": [ { "actor_id": 234, "actor_type": "Team", "bypass_mode": "always" } ], "conditions": { "ref_name": { "include": [ "refs/heads/main", "refs/heads/master" ], "exclude": [ "refs/heads/dev*" ] }, "repository_name": { "include": [ "important_repository", "another_important_repository" ], "exclude": [ "unimportant_repository" ], "protected": true } }, "rules": [ { "type": "commit_author_email_pattern", "parameters": { "operator": "contains", "pattern": "github" } } ], "node_id": "RRS_lACkVXNlcgQB", "_links": { "self": { "href": "https://api.github.com/orgs/my-org/rulesets/21" }, "html": { "href": "https://github.com/organizations/my-org/settings/rules/21" } }, "created_at": "2023-08-15T08:43:03Z", "updated_at": "2023-09-23T16:29:47Z" }

Get an organization repository ruleset

Works with GitHub Apps

Get a repository ruleset for an organization.

Parameters for "Get an organization repository ruleset"

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
org string 必须

The organization name. The name is not case sensitive.

ruleset_id integer 必须

The ID of the ruleset.

HTTP response status codes for "Get an organization repository ruleset"

Status codeDescription
200

OK

404

Resource not found

500

Internal Error

Code samples for "Get an organization repository ruleset"

get/orgs/{org}/rulesets/{ruleset_id}
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/rulesets/RULESET_ID

Response

Status: 200
{ "id": 21, "name": "super cool ruleset", "target": "branch", "source_type": "Organization", "source": "my-org", "enforcement": "active", "bypass_actors": [ { "actor_id": 234, "actor_type": "Team", "bypass_mode": "always" } ], "conditions": { "ref_name": { "include": [ "refs/heads/main", "refs/heads/master" ], "exclude": [ "refs/heads/dev*" ] }, "repository_name": { "include": [ "important_repository", "another_important_repository" ], "exclude": [ "unimportant_repository" ], "protected": true } }, "rules": [ { "type": "commit_author_email_pattern", "parameters": { "operator": "contains", "pattern": "github" } } ], "node_id": "RRS_lACkVXNlcgQB", "_links": { "self": { "href": "https://api.github.com/orgs/my-org/rulesets/21" }, "html": { "href": "https://github.com/organizations/my-org/settings/rules/21" } }, "created_at": "2023-08-15T08:43:03Z", "updated_at": "2023-09-23T16:29:47Z" }

Update an organization repository ruleset

Works with GitHub Apps

Update a ruleset for an organization.

Parameters for "Update an organization repository ruleset"

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
org string 必须

The organization name. The name is not case sensitive.

ruleset_id integer 必须

The ID of the ruleset.

正文参数
名称, 类型, 说明
name string

The name of the ruleset.

target string

The target of the ruleset.

可以是以下选项之一: branch, tag

enforcement string

The enforcement level of the ruleset. evaluate allows admins to test rules before enforcing them. Admins can view insights on the Rule Insights page (evaluate is only available with GitHub Enterprise).

可以是以下选项之一: disabled, active, evaluate

bypass_actors array of objects

The actors that can bypass the rules in this ruleset

名称, 类型, 说明
actor_id integer 必须

The ID of the actor that can bypass a ruleset. If actor_type is OrganizationAdmin, this should be 1.

actor_type string 必须

The type of actor that can bypass a ruleset

可以是以下选项之一: RepositoryRole, Team, Integration, OrganizationAdmin

bypass_mode string 必须

When the specified actor can bypass the ruleset. pull_request means that an actor can only bypass rules on pull requests.

可以是以下选项之一: always, pull_request

conditions object

Conditions for an organization ruleset. The conditions object should contain both repository_name and ref_name properties or both repository_id and ref_name properties.

名称, 类型, 说明
repository_name_and_ref_name object 必须

Conditions to target repositories by name and refs by name

名称, 类型, 说明
ref_name object
名称, 类型, 说明
include array of strings

Array of ref names or patterns to include. One of these patterns must match for the condition to pass. Also accepts ~DEFAULT_BRANCH to include the default branch or ~ALL to include all branches.

exclude array of strings

Array of ref names or patterns to exclude. The condition will not pass if any of these patterns match.

repository_name object 必须
名称, 类型, 说明
include array of strings

Array of repository names or patterns to include. One of these patterns must match for the condition to pass. Also accepts ~ALL to include all repositories.

exclude array of strings

Array of repository names or patterns to exclude. The condition will not pass if any of these patterns match.

protected boolean

Whether renaming of target repositories is prevented.

repository_id_and_ref_name object 必须

Conditions to target repositories by id and refs by name

名称, 类型, 说明
ref_name object
名称, 类型, 说明
include array of strings

Array of ref names or patterns to include. One of these patterns must match for the condition to pass. Also accepts ~DEFAULT_BRANCH to include the default branch or ~ALL to include all branches.

exclude array of strings

Array of ref names or patterns to exclude. The condition will not pass if any of these patterns match.

repository_id object 必须
名称, 类型, 说明
repository_ids array of integers

The repository IDs that the ruleset applies to. One of these IDs must match for the condition to pass.

rules array of objects

An array of rules within the ruleset.

名称, 类型, 说明
creation object 必须

Only allow users with bypass permission to create matching refs.

名称, 类型, 说明
type string 必须

: creation

update object 必须

Only allow users with bypass permission to update matching refs.

名称, 类型, 说明
type string 必须

: update

parameters object
名称, 类型, 说明
update_allows_fetch_and_merge boolean 必须

Branch can pull changes from its upstream repository

deletion object 必须

Only allow users with bypass permissions to delete matching refs.

名称, 类型, 说明
type string 必须

: deletion

required_linear_history object 必须

Prevent merge commits from being pushed to matching refs.

名称, 类型, 说明
type string 必须

: required_linear_history

required_deployments object 必须

Choose which environments must be successfully deployed to before refs can be pushed into a ref that matches this rule.

名称, 类型, 说明
type string 必须

: required_deployments

parameters object
名称, 类型, 说明
required_deployment_environments array of strings 必须

The environments that must be successfully deployed to before branches can be merged.

required_signatures object 必须

Commits pushed to matching refs must have verified signatures.

名称, 类型, 说明
type string 必须

: required_signatures

pull_request object 必须

Require all commits be made to a non-target branch and submitted via a pull request before they can be merged.

名称, 类型, 说明
type string 必须

: pull_request

parameters object
名称, 类型, 说明
dismiss_stale_reviews_on_push boolean 必须

New, reviewable commits pushed will dismiss previous pull request review approvals.

require_code_owner_review boolean 必须

Require an approving review in pull requests that modify files that have a designated code owner.

require_last_push_approval boolean 必须

Whether the most recent reviewable push must be approved by someone other than the person who pushed it.

required_approving_review_count integer 必须

The number of approving reviews that are required before a pull request can be merged.

required_review_thread_resolution boolean 必须

All conversations on code must be resolved before a pull request can be merged.

required_status_checks object 必须

Choose which status checks must pass before the ref is updated. When enabled, commits must first be pushed to another ref where the checks pass.

名称, 类型, 说明
type string 必须

: required_status_checks

parameters object
名称, 类型, 说明
required_status_checks array of objects 必须

Status checks that are required.

名称, 类型, 说明
context string 必须

The status check context name that must be present on the commit.

integration_id integer

The optional integration ID that this status check must originate from.

strict_required_status_checks_policy boolean 必须

Whether pull requests targeting a matching branch must be tested with the latest code. This setting will not take effect unless at least one status check is enabled.

non_fast_forward object 必须

Prevent users with push access from force pushing to refs.

名称, 类型, 说明
type string 必须

: non_fast_forward

commit_message_pattern object 必须

Parameters to be used for the commit_message_pattern rule

名称, 类型, 说明
type string 必须

: commit_message_pattern

parameters object
名称, 类型, 说明
name string

How this rule will appear to users.

negate boolean

If true, the rule will fail if the pattern matches.

operator string 必须

The operator to use for matching.

可以是以下选项之一: starts_with, ends_with, contains, regex

pattern string 必须

The pattern to match with.

commit_author_email_pattern object 必须

Parameters to be used for the commit_author_email_pattern rule

名称, 类型, 说明
type string 必须

: commit_author_email_pattern

parameters object
名称, 类型, 说明
name string

How this rule will appear to users.

negate boolean

If true, the rule will fail if the pattern matches.

operator string 必须

The operator to use for matching.

可以是以下选项之一: starts_with, ends_with, contains, regex

pattern string 必须

The pattern to match with.

committer_email_pattern object 必须

Parameters to be used for the committer_email_pattern rule

名称, 类型, 说明
type string 必须

: committer_email_pattern

parameters object
名称, 类型, 说明
name string

How this rule will appear to users.

negate boolean

If true, the rule will fail if the pattern matches.

operator string 必须

The operator to use for matching.

可以是以下选项之一: starts_with, ends_with, contains, regex

pattern string 必须

The pattern to match with.

branch_name_pattern object 必须

Parameters to be used for the branch_name_pattern rule

名称, 类型, 说明
type string 必须

: branch_name_pattern

parameters object
名称, 类型, 说明
name string

How this rule will appear to users.

negate boolean

If true, the rule will fail if the pattern matches.

operator string 必须

The operator to use for matching.

可以是以下选项之一: starts_with, ends_with, contains, regex

pattern string 必须

The pattern to match with.

tag_name_pattern object 必须

Parameters to be used for the tag_name_pattern rule

名称, 类型, 说明
type string 必须

: tag_name_pattern

parameters object
名称, 类型, 说明
name string

How this rule will appear to users.

negate boolean

If true, the rule will fail if the pattern matches.

operator string 必须

The operator to use for matching.

可以是以下选项之一: starts_with, ends_with, contains, regex

pattern string 必须

The pattern to match with.

workflows object 必须

Require all changes made to a targeted branch to pass the specified workflows before they can be merged.

名称, 类型, 说明
type string 必须

: workflows

parameters object
名称, 类型, 说明
workflows array of objects 必须

Workflows that must pass for this rule to pass.

名称, 类型, 说明
path string 必须

The path to the workflow file

ref string

The ref (branch or tag) of the workflow file to use

repository_id integer 必须

The ID of the repository where the workflow is defined

sha string

The commit SHA of the workflow file to use

HTTP response status codes for "Update an organization repository ruleset"

Status codeDescription
200

OK

404

Resource not found

500

Internal Error

Code samples for "Update an organization repository ruleset"

put/orgs/{org}/rulesets/{ruleset_id}
curl -L \ -X PUT \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/rulesets/RULESET_ID \ -d '{"name":"super cool ruleset","target":"branch","enforcement":"active","bypass_actors":[{"actor_id":234,"actor_type":"Team","bypass_mode":"always"}],"conditions":{"ref_name":{"include":["refs/heads/main","refs/heads/master"],"exclude":["refs/heads/dev*"]},"repository_name":{"include":["important_repository","another_important_repository"],"exclude":["unimportant_repository"],"protected":true}},"rules":[{"type":"commit_author_email_pattern","parameters":{"operator":"contains","pattern":"github"}}]}'

Response

Status: 200
{ "id": 21, "name": "super cool ruleset", "target": "branch", "source_type": "Organization", "source": "my-org", "enforcement": "active", "bypass_actors": [ { "actor_id": 234, "actor_type": "Team", "bypass_mode": "always" } ], "conditions": { "ref_name": { "include": [ "refs/heads/main", "refs/heads/master" ], "exclude": [ "refs/heads/dev*" ] }, "repository_name": { "include": [ "important_repository", "another_important_repository" ], "exclude": [ "unimportant_repository" ], "protected": true } }, "rules": [ { "type": "commit_author_email_pattern", "parameters": { "operator": "contains", "pattern": "github" } } ], "node_id": "RRS_lACkVXNlcgQB", "_links": { "self": { "href": "https://api.github.com/orgs/my-org/rulesets/21" }, "html": { "href": "https://github.com/organizations/my-org/settings/rules/21" } }, "created_at": "2023-08-15T08:43:03Z", "updated_at": "2023-09-23T16:29:47Z" }

Delete an organization repository ruleset

Works with GitHub Apps

Delete a ruleset for an organization.

Parameters for "Delete an organization repository ruleset"

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
org string 必须

The organization name. The name is not case sensitive.

ruleset_id integer 必须

The ID of the ruleset.

HTTP response status codes for "Delete an organization repository ruleset"

Status codeDescription
204

No Content

404

Resource not found

500

Internal Error

Code samples for "Delete an organization repository ruleset"

delete/orgs/{org}/rulesets/{ruleset_id}
curl -L \ -X DELETE \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/orgs/ORG/rulesets/RULESET_ID

Response

Status: 204