Skip to content

Commit 5ac6dbd

Browse files
committed
fix(gitlab): remove custom author when commit signing is enabled
1 parent 504eb42 commit 5ac6dbd

File tree

6 files changed

+96
-53
lines changed

6 files changed

+96
-53
lines changed

docs/configuration.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,11 @@ To configure the application, set the following environment variables:
66

77
| Environment Variable | Description | Default Value |
88
| ------------------------------- | -------------------------------------------------- | ----------------------------- |
9-
| `ENV` | The environment the application is running in | `production` |
10-
| `SENTRY_DSN` | Sentry DSN for monitoring | `http://default.sentry.com` |
11-
| `SENTRY_ENV` | Sentry environment name | `production` |
12-
| `SENTRY_ORG_SLUG` | Organization slug for Sentry | `default_org` |
13-
| `SENTRY_TOKEN` | Authentication token for Sentry | `default-token` |
14-
| `SENTRY_URL` | Base URL for Sentry service | `http://default-sentry-url` |
15-
| `DSN_MR_CONTENT` | Merge request content for DSN | Custom template (see code) |
169
| `DSN_BRANCH_NAME` | Branch name for DSN changes | `auto_add_sentry_dsn` |
17-
| `DSN_MR_TITLE` | Title for DSN-related merge request | `[gitlab2sentry] Merge me...` |
10+
| `DSN_MR_CONTENT` | Merge request content for DSN | Custom template (see code) |
1811
| `DSN_MR_DESCRIPTION` | Description for DSN-related merge request | Custom template (see code) |
19-
| `SENTRYCLIRC_MR_CONTENT` | Merge request content for Sentry CLI configuration | Custom template (see code) |
20-
| `SENTRYCLIRC_BRANCH_NAME` | Branch name for Sentry CLI configuration changes | `auto_add_sentry` |
21-
| `SENTRYCLIRC_MR_TITLE` | Title for Sentry CLI configuration MR | `[gitlab2sentry] Merge me...` |
22-
| `SENTRYCLIRC_FILEPATH` | Filepath for `.sentryclirc` configuration | `.sentryclirc` |
23-
| `SENTRYCLIRC_COM_MSG` | Commit message for `.sentryclirc` update | `Update .sentryclirc` |
24-
| `SENTRYCLIRC_MR_DESCRIPTION` | Description for Sentry CLI configuration MR | Custom template (see code) |
12+
| `DSN_MR_TITLE` | Title for DSN-related merge request | `[gitlab2sentry] Merge me...` |
13+
| `ENV` | The environment the application is running in | `production` |
2514
| `GITLAB_AUTHOR_EMAIL` | GitLab author email for merge requests | `[email protected]` |
2615
| `GITLAB_AUTHOR_NAME` | GitLab author name for merge requests | `Default Author` |
2716
| `GITLAB_GRAPHQL_PAGE_LENGTH` | Page length for GitLab GraphQL queries | `0` |
@@ -34,9 +23,20 @@ To configure the application, set the following environment variables:
3423
| `GITLAB_MR_LABEL_LIST` | Labels to assign to GitLab merge requests | `['sentry']` |
3524
| `GITLAB_PROJECT_CREATION_LIMIT` | Limit for creating GitLab projects | `30` |
3625
| `GITLAB_RMV_SRC_BRANCH` | Remove source branch after merge request | `True` |
26+
| `GITLAB_SIGNED_COMMIT` | Whether to use signed commits in GitLab | `False` |
3727
| `GITLAB_TOKEN` | GitLab access token | `default-token` |
3828
| `GITLAB_URL` | Base URL for GitLab service | `http://default-gitlab-url` |
39-
| `GITLAB_SIGNED_COMMIT` | Whether to use signed commits in GitLab | `False` |
29+
| `SENTRYCLIRC_BRANCH_NAME` | Branch name for Sentry CLI configuration changes | `auto_add_sentry` |
30+
| `SENTRYCLIRC_COM_MSG` | Commit message for `.sentryclirc` update | `Update .sentryclirc` |
31+
| `SENTRYCLIRC_FILEPATH` | Filepath for `.sentryclirc` configuration | `.sentryclirc` |
32+
| `SENTRYCLIRC_MR_CONTENT` | Merge request content for Sentry CLI configuration | Custom template (see code) |
33+
| `SENTRYCLIRC_MR_DESCRIPTION` | Description for Sentry CLI configuration MR | Custom template (see code) |
34+
| `SENTRYCLIRC_MR_TITLE` | Title for Sentry CLI configuration MR | `[gitlab2sentry] Merge me...` |
35+
| `SENTRY_DSN` | Sentry DSN for monitoring | `http://default.sentry.com` |
36+
| `SENTRY_ENV` | Sentry environment name | `production` |
37+
| `SENTRY_ORG_SLUG` | Organization slug for Sentry | `default_org` |
38+
| `SENTRY_TOKEN` | Authentication token for Sentry | `default-token` |
39+
| `SENTRY_URL` | Base URL for Sentry service | `http://default-sentry-url` |
4040

