Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 4da4e13

Browse files
Add private + owner to sync conditions for Shelter (#1076)
1 parent 872d89b commit 4da4e13

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

database/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def after_update_repo(mapper, connection, target: Repository):
7171
state = inspect(target)
7272

7373
for attr in state.attrs:
74-
if attr.key in ["name", "upload_token"]:
74+
if attr.key in ["name", "upload_token", "ownerid", "private"]:
7575
history = attr.history
7676
# Detects if there are changes and if said changes are different.
7777
# has_changes() is True when you update the an entry with the same value,

database/tests/unit/test_events.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import os
22

33
import database.events # noqa: F401
4-
from database.tests.factories import RepositoryFactory
4+
from database.tests.factories import OwnerFactory, RepositoryFactory
55

66

77
def test_shelter_repo_sync(dbsession, mock_configuration, mocker):
88
# this prevents the pubsub SDK from trying to load credentials
99
os.environ["PUBSUB_EMULATOR_HOST"] = "localhost"
10+
publish = mocker.patch("google.cloud.pubsub_v1.PublisherClient.publish")
1011

1112
mock_configuration.set_params(
1213
{
@@ -20,40 +21,45 @@ def test_shelter_repo_sync(dbsession, mock_configuration, mocker):
2021
}
2122
)
2223

23-
publish = mocker.patch("google.cloud.pubsub_v1.PublisherClient.publish")
24-
2524
# this triggers the publish via SQLAlchemy events (after_insert)
26-
repo = RepositoryFactory(repoid=91728376, name="test-123")
25+
repo = RepositoryFactory(
26+
repoid=91728376, name="test-123", owner=OwnerFactory(ownerid=123), private=False
27+
)
2728
dbsession.add(repo)
2829
dbsession.commit()
2930

3031
publish.assert_called_once_with(
3132
"projects/test-project-id/topics/test-topic-id",
3233
b'{"type": "repo", "sync": "one", "id": 91728376}',
3334
)
34-
35-
publish = mocker.patch("google.cloud.pubsub_v1.PublisherClient.publish")
35+
publish_calls = publish.call_args_list
3636

3737
# Synchronize object flush for history.deleted to be perceived by sqlalchemy
3838
dbsession.refresh(repo)
3939

4040
# this triggers the publish via SQLAlchemy events (after_update)
4141
repo.name = "test-456"
4242
dbsession.commit()
43+
dbsession.refresh(repo)
44+
assert len(publish_calls) == 2
4345

44-
# same name shouldn't trigger (after_update)
45-
repo.name = "test-456"
46+
# Does not trigger another publish with untracked field
47+
repo.message = "foo"
4648
dbsession.commit()
49+
dbsession.refresh(repo)
50+
assert len(publish_calls) == 2
4751

48-
# this wouldn't trigger the publish via SQLAlchemy events (after_update) since it's an unimportant attribute
49-
repo.activated = True
52+
# Triggers call when owner is changed
53+
repo.owner = OwnerFactory(ownerid=456)
5054
dbsession.commit()
55+
dbsession.refresh(repo)
56+
assert len(publish_calls) == 3
5157

52-
# this is from the first trigger
53-
publish.assert_called_once_with(
54-
"projects/test-project-id/topics/test-topic-id",
55-
b'{"type": "repo", "sync": "one", "id": 91728376}',
56-
)
58+
# Triggers call when private is changed
59+
repo.private = True
60+
dbsession.commit()
61+
dbsession.refresh(repo)
62+
assert len(publish_calls) == 4
5763

5864

5965
def test_repo_sync_when_shelter_disabled(dbsession, mock_configuration, mocker):

0 commit comments

Comments
 (0)