Created
January 13, 2022 20:04
-
-
Save tvansteenburgh/b91475d4fce1623721266511422097d5 to your computer and use it in GitHub Desktop.
Disable force pushing to main branches of Github repos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import datetime | |
import os | |
import sys | |
from github import Github, GithubException | |
GITHUB_ACCESS_TOKEN = os.environ.get('GITHUB_ACCESS_TOKEN') | |
if not GITHUB_ACCESS_TOKEN: | |
print("You need a GITHUB_ACCESS_TOKEN environment variable defined.\n" | |
"You can create a Github access token at https://github.com/settings/tokens.") | |
sys.exit(1) | |
g = Github(GITHUB_ACCESS_TOKEN) | |
org = g.get_organization("stacklet") | |
for repo in org.get_repos(type='all'): # type: ‘all’, ‘public’, ‘private’, ‘forks’, ‘sources’, ‘member’ | |
try: | |
branch = repo.get_branch(branch="main") | |
except GithubException as e: | |
if e.status == 404: | |
print('{} has no stable branch'.format(repo.name)) | |
continue | |
else: | |
raise | |
branch.edit_protection(allow_force_pushes=False) | |
print('Disabled force pushes for {}/stable'.format(repo.name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment