0

We have a requirement to enable merge queue for 100's of our repositories, I went through lot of information on internet and found out that support to enable merge queue is only available via GitHub Graphql API's and that too is only possible via rulesets.

https://github.com/orgs/community/discussions/77614

I was able to test the code for branch protection using graphql API's but facing issues with right mutation to use to enable merge queue's using rulesets.

Can someone help and share right mutation to use? Also, if there is any other way to enable this using branch protection API's instead of using rulesets or using pygithub, please share that too.

enter image description here

import requests

GITHUB_API_URL = 'https://git.corp.<mycompany>.com/api/graphql'
TOKEN = 'token'

headers = {
    "Authorization": f"Bearer {TOKEN}",
    "Content-Type": "application/json"
}

# Replace these values with your repository details
REPOSITORY_OWNER = "test"
REPOSITORY_NAME = "test-example"
BRANCH_PATTERN = "main"  # Example: "main" for main branch

# GraphQL mutation to create a branch protection rule
mutation = """
mutation($repositoryId: ID!, $branchPattern: String!) {
  createBranchProtectionRule(input: {
    repositoryId: $repositoryId,
    pattern: $branchPattern,
    requiresApprovingReviews: true,
    requiresStatusChecks: true,
    isAdminEnforced: true
  }) {
    branchProtectionRule {
      id
      pattern
      requiresApprovingReviews
      requiresStatusChecks
    }
  }
}
"""

# Query to get the repository ID
repository_query = """
query($owner: String!, $name: String!) {
  repository(owner: $owner, name: $name) {
    id
  }
}
"""

def get_repository_id():
    variables = {"owner": REPOSITORY_OWNER, "name": REPOSITORY_NAME}
    response = requests.post(
        GITHUB_API_URL, 
        json={"query": repository_query, "variables": variables}, 
        headers=headers
    )
    if response.status_code == 200:
        return response.json()["data"]["repository"]["id"]
    else:
        raise Exception(f"Failed to fetch repository ID: {response.json()}")

def create_branch_protection_rule(repository_id):
    variables = {
        "repositoryId": repository_id,
        "branchPattern": BRANCH_PATTERN,
    }
    response = requests.post(
        GITHUB_API_URL, 
        json={"query": mutation, "variables": variables}, 
        headers=headers
    )
    if response.status_code == 200:
        print("Branch protection rule created:", response.json()["data"]["createBranchProtectionRule"]["branchProtectionRule"])
    else:
        raise Exception(f"Failed to create branch protection rule: {response.json()}")

# Main execution
try:
    repository_id = get_repository_id()
    create_branch_protection_rule(repository_id)
except Exception as e:
    print("Error:", e)
1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Commented Dec 11, 2024 at 1:42

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.