Skip to content

Instantly share code, notes, and snippets.

@tkw1536
Created April 5, 2018 09:44
Show Gist options
  • Save tkw1536/ccf431e918a4f87b16de554ec1a58044 to your computer and use it in GitHub Desktop.
Save tkw1536/ccf431e918a4f87b16de554ec1a58044 to your computer and use it in GitHub Desktop.
Generate a bash script that clones all GitLab repositories and runs a commit on all branches.
#!/usr/bin/env python
"""
Generate a bash script that clones all GitLab repositories and runs a commit on all branches.
Usage:
python3 glutils.py $glURL $token $command $commitMessage > script.sh
bash script.sh
"""
import sys
import os.path
try:
import gitlab
except ImportError:
print("Unable to find python-gitlab module. ")
print("Please run `pip install python-gitlab` to install it. ")
sys.exit(1)
def get_gl_branches(gl):
""" Yield pairs (repo, ssh_url, branch) of all repositories """
for project in gl.projects.list(all=True):
name = project.path_with_namespace
url = project.ssh_url_to_repo
try:
for branch in project.branches.list(all=True):
yield name, url, branch.name
except gitlab.exceptions.GitlabListError:
continue
def main():
# read url
if len(sys.argv) > 1:
url = sys.argv[1]
else:
url = input("## Enter the URL to your GitLab Instance [e.g. https://gl.kwarc.info]>")
# get api token
if len(sys.argv) > 2:
token = sys.argv[2]
else:
token = input("## Enter your OAuth token [see {}/profile/personal_access_tokens]>".format(url))
# get command
if len(sys.argv) > 3:
command = sys.argv[3]
else:
command = input("## Enter command to run>")
# commit message
if len(sys.argv) > 4:
msg = sys.argv[4]
else:
msg = input("## Enter commit message>")
# connect to GitLab
gl = gitlab.Gitlab(url, private_token=token)
# iterate through all the branches
for (name, url, branch) in get_gl_branches(gl):
dirname = os.path.join(name, branch)
print("# repo={} branch={}".format(name, branch))
print("mkdir -p {}".format(dirname))
print("pushd {}".format(dirname))
print("git clone --depth 1 --branch {} {} .".format(url, branch))
print(command)
print("git add -A .")
print("git commit -m \"{}\"".format(msg))
print("git push")
print("popd")
print("")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment