Skip to content

Commit 6f469b3

Browse files
Solvikthepetk
authored andcommitted
first working version
now creating merge request and checking if it's merged to create sentry project and post DSNs add README.md add requirements handle projects with subnamespace and already created sentry projects do not handle project if it's not in a team namespace fully functionnal add makefile and fix code qa overall code refactoring, optimization and logging this code is fast enough to be scheduled as a cron instead of sleeping for 600s logic: - we get all MRs for our current user at once - we run on every group - we ensure the group exists on sentry - we iterate on group's projects natively - we base ourselves on MR counts of each project to quicky guess what needs to be done - we keep stats and print them at the end of the run switch action detection from mr count to sentryclirc presence add run target to makefile Update .sentryclirc Udpate .sentryclirc fix some logic for real deployment, add sentry to our project ;) handle and report exceptions on mr proposal errors handle subgroups thanks to @QN do not create a sentry team per subgroup try to created sentry teams only once add Dockerfile and .gitlab-ci.yml for docker image fix TypeError: 'NoneType' object is not subscriptable due to: 2020-11-23 09:20:23 root INFO: creating sentry project team-emailing / Banner_Display_Api {'name': 'Banner_Display_Api', 'slug': 'Banner-Display-Api'} {'slug': ['This value does not match the required pattern.']} add helm chart handle and log create or get sentry project errors helm/values-production: fix SENTRY_TOKEN env var reference makefile: add upgrade task construct sentry project name from path_with_namespace to include sub groups in sentry project names dump deployment image tag apply code QA makefile: add build, push steps add activeDeadlineSeconds to 600 fix activeDeadlineSeconds in the yaml switch to restartPolicy = OnFailure optimize run time: check only repos that were modified < 24h switch to version tag 0.5 make tag and image building dynamic make gitlab2sentry respectful of project default branch add rate limiting to new created keys upgrade helm-charts Add renovate.json Update renovate.json Update dependency sentry-sdk to v0.19.5 Update makefile and gitignore Remove main.py and setup_ratelimiting.py Add pyyaml to requirements.txt Implement configuration yaml file Implement Gitlab2Sentry module Add custom exceptions Implement resources Add run py as entrypoint Import all providers Implement GitlabProvider Implement SetnryProvider Add custom exceptions Implement SentryAPIClient Add type hints and custom exception handling Apply reformats Apply mypy type hints rules Update config.yaml Refactor Gitlab2Sentry to use GraphQL for gitlab integration Add gitlab GraphQL query Implement GraphQLClient for GitlabProvider Refactor SentryProvider Update requirements.txt Implement setup.py Fix logging on gitlab2sentry Add exceptions for MR creation Add exceptions to setnry provider Rename config to g2s Fix update flow after testing for resetly Remove test case Catch error upon configuration initialization Make pagination length configurable Fix logging in sentry_provider Apply reformats Fix MRs error on on check mr states Set page length to 100 Fix returning stats and query performance Initialize data type for statistics Make query more re-usable Update readme.md Update logging Set mentions access_level for MRs Add configuration for access_level Add default Mentions if no specific mentions given Add default env Add time limit to reduce query time Add created_at to G2SProject instances Add creation limit to resources Break query if the time limit is reached Update readme Support multiple or one project update Refactor dockerfile TEMP log env vars to debug 401 error Fix helm files Revert temp logging Add secrets into helm files Update upgrade command Update helm templates Skip project if closed dsn MR found Remove path_with_namespace variable from Gitlab2Sentry Set counters instead of list logging Replace path_with_namespace with full_path in gitlabprovider Update logging in SentryProvider Fix full path error Fix duplicated attribute Move 3 files Update dockerfile Update helm file Update readme Update makefile Remove sensitive data Update readme.md Sync commits Add tests Split requirements files
0 parents  commit 6f469b3

26 files changed

+2420
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.env
2+
/venv/
3+
__pycache__/

