Skip to content

Commit fee61a9

Browse files
author
thepetk
committed
Add project slug in dsn mr
1 parent 0940f70 commit fee61a9

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

gitlab2sentry/__init__.py

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from datetime import datetime, timedelta
44
from typing import Any, Dict, List, Optional
55

6+
from slugify import slugify
7+
68
from gitlab2sentry.exceptions import SentryProjectCreationFailed
79
from gitlab2sentry.resources import (
810
DSN_MR_TITLE,
@@ -239,17 +241,20 @@ def _get_gitlab_groups(self):
239241
return groups
240242

241243
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
243249
) -> Optional[Dict[str, Any]]:
244-
245-
sentry_project_name = "-".join(full_path.split("/")[1:])
246250
try:
247251
return self.sentry_provider.get_or_create_project(
248252
sentry_group_name,
249253
sentry_project_name,
254+
sentry_project_slug,
250255
)
251256
except SentryProjectCreationFailed as creation_err:
252-
logging.warning(
257+
logging.error(
253258
"{} Project {} - Failed to create sentry project: {}".format(
254259
self.__str__(), full_path, str(creation_err)
255260
)
@@ -263,7 +268,10 @@ def _create_sentry_project(
263268
return None
264269

265270
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
267275
) -> bool:
268276
"""
269277
Creates sentry project for all given gitlab projects. It
@@ -300,9 +308,18 @@ def _handle_g2s_project(
300308
):
301309
return False
302310
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()
303318
sentry_project = self._create_sentry_project(
304319
g2s_project.full_path,
305320
sentry_group_name,
321+
sentry_project_name,
322+
sentry_project_slug
306323
)
307324

308325
# If Sentry fails to create project skip
@@ -317,7 +334,9 @@ def _handle_g2s_project(
317334
if not dsn:
318335
return False
319336

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+
)
321340
if mr_created:
322341
self.run_stats["mr_dsn_created"] += 1
323342
return True
@@ -343,24 +362,33 @@ def _handle_g2s_project(
343362
self.run_stats["not_in_g2s_cases"] += 1
344363
return False
345364

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:
347370
"""
348-
kwargs: full_path
371+
args: full_path
349372
description: Full path of project (e.g. my-team/my-project)
350373
374+
args: custom_name
375+
description: Specifies a custom name for the project. It only
376+
works if the full_path is specified
377+
351378
If the fullPath of a specific project is given it will run
352379
the script only for this project.
353380
354381
If no full_path is provided it will run the script. If
355382
creation_days_limit is provided it will fetch all projects
356383
created after this period. If no it will fetch every project
357384
"""
358-
full_path = kwargs.get("full_path", None)
359385
if full_path:
360386
g2s_project = self._get_gitlab_project(full_path)
361387
if g2s_project:
362388
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
364392
else:
365393
logging.info(
366394
"{}: Project with fullPath - {} not found".format(
@@ -378,8 +406,8 @@ def update(self, **kwargs) -> None:
378406
# Skip if sentry is installed or
379407
# Project has disabled MRs
380408
self._handle_g2s_project(
381-
g2s_project, sentry_group_name # type: ignore
382-
)
409+
g2s_project, sentry_group_name
410+
) # type: ignore
383411

384412
for key in self.run_stats.keys():
385413
logging.info(

gitlab2sentry/utils/gitlab_provider.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,9 @@ def create_sentryclirc_mr(self, g2s_project: G2SProject) -> bool:
296296
SENTRYCLIRC_MR_TITLE.format(project_name=g2s_project.name),
297297
)
298298

299-
def create_dsn_mr(self, g2s_project: G2SProject, dsn: str) -> bool:
299+
def create_dsn_mr(
300+
self, g2s_project: G2SProject, dsn: str, project_slug: str
301+
) -> bool:
300302
logging.info(
301303
"{}: [Creating] Project {} - Sentry dsn: {}. Needs dsn MR.".format(
302304
self.__str__(), g2s_project.full_path, dsn
@@ -306,6 +308,8 @@ def create_dsn_mr(self, g2s_project: G2SProject, dsn: str) -> bool:
306308
g2s_project,
307309
DSN_BRANCH_NAME,
308310
SENTRYCLIRC_FILEPATH,
309-
DSN_MR_CONTENT.format(sentry_url=SENTRY_URL, dsn=dsn),
311+
DSN_MR_CONTENT.format(
312+
sentry_url=SENTRY_URL, dsn=dsn, project_slug=project_slug
313+
),
310314
DSN_MR_TITLE.format(project_name=g2s_project.name),
311315
)

gitlab2sentry/utils/sentry_provider.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import requests
66
from requests import Response
7-
from slugify import slugify
87

98
from gitlab2sentry.exceptions import (
109
SentryProjectCreationFailed,
@@ -93,10 +92,9 @@ def _get_or_create_team(self, team_name: str) -> Optional[Dict[str, Any]]:
9392
return result
9493

9594
def get_or_create_project(
96-
self, group_name: str, project_name: str
95+
self, group_name: str, project_name: str, project_slug: str
9796
) -> Optional[Dict[str, Any]]:
9897

99-
project_slug = slugify(project_name).lower()
10098
status_code, result = self._client.simple_request(
10199
"get", "projects/{}/{}/".format(self.org_slug, project_slug)
102100
)

0 commit comments

Comments
 (0)