aboutsummaryrefslogtreecommitdiff
path: root/github/github_branch_protections.py
diff options
context:
space:
mode:
authorChristian Cleberg <156287552+ccleberg@users.noreply.github.com>2024-10-28 19:50:25 +0000
committerGitHub <noreply@github.com>2024-10-28 19:50:25 +0000
commitc8db45d0004145c4668ba7b933216d928d4d1732 (patch)
tree830cf6c7bfb78662f4edf2e11a540ff30434d78c /github/github_branch_protections.py
parent5cca16c570ea0978d77165b11eadc22bb53019ae (diff)
downloadaudit-tools-c8db45d0004145c4668ba7b933216d928d4d1732.tar.gz
audit-tools-c8db45d0004145c4668ba7b933216d928d4d1732.tar.bz2
audit-tools-c8db45d0004145c4668ba7b933216d928d4d1732.zip
add github branch protections script
Diffstat (limited to 'github/github_branch_protections.py')
-rw-r--r--github/github_branch_protections.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/github/github_branch_protections.py b/github/github_branch_protections.py
new file mode 100644
index 0000000..73c94b6
--- /dev/null
+++ b/github/github_branch_protections.py
@@ -0,0 +1,78 @@
+"""
+Gathers branch protection rules for a repository.
+"""
+
+import requests
+
+GITHUB_TOKEN = 'your_personal_access_token'
+ORGANIZATION = 'your_organization'
+REPOSITORY = 'your_repository'
+TIMEOUT = 30
+
+headers = {
+ 'Authorization': f'token {GITHUB_TOKEN}',
+ 'Accept': 'application/vnd.github.v3+json'
+}
+
+def get_all_branches(org, repo):
+ """
+ Get all branches in a repository
+ """
+ all_branches = []
+ page = 1
+ while True:
+ url = f'https://api.github.com/repos/{org}/{repo}/branches?page={page}&per_page=100'
+ response = requests.get(url, headers=headers, timeout=TIMEOUT)
+ response.raise_for_status()
+ page_branches = response.json()
+ if not page_branches:
+ break
+ all_branches.extend(page_branches)
+ page += 1
+ return all_branches
+
+def get_branch_protection(org, repo, repo_branch):
+ """
+ Get branch protection settings
+ """
+ url = f'https://api.github.com/repos/{org}/{repo}/branches/{repo_branch}/protection'
+ response = requests.get(url, headers=headers, timeout=TIMEOUT)
+ if response.status_code == 404:
+ return None # No protection settings for this branch
+ response.raise_for_status()
+ return response.json()
+
+def get_repository_rulesets(org, repo):
+ """
+ Get repository rulesets
+ """
+ url = f'https://api.github.com/repos/{org}/{repo}/rulesets'
+ response = requests.get(url, headers=headers, timeout=TIMEOUT)
+ response.raise_for_status()
+ return response.json()
+
+if __name__ == '__main__':
+ try:
+ # Get all branches in the repository
+ branches = get_all_branches(ORGANIZATION, REPOSITORY)
+ print(f"Total branches in the repository '{REPOSITORY}': {len(branches)}")
+
+ # Get protection settings for each branch
+ for branch in branches:
+ branch_name = branch['name']
+ protection_settings = get_branch_protection(ORGANIZATION, REPOSITORY, branch_name)
+ print(f"\nBranch: {branch_name}")
+ if protection_settings:
+ print(f"Protection settings: {protection_settings}")
+ else:
+ print("No protection settings")
+
+ # Get repository rulesets
+ rulesets = get_repository_rulesets(ORGANIZATION, REPOSITORY)
+ print(f"\nRepository rulesets for '{REPOSITORY}':")
+ print(rulesets)
+
+ except requests.exceptions.Timeout:
+ print("The request timed out")
+ except requests.exceptions.RequestException as e:
+ print(f"An error occurred: {e}")