Skip to content

Commit 6dbc8ac

Browse files
tarekbadrshafrittoli
authored andcommitted
add repository obj
1 parent 6632fc6 commit 6dbc8ac

File tree

4 files changed

+70
-43
lines changed

4 files changed

+70
-43
lines changed

cli/cdevents/cli/repository.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
"""Module for cli repository commands."""
22
from __future__ import annotations
3-
4-
import os
53
from typing import List
6-
74
import click
85

96
from cdevents.cli.utils import add_disclaimer_text, print_function_args
107
from cdevents.cli.cdevents_command import CDeventsCommand
118

12-
from cdevents.core.events import Events
13-
from cdevents.core import event_type
9+
from cdevents.core.repository import Repository, RepositoryType
1410

1511
# pylint: disable=unused-argument
1612
def common_repository_options(function):
@@ -57,10 +53,10 @@ def created(
5753
data: List[str] = None,
5854
):
5955
print_function_args()
60-
e = Events()
61-
new_event = e.create_repository_event(event_type.RepositoryCreatedEventV1, id, name, url, data)
56+
repository = Repository(repository_type=RepositoryType.RepositoryCreatedEventV1, id=id, name=name, url=url)
57+
pipelinerun_event = repository.create_event(data)
6258
cdevents_command = CDeventsCommand()
63-
cdevents_command.run(new_event)
59+
cdevents_command.run(pipelinerun_event)
6460

6561

6662
@click.command(help=add_disclaimer_text("Repository Modified CloudEvent."))
@@ -72,10 +68,10 @@ def modified(
7268
data: List[str] = None,
7369
):
7470
print_function_args()
75-
e = Events()
76-
new_event = e.create_repository_event(event_type.RepositoryModifiedEventV1, id, name, url, data)
71+
repository = Repository(repository_type=RepositoryType.RepositoryModifiedEventV1, id=id, name=name, url=url)
72+
pipelinerun_event = repository.create_event(data)
7773
cdevents_command = CDeventsCommand()
78-
cdevents_command.run(new_event)
74+
cdevents_command.run(pipelinerun_event)
7975

8076

8177
@click.command(help=add_disclaimer_text("Repository Deleted CloudEvent."))
@@ -87,8 +83,8 @@ def deleted(
8783
data: List[str] = None,
8884
):
8985
print_function_args()
90-
e = Events()
91-
new_event = e.create_repository_event(event_type.RepositoryModifiedEventV1, id, name, url, data)
86+
repository = Repository(repository_type=RepositoryType.RepositoryDeletedEventV1, id=id, name=name, url=url)
87+
pipelinerun_event = repository.create_event(data)
9288
cdevents_command = CDeventsCommand()
93-
cdevents_command.run(new_event)
89+
cdevents_command.run(pipelinerun_event)
9490

core/cdevents/core/events.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@ def create_event(self, event_type: str, extensions:dict, data = {}) -> CloudEven
2222

2323
return event
2424

25-
def create_repository_event(self, event_type: str , id: str, name: str, url: str, data = {}) -> CloudEvent:
26-
"""Create repository event.
27-
"""
28-
29-
extensions = {
30-
"repositoryid": id,
31-
"repositoryname": name,
32-
"repositoryurl": url,
33-
}
34-
35-
event = self.create_event(event_type, extensions, data)
36-
37-
return event
3825

3926

4027
def create_service_event(self, event_type: str , envid: str, name: str, version: str, data = {}) -> CloudEvent:

core/cdevents/core/repository.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""repository"""
2+
3+
from enum import Enum
4+
from cdevents.core.events import Events
5+
6+
class RepositoryType(Enum):
7+
RepositoryCreatedEventV1 :str = "cd.repository.created.v1"
8+
RepositoryModifiedEventV1 :str = "cd.repository.modified.v1"
9+
RepositoryDeletedEventV1 :str = "cd.repository.deleted.v1"
10+
11+
12+
class Repository(Events):
13+
"""Repository."""
14+
15+
def __init__(self, repository_type: RepositoryType, id: str, name: str, url: str):
16+
"""Initializes class.
17+
"""
18+
self._event_type = repository_type
19+
self._id = id
20+
self._name = name
21+
self._url = url
22+
23+
def create_extensions(self):
24+
"""Create extensions.
25+
"""
26+
extensions = {
27+
"repositoryid": self._id,
28+
"repositoryname": self._name,
29+
"repositoryurl": self._url,
30+
}
31+
return extensions
32+
33+
def create_event(self, data: dict={}):
34+
"""Create Repository event.
35+
"""
36+
extensions = self.create_extensions()
37+
event = super().create_event(event_type=self._event_type.value, extensions=extensions, data=data)
38+
return event

core/tests/test_repository.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
from cdevents.core import event_type
22
import pytest
33

4-
from cdevents.core.events import Events
4+
from cdevents.core.repository import Repository, RepositoryType
55

66
@pytest.mark.unit
77
def test_repository_created():
8-
event = Events().create_repository_event(event_type.RepositoryCreatedEventV1, id="_id", name="_name", url="_url", data={"repository": "_repository"})
9-
assert event is not None
10-
assert event._attributes["type"] == event_type.RepositoryCreatedEventV1
11-
assert event._attributes["extensions"] == {"repositoryid": "_id", "repositoryname": "_name", "repositoryurl": "_url"}
12-
assert event.data == {"repository": "_repository"}
8+
repository = Repository(repository_type=RepositoryType.RepositoryCreatedEventV1, id="_id", name="_name", url="_url")
9+
repository_event = repository.create_event(data={"key1": "value1"})
10+
assert repository_event is not None
11+
assert repository_event._attributes["type"] == RepositoryType.RepositoryCreatedEventV1.value
12+
assert repository_event._attributes["extensions"] == {"repositoryid": "_id", "repositoryname": "_name", "repositoryurl": "_url"}
13+
assert repository_event.data == {"key1": "value1"}
14+
1315

1416
@pytest.mark.unit
1517
def test_repository_modified():
16-
event = Events().create_repository_event(event_type.RepositoryModifiedEventV1, id="_id", name="_name", url="_url", data={"repository": "_repository"})
17-
assert event is not None
18-
assert event._attributes["type"] == event_type.RepositoryModifiedEventV1
19-
assert event._attributes["extensions"] == {"repositoryid": "_id", "repositoryname": "_name", "repositoryurl": "_url"}
20-
assert event.data == {"repository": "_repository"}
18+
repository = Repository(repository_type=RepositoryType.RepositoryModifiedEventV1, id="_id", name="_name", url="_url")
19+
repository_event = repository.create_event(data={"key1": "value1"})
20+
assert repository_event is not None
21+
assert repository_event._attributes["type"] == RepositoryType.RepositoryModifiedEventV1.value
22+
assert repository_event._attributes["extensions"] == {"repositoryid": "_id", "repositoryname": "_name", "repositoryurl": "_url"}
23+
assert repository_event.data == {"key1": "value1"}
24+
2125

2226
@pytest.mark.unit
2327
def test_repository_deleted():
24-
event = Events().create_repository_event(event_type.RepositoryDeletedEventV1, id="_id", name="_name", url="_url", data={"repository": "_repository"})
25-
assert event is not None
26-
assert event._attributes["type"] == event_type.RepositoryDeletedEventV1
27-
assert event._attributes["extensions"] == {"repositoryid": "_id", "repositoryname": "_name", "repositoryurl": "_url"}
28-
assert event.data == {"repository": "_repository"}
28+
repository = Repository(repository_type=RepositoryType.RepositoryDeletedEventV1, id="_id", name="_name", url="_url")
29+
repository_event = repository.create_event(data={"key1": "value1"})
30+
assert repository_event is not None
31+
assert repository_event._attributes["type"] == RepositoryType.RepositoryDeletedEventV1.value
32+
assert repository_event._attributes["extensions"] == {"repositoryid": "_id", "repositoryname": "_name", "repositoryurl": "_url"}
33+
assert repository_event.data == {"key1": "value1"}
34+

0 commit comments

Comments
 (0)