Skip to content

Commit b4874c1

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

9 files changed

Lines changed: 62 additions & 42 deletions

File tree

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: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ def list(self, request, *args, **kwargs) -> Response: # type: ignore[no-untyped
6868
)
6969
elif resource_type.startswith("GITLAB_"):
7070
try:
71+
import re as _re
72+
7173
from integrations.gitlab.client import (
7274
get_gitlab_issue_mr_title_and_state as get_gitlab_metadata,
7375
)
7476
from integrations.gitlab.models import (
7577
GitLabConfiguration,
7678
)
77-
import re as _re
7879

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

94101
if match:
@@ -126,12 +133,16 @@ def _create_gitlab_resource(self, request, feature, resource_type, *args, **kwar
126133
if resource_type == "GITLAB_MR":
127134
pattern = r"https?://[^/]+/([^/-]+(?:/[^/-]+)*)/-/merge_requests/(\d+)$"
128135
else:
129-
pattern = r"https?://[^/]+/([^/-]+(?:/[^/-]+)*)/-/(?:issues|work_items)/(\d+)$"
136+
pattern = (
137+
r"https?://[^/]+/([^/-]+(?:/[^/-]+)*)/-/(?:issues|work_items)/(\d+)$"
138+
)
130139

131140
url_match = re.search(pattern, url)
132141
if url_match:
133142
_project_path, resource_iid = url_match.groups()
134-
api_resource_type = "merge_requests" if resource_type == "GITLAB_MR" else "issues"
143+
api_resource_type = (
144+
"merge_requests" if resource_type == "GITLAB_MR" else "issues"
145+
)
135146
if gitlab_config.tagging_enabled and gitlab_config.gitlab_project_id:
136147
label_gitlab_issue_mr(
137148
instance_url=gitlab_config.gitlab_instance_url,
@@ -211,10 +222,14 @@ def create(self, request, *args, **kwargs): # type: ignore[no-untyped-def]
211222

212223
# Handle GitLab resources
213224
if resource_type in ("GITLAB_MR", "GITLAB_ISSUE"):
214-
return self._create_gitlab_resource(request, feature, resource_type, *args, **kwargs)
225+
return self._create_gitlab_resource(
226+
request, feature, resource_type, *args, **kwargs
227+
)
215228

216229
# Handle GitHub resources
217-
return self._create_github_resource(request, feature, resource_type, *args, **kwargs)
230+
return self._create_github_resource(
231+
request, feature, resource_type, *args, **kwargs
232+
)
218233

219234
def perform_update(self, serializer): # type: ignore[no-untyped-def]
220235
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/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ class GitLabConfiguration(LifecycleModelMixin, SoftDeleteExportableModel): # ty
5454

5555
@staticmethod
5656
def has_gitlab_configuration(project_id: int) -> bool:
57-
return GitLabConfiguration.objects.filter(
58-
project_id=project_id
59-
).exists()
57+
return GitLabConfiguration.objects.filter(project_id=project_id).exists()
6058

6159
class Meta:
6260
constraints = [

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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
from integrations.gitlab.client import (
44
build_request_headers,
5-
create_flagsmith_flag_label,
65
create_gitlab_issue,
76
fetch_gitlab_projects,
87
fetch_search_gitlab_resource,
98
get_gitlab_issue_mr_title_and_state,
10-
label_gitlab_issue_mr,
119
post_comment_to_gitlab,
1210
)
1311
from integrations.gitlab.dataclasses import IssueQueryParams, PaginatedQueryParams
1412

15-
1613
INSTANCE_URL = "https://gitlab.example.com"
1714
ACCESS_TOKEN = "test-access-token"
1815

@@ -137,7 +134,9 @@ def test_create_gitlab_issue__valid_data__creates_issue() -> None:
137134

138135

139136
@responses.activate
140-
def test_get_gitlab_issue_mr_title_and_state__valid_resource__returns_metadata() -> None:
137+
def test_get_gitlab_issue_mr_title_and_state__valid_resource__returns_metadata() -> (
138+
None
139+
):
141140
# Given
142141
responses.add(
143142
responses.GET,

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ def test_create_gitlab_configuration__valid_data__returns_201(
7070
response = admin_client_new.post(url, data)
7171
# Then
7272
assert response.status_code == status.HTTP_201_CREATED
73-
assert GitLabConfiguration.objects.filter(
74-
project=project
75-
).exists()
73+
assert GitLabConfiguration.objects.filter(project=project).exists()
7674

7775

7876
def test_create_gitlab_configuration__duplicate__returns_400(
@@ -114,9 +112,7 @@ def test_delete_gitlab_configuration__valid_configuration__returns_204(
114112
response = admin_client_new.delete(url)
115113
# Then
116114
assert response.status_code == status.HTTP_204_NO_CONTENT
117-
assert not GitLabConfiguration.objects.filter(
118-
id=gitlab_configuration.id
119-
).exists()
115+
assert not GitLabConfiguration.objects.filter(id=gitlab_configuration.id).exists()
120116

121117

122118
def test_delete_gitlab_configuration__has_external_resources__removes_them(

0 commit comments

Comments
 (0)