.mypy.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[mypy]
2+
python_version = 3.7
3+
mypy_path = gitlab2sentry
4+
5+
[mypy-pytest]
6+
ignore_missing_imports = True

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM python:3.9-slim
2+
3+
WORKDIR /usr/src/app
4+
5+
COPY requirements.txt requirements.txt
6+
RUN pip3 install -r requirements.txt
7+
8+
COPY gitlab2sentry/ gitlab2sentry/
9+
COPY run.py run.py
10+
COPY g2s.yaml g2s.yaml
11+
12+
CMD ["python3", "run.py"]

Makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# project name
2+
PRJ ?= gitlab2sentry
3+
REG ?= your-registry
4+
NS ?= your-namespace
5+
IMG ?= $(REG)/$(NS)/$(PRJ)
6+
TAG ?= $(shell git describe --tags)
7+
8+
build:
9+
docker build -t $(IMG):$(TAG) .
10+
11+
push: build
12+
docker push $(IMG):$(TAG)
13+
14+
qa:
15+
isort --profile black . && black . && flake8
16+
17+
mypy:
18+
mypy gitlab2sentry/ --config-file .mypy.ini --ignore-missing-imports
19+
20+
run:
21+
# needed env: GITLAB_TOKEN + SENTRY_TOKEN
22+
python3 gitlab2sentry/run.py
23+
24+
upgrade: push
25+
helm secrets -d vault -n $(NS) upgrade -f helm/values-production.yaml --set cronjob.imageTag=$(TAG) gitlab2sentry ./helm

