Skip to content

Commit 40afbbc

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent f3e5794 commit 40afbbc

File tree

9 files changed

+133
-97
lines changed

9 files changed

+133
-97
lines changed

api/features/feature_external_resources/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,7 @@ def _handle_gitlab_after_save(self, state: str) -> None:
170170
self.feature.save()
171171

172172
feature_states: list[FeatureState] = []
173-
environments = Environment.objects.filter(
174-
project_id=self.feature.project_id
175-
)
173+
environments = Environment.objects.filter(project_id=self.feature.project_id)
176174
for environment in environments:
177175
q = Q(
178176
feature_id=self.feature_id,

api/features/feature_external_resources/views.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,14 @@ def list(self, request, *args, **kwargs) -> Response: # type: ignore[no-untyped
6969
)
7070
elif resource_type.startswith("GITLAB_"):
7171
try:
72+
import re as _re
73+
7274
from integrations.gitlab.client import (
7375
get_gitlab_issue_mr_title_and_state as get_gitlab_metadata,
7476
)
7577
from integrations.gitlab.models import (
7678
GitLabConfiguration,
7779
)
78-
import re as _re
7980

8081
feature_obj = get_object_or_404(
8182
Feature.objects.filter(id=self.kwargs["feature_pk"]),
@@ -86,10 +87,16 @@ def list(self, request, *args, **kwargs) -> Response: # type: ignore[no-untyped
8687
if gitlab_config and gitlab_config.gitlab_project_id:
8788
# Parse resource IID from URL
8889
if resource_type == "GITLAB_MR":
89-
match = _re.search(r"https?://[^/]+/([^/-]+(?:/[^/-]+)*)/-/merge_requests/(\d+)$", resource_url)
90+
match = _re.search(
91+
r"https?://[^/]+/([^/-]+(?:/[^/-]+)*)/-/merge_requests/(\d+)$",
92+
resource_url,
93+
)
9094
api_type = "merge_requests"
9195
else:
92-
match = _re.search(r"https?://[^/]+/([^/-]+(?:/[^/-]+)*)/-/(?:issues|work_items)/(\d+)$", resource_url)
96+
match = _re.search(
97+
r"https?://[^/]+/([^/-]+(?:/[^/-]+)*)/-/(?:issues|work_items)/(\d+)$",
98+
resource_url,
99+
)
93100
api_type = "issues"
94101

95102
if match:
@@ -106,7 +113,9 @@ def list(self, request, *args, **kwargs) -> Response: # type: ignore[no-untyped
106113

107114
return Response(data={"results": data})
108115

109-
def _create_gitlab_resource(self, request: Any, feature: Any, resource_type: str, *args: Any, **kwargs: Any) -> Response:
116+
def _create_gitlab_resource(
117+
self, request: Any, feature: Any, resource_type: str, *args: Any, **kwargs: Any
118+
) -> Response:
110119
from integrations.gitlab.models import GitLabConfiguration
111120

112121
try:
@@ -127,12 +136,16 @@ def _create_gitlab_resource(self, request: Any, feature: Any, resource_type: str
127136
if resource_type == "GITLAB_MR":
128137
pattern = r"https?://[^/]+/([^/-]+(?:/[^/-]+)*)/-/merge_requests/(\d+)$"
129138
else:
130-
pattern = r"https?://[^/]+/([^/-]+(?:/[^/-]+)*)/-/(?:issues|work_items)/(\d+)$"
139+
pattern = (
140+
r"https?://[^/]+/([^/-]+(?:/[^/-]+)*)/-/(?:issues|work_items)/(\d+)$"
141+
)
131142

132143
url_match = re.search(pattern, url)
133144
if url_match:
134145
_project_path, resource_iid = url_match.groups()
135-
api_resource_type = "merge_requests" if resource_type == "GITLAB_MR" else "issues"
146+
api_resource_type = (
147+
"merge_requests" if resource_type == "GITLAB_MR" else "issues"
148+
)
136149
if gitlab_config.tagging_enabled and gitlab_config.gitlab_project_id:
137150
label_gitlab_issue_mr(
138151
instance_url=gitlab_config.gitlab_instance_url,
@@ -149,7 +162,9 @@ def _create_gitlab_resource(self, request: Any, feature: Any, resource_type: str
149162
status=status.HTTP_400_BAD_REQUEST,
150163
)
151164

152-
def _create_github_resource(self, request: Any, feature: Any, resource_type: str, *args: Any, **kwargs: Any) -> Response:
165+
def _create_github_resource(
166+
self, request: Any, feature: Any, resource_type: str, *args: Any, **kwargs: Any
167+
) -> Response:
153168
github_configuration = (
154169
Organisation.objects.prefetch_related("github_config")
155170
.get(id=feature.project.organisation_id)
@@ -212,10 +227,14 @@ def create(self, request, *args, **kwargs): # type: ignore[no-untyped-def]
212227

213228
# Handle GitLab resources
214229
if resource_type in ("GITLAB_MR", "GITLAB_ISSUE"):
215-
return self._create_gitlab_resource(request, feature, resource_type, *args, **kwargs)
230+
return self._create_gitlab_resource(
231+
request, feature, resource_type, *args, **kwargs
232+
)
216233

217234
# Handle GitHub resources
218-
return self._create_github_resource(request, feature, resource_type, *args, **kwargs)
235+
return self._create_github_resource(
236+
request, feature, resource_type, *args, **kwargs
237+
)
219238

220239
def perform_update(self, serializer): # type: ignore[no-untyped-def]
221240
external_resource_id = int(self.kwargs["pk"])

api/integrations/gitlab/gitlab.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ def _get_tag_value_for_event(
6161

6262

6363
def tag_feature_per_gitlab_event(
64-
event_type: str, action: str, metadata: dict[str, Any], project_path: str,
64+
event_type: str,
65+
action: str,
66+
metadata: dict[str, Any],
67+
project_path: str,
6568
) -> None:
6669
web_url = metadata.get("web_url", "")
6770

@@ -263,9 +266,7 @@ def call_gitlab_task(
263266
url: str | None,
264267
feature_states: typing.Union[list[typing.Any], list[typing.Any]] | None,
265268
) -> None:
266-
gitlab_configuration = GitLabConfiguration.objects.get(
267-
project_id=project_id
268-
)
269+
gitlab_configuration = GitLabConfiguration.objects.get(project_id=project_id)
269270

270271
feature_data: GitLabData = generate_data(
271272
gitlab_configuration=gitlab_configuration,

api/integrations/gitlab/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
def gitlab_webhook_payload_is_valid(
2-
secret_token: str, gitlab_token_header: str | None,
2+
secret_token: str,
3+
gitlab_token_header: str | None,
34
) -> bool:
45
"""Verify that the webhook was sent from GitLab by comparing the secret token."""
56
if not gitlab_token_header:

api/integrations/gitlab/views.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@
5151
def gitlab_auth_required(func): # type: ignore[no-untyped-def]
5252
@wraps(func)
5353
def wrapper(request, project_pk): # type: ignore[no-untyped-def]
54-
if not GitLabConfiguration.has_gitlab_configuration(
55-
project_id=project_pk
56-
):
54+
if not GitLabConfiguration.has_gitlab_configuration(project_id=project_pk):
5755
return Response(
5856
data={
5957
"detail": "This Project doesn't have a valid GitLab Configuration"
@@ -110,7 +108,9 @@ def get_serializer_class(self): # type: ignore[no-untyped-def]
110108
def perform_create(self, serializer): # type: ignore[no-untyped-def]
111109
project_id = self.kwargs["project_pk"]
112110
serializer.save(project_id=project_id)
113-
if serializer.validated_data.get("tagging_enabled", False) and serializer.validated_data.get("gitlab_project_id"):
111+
if serializer.validated_data.get(
112+
"tagging_enabled", False
113+
) and serializer.validated_data.get("gitlab_project_id"):
114114
create_flagsmith_flag_label(
115115
instance_url=serializer.validated_data["gitlab_instance_url"],
116116
access_token=serializer.validated_data["access_token"],
@@ -121,9 +121,7 @@ def get_queryset(self): # type: ignore[no-untyped-def]
121121
if getattr(self, "swagger_fake_view", False):
122122
return GitLabConfiguration.objects.none()
123123

124-
return GitLabConfiguration.objects.filter(
125-
project_id=self.kwargs["project_pk"]
126-
)
124+
return GitLabConfiguration.objects.filter(project_id=self.kwargs["project_pk"])
127125

128126
def create(self, request, *args, **kwargs): # type: ignore[no-untyped-def]
129127
try:
@@ -165,7 +163,9 @@ def fetch_merge_requests(request, project_pk) -> Response: # type: ignore[no-un
165163
access_token=gitlab_config.access_token,
166164
params=query_serializer.validated_data,
167165
)
168-
return Response(data=data, content_type="application/json", status=status.HTTP_200_OK)
166+
return Response(
167+
data=data, content_type="application/json", status=status.HTTP_200_OK
168+
)
169169

170170

171171
@api_view(["GET"])
@@ -186,7 +186,9 @@ def fetch_issues(request, project_pk) -> Response: # type: ignore[no-untyped-de
186186
access_token=gitlab_config.access_token,
187187
params=query_serializer.validated_data,
188188
)
189-
return Response(data=data, content_type="application/json", status=status.HTTP_200_OK)
189+
return Response(
190+
data=data, content_type="application/json", status=status.HTTP_200_OK
191+
)
190192

191193

192194
@api_view(["GET"])
@@ -205,7 +207,9 @@ def fetch_projects(request, project_pk: int) -> Response | None: # type: ignore
205207
access_token=gitlab_config.access_token,
206208
params=query_serializer.validated_data,
207209
)
208-
return Response(data=data, content_type="application/json", status=status.HTTP_200_OK)
210+
return Response(
211+
data=data, content_type="application/json", status=status.HTTP_200_OK
212+
)
209213

210214

211215
@api_view(["GET"])
@@ -225,7 +229,9 @@ def fetch_project_members(request, project_pk) -> Response: # type: ignore[no-u
225229
access_token=gitlab_config.access_token,
226230
params=query_serializer.validated_data,
227231
)
228-
return Response(data=response, content_type="application/json", status=status.HTTP_200_OK)
232+
return Response(
233+
data=response, content_type="application/json", status=status.HTTP_200_OK
234+
)
229235

230236

231237
@api_view(["POST"])
@@ -279,7 +285,9 @@ def create_cleanup_issue(request, project_pk: int) -> Response: # type: ignore[
279285
url_parts = summary.repository_url.rstrip("/").split("/")
280286
repo_path = "/".join(url_parts[-2:]) # e.g. "group/project"
281287

282-
if not gitlab_config.project_name or not gitlab_config.project_name.endswith(repo_path):
288+
if not gitlab_config.project_name or not gitlab_config.project_name.endswith(
289+
repo_path
290+
):
283291
continue
284292

285293
if not gitlab_config.gitlab_project_id:

api/projects/urls.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
from integrations.datadog.views import DataDogConfigurationViewSet
2222
from integrations.gitlab.views import (
2323
GitLabConfigurationViewSet,
24-
create_cleanup_issue as gitlab_create_cleanup_issue,
25-
fetch_issues as gitlab_fetch_issues,
2624
fetch_merge_requests,
2725
fetch_project_members,
2826
fetch_projects,
2927
)
28+
from integrations.gitlab.views import (
29+
create_cleanup_issue as gitlab_create_cleanup_issue,
30+
)
31+
from integrations.gitlab.views import (
32+
fetch_issues as gitlab_fetch_issues,
33+
)
3034
from integrations.grafana.views import GrafanaProjectConfigurationViewSet
3135
from integrations.launch_darkly.views import LaunchDarklyImportRequestViewSet
3236
from integrations.new_relic.views import NewRelicConfigurationViewSet

api/tests/unit/integrations/gitlab/test_unit_gitlab_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
ProjectQueryParams,
2222
)
2323

24-
2524
INSTANCE_URL = "https://gitlab.example.com"
2625
ACCESS_TOKEN = "test-access-token"
2726

@@ -146,7 +145,9 @@ def test_create_gitlab_issue__valid_data__creates_issue() -> None:
146145

147146

148147
@responses.activate
149-
def test_get_gitlab_issue_mr_title_and_state__valid_resource__returns_metadata() -> None:
148+
def test_get_gitlab_issue_mr_title_and_state__valid_resource__returns_metadata() -> (
149+
None
150+
):
150151
# Given
151152
responses.add(
152153
responses.GET,
@@ -306,7 +307,9 @@ def test_label_gitlab_issue_mr__happy_path__adds_label() -> None:
306307
# ---------------------------------------------------------------
307308

308309

309-
def test_build_paginated_response__pagination_headers__returns_previous_and_next() -> None:
310+
def test_build_paginated_response__pagination_headers__returns_previous_and_next() -> (
311+
None
312+
):
310313
# Given
311314
mock_response = responses.Response(
312315
method=responses.GET,

0 commit comments

Comments
 (0)