#!/usr/bin/env python
#Created by Sam Gleske
#MIT License
#Created 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)
parser.add_option("--desc",dest="desc",metavar="DESC",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]

#current python-gitlab behavior
#git=gitlab.Gitlab(gitlab_url,token_secret,version=6)
#command for my forked version https://github.com/sag47/python-gitlab.git
git=gitlab.Gitlab(gitlab_url,token_secret)

def findgroup(gname):
  #Locate the group
  page=1
  while len(git.getGroups(page=page)) > 0:
    for group in git.getGroups(page=page):
      if group['name'] == gname:
        return group
    page += 1
  else:
    print >> stderr, "Project namespace (user or group) not found or user does not have permission of existing group."
    print >> stderr, "gitlab-mirrors will not automatically create the project namespace."
    exit(1)

def findproject(gname,pname):
  page=1
  while len(git.getProjects(page=page)) > 0:
    for project in git.getProjects(page=page):
      if project['namespace']['name'] == gname and project['name'] == pname:
        return project
    page += 1
  else:
    return False

def createproject(pname):
  if len(options.desc) == 0:
    if options.public:
      description="Public mirror of %s." % project_name
    else:
      description="Git mirror of %s." % project_name
  else:
    description=options.desc
  new_project=git.createProject(pname,description=description,issues_enabled=str(int(options.issues)),wall_enabled=str(int(options.wall)),merge_requests_enabled=str(int(options.merge)),wiki_enabled=str(int(options.wiki)),snippets_enabled=str(int(options.snippets)),public=str(int(options.public)))
  if gitlab_user != gitlab_namespace:
    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']
else:
  print >> stderr, "No --create or --delete option added."
  exit(1)

@@ -2688,36 +2688,32 @@
 ,issues_enabled=
-str(
 int(options.issu
@@ -2715,17 +2715,16 @@
 .issues)
-)
 ,wall_en
@@ -2721,36 +2721,32 @@
 s),wall_enabled=
-str(
 int(options.wall
@@ -2746,17 +2746,16 @@
 ns.wall)
-)
 ,merge_r
@@ -2762,36 +2762,32 @@
 equests_enabled=
-str(
 int(options.merg
@@ -2788,17 +2788,16 @@
 s.merge)
-)
 ,wiki_en
@@ -2794,36 +2794,32 @@
 e),wiki_enabled=
-str(
 int(options.wiki
@@ -2819,17 +2819,16 @@
 ns.wiki)
-)
 ,snippet
@@ -2837,20 +2837,16 @@
 enabled=
-str(
 int(opti
@@ -2862,17 +2862,16 @@
 ets)
-)
 ,public=
 str(
@@ -2870,12 +2870,8 @@
 lic=
-str(
 int(
@@ -2886,17 +2886,16 @@
 public))
-)
 %0A  if gi

Update for using recent version of pyapi-gitlab