3
3
from datetime import datetime , timedelta
4
4
from typing import Any , Dict , List , Optional
5
5
6
+ from slugify import slugify
7
+
6
8
from gitlab2sentry .exceptions import SentryProjectCreationFailed
7
9
from gitlab2sentry .resources import (
8
10
DSN_MR_TITLE ,
@@ -239,17 +241,20 @@ def _get_gitlab_groups(self):
239
241
return groups
240
242
241
243
def _create_sentry_project (
242
- self , full_path : str , sentry_group_name : str
244
+ self ,
245
+ full_path : str ,
246
+ sentry_group_name : str ,
247
+ sentry_project_name : str ,
248
+ sentry_project_slug : str
243
249
) -> Optional [Dict [str , Any ]]:
244
-
245
- sentry_project_name = "-" .join (full_path .split ("/" )[1 :])
246
250
try :
247
251
return self .sentry_provider .get_or_create_project (
248
252
sentry_group_name ,
249
253
sentry_project_name ,
254
+ sentry_project_slug ,
250
255
)
251
256
except SentryProjectCreationFailed as creation_err :
252
- logging .warning (
257
+ logging .error (
253
258
"{} Project {} - Failed to create sentry project: {}" .format (
254
259
self .__str__ (), full_path , str (creation_err )
255
260
)
@@ -263,7 +268,10 @@ def _create_sentry_project(
263
268
return None
264
269
265
270
def _handle_g2s_project (
266
- self , g2s_project : G2SProject , sentry_group_name : str
271
+ self ,
272
+ g2s_project : G2SProject ,
273
+ sentry_group_name : str ,
274
+ custom_name : Optional [str ] = None
267
275
) -> bool :
268
276
"""
269
277
Creates sentry project for all given gitlab projects. It
@@ -300,9 +308,18 @@ def _handle_g2s_project(
300
308
):
301
309
return False
302
310
else :
311
+
312
+ sentry_project_name = (
313
+ custom_name
314
+ if custom_name
315
+ else "-" .join (g2s_project .full_path .split ("/" )[1 :])
316
+ )
317
+ sentry_project_slug = slugify (sentry_project_name ).lower ()
303
318
sentry_project = self ._create_sentry_project (
304
319
g2s_project .full_path ,
305
320
sentry_group_name ,
321
+ sentry_project_name ,
322
+ sentry_project_slug
306
323
)
307
324
308
325
# If Sentry fails to create project skip
@@ -317,7 +334,9 @@ def _handle_g2s_project(
317
334
if not dsn :
318
335
return False
319
336
320
- mr_created = self .gitlab_provider .create_dsn_mr (g2s_project , dsn )
337
+ mr_created = self .gitlab_provider .create_dsn_mr (
338
+ g2s_project , dsn , sentry_project_slug
339
+ )
321
340
if mr_created :
322
341
self .run_stats ["mr_dsn_created" ] += 1
323
342
return True
@@ -343,24 +362,33 @@ def _handle_g2s_project(
343
362
self .run_stats ["not_in_g2s_cases" ] += 1
344
363
return False
345
364
346
- def update (self , ** kwargs ) -> None :
365
+ def update (
366
+ self ,
367
+ full_path : Optional [str ] = None ,
368
+ custom_name : Optional [str ] = None
369
+ ) -> None :
347
370
"""
348
- kwargs : full_path
371
+ args : full_path
349
372
description: Full path of project (e.g. my-team/my-project)
350
373
374
+ args: custom_name
375
+ description: Specifies a custom name for the project. It only
376
+ works if the full_path is specified
377
+
351
378
If the fullPath of a specific project is given it will run
352
379
the script only for this project.
353
380
354
381
If no full_path is provided it will run the script. If
355
382
creation_days_limit is provided it will fetch all projects
356
383
created after this period. If no it will fetch every project
357
384
"""
358
- full_path = kwargs .get ("full_path" , None )
359
385
if full_path :
360
386
g2s_project = self ._get_gitlab_project (full_path )
361
387
if g2s_project :
362
388
sentry_group_name = g2s_project .group .split ("/" )[0 ].strip ()
363
- self ._handle_g2s_project (g2s_project , sentry_group_name ) # type: ignore
389
+ self ._handle_g2s_project (
390
+ g2s_project , sentry_group_name , custom_name
391
+ ) # type: ignore
364
392
else :
365
393
logging .info (
366
394
"{}: Project with fullPath - {} not found" .format (
@@ -378,8 +406,8 @@ def update(self, **kwargs) -> None:
378
406
# Skip if sentry is installed or
379
407
# Project has disabled MRs
380
408
self ._handle_g2s_project (
381
- g2s_project , sentry_group_name # type: ignore
382
- )
409
+ g2s_project , sentry_group_name
410
+ ) # type: ignore
383
411
384
412
for key in self .run_stats .keys ():
385
413
logging .info (
0 commit comments