aboutsummaryrefslogtreecommitdiff
path: root/applications/github/github_branch_protections.py
diff options
context:
space:
mode:
authorChristian Cleberg <hello@cleberg.net>2025-04-05 13:43:01 -0500
committerGitHub <noreply@github.com>2025-04-05 13:43:01 -0500
commitbee22b97b652cd04fa470d7f31c3b917e44f3ab9 (patch)
treee1ec29a3388b94a8d41e3b28ca1f2357f3efaec7 /applications/github/github_branch_protections.py
parent3041472781997ff66c5f9fa80a7f5ffbd284a438 (diff)
downloadaudit-tools-bee22b97b652cd04fa470d7f31c3b917e44f3ab9.tar.gz
audit-tools-bee22b97b652cd04fa470d7f31c3b917e44f3ab9.tar.bz2
audit-tools-bee22b97b652cd04fa470d7f31c3b917e44f3ab9.zip
migrate from pylint to ruff (#1)
* migrate from pylint to ruff * Commit from GitHub Actions (Pylint) * rename pylint.yml to ruff.yml * only run ruff-action when python files change --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Diffstat (limited to 'applications/github/github_branch_protections.py')
-rw-r--r--applications/github/github_branch_protections.py28
1 files changed, 17 insertions, 11 deletions
diff --git a/applications/github/github_branch_protections.py b/applications/github/github_branch_protections.py
index 73c94b6..705e498 100644
--- a/applications/github/github_branch_protections.py
+++ b/applications/github/github_branch_protections.py
@@ -4,16 +4,17 @@ Gathers branch protection rules for a repository.
import requests
-GITHUB_TOKEN = 'your_personal_access_token'
-ORGANIZATION = 'your_organization'
-REPOSITORY = 'your_repository'
+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'
+ "Authorization": f"token {GITHUB_TOKEN}",
+ "Accept": "application/vnd.github.v3+json",
}
+
def get_all_branches(org, repo):
"""
Get all branches in a repository
@@ -21,7 +22,7 @@ def get_all_branches(org, repo):
all_branches = []
page = 1
while True:
- url = f'https://api.github.com/repos/{org}/{repo}/branches?page={page}&per_page=100'
+ 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()
@@ -31,27 +32,30 @@ def get_all_branches(org, repo):
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'
+ 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'
+ 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__':
+
+if __name__ == "__main__":
try:
# Get all branches in the repository
branches = get_all_branches(ORGANIZATION, REPOSITORY)
@@ -59,8 +63,10 @@ if __name__ == '__main__':
# Get protection settings for each branch
for branch in branches:
- branch_name = branch['name']
- protection_settings = get_branch_protection(ORGANIZATION, REPOSITORY, branch_name)
+ 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}")