@@ -131,14 +131,116 @@ def get_repository(self, user, repo):
131
131
return self .gl .projects .get ('{}/{}' .format (user , repo ))
132
132
except GitlabGetError as err :
133
133
if err .response_code == 404 :
134
- raise ResourceNotFoundError ("Cannot delete : repository {}/{} does not exists." .format (user , repo )) from err
134
+ raise ResourceNotFoundError ("Cannot get : repository {}/{} does not exists." .format (user , repo )) from err
135
135
136
136
@classmethod
137
137
def get_auth_token (cls , login , password , prompt = None ):
138
138
gl = gitlab .Gitlab (url = 'https://{}' .format (cls .fqdn ), email = login , password = password )
139
139
gl .auth ()
140
140
return gl .user .private_token
141
141
142
+ def _deconstruct_snippet_uri (self , uri ):
143
+ path = uri .split ('https://{}/' .format (self .fqdn ))[- 1 ]
144
+ path = path .split ('/' )
145
+ if 3 == len (path ):
146
+ user , project_name , _ , snippet_id = path
147
+ elif 4 == len (path ):
148
+ user , _ , snippet_id = path
149
+ project_name = None
150
+ else :
151
+ raise ResourceNotFoundError ('URL is not of a snippet' )
152
+ return (user , project_name , snippet_id )
153
+
154
+ def gist_list (self , project = None ):
155
+ if not project :
156
+ raise NotImplementedError ('Feature API implementation scheduled for gitlab 8.15' )
157
+ for project in self .gl .snippets .list ():
158
+ yield ('https://{}/{}/snippets/{}' .format (
159
+ self .fqdn ,
160
+ snippet .author .username ,
161
+ snippet .id
162
+ ), snippet .title )
163
+ else :
164
+ project = self .gl .projects .list (search = project )
165
+ if len (project ):
166
+ project = project [0 ]
167
+ for snippet in project .snippets .list (per_page = 100 ):
168
+ yield ('https://{}/{}/{}/snippets/{}' .format (
169
+ self .fqdn ,
170
+ snippet .author .username ,
171
+ project .name ,
172
+ snippet .id
173
+ ), 0 , snippet .title )
174
+
175
+ def gist_fetch (self , snippet , fname = None ):
176
+ if fname :
177
+ raise ArgumentError ('Snippets contain only single file in gitlab.' )
178
+ try :
179
+ _ , project_name , snippet_id = self ._deconstruct_snippet_uri (snippet )
180
+ if project_name :
181
+ project = self .gl .projects .list (search = project_name )[0 ]
182
+ snippet = self .gl .project_snippets .get (project_id = project .id , id = snippet_id )
183
+ else :
184
+ raise NotImplementedError ('Feature API implementation scheduled for gitlab 8.15' )
185
+ snippet = self .gl .snippets .get (id = snippet_id )
186
+ except Exception as err :
187
+ raise ResourceNotFoundError ('Could not find snippet' ) from err
188
+
189
+ return snippet .Content ().decode ('utf-8' )
190
+
191
+ def gist_clone (self , gist ):
192
+ raise ArgumentError ('Snippets cannot be cloned in gitlab.' )
193
+
194
+ def gist_create (self , gist_pathes , description , secret = False ):
195
+ def load_file (fname , path = '.' ):
196
+ with open (os .path .join (path , fname ), 'r' ) as f :
197
+ return f .read ()
198
+
199
+ if len (gist_pathes ) > 2 :
200
+ raise ArgumentError ('Snippets contain only single file in gitlab.' )
201
+
202
+ data = {
203
+ 'title' : description ,
204
+ 'visibility_level' : 0 if secret else 20
205
+ }
206
+
207
+ if len (gist_pathes ) == 2 :
208
+ gist_proj = gist_pathes [0 ]
209
+ gist_path = gist_pathes [1 ]
210
+ data .update ({
211
+ 'id' : gist_proj ,
212
+ 'code' : load_file (gist_path ),
213
+ 'file_name' : os .path .basename (gist_path ),
214
+ }
215
+ )
216
+ gist = self .gl .project_snippets .create (data )
217
+
218
+ elif len (gist_pathes ) == 1 :
219
+ raise NotImplementedError ('Feature API implementation scheduled for gitlab 8.15' )
220
+ gist_path = gist_pathes [0 ]
221
+ data .update ({
222
+ 'content' : load_file (gist_path ),
223
+ 'file_name' : os .path .basename (gist_path ),
224
+ }
225
+ )
226
+ gist = self .gl .snippets .create (data )
227
+
228
+ return gist .html_url
229
+
230
+ def gist_delete (self , gist_id ):
231
+ try :
232
+ _ , project_name , snippet_id = self ._deconstruct_snippet_uri (snippet )
233
+ if project_name :
234
+ project = self .gl .projects .list (search = project_name )[0 ]
235
+ snippet = self .gl .project_snippets .get (project_id = project .id , id = snippet_id )
236
+ else :
237
+ raise NotImplementedError ('Pending feature' )
238
+ snippet = self .gl .snippets .get (id = snippet_id )
239
+ except Exception as err :
240
+ raise ResourceNotFoundError ('Could not find snippet' ) from err
241
+
242
+ return snippet .delete ()
243
+
142
244
@property
143
245
def user (self ):
144
246
return self .gl .user .username
0 commit comments