Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enable master branch protection on all github repositories you are an owner of.
#!/usr/bin/env python
from __future__ import print_function
REPO_URL = 'git+git://github.com/sigmavirus24/github3.py.git'
import os
import sys
try:
from github3 import login
except ImportError:
print('Please install github3.py: pip install {0}'.format(REPO_URL))
sys.exit(1)
def prompt(msg):
"""Output message and return striped input."""
sys.stdout.write('{0}: '.format(msg))
sys.stdout.flush()
return sys.stdin.readline().strip()
def get_session():
"""Return a github session from GITHUB_KEY, or user/pass authentication."""
key = os.getenv('GITHUB_KEY')
if key:
github = login(token=key)
else:
from getpass import getpass
username = prompt('GITHUB Username')
password = getpass('Password for {0}: '.format(username))
github = login(username, password,
two_factor_callback=lambda: prompt('Two factor token'))
return github
def main():
github = get_session()
for repository in github.repositories('owner'):
master = repository.branch('master')
if master.protection['enabled']:
print(' Protected: {0}'.format(repository))
else:
print('Protecting: {0}'.format(repository))
master.protect()
return 0
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment