diff --git a/add_mirror.sh b/add_mirror.sh index fc522a909986a298202cd27de4cc1b7e633ee42c..8039f6d4678fbf84e94aa476ad061a22bd5b58d9 100755 --- a/add_mirror.sh +++ b/add_mirror.sh @@ -27,6 +27,9 @@ usage() cat <<EOF ${PROGNAME} ${PROGVERSION} - MIT License by Sam Gleske +USAGE: + ${PROGNAME} --git|--svn --project NAME --mirror URL + DESCRIPTION: This will add a git or SVN repository to be mirrored by GitLab. It first checks to see if the project exists in gitlab. If it does @@ -86,20 +89,88 @@ while true; do esac done +# +# Program functions +# + function preflight() { + STATUS=0 if ${git} && ${svn};then - + red_echo -n "Must not set " 1>&2 + yellow_echo -n "--svn" 1>&2 + red_echo -n " and " 1>&2 + yellow_echo -n "--git" 1>&2 + red_echo " options. Choose one or other." 1>&2 + STATUS=1 fi + if ! ${git} && ! ${svn};then + red_echo -n "Must specify the " 1>&2 + yellow_echo -n "--git" 1>&2 + red_echo -n " or " 1>&2 + yellow_echo -n "--svn" 1>&2 + red_echo "options" 1>&2 + STATUS=1 + fi + if [ -z "${project_name}" ];then + red_echo -n "Missing " 1>&2 + yellow_echo -n "--project" 1>&2 + red_echo " option." 1>&2 + STATUS=1 + fi + if [ -z "${mirror}" ];then + red_echo -n "Missing " 1>&2 + yellow_echo -n "--mirror" 1>&2 + red_echo " option." 1>&2 + STATUS=1 + fi + return ${STATUS} } -echo "svn=${svn}" -echo "git=${git}" -echo "project_name=${project_name}" -echo "mirror=${mirror}" +# +# Main execution +# +#Run a preflight check on options for compatibility. +if ! preflight;then + echo "Command aborted due to previous errors." 1>&2 + exit 1 +fi +#Check for namespace directory existence +if [ ! -e "${repo_dir}/${gitlab_namespace}" ];then + mkdir -p "${repo_dir}/${gitlab_namespace}" +elif [ ! -d "${repo_dir}/${gitlab_namespace}" ];then + red_echo "Error: \"${repo_dir}/${gitlab_namespace}\" exists but is not a directory." 1>&2 + exit 1 +fi +#Set up project creation options based on config.sh to be passed to create manage_gitlab_project.py +CREATE_OPTS="" +if ${issues_enabled};then + CREATE_OPTS="--issues ${CREATE_OPTS}" +fi +if ${wall_enabled};then + CREATE_OPTS="--wall ${CREATE_OPTS}" +fi +if ${merge_requests_enabled};then + CREATE_OPTS="--merge ${CREATE_OPTS}" +fi +if ${wiki_enabled};then + CREATE_OPTS="--wiki ${CREATE_OPTS}" +fi +if ${snippets_enabled};then + CREATE_OPTS="--snippets ${CREATE_OPTS}" +fi +if ${public};then + CREATE_OPTS="--public ${CREATE_OPTS}" +fi +#export env vars for python script +export gitlab_user_token_secret gitlab_url gitlab_namespace gitlab_user + + +if ${git};then +fi @@ -110,26 +181,20 @@ echo "mirror=${mirror}" -exit -if [ "${#}" -lt "2" ];then - echo "Not enough arguments." 1>&2 - echo "e.g. ./add_mirror.sh project_name http://example.com/project.git" 1>&2 - exit 1 -fi -#export env vars for python script -export gitlab_user_token_secret gitlab_url gitlab_namespace gitlab_user + +exit #Get the remote gitlab url for the specified project. #If the project doesn't already exist in gitlab then create it. echo "Resolving gitlab remote." -if python lib/create_gitlab_project.py $1 1> /dev/null;then - gitlab_remote=$(python lib/create_gitlab_project.py $1) +if python lib/manage_gitlab_project.py $1 1> /dev/null;then + gitlab_remote=$(python lib/manage_gitlab_project.py $1) echo "gitlab remote ${gitlab_remote}" else - echo "There was an unknown issue with create_gitlab_project.py" 1>&2 + echo "There was an unknown issue with manage_gitlab_project.py" 1>&2 exit 1 fi @@ -144,7 +209,7 @@ cd "$1" echo "Adding gitlab remote to project." git remote add gitlab ${gitlab_remote} git config --add remote.gitlab.push '+refs/heads/*:refs/heads/*' -git config --add remote.gitlab.push '+refs/heads/*:refs/heads/*' +git config --add remote.gitlab.push '+refs/tags/*:refs/tags/*' #Check the initial repository into gitlab echo "Checking the mirror into gitlab." git fetch diff --git a/lib/create_gitlab_project.py b/lib/create_gitlab_project.py deleted file mode 100755 index 2985b44a962a8c46cccc737453a3425790f4ae93..0000000000000000000000000000000000000000 --- a/lib/create_gitlab_project.py +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env python -#Tue Sep 10 23:01:08 EDT 2013 - -from sys import argv,exit,stderr -import os -import gitlab - -try: - project_name=argv[1] - token_secret=os.environ['gitlab_user_token_secret'] - gitlab_url=os.environ['gitlab_url'] - gitlab_namespace=os.environ['gitlab_namespace'] - gitlab_user=os.environ['gitlab_user'] -except KeyError: - print "Environment config missing. Do not run this script standalone." - exit(1) -except IndexError: - print "No project name specified. Do not run this script standalone." - exit(1) - -git=gitlab.Gitlab(gitlab_url,token_secret) - -def findgroup(gname): - #Locate the group - found_group=False - for group in git.getGroups(): - if group['name'] == gname: - return group - else: - if not found_group: - print >> stderr, "Project namespace (user or group) not found or user does not have permission of existing group." - exit(1) - -def findproject(gname,pname): - for project in git.getProjects(): - if project['namespace']['name'] == gname and project['name'] == pname: - return project - else: - return False - -def createproject(pname): - description="Public mirror of %s." % project_name - new_project=git.createProject(pname,description=description,issues_enabled=False,wall_enabled=False,merge_requests_enabled=False,wiki_enabled=False,snippets_enabled=False) - new_project=findproject(gitlab_user,pname) - new_project=git.moveProject(found_group['id'],new_project['id']) - if findproject(gitlab_namespace,pname): - return findproject(gitlab_namespace,pname) - else: - return False - -found_group=findgroup(gitlab_namespace) -found_project=findproject(gitlab_namespace,project_name) - -if not found_project: - found_project=createproject(project_name) - if not found_project: - #print >> stderr, "There was a problem creating %s/%s. Did you give %s user Admin rights in gitlab?" % {gitlab_namespace,project_name,gitlab_user} - print >> stderr, "There was a problem creating {group}/{project}. Did you give {user} user Admin rights in gitlab?".format(group=gitlab_namespace,project=project_name,user=gitlab_user) - exit(1) - -print found_project['ssh_url_to_repo'] diff --git a/lib/manage_gitlab_project.py b/lib/manage_gitlab_project.py new file mode 100755 index 0000000000000000000000000000000000000000..b0804aa489f39294d22c18888da90a5c727dfccc --- /dev/null +++ b/lib/manage_gitlab_project.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +#Tue Sep 10 23:01:08 EDT 2013 + +from sys import argv,exit,stderr +from optparse import OptionParser +import os +import gitlab + + + +try: + token_secret=os.environ['gitlab_user_token_secret'] + gitlab_url=os.environ['gitlab_url'] + gitlab_namespace=os.environ['gitlab_namespace'] + gitlab_user=os.environ['gitlab_user'] +except KeyError: + print >> stderr, "Environment config missing. Do not run this script standalone." + exit(1) +parser = OptionParser() +parser.add_option("--issues",dest="issues",action="store_true",default=False) +parser.add_option("--wall",dest="wall",action="store_true",default=False) +parser.add_option("--merge",dest="merge",action="store_true",default=False) +parser.add_option("--wiki",dest="wiki",action="store_true",default=False) +parser.add_option("--snippets",dest="snippets",action="store_true",default=False) +parser.add_option("--public",dest="public",action="store_true",default=False) +parser.add_option("--create",dest="create",action="store_true",default=False) +parser.add_option("--delete",dest="delete",action="store_true",default=False) +(options,args) = parser.parse_args() +if len(args) == 0: + print >> stderr, "No project name specified. Do not run this script standalone." + exit(1) +elif len(args) > 1: + print >> stderr, "Too many arguments. Do not run this script standalone." + exit(1) + +project_name=args[0] + +git=gitlab.Gitlab(gitlab_url,token_secret,version=6) + +def findgroup(gname): + #Locate the group + found_group=False + for group in git.getGroups(): + if group['name'] == gname: + return group + else: + if not found_group: + print >> stderr, "Project namespace (user or group) not found or user does not have permission of existing group." + exit(1) + +def findproject(gname,pname): + for project in git.getProjects(): + if project['namespace']['name'] == gname and project['name'] == pname: + return project + else: + return False + +def createproject(pname): + if options.public: + description="Public mirror of %s." % project_name + else: + description="Git mirror of %s." % project_name + new_project=git.createProject(pname,description=description,issues_enabled=options.issues,wall_enabled=options.wall,merge_requests_enabled=options.merge,wiki_enabled=options.wiki,snippets_enabled=options.snippets,public=options.public) + new_project=findproject(gitlab_user,pname) + new_project=git.moveProject(found_group['id'],new_project['id']) + if findproject(gitlab_namespace,pname): + return findproject(gitlab_namespace,pname) + else: + return False + +if options.create: + found_group=findgroup(gitlab_namespace) + found_project=findproject(gitlab_namespace,project_name) + + if not found_project: + found_project=createproject(project_name) + if not found_project: + print >> stderr, "There was a problem creating {group}/{project}. Did you give {user} user Admin rights in gitlab?".format(group=gitlab_namespace,project=project_name,user=gitlab_user) + exit(1) + + print found_project['ssh_url_to_repo']