Created
November 5, 2020 23:06
-
-
Save eeeady/9f922ccc01599adb1d8310110d44b856 to your computer and use it in GitHub Desktop.
Update ALL THE REPOS
This file contains 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
import yaml | |
import os | |
import requests | |
import tempfile | |
import subprocess | |
from github import Github | |
g = Github(os.environ["GITHUB_TOKEN"]) | |
def getRepos(): | |
repos = [] | |
# today there are 158 repos... should be 16 pages | |
for page in range(1,17): | |
r = requests.get('https://api.github.com/orgs/trussworks/repos?sort=full_name&per_page=10&page=' + str(page), | |
headers = {"Accept": "application/vnd.github.v3+json", "Authorization": "token " + os.environ["GITHUB_TOKEN"] }) | |
repos = repos + r.json() | |
return repos | |
def getReposForManualIntervention(): | |
repos = getRepos() | |
for repo in repos: | |
print('{') | |
print("\"name\": \"{}\",".format(repo["name"])) | |
print("\"ssh_url\": \"{}\",".format(repo["ssh_url"])) | |
print("\"archived\": \"{}\"".format(repo["archived"])) | |
print('},') | |
def updateConfig(configFile): | |
with open(configFile) as file: | |
config = yaml.load(file, Loader=yaml.FullLoader) | |
jobs = config["jobs"] | |
for job in jobs: | |
for idx, image in enumerate(jobs[job]["docker"]): | |
jobs[job]["docker"][idx]["auth"] = { | |
"username": '$DOCKER_USERNAME', | |
"password": '$DOCKER_PASSWORD' | |
} | |
workflows = config["workflows"] | |
for workflow in workflows: | |
if isinstance(workflows[workflow], dict): | |
#print(workflows[workflow]) | |
for idx, job in enumerate(workflows[workflow]["jobs"]): | |
if type(job) == str: | |
workflows[workflow]["jobs"][idx] = {job: {"context": ["org-global"]}} | |
#print(workflows[workflow]) | |
os.rename(configFile, configFile + '.back') | |
with open(configFile, 'w') as newfile: | |
yaml.dump(config, newfile) | |
#os.remove(configFile + '.back') | |
pr_list = [] | |
with tempfile.TemporaryDirectory() as directory: | |
repos = getRepos() | |
#repos = [] | |
working_branch = "auto-update-ci" | |
for repo in repos: | |
print("Moving to temp directory {} \n".format(directory)) | |
os.chdir(directory) | |
repo_name = repo["name"] | |
clone_url = repo["ssh_url"] | |
if repo["archived"] == "False": | |
print("Attempting to clone {} \n".format(repo_name)) | |
clone_response = subprocess.run("git clone {}".format(clone_url), shell=True, check=True) | |
configFile = directory + "/" + repo_name + '/.circleci/config.yml' | |
print(configFile) | |
print(os.path.isfile(configFile)) | |
if os.path.isfile(configFile): | |
print("Repo contains config to be updated \n") | |
os.chdir(directory + "/" + repo_name) | |
print("Checking out {} \n".format(repo_name)) | |
checkout_response = subprocess.run("git checkout -b {}".format(working_branch), shell=True, check=True) | |
# make changes | |
print("Attempting to update configuration file\n") | |
updateConfig(configFile) | |
# commit | |
print("\n") | |
commit_response = subprocess.run('git commit --no-verify -a -m "Updating CircleCi config"', shell=True, check=True) | |
# push change | |
print("Pushing changes\n") | |
push_response = subprocess.run('git push -u origin {}'.format(working_branch), shell=True, check=True) | |
# create pr | |
repo = g.get_repo("trussworks/{}".format(repo_name)) | |
pr = repo.create_pull(title="Automated CircleCI config update", body="Updating CircleCi configuration to use auth when pulling worker images.", head=working_branch, base="master") | |
pr_list.append("https://github.com/trussworks/{}/pull/{}".format(repo_name,pr.number)) | |
else: | |
print("repo {} is archived, skipping".format(repo_name)) | |
for pr in pr_list: | |
print("{}\n".format(pr)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment