-
Notifications
You must be signed in to change notification settings - Fork 504
feat(GitLab): Add GitLab to Project Integrations #7239
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
Open
emyller
wants to merge
6
commits into
main
Choose a base branch
from
feat/gitlab-integration-credentials
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f746063
Introduce GitLab integration basics
emyller 9b5078f
Fix missing access token in REST contract
emyller cc3811b
Add GitLab to the Integrations tab
emyller 626f876
Update GitLab logo with latest official
emyller 1987f2d
Good linter
emyller c95b314
Better telemetry
emyller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class GitLabIntegrationConfig(AppConfig): | ||
| name = "integrations.gitlab" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Generated by Django 5.2.13 | ||
|
|
||
| import uuid | ||
|
|
||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ("projects", "0028_add_enforce_feature_owners_to_project"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="GitLabConfiguration", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.AutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ( | ||
| "deleted_at", | ||
| models.DateTimeField( | ||
| blank=True, | ||
| db_index=True, | ||
| default=None, | ||
| editable=False, | ||
| null=True, | ||
| ), | ||
| ), | ||
| ( | ||
| "uuid", | ||
| models.UUIDField( | ||
| default=uuid.uuid4, | ||
| editable=False, | ||
| unique=True, | ||
| ), | ||
| ), | ||
| ( | ||
| "gitlab_instance_url", | ||
| models.URLField(max_length=200), | ||
| ), | ||
| ( | ||
| "access_token", | ||
| models.CharField(max_length=300), | ||
| ), | ||
| ( | ||
| "project", | ||
| models.OneToOneField( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="gitlab_config", | ||
| to="projects.project", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "abstract": False, | ||
| }, | ||
| ), | ||
| ] |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from django.db import models | ||
|
|
||
| from core.models import SoftDeleteExportableModel | ||
|
|
||
|
|
||
| class GitLabConfiguration(SoftDeleteExportableModel): | ||
| project = models.OneToOneField( | ||
| "projects.Project", | ||
| on_delete=models.CASCADE, | ||
| related_name="gitlab_config", | ||
| ) | ||
| gitlab_instance_url = models.URLField(max_length=200) | ||
| access_token = models.CharField(max_length=300) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from typing import Any | ||
|
|
||
| from integrations.common.serializers import BaseProjectIntegrationModelSerializer | ||
| from integrations.gitlab.models import GitLabConfiguration | ||
|
|
||
| WRITE_ONLY_PLACEHOLDER = "write-only" | ||
|
|
||
|
|
||
| class GitLabConfigurationSerializer(BaseProjectIntegrationModelSerializer): | ||
| class Meta: | ||
| model = GitLabConfiguration | ||
| fields = ("id", "gitlab_instance_url", "access_token") | ||
|
|
||
| def to_representation(self, instance: GitLabConfiguration) -> dict[str, Any]: | ||
| data = super().to_representation(instance) | ||
| data["access_token"] = WRITE_ONLY_PLACEHOLDER | ||
| return data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import structlog | ||
|
|
||
| from integrations.common.views import ProjectIntegrationBaseViewSet | ||
| from integrations.gitlab.models import GitLabConfiguration | ||
| from integrations.gitlab.serializers import GitLabConfigurationSerializer | ||
|
|
||
| logger = structlog.get_logger("gitlab") | ||
|
|
||
|
|
||
| class GitLabConfigurationViewSet(ProjectIntegrationBaseViewSet): | ||
| serializer_class = GitLabConfigurationSerializer # type: ignore[assignment] | ||
| model_class = GitLabConfiguration # type: ignore[assignment] | ||
| pagination_class = None | ||
|
|
||
| def _log_for( | ||
| self, instance: GitLabConfiguration | ||
| ) -> structlog.typing.FilteringBoundLogger: | ||
| return logger.bind( # type: ignore[no-any-return] | ||
| project__id=instance.project.id, | ||
| organisation__id=instance.project.organisation_id, | ||
| ) | ||
|
|
||
| def perform_create(self, serializer: GitLabConfigurationSerializer) -> None: # type: ignore[override] | ||
| super().perform_create(serializer) | ||
| instance: GitLabConfiguration = serializer.instance # type: ignore[assignment] | ||
| self._log_for(instance).info( | ||
| "gitlab-configuration-created", | ||
| gitlab_instance_url=instance.gitlab_instance_url, | ||
| ) | ||
|
|
||
| def perform_update(self, serializer: GitLabConfigurationSerializer) -> None: # type: ignore[override] | ||
| super().perform_update(serializer) | ||
| instance: GitLabConfiguration = serializer.instance # type: ignore[assignment] | ||
| self._log_for(instance).info( | ||
| "gitlab-configuration-updated", | ||
| gitlab_instance_url=instance.gitlab_instance_url, | ||
| ) | ||
|
|
||
| def perform_destroy(self, instance: GitLabConfiguration) -> None: | ||
| log = self._log_for(instance) | ||
| super().perform_destroy(instance) | ||
| log.info("gitlab-configuration-deleted") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| import pytest | ||
| from pytest_structlog import StructuredLogCapture | ||
| from rest_framework import status | ||
| from rest_framework.test import APIClient | ||
|
|
||
| from integrations.gitlab.models import GitLabConfiguration | ||
| from projects.models import Project | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def gitlab_configuration(project: Project) -> GitLabConfiguration: | ||
| return GitLabConfiguration.objects.create( # type: ignore[no-any-return] | ||
| project=project, | ||
| gitlab_instance_url="https://gitlab.example.com", | ||
| access_token="glpat-test-token", | ||
| ) | ||
|
|
||
|
|
||
| def test_create_configuration__valid_data__persists_and_masks_token( | ||
| admin_client_new: APIClient, | ||
| project: Project, | ||
| log: StructuredLogCapture, | ||
| ) -> None: | ||
| # Given / When | ||
| response = admin_client_new.post( | ||
| f"/api/v1/projects/{project.id}/integrations/gitlab/", | ||
| data={ | ||
| "gitlab_instance_url": "https://gitlab.example.com", | ||
| "access_token": "glpat-xxxxxxxxxxxxxxxxxxxx", | ||
| }, | ||
| format="json", | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_201_CREATED | ||
| assert response.json()["access_token"] == "write-only" | ||
|
|
||
| config = GitLabConfiguration.objects.get(project=project) | ||
| assert config.gitlab_instance_url == "https://gitlab.example.com" | ||
| assert config.access_token == "glpat-xxxxxxxxxxxxxxxxxxxx" | ||
|
|
||
| assert log.events == [ | ||
| { | ||
| "event": "gitlab-configuration-created", | ||
| "level": "info", | ||
| "gitlab_instance_url": "https://gitlab.example.com", | ||
| "project__id": project.id, | ||
| "organisation__id": project.organisation_id, | ||
| }, | ||
| ] | ||
|
Comment on lines
+42
to
+50
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! nit: maybe use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
|
|
||
| def test_create_configuration__already_exists__returns_400( | ||
| admin_client_new: APIClient, | ||
| project: Project, | ||
| gitlab_configuration: GitLabConfiguration, | ||
| ) -> None: | ||
| # Given / When | ||
| response = admin_client_new.post( | ||
| f"/api/v1/projects/{project.id}/integrations/gitlab/", | ||
| data={ | ||
| "gitlab_instance_url": "https://gitlab.other.com", | ||
| "access_token": "glpat-another-token", | ||
| }, | ||
| format="json", | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
|
|
||
|
|
||
| def test_update_configuration__valid_data__persists_and_masks_token( | ||
| admin_client_new: APIClient, | ||
| project: Project, | ||
| gitlab_configuration: GitLabConfiguration, | ||
| log: StructuredLogCapture, | ||
| ) -> None: | ||
| # Given / When | ||
| response = admin_client_new.put( | ||
| f"/api/v1/projects/{project.id}/integrations/gitlab/{gitlab_configuration.id}/", | ||
| data={ | ||
| "gitlab_instance_url": "https://gitlab.updated.com", | ||
| "access_token": "glpat-updated-token", | ||
| }, | ||
| format="json", | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_200_OK | ||
| assert response.json()["access_token"] == "write-only" | ||
|
|
||
| gitlab_configuration.refresh_from_db() | ||
| assert gitlab_configuration.gitlab_instance_url == "https://gitlab.updated.com" | ||
| assert gitlab_configuration.access_token == "glpat-updated-token" | ||
|
|
||
| assert log.events == [ | ||
| { | ||
| "event": "gitlab-configuration-updated", | ||
| "level": "info", | ||
| "gitlab_instance_url": "https://gitlab.updated.com", | ||
| "project__id": project.id, | ||
| "organisation__id": project.organisation_id, | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| def test_delete_configuration__existing__soft_deletes( | ||
| admin_client_new: APIClient, | ||
| project: Project, | ||
| gitlab_configuration: GitLabConfiguration, | ||
| log: StructuredLogCapture, | ||
| ) -> None: | ||
| # Given / When | ||
| response = admin_client_new.delete( | ||
| f"/api/v1/projects/{project.id}/integrations/gitlab/{gitlab_configuration.id}/", | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_204_NO_CONTENT | ||
| assert not GitLabConfiguration.objects.filter(project=project).exists() | ||
|
|
||
| assert log.events == [ | ||
| { | ||
| "event": "gitlab-configuration-deleted", | ||
| "level": "info", | ||
| "project__id": project.id, | ||
| "organisation__id": project.organisation_id, | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| def test_list_configurations__existing__masks_token( | ||
| admin_client_new: APIClient, | ||
| project: Project, | ||
| gitlab_configuration: GitLabConfiguration, | ||
| ) -> None: | ||
| # Given / When | ||
| response = admin_client_new.get( | ||
| f"/api/v1/projects/{project.id}/integrations/gitlab/", | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_200_OK | ||
| results = response.json() | ||
| assert len(results) == 1 | ||
| assert results[0]["gitlab_instance_url"] == "https://gitlab.example.com" | ||
| assert results[0]["access_token"] == "write-only" | ||
|
|
||
|
|
||
| def test_create_configuration__non_admin__returns_403( | ||
| staff_client: APIClient, | ||
| project: Project, | ||
| ) -> None: | ||
| # Given / When | ||
| response = staff_client.post( | ||
| f"/api/v1/projects/{project.id}/integrations/gitlab/", | ||
| data={ | ||
| "gitlab_instance_url": "https://gitlab.example.com", | ||
| "access_token": "glpat-token", | ||
| }, | ||
| format="json", | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_403_FORBIDDEN | ||
|
|
||
|
|
||
| def test_delete_configuration__non_admin__returns_403( | ||
| staff_client: APIClient, | ||
| project: Project, | ||
| gitlab_configuration: GitLabConfiguration, | ||
| ) -> None: | ||
| # Given / When | ||
| response = staff_client.delete( | ||
| f"/api/v1/projects/{project.id}/integrations/gitlab/{gitlab_configuration.id}/", | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_403_FORBIDDEN | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: I guess there's nothing to be done here except making ProjectIntegrationBaseViewSet generic (which is I appreciate is way out of scope for this PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Precisely my thought too. Also the serialiser. Worth an issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#7253