Skip to content

Instantly share code, notes, and snippets.

@vchrombie
Last active April 1, 2020 14:52
Show Gist options
  • Save vchrombie/5593ea202e7f85d0478d2c1532ca2a9b to your computer and use it in GitHub Desktop.
Save vchrombie/5593ea202e7f85d0478d2c1532ca2a9b to your computer and use it in GitHub Desktop.
automate a few tasks for the GrimoireLab project 🚀 🦉

automate a few tasks for the GrimoireLab project 🚀 🦉

To use these scripts, you need to install the PyGitHub and GitPython modules in the virtualenvironment (or the machine).

python3 -m pip install PyGitHub GitPython

How to use

  1. First of all, you need a GitHub Personal Access Token. You can get one form here, New personal access token | GitHub.
  2. Download the script which is needed. You can click on the Raw button and the save the script to your machine.
  3. Replace the xxxx with a valid working token.
    token = "xxxx"
  4. Execute the script. ⛷️
    python3 script_name.py
    

For setting up the dev-environment in PyCharm, we need to fork, clone and add the origin/upstream remotes to each project. This script automates the whole process.

NOTE: Execute this script where you need the sources folder.

Result: You will have all the 15 repositories forked (if not done earlier), cloned into sources folder and all the upstream remotes setup correctly.

From time to time, we need to update the forks. This script automates the process of updating the master branches of all the 15 repositories present in the sources folder.

NOTE: Execute this script outside the sources folder.

Result: You will have all the 15 repositories, present in the sources folder, updated (master branch) with the latest changes.


If you like the work, please drop a ⭐. Comments and Suggestions are always welcomed. 🤗

# !/usr/bin/python3
# coding=utf-8
from github import Github, BadCredentialsException
import git.repo.base as grb
import os
# your access token goes here
token = "xxxx"
g = Github(token)
try:
user = g.get_user()
print("The PAT is working. You are " + user.login + ".\n")
except BadCredentialsException:
print("Please check your PAT. Exiting.\n")
exit()
current_path = os.getcwd()
required_path = os.path.join(current_path, "sources")
print("Creating the folder `sources`.")
try:
os.mkdir(required_path)
except FileExistsError:
print("The folder with name `sources` already exists. Moving into the existing folder.")
pass
os.chdir(required_path)
repos = ["chaoss/grimoirelab-sirmordred", "chaoss/grimoirelab-elk", "chaoss/grimoirelab-kingarthur",
"chaoss/grimoirelab-graal", "chaoss/grimoirelab-perceval", "chaoss/grimoirelab-perceval-mozilla",
"chaoss/grimoirelab-perceval-opnfv", "chaoss/grimoirelab-perceval-puppet",
"Bitergia/grimoirelab-perceval-finos", "chaoss/grimoirelab-sortinghat", "chaoss/grimoirelab-sigils",
"chaoss/grimoirelab-kidash", "chaoss/grimoirelab-toolkit", "chaoss/grimoirelab-cereslib",
"chaoss/grimoirelab-manuscripts"]
for item in repos:
repo_url = item.split("/")
print("\n" + repo_url[0] + "/" + repo_url[1])
org = g.get_organization(repo_url[0])
repo = org.get_repo(repo_url[1])
print("forking the repository " + repo.name + " to " + user.login)
my_fork = user.create_fork(repo) # ignores if already forked
try:
print("cloning the repository " + user.login + "/" + repo.name)
local_repo = grb.Repo.clone_from("https://github.com/" + user.login + "/" + repo.name + ".git",
os.getcwd() + "/" + repo.name, branch="master")
print("adding the `upstream` remote.")
grb.Repo.create_remote(local_repo, "upstream",
"https://github.com/" + repo_url[0] + "/" + repo.name + ".git")
except grb.GitCommandError:
print(str(repo.name) + " already exists and is not empty. Not cloning.")
pass
print()
# print(os.getcwd())
# os.chdir(current_path)
# print(os.getcwd())
print("Done! 🎉")
# !/usr/bin/python3
# coding=utf-8
from github import Github, BadCredentialsException
from subprocess import call
import os
# your access token goes here
token = "xxxx"
g = Github(token)
try:
user = g.get_user()
print("The PAT is working. You are " + user.login + ".\n")
except BadCredentialsException:
print("Please check your PAT. Exiting.\n")
exit()
CHECK_REMOTES_CMD = ['git', 'remote', '-v']
FETCH_UPSTREAM_CMD = ['git', 'fetch', 'upstream']
CHECKOUT_MASTER_CMD = ['git', 'checkout', 'master']
REBASE_UPSTREAM_CMD = ['git', 'rebase', 'upstream/master']
PUSH_TO_UPSTREAM_CMD = ['git', 'push', '', 'master']
current_path = os.getcwd()
print("Current path: " + current_path)
required_path = os.path.join(current_path, "sources")
print("Moving into the `sources` folder.\n")
os.chdir(required_path)
repos = [f.path for f in os.scandir(os.getcwd()) if f.is_dir()]
for repo in repos:
os.chdir(repo)
try:
print("\n## " + repo.split("/")[-1])
origin = "https://" + user.login + ":" + token + "@github.com/" + user.login + "/" + repo.split("/")[-1]
PUSH_TO_UPSTREAM_CMD[2] = origin
print("\n>>> Checking remotes")
call(CHECK_REMOTES_CMD)
print("\n>>> Fetching upstream")
call(FETCH_UPSTREAM_CMD)
print("\n>>> Checking out master")
call(CHECKOUT_MASTER_CMD)
print("\n>>> Rebasing master")
call(REBASE_UPSTREAM_CMD)
print("\n>>> Pushing to origin master")
call(PUSH_TO_UPSTREAM_CMD)
except Exception as e:
print(e)
exit()
print()
# print(os.getcwd())
# os.chdir(current_path)
# print(os.getcwd())
print("Done! 🎉")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment