Skip to content

Commit

Permalink
feat: Emit pubsub event when org token is updated (#241)
Browse files Browse the repository at this point in the history
Co-authored-by: Trent Schmidt <[email protected]>
  • Loading branch information
scott-codecov and trent-codecov authored Nov 17, 2023
1 parent 81b13ba commit 63bcae7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
36 changes: 35 additions & 1 deletion codecov_auth/signals.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import json
import logging
from datetime import datetime

from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from google.cloud import pubsub_v1

from codecov_auth.models import Owner, OwnerProfile
from codecov_auth.models import OrganizationLevelToken, Owner, OwnerProfile


@receiver(post_save, sender=Owner)
Expand All @@ -13,3 +16,34 @@ def create_owner_profile_when_owner_is_created(
):
if created:
return OwnerProfile.objects.create(owner_id=instance.ownerid)


_pubsub_publisher = None


def _get_pubsub_publisher():
global _pubsub_publisher
if not _pubsub_publisher:
_pubsub_publisher = pubsub_v1.PublisherClient()
return _pubsub_publisher


@receiver(
post_save, sender=OrganizationLevelToken, dispatch_uid="shelter_sync_org_token"
)
def update_repository(sender, instance: OrganizationLevelToken, **kwargs):
pubsub_project_id = settings.SHELTER_PUBSUB_PROJECT_ID
topic_id = settings.SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID
if pubsub_project_id and topic_id:
publisher = _get_pubsub_publisher()
topic_path = publisher.topic_path(pubsub_project_id, topic_id)
publisher.publish(
topic_path,
json.dumps(
{
"type": "org_token",
"sync": "one",
"id": instance.id,
}
).encode("utf-8"),
)
26 changes: 26 additions & 0 deletions codecov_auth/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os

import pytest
from django.test import override_settings

from codecov_auth.tests.factories import OrganizationLevelTokenFactory


@override_settings(
SHELTER_PUBSUB_PROJECT_ID="test-project-id",
SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID="test-topic-id",
)
@pytest.mark.django_db
def test_shelter_org_token_sync(mocker):
# this prevents the pubsub SDK from trying to load credentials
os.environ["PUBSUB_EMULATOR_HOST"] = "localhost"

publish = mocker.patch("google.cloud.pubsub_v1.PublisherClient.publish")

# this triggers the publish via Django signals
OrganizationLevelTokenFactory(id=91728376)

publish.assert_called_once_with(
"projects/test-project-id/topics/test-topic-id",
b'{"type": "org_token", "sync": "one", "id": 91728376}',
)
11 changes: 9 additions & 2 deletions core/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ def _get_pubsub_publisher():


@receiver(post_save, sender=Repository, dispatch_uid="shelter_sync_repo")
def update_repository(sender, instance, **kwargs):
def update_repository(sender, instance: Repository, **kwargs):
pubsub_project_id = settings.SHELTER_PUBSUB_PROJECT_ID
topic_id = settings.SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID
if pubsub_project_id and topic_id:
publisher = _get_pubsub_publisher()
topic_path = publisher.topic_path(pubsub_project_id, topic_id)
publisher.publish(
topic_path, json.dumps({"sync": instance.repoid}).encode("utf-8")
topic_path,
json.dumps(
{
"type": "repo",
"sync": "one",
"id": instance.repoid,
}
).encode("utf-8"),
)
3 changes: 2 additions & 1 deletion core/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ def test_shelter_repo_sync(mocker):
RepositoryFactory(repoid=91728376)

publish.assert_called_once_with(
"projects/test-project-id/topics/test-topic-id", b'{"sync": 91728376}'
"projects/test-project-id/topics/test-topic-id",
b'{"type": "repo", "sync": "one", "id": 91728376}',
)

0 comments on commit 63bcae7

Please sign in to comment.