4141
To override any configuration, simply set the respective environment variable before running the application. For instance:
4242

gitlab2sentry/__init__.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
from slugify import slugify
77

88
from gitlab2sentry.exceptions import SentryProjectCreationFailed
9-
from gitlab2sentry.resources import settings, G2SProject, G2S_STATS, GRAPHQL_LIST_PROJECTS_QUERY, GRAPHQL_FETCH_PROJECT_QUERY
9+
from gitlab2sentry.resources import (
10+
G2S_STATS,
11+
GRAPHQL_FETCH_PROJECT_QUERY,
12+
GRAPHQL_LIST_PROJECTS_QUERY,
13+
G2SProject,
14+
settings,
15+
)
1016
from gitlab2sentry.utils import GitlabProvider, SentryProvider
1117

1218
logging.basicConfig(
@@ -31,7 +37,9 @@ def _get_gitlab_provider(self) -> GitlabProvider:
3137
return GitlabProvider(settings.gitlab_url, settings.gitlab_token)
3238

3339
def _get_sentry_provider(self) -> SentryProvider:
34-
return SentryProvider(settings.sentry_url, settings.sentry_token, settings.sentry_org_slug)
40+
return SentryProvider(
41+
settings.sentry_url, settings.sentry_token, settings.sentry_org_slug
42+
)
3543

3644
def _ensure_sentry_group(self, name: str) -> None:
3745
if name not in self.sentry_groups:
@@ -108,7 +116,9 @@ def _get_mr_states(
108116
):
109117
if not (sentryclirc_mr_state and sentryclirc_mr_state == "opened"):
110118
sentryclirc_mr_state = mr["state"]
111-
elif mr["title"] == settings.dsn_mr_title.format(project_name=project_name):
119+
elif mr["title"] == settings.dsn_mr_title.format(
120+
project_name=project_name
121+
):
112122
if not (dsn_mr_state and dsn_mr_state == "opened"):
113123
dsn_mr_state = mr["state"]
114124
else:
@@ -230,7 +240,7 @@ def _create_sentry_project(
230240
full_path: str,
231241
sentry_group_name: str,
232242
sentry_project_name: str,
233-
sentry_project_slug: str
243+
sentry_project_slug: str,
234244
) -> Optional[Dict[str, Any]]:
235245
try:
236246
return self.sentry_provider.get_or_create_project(
@@ -256,7 +266,7 @@ def _handle_g2s_project(
256266
self,
257267
g2s_project: G2SProject,
258268
sentry_group_name: str,
259-
custom_name: Optional[str] = None
269+
custom_name: Optional[str] = None,
260270
) -> bool:
261271
"""
262272
Creates sentry project for all given gitlab projects. It
@@ -304,7 +314,7 @@ def _handle_g2s_project(
304314
g2s_project.full_path,
305315
sentry_group_name,
306316
sentry_project_name,
307-
sentry_project_slug
317+
sentry_project_slug,
308318
)
309319

310320
# If Sentry fails to create project skip
@@ -348,9 +358,7 @@ def _handle_g2s_project(
348358
return False
349359

350360
def update(
351-
self,
352-
full_path: Optional[str] = None,
353-
custom_name: Optional[str] = None
361+
self, full_path: Optional[str] = None, custom_name: Optional[str] = None
354362
) -> None:
355363
"""
356364
args: full_path
@@ -372,7 +380,7 @@ def update(
372380
if g2s_project:
373381
sentry_group_name = g2s_project.group.split("/")[0].strip()
374382
self._handle_g2s_project(
375-
g2s_project, sentry_group_name, custom_name # type: ignore
383+
g2s_project, sentry_group_name, custom_name # type: ignore
376384
)
377385
else:
378386
logging.info(

gitlab2sentry/resources.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from collections import namedtuple
22
from typing import List, Tuple
3+
34
from pydantic import Field
45
from pydantic_settings import BaseSettings
56

67

7-
8-
98
class Settings(BaseSettings):
109
dsn_branch_name: str = Field("auto_add_sentry_dsn")
1110
dsn_mr_content: str = Field(
@@ -36,6 +35,7 @@ class Settings(BaseSettings):
3635
gitlab_mr_label_list: List[str] = Field(["sentry"])
3736
gitlab_project_creation_limit: int = Field(30)
3837
gitlab_rmv_src_branch: bool = Field(True)
38+
gitlab_signed_commit: bool = Field(False)
3939
gitlab_token: str = Field("default-token")
4040
gitlab_url: str = Field("http://default-gitlab-url")
4141
sentry_dsn: str = Field("http://default.sentry.com")

gitlab2sentry/utils/gitlab_provider.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
class GraphQLClient:
1818
def __init__(
19-
self, url: Optional[str] = settings.gitlab_url, token: Optional[str] = settings.gitlab_token
19+
self,
20+
url: Optional[str] = settings.gitlab_url,
21+
token: Optional[str] = settings.gitlab_token,
2022
):
2123
self._client = Client(
2224
transport=self._get_transport(url, token),
@@ -81,7 +83,9 @@ def project_list_query(
8183

8284
class GitlabProvider:
8385
def __init__(
84-
self, url: Optional[str] = settings.gitlab_url, token: Optional[str] = settings.gitlab_token
86+
self,
87+
url: Optional[str] = settings.gitlab_url,
88+
token: Optional[str] = settings.gitlab_token,
8589
) -> None:
8690
self.gitlab = self._get_gitlab(url, token)
8791
self._gql_client = GraphQLClient(url, token)
@@ -98,7 +102,9 @@ def _get_gitlab(self, url: Optional[str], token: Optional[str]) -> Gitlab:
98102

99103
def _get_update_limit(self) -> Optional[datetime]:
100104
if settings.gitlab_project_creation_limit:
101-
return datetime.now() - timedelta(days=settings.gitlab_project_creation_limit)
105+
return datetime.now() - timedelta(
106+
days=settings.gitlab_project_creation_limit
107+
)
102108
else:
103109
return None
104110

@@ -181,16 +187,21 @@ def _get_or_create_sentryclirc(
181187
full_path,
182188
)
183189
)
184-
f = project.files.create(
185-
{
186-
"author_email": settings.gitlab_author_email,
187-
"author_name": settings.gitlab_author_name,
188-
"branch": branch_name,
189-
"commit_message": settings.sentryclirc_com_msg,
190-
"content": content,
191-
"file_path": file_path,
192-
}
193-
)
190+
data = {
191+
"author_email": settings.gitlab_author_email,
192+
"author_name": settings.gitlab_author_name,
193+
"branch": branch_name,
194+
"commit_message": settings.sentryclirc_com_msg,
195+
"content": content,
196+
"file_path": file_path,
197+
}
198+
# When commit signing is enabled in GitLab (e.g. via pre-hook),
199+
# commit requires that the author information matches the signer identity
200+
# https://gitlab.com/gitlab-org/gitlab/-/merge_requests/150855
201+
if settings.gitlab_signed_commit:
202+
data.pop("author_email")
203+
data.pop("author_name")
204+
f = project.files.create(data=data)
194205

195206
def _get_default_mentions(self, project: Project) -> str:
196207
return ", ".join(

tests/conftest.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55

66
from gitlab2sentry import Gitlab2Sentry
77
from gitlab2sentry.resources import G2SProject, settings
8-
from gitlab2sentry.resources import (
9-
settings,
10-
G2SProject,
11-
)
128
from gitlab2sentry.utils.gitlab_provider import GitlabProvider, GraphQLClient
139
from gitlab2sentry.utils.sentry_provider import SentryProvider
1410

@@ -23,6 +19,7 @@
2319
"%Y-%m-%dT%H:%M:%SZ",
2420
)
2521

22+
2623
@pytest.fixture
2724
def g2s_fixture():
2825
yield Gitlab2Sentry()
@@ -109,11 +106,7 @@ def create_graphql_json_object(**kwargs):
109106
"rawTextBlob": settings.dsn_mr_content.format(
110107
sentry_url=settings.sentry_url,
111108
dsn=settings.sentry_dsn,
112-
<<<<<<< HEAD
113109
project_slug=TEST_PROJECT_NAME,
114-
=======
115-
project_slug=TEST_PROJECT_NAME
116-
>>>>>>> 438fae9 (refactor: use pydandic settings to handle configuration)
117110
),
118111
}
119112
else:
@@ -138,13 +131,9 @@ def create_graphql_json_object(**kwargs):
138131
if kwargs["dsn_mr_state"]:
139132
dsn_mr = {
140133
"id": "gid://gitlab/MergeRequest/0001",
141-
<<<<<<< HEAD
142134
"title": settings.dsn_mr_title.format(
143135
project_name=response_dict["node"]["name"]
144136
),
145-
=======
146-
"title": settings.dsn_mr_title.format(project_name=response_dict["node"]["name"]),
147-
>>>>>>> 438fae9 (refactor: use pydandic settings to handle configuration)
148137
"state": kwargs["dsn_mr_state"],
149138
}
150139
response_dict["node"]["mergeRequests"]["nodes"].append(dsn_mr)
@@ -361,3 +350,37 @@ def payload_sentry_project():
361350
sentryclirc_mr_state="merged",
362351
dsn_mr_state="merged",
363352
)
353+
354+
GRAPHQL_TEST_QUERY = {
355+
"name": "TEST_QUERY",
356+
"instance": "projects",
357+
"body": """
358+
{
359+
project(fullPath: "none") {
360+
id
361+
fullPath
362+
name
363+
createdAt
364+
mergeRequestsEnabled
365+
group {
366+
name
367+
}
368+
repository {
369+
blobs {
370+
nodes {
371+
name
372+
rawTextBlob
373+
}
374+
}
375+
}
376+
mergeRequests {
377+
nodes {
378+
id
379+
title
380+
state
381+
}
382+
}
383+
}
384+
}
385+
""",
386+
}

tests/test_gitlab_provider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ def test_project_list_query(gql_client_fixture, payload_new_project, mocker):
6464

6565
def test_get_gitlab(gitlab_provider_fixture):
6666
assert isinstance(
67-
gitlab_provider_fixture._get_gitlab(settings.gitlab_url, settings.gitlab_token), Gitlab
67+
gitlab_provider_fixture._get_gitlab(settings.gitlab_url, settings.gitlab_token),
68+
Gitlab,
6869
)
6970

7071

0 commit comments

Comments
 (0)