Skip to content

fix(gitlab): remove custom author when commit signing is enabled #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 81 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:

Expand All @@ -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
Expand All @@ -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!

46 changes: 46 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -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 | `[email protected]` |
| `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"
```
4 changes: 1 addition & 3 deletions gitlab2sentry/resources.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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")
Expand Down
25 changes: 15 additions & 10 deletions gitlab2sentry/utils/gitlab_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ def payload_sentry_project():
dsn_mr_state="merged",
)


GRAPHQL_TEST_QUERY = {
"name": "TEST_QUERY",
"instance": "projects",
Expand Down
Loading