Skip to content

Instantly share code, notes, and snippets.

@lexorus
Created May 25, 2020 14:34
Show Gist options
  • Save lexorus/b717cc716da8718c546b6bad96668824 to your computer and use it in GitHub Desktop.
Save lexorus/b717cc716da8718c546b6bad96668824 to your computer and use it in GitHub Desktop.
Automate similar pull request creation for multiple repositories with a small python 3.7 script.
# Automate similar pull request creation for multiple repositories with a small python 3.7 script.
# All you have to do:
# 1. Provide your Github credentials in "Globals" section
# 2. Declare repositories and their base branches for which you want to create PR in "repositories_to_base" dictionary
# 3. Complete PR info in "PR constants" section and "create_pr" function
# 4. Implement "execute_changes" function
import os
from os import path
from github import Github
def print_red(text): print("\033[91m {}\033[00m".format(text))
def print_yellow(text): print("\033[93m {}\033[00m".format(text))
def print_green(text): print("\033[92m {}\033[00m".format(text))
# Globals
github_token = "your_github_token"
organization_name = "your_organization"
github = Github(github_token)
organization = github.get_organization(organization_name)
# Repositories to update
repositories_to_base = {
'first_repo': 'master',
'second_repo': 'develop',
'third_repo': 'master'
}
# PR constants
ticket = "ticket_id"
branch = "%s-update-cocoapods"%(ticket)
# Changes to be executed
def execute_changes(repository_name):
print_yellow("Installing Pods")
if not path.exists('Podfile'):
print_red("\nRepository %s does not contain Podfile. Skipping...\n"%(repository_name))
return False
os.system('pod install')
return True
def get_commit_title():
cocoapods_version = os.popen('pod --version').read()
return "Update cocoapods to %s"%(cocoapods_version)
# Utility functions
def get_repository(repository_name):
return organization.get_repo(repository_name)
def get_base_branch(repository_name):
return repositories_to_base.get(repository_name)
def clone_repository(repository_name):
print_yellow("Clone repository %s"%(repository_name))
repository = get_repository(repository_name)
ssh_url = repository.ssh_url
os.system("git clone %s"%(ssh_url))
def prepare_branch(repository_name, base_branch, branch):
print_yellow("Preparing branch %s"%(branch))
os.system("git add --all; git reset --hard head; git checkout %s; git pull origin %s; git checkout -b %s"%(base_branch, base_branch, branch))
def is_anything_to_commit():
if os.popen('git diff --exit-code').read():
return True
print_red("\nNo changes appeared after execution. Skipping...\n")
return False
def commit_and_push(branch):
print_yellow("Commit and push")
commit_title = get_commit_title()
os.system('git add --all')
os.system("git commit -m\"%s\";"%(commit_title))
os.system("git push origin %s"%(branch))
def create_pr(branch, base_branch):
print_yellow("Creating PR")
pr_title = get_commit_title()
how_to_test = "Run pod install and make sure no changes appeared"
pr_body = """
Ticket: %s
### What has been done?
1. %s
### How to test?
1. %s
"""%(ticket, pr_title, how_to_test)
get_repository(repository_name).create_pull(pr_title, pr_body, base_branch, branch)
# Logs
print_green('\n\n\nPR successfuly created 🎉')
print("\nPR title: %s "%(pr_title))
print("\nPR body: %s "%(pr_body))
changes = os.popen('git show head').read()
print("\nPR changes:\n%s\n"%(changes))
def cleanup(repository_name):
print_yellow("Cleanup")
os.chdir("..")
os.system("rm -rf %s"%(repository_name))
# Main :)
for repository_name in repositories_to_base.keys():
print_yellow("\n\n\n************************* Repository: %s *************************\n\n"%(repository_name))
base_branch = repositories_to_base.get(repository_name)
clone_repository(repository_name)
os.chdir(repository_name)
prepare_branch(repository_name, base_branch, branch)
are_changes_executed = execute_changes(repository_name)
if not are_changes_executed:
cleanup(repository_name)
continue
if not is_anything_to_commit():
cleanup(repository_name)
continue
commit_and_push(branch)
create_pr(branch, base_branch)
cleanup(repository_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment