-
Notifications
You must be signed in to change notification settings - Fork 524
feat(integrations): Azure DevOps models, serializer, configuration viewset (PR 2/N) #7622
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
Closed
asaphko
wants to merge
9
commits into
feat/azure-devops-01-resource-types
from
feat/azure-devops-02-models
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
295145f
docs(superpowers): add PR 2 implementation plan for Azure DevOps inte…
asaphko 3f04858
feat(integrations): scaffold the integrations.azure_devops Django app
asaphko 3af5fac
feat(integrations): add AzureDevOpsConfiguration model
asaphko 10d8a9f
style(tests): remove stale GWT scaffolding comment in test_models
asaphko ebcbc60
feat(integrations): add AzureDevOpsServiceHook model
asaphko 1e2431f
feat(integrations): add Azure DevOps configuration serializer
asaphko dd00607
feat(integrations): add Azure DevOps configuration viewset and URL wi…
asaphko 75cf752
style(tests): remove unused pytest import + ruff reformat
asaphko c26daeb
fix(integrations): drop redundant uuid redeclaration + add permission…
asaphko 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 AzureDevOpsIntegrationConfig(AppConfig): | ||
| name = "integrations.azure_devops" |
111 changes: 111 additions & 0 deletions
111
api/integrations/azure_devops/migrations/0001_initial.py
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,111 @@ | ||
| # Generated by Django 5.2.14 on 2026-05-28 11:57 | ||
|
|
||
| import django.db.models.deletion | ||
| import uuid | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ("projects", "0029_bump_default_project_limits"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="AzureDevOpsConfiguration", | ||
| 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), | ||
| ), | ||
| ("organisation_url", models.URLField(max_length=300)), | ||
| ("personal_access_token", models.CharField(max_length=300)), | ||
| ("labeling_enabled", models.BooleanField(default=False)), | ||
| ("tagging_enabled", models.BooleanField(default=False)), | ||
| ( | ||
| "project", | ||
| models.OneToOneField( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="azure_devops_config", | ||
| to="projects.project", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "abstract": False, | ||
| }, | ||
| ), | ||
| migrations.CreateModel( | ||
| name="AzureDevOpsServiceHook", | ||
| 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), | ||
| ), | ||
| ("ado_project_id", models.UUIDField()), | ||
| ("ado_project_name", models.CharField(max_length=200)), | ||
| ("event_type", models.CharField(max_length=64)), | ||
| ("subscription_id", models.UUIDField()), | ||
| ("secret", models.CharField(max_length=128)), | ||
| ("created_at", models.DateTimeField(auto_now_add=True)), | ||
| ( | ||
| "configuration", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="service_hooks", | ||
| to="azure_devops.azuredevopsconfiguration", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "constraints": [ | ||
| models.UniqueConstraint( | ||
| condition=models.Q(("deleted_at__isnull", True)), | ||
| fields=("configuration", "ado_project_id", "event_type"), | ||
| name="unique_azure_devops_service_hook_per_event", | ||
| ) | ||
| ], | ||
| }, | ||
| ), | ||
| ] |
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,38 @@ | ||
| from django.db import models | ||
|
|
||
| from core.models import SoftDeleteExportableModel | ||
|
|
||
|
|
||
| class AzureDevOpsConfiguration(SoftDeleteExportableModel): | ||
| project = models.OneToOneField( | ||
| "projects.Project", | ||
| on_delete=models.CASCADE, | ||
| related_name="azure_devops_config", | ||
| ) | ||
| organisation_url = models.URLField(max_length=300) | ||
| personal_access_token = models.CharField(max_length=300) | ||
| labeling_enabled = models.BooleanField(default=False) | ||
| tagging_enabled = models.BooleanField(default=False) | ||
|
|
||
|
|
||
| class AzureDevOpsServiceHook(SoftDeleteExportableModel): | ||
| configuration = models.ForeignKey( | ||
| AzureDevOpsConfiguration, | ||
| on_delete=models.CASCADE, | ||
| related_name="service_hooks", | ||
| ) | ||
| ado_project_id = models.UUIDField() | ||
| ado_project_name = models.CharField(max_length=200) | ||
| event_type = models.CharField(max_length=64) | ||
| subscription_id = models.UUIDField() | ||
| secret = models.CharField(max_length=128) | ||
| created_at = models.DateTimeField(auto_now_add=True) | ||
|
|
||
| class Meta: | ||
| constraints = [ | ||
| models.UniqueConstraint( | ||
| fields=["configuration", "ado_project_id", "event_type"], | ||
| name="unique_azure_devops_service_hook_per_event", | ||
| condition=models.Q(deleted_at__isnull=True), | ||
| ), | ||
| ] |
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,23 @@ | ||
| from typing import Any | ||
|
|
||
| from integrations.azure_devops.models import AzureDevOpsConfiguration | ||
| from integrations.common.serializers import BaseProjectIntegrationModelSerializer | ||
|
|
||
| WRITE_ONLY_PLACEHOLDER = "write-only" | ||
|
|
||
|
|
||
| class AzureDevOpsConfigurationSerializer(BaseProjectIntegrationModelSerializer): | ||
| class Meta: | ||
| model = AzureDevOpsConfiguration | ||
| fields = ( | ||
| "id", | ||
| "organisation_url", | ||
| "personal_access_token", | ||
| "labeling_enabled", | ||
| "tagging_enabled", | ||
| ) | ||
|
|
||
| def to_representation(self, instance: AzureDevOpsConfiguration) -> dict[str, Any]: | ||
| data = super().to_representation(instance) | ||
| data["personal_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,5 @@ | ||
| from integrations.azure_devops.views.configuration import ( | ||
| AzureDevOpsConfigurationViewSet, | ||
| ) | ||
|
|
||
| __all__ = ["AzureDevOpsConfigurationViewSet"] |
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,43 @@ | ||
| import structlog | ||
| from structlog.typing import FilteringBoundLogger | ||
|
|
||
| from integrations.azure_devops.models import AzureDevOpsConfiguration | ||
| from integrations.azure_devops.serializers import ( | ||
| AzureDevOpsConfigurationSerializer, | ||
| ) | ||
| from integrations.common.views import ProjectIntegrationBaseViewSet | ||
|
|
||
| logger = structlog.get_logger("azure_devops") | ||
|
|
||
|
|
||
| class AzureDevOpsConfigurationViewSet(ProjectIntegrationBaseViewSet): | ||
| serializer_class = AzureDevOpsConfigurationSerializer # type: ignore[assignment] | ||
| model_class = AzureDevOpsConfiguration # type: ignore[assignment] | ||
| pagination_class = None | ||
|
|
||
| def _log_for(self, config: AzureDevOpsConfiguration) -> FilteringBoundLogger: | ||
| return logger.bind( # type: ignore[no-any-return] | ||
| organisation__id=config.project.organisation_id, | ||
| project__id=config.project.id, | ||
| ) | ||
|
|
||
| def perform_create(self, serializer: AzureDevOpsConfigurationSerializer) -> None: # type: ignore[override] | ||
| super().perform_create(serializer) | ||
| instance: AzureDevOpsConfiguration = serializer.instance # type: ignore[assignment] | ||
| self._log_for(instance).info( | ||
| "configuration.created", | ||
| ado__organisation__url=instance.organisation_url, | ||
| ) | ||
|
|
||
| def perform_update(self, serializer: AzureDevOpsConfigurationSerializer) -> None: # type: ignore[override] | ||
| super().perform_update(serializer) | ||
| instance: AzureDevOpsConfiguration = serializer.instance # type: ignore[assignment] | ||
| self._log_for(instance).info( | ||
| "configuration.updated", | ||
| ado__organisation__url=instance.organisation_url, | ||
| ) | ||
|
|
||
| def perform_destroy(self, instance: AzureDevOpsConfiguration) -> None: | ||
| log = self._log_for(instance) | ||
| super().perform_destroy(instance) | ||
| log.info("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,13 @@ | ||
| import pytest | ||
|
|
||
| from integrations.azure_devops.models import AzureDevOpsConfiguration | ||
| from projects.models import Project | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def azure_devops_configuration(project: Project) -> AzureDevOpsConfiguration: | ||
| return AzureDevOpsConfiguration.objects.create( # type: ignore[no-any-return] | ||
| project=project, | ||
| organisation_url="https://dev.azure.com/test-org", | ||
| personal_access_token="ado-test-token", | ||
| ) |
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,12 @@ | ||
| from django.apps import apps | ||
|
|
||
|
|
||
| def test_azure_devops_app__django_registry__contains_config() -> None: | ||
| # Given | ||
| app_label = "azure_devops" | ||
|
|
||
| # When | ||
| config = apps.get_app_config(app_label) | ||
|
|
||
| # Then | ||
| assert config.name == "integrations.azure_devops" |
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.
Critical Security & Correctness Issue: Token Overwrite Bug
When a client retrieves the configuration, the
personal_access_tokenis masked with the placeholder value"write-only"into_representation.However, if the client subsequently performs an update (e.g., a
PUTrequest to togglelabeling_enabledortagging_enabled) and sends back the masked payload, the serializer will validate and save the literal string"write-only"as the new personal access token, destroying the actual token in the database.To prevent this, we must override
validate_personal_access_tokento check if the incoming value is the placeholder"write-only". If it is, and we are updating an existing instance, we should preserve the existing token.