Skip to content

Commit fafb958

Browse files
authored
Merge pull request #12 from numberly/feature/remove_g2s_and_support_project_slug
Feature/remove g2s and support project slug
2 parents 0940f70 + c6bd352 commit fafb958

13 files changed

+414
-182
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ RUN groupadd --gid 1000 appuser \
1111
USER appuser
1212
COPY gitlab2sentry/ gitlab2sentry/
1313
COPY run.py run.py
14-
COPY g2s.yaml g2s.yaml
1514

1615
CMD ["python3", "run.py"]

README.md

Lines changed: 81 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -50,71 +50,95 @@ make upgrade
5050
5151
```Gitlab2Sentry``` requires some configuration in 3 specific files.
5252
53-
1. First of all you have to configure the ```g2s.yaml``` file where everything is configured for the ```gitlab2sentry``` service. Here you can find a description for every field:
53+
1. First of all you have to configure the ```helm/values-production.yaml``` file where everything is configured for the ```gitlab2sentry``` service. Here you can find a description for every field:
5454
5555
```yaml
56-
sentry:
57-
# Sentry configuration.
58-
slug: <your sentry organization>
59-
60-
gitlab:
61-
# DSN PR configuration.
62-
dsn_mr:
63-
# Default values for dsn (2nd) pull request configuration.
64-
content: |
65-
## File generated by gitlab2sentry
66-
[defaults]
67-
url = {sentry_url}
68-
dsn = {dsn}
69-
description: <your description in the DSN PR>
70-
branch_name: <default dsn branch name>
71-
title: <default title of the dsn mr>
72-
73-
# Sentryclirc PR configuration.
74-
sentryclirc_mr:
75-
# Default values for .sentryclirc (1st) pull request configuration.
76-
content: |
77-
## File generated by gitlab2sentry
78-
[defaults]
79-
url = {sentry_url}
80-
description: <your description in the sentryclirc MR>
81-
branch_name: <your sentryclirc branch_name
82-
filepath: .sentryclirc
83-
commit_message: <commit message>
84-
title: <default title of the sentryclirc mr>
85-
86-
# Gitlab configuration.
87-
config:
88-
author:
89-
name: <name>
90-
email: <email>
91-
graphql_suffix: <endpoint of gitlab graphql>
92-
# Mention some specific people.
93-
mentions:
94-
- "@<mention>
95-
- # everyone you want to include on those MRs
96-
# If no "mentions" field is included, you have to define
97-
# The access_level of those who will be mentioned in your PR.
98-
mentions_access_level: 40
99-
# This keyword must be included in your Pull Request title.
100-
# Gitlab2sentry will search with this keyword for PRs
101-
keyword: sentry
102-
# How old has to be the projects checked from gitlab2sentry
103-
creation_days_limit: 60
104-
# Do you want to remove the source branch after merge?
105-
remove_source: true
106-
# Gitlab2Sentry searches only for group projects.
107-
group_identifier: <define a keyword for the groups you want to include>
108-
# Configure the aiohttp timeout for the GraphQL API of gitlab
109-
graphql_aiohttp_timeout: 40
110-
# How many items per page will have the response of the GraphQL API.
111-
graphql_page_length: 100
56+
# Sentry values
57+
- name: SENTRY_TOKEN
58+
valueFrom:
59+
secretKeyRef:
60+
key: SENTRY_TOKEN
61+
name: gitlab2sentry-production
62+
- name: SENTRY_DSN
63+
value: your-sentry-dsn
64+
- name: SENTRY_URL
65+
value: your-sentry-url
66+
- name: SENTRY_ORG_SLUG
67+
value: your-sentry-organization-slug
68+
# Gitlab values
69+
- name: GITLAB_TOKEN
70+
valueFrom:
71+
secretKeyRef:
72+
key: GITLAB_TOKEN
73+
name: your-secret
74+
- name: GITLAB_URL
75+
value: your-gitlab-url
76+
# DSN MR (1) values
77+
- name: GITLAB_DSN_MR_CONTENT
78+
value: the content of your dsn mr
79+
- name: GITLAB_DSN_MR_DESCRIPTION
80+
value: the description of your dsn mr
81+
- name: GITLAB_DSN_MR_BRANCH_NAME
82+
value: your-branch-name
83+
- name: GITLAB_DSN_MR_TITLE
84+
value: "your-dsn-mr-title"
85+
# Sentryclirc MR (2) values
86+
- name: GITLAB_SENTRYCLIRC_MR_CONTENT
87+
value: your-sentryclirc-mr-content
88+
- name: GITLAB_SENTRYCLIRC_MR_DESCRIPTION
89+
value: your-sentryclirc-mr-description
90+
- name: GITLAB_SENTRYCLIRC_MR_BRANCH_NAME
91+
value: your-sentryclirc-mr-branch-name
92+
- name: GITLAB_SENTRYCLIRC_MR_FILEPATH
93+
value: .sentryclirc
94+
- name: GITLAB_SENTRYCLIRC_MR_COMMIT_MSG
95+
value: your-commit-msg
96+
- name: GITLAB_SENTRYCLIRC_MR_TITLE
97+
value: "your sentryclirc mr title"
98+
# Gitlab configuration values
99+
- name: GITLAB_AUTHOR_NAME
100+
value: author-name
101+
- name: GITLAB_AUTHOR_EMAIL
102+
value: your-author-email
103+
- name: GITLAB_GRAPHQL_SUFFIX
104+
value: api/graphql
105+
# - name: GITLAB_MENTIONS
106+
# value:
107+
# - "@all"
108+
- name: GITLAB_MENTIONS_ACCESS_LEVEL
109+
value: 40 # maintainer
110+
- name: GITLAB_CREATION_DAYS_LIMIT
111+
value: 60 # Max days old per project
112+
- name: GITLAB_MR_KEYWORD
113+
value: sentry # key word for searching mrs
114+
- name: GITLAB_REMOVE_SOURCE
115+
value: true # If the mr will remove the source branch
116+
- name: GITLAB_GROUP_IDENTIFIER
117+
value: your-group-identifier # will look only for group projects having this identifier
118+
- name: GITLAB_AIOHTTP_TIMEOUT
119+
value: 60
120+
- name: GITLAB_GRAPHQL_PAGE_LENGTH
121+
value: 100
112122
```
113123
114124
2. If you want to follow the ```helm``` deployment process you will have to fill your details into the ```helm/values-production.yaml``` and ```helm/Chart.yaml```.
115125
116126
3. You can update ```REG ?= your-registry``` and ```NS ?= your-namespace``` values inside ```Makefile```.
117127
128+
## Manual run
129+
130+
If you want to update a specific project (for example if the project has a very big name or is older than the ```GITLAB_CREATION_DAYS_LIMIT``` value), you can run the ```gitlab2sentry``` manually.
131+
132+
* First, you have to ```export``` all env variables which are listed above in the ```helm/values-production.yaml``` file.
133+
134+
* Next you can run the following commands:
135+
136+
```python
137+
>>> from gitlab2sentry import Gitlab2Sentry
138+
>>> g2s = Gitlab2Sentry()
139+
>>> g2s.update(full_path="projects_full_path", custom_name="optional_custom_name")
140+
```
141+
118142
## Contributions & comments welcomed
119143
120144
Numberly decided to Open Source this project because it saves a lot of time internally to all our developers and helped foster the mass adoption of Sentry in all our Tech teams. We hope this project can benefit someone else.

g2s.yaml

Lines changed: 0 additions & 48 deletions
This file was deleted.

gitlab2sentry/__init__.py

Lines changed: 38 additions & 11 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 # type: ignore
391+
)
364392
else:
365393
logging.info(
366394
"{}: Project with fullPath - {} not found".format(
@@ -380,7 +408,6 @@ def update(self, **kwargs) -> None:
380408
self._handle_g2s_project(
381409
g2s_project, sentry_group_name # type: ignore
382410
)
383-
384411
for key in self.run_stats.keys():
385412
logging.info(
386413
"{}: RESULTS - {}: {}".format(self.__str__(), key, self.run_stats[key])

gitlab2sentry/exceptions.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
class InvalidYamlConfigError(Exception):
2-
pass
3-
4-
51
class SentryProjectCreationFailed(Exception):
62
pass
73

0 commit comments

Comments
 (0)