I'd like to get the value set for the Require a pull request before merging via the GraphQL API. I'm not finding or am missing the information for the docs on the BranchProtectionRule. I'd imagine this should be obvious but isn't for me at the moment.
And for the purposes of helping out anyone else on a related path, here's my code for getting the branch protection rules using PowerShell and GitHub GraphQL API:
$JsonContent = & "gh" api graphql -H 'X-Github-Next-Global-ID: 1' -F owner="$repoowner" -F name="$reponame" -f query='
query($name: String!, $owner: String!) {
repository(owner: $owner, name: $name) {
branchProtectionRules(first: 100) {
nodes {
matchingRefs(first: 100) {
nodes {
name
}
}
pattern
allowsDeletions
allowsForcePushes
dismissesStaleReviews
isAdminEnforced
pushAllowances(first: 100) {
nodes {
actor {
... on App {
id
name
slug
}
... on Team {
id
name
slug
}
... on User {
id
name
login
}
}
}
}
requiresApprovingReviews
requiredApprovingReviewCount
requiresCodeOwnerReviews
requiresCommitSignatures
requiresConversationResolution
requiresLinearHistory
requiresStatusChecks
requiredStatusCheckContexts
requiresStrictStatusChecks
restrictsPushes
restrictsReviewDismissals
}
}
}
}
'
I think I've found the answer; confusingly, the Require a pull request before merging setting seems to correspond with the requiresApprovingReviews
value in conjunction with the requiredApprovingReviewCount
value—despite the setting in the GitHub UI not mentioning reviews at all.
To try and figure this out, I selected all available properties in my GraphQL query for a single repository, then toggled the setting on and off to see what was different.
query MyQuery {
repository(name: "MY_REPO_NAME", owner: "MY_ORGANISATION") {
branchProtectionRules(first: 10) {
edges {
node {
allowsDeletions
allowsForcePushes
blocksCreations
dismissesStaleReviews
isAdminEnforced
lockAllowsFetchAndMerge
lockBranch
pattern
requireLastPushApproval
requiredApprovingReviewCount
requiredDeploymentEnvironments
requiresApprovingReviews
requiresCodeOwnerReviews
requiresCommitSignatures
requiresConversationResolution
requiresDeployments
requiresLinearHistory
requiresStatusChecks
requiresStrictStatusChecks
restrictsPushes
restrictsReviewDismissals
}
}
}
}
}
So the results of that were that with the setting unchecked:
We get:
{
"data": {
"repository": {
"branchProtectionRules": {
"edges": [
{
"node": {
"allowsDeletions": false,
"allowsForcePushes": false,
"blocksCreations": false,
"dismissesStaleReviews": false,
"isAdminEnforced": false,
"lockAllowsFetchAndMerge": false,
"lockBranch": false,
"pattern": "master",
"requireLastPushApproval": false,
"requiredApprovingReviewCount": null,
"requiredDeploymentEnvironments": [],
"requiresApprovingReviews": false,
"requiresCodeOwnerReviews": false,
"requiresCommitSignatures": false,
"requiresConversationResolution": false,
"requiresDeployments": false,
"requiresLinearHistory": false,
"requiresStatusChecks": false,
"requiresStrictStatusChecks": true,
"restrictsPushes": false,
"restrictsReviewDismissals": false
}
}
]
}
}
}
}
And with the setting checked:
We get:
{
"data": {
"repository": {
"branchProtectionRules": {
"edges": [
{
"node": {
"allowsDeletions": false,
"allowsForcePushes": false,
"blocksCreations": false,
"dismissesStaleReviews": false,
"isAdminEnforced": false,
"lockAllowsFetchAndMerge": false,
"lockBranch": false,
"pattern": "master",
"requireLastPushApproval": false,
"requiredApprovingReviewCount": 0,
"requiredDeploymentEnvironments": [],
"requiresApprovingReviews": true,
"requiresCodeOwnerReviews": false,
"requiresCommitSignatures": false,
"requiresConversationResolution": false,
"requiresDeployments": false,
"requiresLinearHistory": false,
"requiresStatusChecks": false,
"requiresStrictStatusChecks": true,
"restrictsPushes": false,
"restrictsReviewDismissals": false
}
}
]
}
}
}
}
So the difference is:
So it seems you can say that if requiresApprovingReviews
is true
then the Require a pull request before merging setting is enabled for that repository. The requiredApprovingReviewCount
will be 0
if no PR approvals are required to merge.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With