|
14 | 14 | label_github_issue_pr, |
15 | 15 | ) |
16 | 16 | from integrations.github.models import GitHubRepository |
| 17 | +from integrations.gitlab.models import GitLabConfiguration |
| 18 | +from integrations.gitlab.services import parse_project_path |
| 19 | +from integrations.gitlab.tasks import register_gitlab_webhook |
17 | 20 | from organisations.models import Organisation |
18 | 21 |
|
19 | | -from .models import FeatureExternalResource, ResourceType |
| 22 | +from .models import GITLAB_RESOURCE_TYPES, FeatureExternalResource, ResourceType |
20 | 23 | from .serializers import FeatureExternalResourceSerializer |
21 | 24 |
|
22 | 25 |
|
@@ -72,10 +75,16 @@ def list(self, request, *args, **kwargs) -> Response: # type: ignore[no-untyped |
72 | 75 | return Response(data={"results": data}) |
73 | 76 |
|
74 | 77 | def create(self, request, *args, **kwargs): # type: ignore[no-untyped-def] |
75 | | - if request.data.get("type") not in [ |
| 78 | + resource_type = request.data.get("type") |
| 79 | + |
| 80 | + # GitLab side effects run in ``perform_create`` below. |
| 81 | + if resource_type in GITLAB_RESOURCE_TYPES: |
| 82 | + return super().create(request, *args, **kwargs) |
| 83 | + |
| 84 | + if resource_type not in ( |
76 | 85 | ResourceType.GITHUB_ISSUE, |
77 | 86 | ResourceType.GITHUB_PR, |
78 | | - ]: |
| 87 | + ): |
79 | 88 | return super().create(request, *args, **kwargs) |
80 | 89 |
|
81 | 90 | feature = get_object_or_404( |
@@ -137,12 +146,21 @@ def create(self, request, *args, **kwargs): # type: ignore[no-untyped-def] |
137 | 146 |
|
138 | 147 | def perform_create(self, serializer: FeatureExternalResourceSerializer) -> None: # type: ignore[override] |
139 | 148 | resource = serializer.save() |
| 149 | + resource_type = ResourceType(resource.type) |
| 150 | + |
| 151 | + if resource_type in GITLAB_RESOURCE_TYPES: |
| 152 | + project_path = parse_project_path(resource.url) |
| 153 | + config = GitLabConfiguration.objects.filter( |
| 154 | + project=resource.feature.project, |
| 155 | + ).first() |
| 156 | + if config is not None and project_path is not None: |
| 157 | + register_gitlab_webhook.delay(args=(config.id, project_path)) |
140 | 158 |
|
141 | 159 | log_event_names: dict[ResourceType, tuple[str, str]] = { |
142 | 160 | ResourceType.GITLAB_ISSUE: ("gitlab", "issue.linked"), |
143 | 161 | ResourceType.GITLAB_MR: ("gitlab", "merge_request.linked"), |
144 | 162 | } |
145 | | - if (resource_type := ResourceType(resource.type)) in log_event_names: |
| 163 | + if resource_type in log_event_names: |
146 | 164 | logger_name, event_name = log_event_names[resource_type] |
147 | 165 | structlog.get_logger(logger_name).info( |
148 | 166 | event_name, |
|
0 commit comments