README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# gitlab2sentry
2+
3+
This project aims to automate Sentry project creation. Moreover it aims to create a sentry project for every gitlab team's project.
4+
5+
The script aims to add sentry for every new project you have created.
6+
7+
8+
## Two-Steps process
9+
10+
1. After creating your new project, in your gitlab, ```gitlab2sentry``` will create the first Pull Request. This pull request will contain the creation of the ```.sentryclirc``` file. Although, it will not contain the ```DSN``` for this project and no project will be created to ```sentry```
11+
12+
2. Once the user has merged the first Pull request, ```gitlab2sentry``` will create the second Pull Request. This pull request will update the newly created ```.sentryclirc``` file with the ```DSN``` of the sentry project. Moreover, after the merge of the first Pull Request ```gitlabsentry``` will create a new ```sentry project```, update its rate limit and save the ```DSN``` inside ```.sentryclirc```. Once the user has merged the second pull request everything will be set up.
13+
14+
**NOTE**: ```Gitlab2Sentry``` looks only for group projects and searches for PRs having specific keyword inside (check "Configuration" section)
15+
16+
17+
## Run locally
18+
19+
You can install all requirements for this project with
20+
21+
```
22+
python3 -m venv venv
23+
pip3 install -r requirements.txt
24+
source venv/bin/ac
25+
```
26+
27+
After the installation of all requirements you have to:
28+
29+
```
30+
export SENTRY_URL=<your sentry's url>
31+
export SENTRY_TOKEN=<your sentry token>
32+
export SENTRY_ENV=<your environment - default production>
33+
export GITLAB_TOKEN=<your gitlab token>
34+
export GITLAB_URL=<your gitlab url>
35+
python3 run.py
36+
```
37+
38+
## Deployment
39+
40+
We prefer to deploy and manage ```gitlab2sentry``` with ```helm```. Inside ```helm/``` folder you can find an example deployment.
41+
42+
You can upgrade your deployment with:
43+
44+
```
45+
make upgrade
46+
```
47+
48+
## Configuration
49+
50+
```Gitlab2Sentry``` requires some configuration in 3 specific files.
51+
52+
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+
54+
```
55+
sentry:
56+
# Sentry configuration.
57+
slug: <your sentry organization>
58+
gitlab:
59+
# DSN PR configuration.
60+
dsn_mr:
61+
# Default values for dsn (2nd) pull request configuration.
62+
content: |
63+
## File generated by gitlab2sentry
64+
[defaults]
65+
url = {sentry_url}
66+
dsn = {dsn}
67+
description: <your description in the DSN PR>
68+
branch_name: <default dsn branch name>
69+
title: <default title of the dsn mr>
70+
# Sentryclirc PR configuration.
71+
sentryclirc_mr:
72+
# Default values for .sentryclirc (1st) pull request configuration.
73+
content: |
74+
## File generated by gitlab2sentry
75+
[defaults]
76+
url = {sentry_url}
77+
description: <your description in the sentryclirc MR>
78+
branch_name: <your sentryclirc branch_name
79+
filepath: .sentryclirc
80+
commit_message: <commit message>
81+
title: <default title of the sentryclirc mr>
82+
# Gitlab configuration.
83+
config:
84+
author:
85+
name: <name>
86+
email: <email>
87+
graphql_suffix: <endpoint of gitlab graphql>
88+
# Mention some specific people.
89+
mentions:
90+
- "@<mention>
91+
- # everyone you want to include on those MRs
92+
# If no "mentions" field is included, you have to define
93+
# The access_level of those who will be mentioned in your PR.
94+
mentions_access_level: 40
95+
# This keyword must be included in your Pull Request title.
96+
# Gitlab2sentry will search with this keyword for PRs
97+
keyword: sentry
98+
# How old has to be the projects checked from gitlab2sentry
99+
creation_days_limit: 60
100+
# Do you want to remove the source branch after merge?
101+
remove_source: true
102+
# Gitlab2Sentry searches only for group projects.
103+
group_identifier: <define a keyword for the groups you want to include>
104+
# Configure the aiohttp timeout for the GraphQL API of gitlab
105+
graphql_aiohttp_timeout: 40
106+
# How many items per page will have the response of the GraphQL API.
107+
graphql_page_length: 100
108+
```
109+
110+
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```.
111+
112+
3. You can update ```REG ?= your-registry``` and ```NS ?= your-namespace``` values inside ```Makefile```.

dev-requirements.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-r requirements.txt
2+
3+
black
4+
flake8
5+
isort
6+
mock
7+
mypy
8+
pytest
9+
pytest-mock
10+
types-python-slugify
11+
types-PyYAML
12+
types-requests

g2s.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
sentry:
2+
# Sentry configuration.
3+
slug: your-sentry-slug
4+
gitlab:
5+
# DSN MR configuration.
6+
dsn_mr:
7+
content: |
8+
## File generated by gitlab2sentry
9+
[defaults]
10+
url = {sentry_url}
11+
dsn = {dsn}
12+
description: |
13+
{mentions} Congrats, your Sentry project has been
14+
created, merge this
15+
to finalize your Sentry integration of
16+
{name_with_namespace} :clap: :cookie:
17+
branch_name: auto_add_sentry_dsn
18+
title: "[gitlab2sentry] Merge me to add your sentry DSN to {project_name}"
19+
# Sentryclirc MR configuration.
20+
sentryclirc_mr:
21+
content: |
22+
## File generated by gitlab2sentry
23+
[defaults]
24+
url = {sentry_url}
25+
description: |
26+
{mentions} Merge this and it will automatically
27+
create a Sentry project \n
28+
for {name_with_namespace} :cookie:
29+
branch_name: auto_add_sentry
30+
filepath: .sentryclirc
31+
commit_message: Update .sentryclirc
32+
title: "[gitlab2sentry] Merge me to add sentry to {project_name} or close me"
33+
# Gitlab configuration.
34+
config:
35+
author:
36+
name: gitlab2sentry
37+
email: your-author-email
38+
graphql_suffix: api/graphql
39+
# mentions:
40+
# - "@all"
41+
# If mentions exist then access_level is not used
42+
mentions_access_level: 40
43+
creation_days_limit: 60
44+
keyword: sentry
45+
remove_source: true
46+
group_identifier: your-group-identifier
47+
graphql_aiohttp_timeout: 40
48+
graphql_page_length: 100

0 commit comments

Comments
 (0)