7
7
from optparse import OptionParser
8
8
import os
9
9
try :
10
- import gitlab3 as gitlab
10
+ import gitlab
11
11
except ImportError :
12
- raise ImportError ("python-gitlab3 module is not installed. You probably didn't read the install instructions closely enough. See docs/prerequisites.md." )
12
+ raise ImportError ("python-gitlab module is not installed. You probably didn't read the install instructions closely enough. See docs/prerequisites.md." )
13
13
14
14
15
15
19
19
gitlab_namespace = os .environ ['gitlab_namespace' ]
20
20
gitlab_user = os .environ ['gitlab_user' ]
21
21
ssl_verify = os .environ ['ssl_verify' ]
22
+ gitlab_api_version = os .environ ['gitlab_api_version' ]
22
23
except KeyError :
23
24
print >> stderr , "Environment config missing. Do not run this script standalone."
24
25
exit (1 )
44
45
project_name = args [0 ]
45
46
46
47
if not eval (ssl_verify .capitalize ()):
47
- git = gitlab .GitLab (gitlab_url = gitlab_url , token = token_secret ,ssl_verify = False )
48
+ git = gitlab .Gitlab (gitlab_url , token_secret ,ssl_verify = False , api_version = gitlab_api_version )
48
49
else :
49
- git = gitlab .GitLab (gitlab_url = gitlab_url ,token = token_secret ,ssl_verify = True )
50
+ git = gitlab .Gitlab (gitlab_url ,token_secret ,ssl_verify = True ,api_version = gitlab_api_version )
51
+
52
+ def find_group (** kwargs ):
53
+ groups = git .groups .list ()
54
+ return _find_matches (groups , kwargs , False )
55
+
56
+ def find_project (** kwargs ):
57
+ projects = git .projects .list (as_list = True )
58
+ return _find_matches (projects , kwargs , False )
59
+
60
+ def _find_matches (objects , kwargs , find_all ):
61
+ """Helper function for _add_find_fn. Find objects whose properties
62
+ match all key, value pairs in kwargs.
63
+ Source: https://github.com/doctormo/python-gitlab3/blob/master/gitlab3/__init__.py
64
+ """
65
+ ret = []
66
+ for obj in objects :
67
+ match = True
68
+ # Match all supplied parameters
69
+ for param , val in kwargs .items ():
70
+ if not getattr (obj , param ) == val :
71
+ match = False
72
+ break
73
+ if match :
74
+ if find_all :
75
+ ret .append (obj )
76
+ else :
77
+ return obj
78
+ if not find_all :
79
+ return None
80
+ return ret
50
81
51
82
# transfer the project from the source namespace to the specified group namespace
52
83
def transfer_project (src_project , group ):
53
84
value = group .transfer_project (src_project .id )
54
- dest_project = git . find_project (name = src_project .name )
85
+ dest_project = find_project (name = src_project .name )
55
86
return dest_project
56
87
57
88
def createproject (pname ):
@@ -69,14 +100,16 @@ def createproject(pname):
69
100
'wiki_enabled' : options .wiki ,
70
101
'snippets_enabled' : options .snippets ,
71
102
'public' : options .public ,
72
- 'namespace_id' : git . find_group (name = gitlab_namespace ).id ,
103
+ 'namespace_id' : find_group (name = gitlab_namespace ).id ,
73
104
}
74
105
#make all project options lowercase boolean strings i.e. true instead of True
75
106
for x in project_options .keys ():
76
107
project_options [x ] = str (project_options [x ]).lower ()
77
108
print >> stderr , "Creating new project %s" % pname
78
- git .add_project (pname ,description = description ,** project_options )
79
- found_project = git .find_project (name = pname )
109
+ project_options ['name' ] = pname
110
+ project_options ['description' ] = description
111
+ git .projects .create (project_options )
112
+ found_project = find_project (name = pname )
80
113
if needs_transfer (gitlab_user , gitlab_namespace , found_project ):
81
114
found_project = transfer_project (found_project , found_group )
82
115
return found_project
@@ -88,13 +121,19 @@ def needs_transfer(user, groupname, project):
88
121
namespace = groupname
89
122
else :
90
123
namespace = user
91
- return project .namespace ['name' ] != namespace
124
+ if type (project .namespace ) == gitlab .v3 .objects .Group :
125
+ return project .namespace .name != namespace
126
+ else :
127
+ return project .namespace ['name' ] != namespace
128
+
92
129
93
130
if options .create :
94
- found_group = git . find_group (name = gitlab_namespace )
131
+ found_group = find_group (name = gitlab_namespace )
95
132
found_project = None
96
- # search the group namespace first
97
- found_project = git .find_project (name = project_name )
133
+
134
+ found_project = find_project (name = project_name )
135
+ #exit()
136
+
98
137
if found_project :
99
138
if needs_transfer (gitlab_user , gitlab_namespace , found_project ):
100
139
found_project = transfer_project (found_project , found_group )
@@ -112,7 +151,7 @@ def needs_transfer(user, groupname, project):
112
151
print found_project .ssh_url_to_repo
113
152
elif options .delete :
114
153
try :
115
- deleted_project = git . find_project (name = project_name ).delete ()
154
+ deleted_project = find_project (name = project_name ).delete ()
116
155
except Exception as e :
117
156
print >> stderr , e
118
157
exit (1 )
0 commit comments