Skip to content
Snippets Groups Projects
Commit 8a5426fe authored by Sam Gleske's avatar Sam Gleske
Browse files

renamed create -> manage_gitlab_project.py

python-gitlab API has added the ability for public project creation.
This is because of changes in the GitLab 6 API.

I have also added optparse for option parsing so that I can pass
more advanced options and arguments.

I have renamed creatE_gitlab_project.py to manage_gitlab_project.py.
parent 2313fe6f
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -2,23 +2,40 @@
#Tue Sep 10 23:01:08 EDT 2013
from sys import argv,exit,stderr
from optparse import OptionParser
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."
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)
except IndexError:
print "No project name specified. Do not run this script standalone."
elif len(args) > 1:
print >> stderr, "Too many arguments. Do not run this script standalone."
exit(1)
git=gitlab.Gitlab(gitlab_url,token_secret)
project_name=args[0]
git=gitlab.Gitlab(gitlab_url,token_secret,version=6)
def findgroup(gname):
#Locate the group
......@@ -39,8 +56,11 @@ def findproject(gname,pname):
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)
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):
......@@ -48,14 +68,14 @@ def createproject(pname):
else:
return False
found_group=findgroup(gitlab_namespace)
found_project=findproject(gitlab_namespace,project_name)
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 %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)
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']
print found_project['ssh_url_to_repo']
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment