Skip to content

Instantly share code, notes, and snippets.

@mattlorimor
Created July 13, 2018 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattlorimor/a1de15cd57b5cb7b70408fc43a6a2d77 to your computer and use it in GitHub Desktop.
Save mattlorimor/a1de15cd57b5cb7b70408fc43a6a2d77 to your computer and use it in GitHub Desktop.
Programmatically turn on dependency tracking for repos
import requests as r
from bs4 import BeautifulSoup
# Copy your GitHub cookie here before running
headers = {'Cookie': 'REDACTED'}
# List of repos to enable
# e.g., ['coolRepo', 'awesomeRepo']
repo_list = []
user_or_org = ''
failed_on_list = []
succeeded_on_list = []
def main():
global failed_on_list
global succeeded_on_list
count = 1
for repo in repo_list:
try:
print('Enabling dependency graph on repo: {} ({} of {})'.format(repo, count, len(repo_list)))
push_the_button(repo)
succeeded_on_list.append(repo)
count += 1
except Exception as e:
# Failed to enable in some way
# Repo likely already had it enabled
# Add to the list of repos to double check anyway
failed_on_list.append(repo)
count += 1
print('Failed on: {}'.format(failed_on_list))
print('Succeeded on: {}'.format(succeeded_on_list))
def push_the_button(repo):
global headers
global repo_list
# First do a get on the repo that we are going to push the button on. This needs to be done
# to get the authenticity_token to include in the push POST.
get_url = 'https://github.com/{}/{}/network/dependencies'.format(user_or_org, repo)
#print('Getting: {}'.format(repo))
get_response = r.get(get_url, headers=headers)
if get_response.status_code >= 400:
raise Exception
#print('Response code: {}'.format(get_response))
new_cookie = get_response.headers.get('Set-Cookie')
headers = {'Cookie': new_cookie}
# Pull out the authenticity_token
soup = BeautifulSoup(get_response.text, 'html.parser')
action = '/{}/{}/network/dependencies/enable'.format(user_or_org, repo)
authenticity_token = soup.find('form', {'action': action}).find('input', {'name': 'authenticity_token'}).get('value')
#print(authenticity_token)
# Push the button with a POST
#print('Pushing button: {}'.format(repo))
headers = {**headers, 'content-type': 'application/x-www-form-urlencoded'}
params = {
'utf8': '✓',
'_method': 'put',
'authenticity_token': authenticity_token
}
post_url = 'https://github.com/{}/{}/network/dependencies/enable'.format(user_or_org, repo)
post_response = r.post(post_url, headers=headers, data=params)
if post_response.status_code >= 400:
raise Exception
#print('Response code: {}'.format(post_response))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment