diff --git a/README.md b/README.md index 82444dd..4b4c2da 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,11 @@ Any new Gitlab repository you create will be offered a Sentry project if you acc ## Two-Steps process -1. After creating your new project on Gitlab, ```gitlab2sentry``` will create a first Merge Request asking if you want it to create an associated Sentry project for it. This Merge Request will contain the creation of a ```.sentryclirc``` file which, if you merge it, will be contributed back the newly created Sentry project ```DSN``` for this project. +1. After creating your new project on Gitlab, `gitlab2sentry` will create a first Merge Request asking if you want it to create an associated Sentry project for it. This Merge Request will contain the creation of a `.sentryclirc` file which, if you merge it, will be contributed back the newly created Sentry project `DSN` for this project. -2. If you merged the first Merge Request, ```gitlab2sentry``` will create a second one to update the newly created ```.sentryclirc``` file with the ```DSN``` of the sentry project. Moreover, after the merge of the first Merge Request ```gitlabsentry``` will create a new ```sentry project```, update its rate limit and save the ```DSN``` inside ```.sentryclirc```. Once you have merged this second Merge Request everything will be set up! - -**NOTE**: ```Gitlab2Sentry``` looks only for group projects and searches for MRs having specific keyword inside (check "Configuration" section) +2. If you merged the first Merge Request, `gitlab2sentry` will create a second one to update the newly created `.sentryclirc` file with the `DSN` of the sentry project. Moreover, after the merge of the first Merge Request `gitlabsentry` will create a new `sentry project`, update its rate limit and save the `DSN` inside `.sentryclirc`. Once you have merged this second Merge Request everything will be set up! +**NOTE**: `Gitlab2Sentry` looks only for group projects and searches for MRs having specific keyword inside (check "Configuration" section) ## Run locally @@ -38,7 +37,7 @@ python3 run.py ## Deployment -We prefer to deploy and manage ```gitlab2sentry``` with ```helm```. Inside ```helm/``` folder you can find an example deployment. +We prefer to deploy and manage `gitlab2sentry` with `helm`. Inside `helm/` folder you can find an example deployment. You can upgrade your deployment with: @@ -48,92 +47,94 @@ make upgrade ## Configuration -```Gitlab2Sentry``` requires some configuration in 3 specific files. +`Gitlab2Sentry` requires some configuration in 3 specific files. + +**[All configuration variables here](./docs/configuration.md)** -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: +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: ```yaml - # Sentry values - - name: SENTRY_TOKEN - valueFrom: - secretKeyRef: - key: SENTRY_TOKEN - name: gitlab2sentry-production - - name: SENTRY_DSN - value: your-sentry-dsn - - name: SENTRY_URL - value: your-sentry-url - - name: SENTRY_ORG_SLUG - value: your-sentry-organization-slug - # Gitlab values - - name: GITLAB_TOKEN - valueFrom: - secretKeyRef: - key: GITLAB_TOKEN - name: your-secret - - name: GITLAB_URL - value: your-gitlab-url - # DSN MR (1) values - - name: GITLAB_DSN_MR_CONTENT - value: the content of your dsn mr - - name: GITLAB_DSN_MR_DESCRIPTION - value: the description of your dsn mr - - name: GITLAB_DSN_MR_BRANCH_NAME - value: your-branch-name - - name: GITLAB_DSN_MR_TITLE - value: "your-dsn-mr-title" - # Sentryclirc MR (2) values - - name: GITLAB_SENTRYCLIRC_MR_CONTENT - value: your-sentryclirc-mr-content - - name: GITLAB_SENTRYCLIRC_MR_DESCRIPTION - value: your-sentryclirc-mr-description - - name: GITLAB_SENTRYCLIRC_MR_BRANCH_NAME - value: your-sentryclirc-mr-branch-name - - name: GITLAB_SENTRYCLIRC_MR_FILEPATH - value: .sentryclirc - - name: GITLAB_SENTRYCLIRC_MR_COMMIT_MSG - value: your-commit-msg - - name: GITLAB_SENTRYCLIRC_MR_TITLE - value: "your sentryclirc mr title" - # Gitlab configuration values - - name: GITLAB_AUTHOR_NAME - value: author-name - - name: GITLAB_AUTHOR_EMAIL - value: your-author-email - - name: GITLAB_GRAPHQL_SUFFIX - value: api/graphql - # - name: GITLAB_MENTIONS - # value: - # - "@all" - - name: GITLAB_MENTIONS_ACCESS_LEVEL - value: 40 # maintainer - - name: GITLAB_CREATION_DAYS_LIMIT - value: 60 # Max days old per project - - name: GITLAB_MR_KEYWORD - value: sentry # key word for searching mrs - - name: GITLAB_REMOVE_SOURCE - value: true # If the mr will remove the source branch - - name: GITLAB_GROUP_IDENTIFIER - value: your-group-identifier # will look only for group projects having this identifier - - name: GITLAB_AIOHTTP_TIMEOUT - value: 60 - - name: GITLAB_GRAPHQL_PAGE_LENGTH - value: 100 - - name: GITLAB_MR_LABEL_LIST - value: "sentry,your-label" # comma separated list of labels for the mr +# Sentry values +- name: SENTRY_TOKEN + valueFrom: + secretKeyRef: + key: SENTRY_TOKEN + name: gitlab2sentry-production +- name: SENTRY_DSN + value: your-sentry-dsn +- name: SENTRY_URL + value: your-sentry-url +- name: SENTRY_ORG_SLUG + value: your-sentry-organization-slug +# Gitlab values +- name: GITLAB_TOKEN + valueFrom: + secretKeyRef: + key: GITLAB_TOKEN + name: your-secret +- name: GITLAB_URL + value: your-gitlab-url + # DSN MR (1) values +- name: GITLAB_DSN_MR_CONTENT + value: the content of your dsn mr +- name: GITLAB_DSN_MR_DESCRIPTION + value: the description of your dsn mr +- name: GITLAB_DSN_MR_BRANCH_NAME + value: your-branch-name +- name: GITLAB_DSN_MR_TITLE + value: "your-dsn-mr-title" + # Sentryclirc MR (2) values +- name: GITLAB_SENTRYCLIRC_MR_CONTENT + value: your-sentryclirc-mr-content +- name: GITLAB_SENTRYCLIRC_MR_DESCRIPTION + value: your-sentryclirc-mr-description +- name: GITLAB_SENTRYCLIRC_MR_BRANCH_NAME + value: your-sentryclirc-mr-branch-name +- name: GITLAB_SENTRYCLIRC_MR_FILEPATH + value: .sentryclirc +- name: GITLAB_SENTRYCLIRC_MR_COMMIT_MSG + value: your-commit-msg +- name: GITLAB_SENTRYCLIRC_MR_TITLE + value: "your sentryclirc mr title" + # Gitlab configuration values +- name: GITLAB_AUTHOR_NAME + value: author-name +- name: GITLAB_AUTHOR_EMAIL + value: your-author-email +- name: GITLAB_GRAPHQL_SUFFIX + value: api/graphql +# - name: GITLAB_MENTIONS +# value: +# - "@all" +- name: GITLAB_MENTIONS_ACCESS_LEVEL + value: 40 # maintainer +- name: GITLAB_CREATION_DAYS_LIMIT + value: 60 # Max days old per project +- name: GITLAB_MR_KEYWORD + value: sentry # key word for searching mrs +- name: GITLAB_REMOVE_SOURCE + value: true # If the mr will remove the source branch +- name: GITLAB_GROUP_IDENTIFIER + value: your-group-identifier # will look only for group projects having this identifier +- name: GITLAB_AIOHTTP_TIMEOUT + value: 60 +- name: GITLAB_GRAPHQL_PAGE_LENGTH + value: 100 +- name: GITLAB_MR_LABEL_LIST + value: "sentry,your-label" # comma separated list of labels for the mr ``` -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```. +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`. -3. You can update ```REG ?= your-registry``` and ```NS ?= your-namespace``` values inside ```Makefile```. +3. You can update `REG ?= your-registry` and `NS ?= your-namespace` values inside `Makefile`. ## Manual run -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. +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. -* First, you have to ```export``` all env variables which are listed above in the ```helm/values-production.yaml``` file. +- First, you have to `export` all env variables which are listed above in the `helm/values-production.yaml` file. -* Next you can run the following commands: +- Next you can run the following commands: ```python >>> from gitlab2sentry import Gitlab2Sentry @@ -146,4 +147,3 @@ If you want to update a specific project (for example if the project has a very 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. Feel free to ask questions, suggest improvements and of course contribute features or fixes you might need! - diff --git a/docs/configuration.md b/docs/configuration.md new file mode 100644 index 0000000..5815f20 --- /dev/null +++ b/docs/configuration.md @@ -0,0 +1,46 @@ +# Configuration Guide + +This application uses `pydantic`'s `BaseSettings` for configuration, which allows you to set and override parameters using environment variables. Below, you'll find a list of all the configuration options and the expected environment variables. Each configuration setting has a default value, but you can easily override them to suit your deployment needs. + +To configure the application, set the following environment variables: + +| Environment Variable | Description | Default Value | +| ------------------------------- | -------------------------------------------------- | ----------------------------- | +| `DSN_BRANCH_NAME` | Branch name for DSN changes | `auto_add_sentry_dsn` | +| `DSN_MR_CONTENT` | Merge request content for DSN | Custom template (see code) | +| `DSN_MR_DESCRIPTION` | Description for DSN-related merge request | Custom template (see code) | +| `DSN_MR_TITLE` | Title for DSN-related merge request | `[gitlab2sentry] Merge me...` | +| `ENV` | The environment the application is running in | `production` | +| `GITLAB_AUTHOR_EMAIL` | GitLab author email for merge requests | `default-email@example.com` | +| `GITLAB_AUTHOR_NAME` | GitLab author name for merge requests | `Default Author` | +| `GITLAB_GRAPHQL_PAGE_LENGTH` | Page length for GitLab GraphQL queries | `0` | +| `GITLAB_GRAPHQL_SUFFIX` | Suffix for GitLab GraphQL queries | `default-content` | +| `GITLAB_GRAPHQL_TIMEOUT` | Timeout for GitLab GraphQL queries (in seconds) | `10` | +| `GITLAB_GROUP_IDENTIFIER` | Group identifier for GitLab projects | Empty string | +| `GITLAB_MENTIONS_ACCESS_LEVEL` | Access level to mention users in GitLab MRs | `40` | +| `GITLAB_MENTIONS` | GitLab usernames to mention | Empty string | +| `GITLAB_MR_KEYWORD` | Keyword to include in GitLab merge requests | `sentry` | +| `GITLAB_MR_LABEL_LIST` | Labels to assign to GitLab merge requests | `['sentry']` | +| `GITLAB_PROJECT_CREATION_LIMIT` | Limit for creating GitLab projects | `30` | +| `GITLAB_RMV_SRC_BRANCH` | Remove source branch after merge request | `True` | +| `GITLAB_SIGNED_COMMIT` | Whether to use signed commits in GitLab | `False` | +| `GITLAB_TOKEN` | GitLab access token | `default-token` | +| `GITLAB_URL` | Base URL for GitLab service | `http://default-gitlab-url` | +| `SENTRYCLIRC_BRANCH_NAME` | Branch name for Sentry CLI configuration changes | `auto_add_sentry` | +| `SENTRYCLIRC_COM_MSG` | Commit message for `.sentryclirc` update | `Update .sentryclirc` | +| `SENTRYCLIRC_FILEPATH` | Filepath for `.sentryclirc` configuration | `.sentryclirc` | +| `SENTRYCLIRC_MR_CONTENT` | Merge request content for Sentry CLI configuration | Custom template (see code) | +| `SENTRYCLIRC_MR_DESCRIPTION` | Description for Sentry CLI configuration MR | Custom template (see code) | +| `SENTRYCLIRC_MR_TITLE` | Title for Sentry CLI configuration MR | `[gitlab2sentry] Merge me...` | +| `SENTRY_DSN` | Sentry DSN for monitoring | `http://default.sentry.com` | +| `SENTRY_ENV` | Sentry environment name | `production` | +| `SENTRY_ORG_SLUG` | Organization slug for Sentry | `default_org` | +| `SENTRY_TOKEN` | Authentication token for Sentry | `default-token` | +| `SENTRY_URL` | Base URL for Sentry service | `http://default-sentry-url` | + +To override any configuration, simply set the respective environment variable before running the application. For instance: + +```sh +export SENTRY_DSN="http://your.custom.sentry.dsn" +export GITLAB_URL="http://your.gitlab.url" +``` diff --git a/gitlab2sentry/resources.py b/gitlab2sentry/resources.py index 059023d..add6455 100644 --- a/gitlab2sentry/resources.py +++ b/gitlab2sentry/resources.py @@ -1,12 +1,9 @@ -import os from collections import namedtuple from typing import List, Tuple from pydantic import Field from pydantic_settings import BaseSettings -ENV = os.getenv("ENV", "production") - class Settings(BaseSettings): dsn_branch_name: str = Field("auto_add_sentry_dsn") @@ -38,6 +35,7 @@ class Settings(BaseSettings): gitlab_mr_label_list: List[str] = Field(["sentry"]) gitlab_project_creation_limit: int = Field(30) gitlab_rmv_src_branch: bool = Field(True) + gitlab_signed_commit: bool = Field(False) gitlab_token: str = Field("default-token") gitlab_url: str = Field("http://default-gitlab-url") sentry_dsn: str = Field("http://default.sentry.com") diff --git a/gitlab2sentry/utils/gitlab_provider.py b/gitlab2sentry/utils/gitlab_provider.py index 343fba7..548f96f 100644 --- a/gitlab2sentry/utils/gitlab_provider.py +++ b/gitlab2sentry/utils/gitlab_provider.py @@ -187,16 +187,21 @@ def _get_or_create_sentryclirc( full_path, ) ) - f = project.files.create( - { - "author_email": settings.gitlab_author_email, - "author_name": settings.gitlab_author_name, - "branch": branch_name, - "commit_message": settings.sentryclirc_com_msg, - "content": content, - "file_path": file_path, - } - ) + data = { + "author_email": settings.gitlab_author_email, + "author_name": settings.gitlab_author_name, + "branch": branch_name, + "commit_message": settings.sentryclirc_com_msg, + "content": content, + "file_path": file_path, + } + # When commit signing is enabled in GitLab (e.g. via pre-hook), + # commit requires that the author information matches the signer identity + # https://gitlab.com/gitlab-org/gitlab/-/merge_requests/150855 + if settings.gitlab_signed_commit: + data.pop("author_email") + data.pop("author_name") + f = project.files.create(data=data) def _get_default_mentions(self, project: Project) -> str: return ", ".join( diff --git a/tests/conftest.py b/tests/conftest.py index f8b9ee6..ef99f23 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -351,7 +351,6 @@ def payload_sentry_project(): dsn_mr_state="merged", ) - GRAPHQL_TEST_QUERY = { "name": "TEST_QUERY", "instance": "projects",