Skip to content

Commit e02ed75

Browse files
tarekbadrshafrittoli
authored andcommitted
add build and env obj
1 parent f1f61d7 commit e02ed75

File tree

12 files changed

+141
-119
lines changed

12 files changed

+141
-119
lines changed

cli/cdevents/cli/build.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from cdevents.cli.utils import add_disclaimer_text, print_function_args
1010
from cdevents.cli.cdevents_command import CDeventsCommand
1111

12-
from cdevents.core.events import Events
13-
from cdevents.core import event_type
12+
from cdevents.core.build import Build, BuildType
1413

1514
# pylint: disable=unused-argument
1615
def common_build_options(function):
@@ -58,10 +57,10 @@ def started(
5857
data: List[str] = None,
5958
):
6059
print_function_args()
61-
e = Events()
62-
new_event = e.create_build_event(event_type.BuildStartedEventV1, id, name, artifact, data)
60+
build = Build(build_type=BuildType.BuildStartedEventV1, id=id, name=name, artifact=artifact)
61+
build_event = build.create_event(data)
6362
cdevents_command = CDeventsCommand()
64-
cdevents_command.run(new_event)
63+
cdevents_command.run(build_event)
6564

6665
@click.command(help=add_disclaimer_text("Build Finished CloudEvent."))
6766
@common_build_options
@@ -72,10 +71,10 @@ def finished(
7271
data: List[str] = None,
7372
):
7473
print_function_args()
75-
e = Events()
76-
new_event = e.create_build_event(event_type.BuildFinishedEventV1, id, name, artifact, data)
74+
build = Build(build_type=BuildType.BuildFinishedEventV1, id=id, name=name, artifact=artifact)
75+
build_event = build.create_event(data)
7776
cdevents_command = CDeventsCommand()
78-
cdevents_command.run(new_event)
77+
cdevents_command.run(build_event)
7978

8079
@click.command(help=add_disclaimer_text("PipelineRun Queued CloudEvent."))
8180
@common_build_options
@@ -86,7 +85,7 @@ def queued(
8685
data: List[str] = None,
8786
):
8887
print_function_args()
89-
e = Events()
90-
new_event = e.create_build_event(event_type.BuildQueuedEventV1, id, name, artifact, data)
88+
build = Build(build_type=BuildType.BuildQueuedEventV1, id=id, name=name, artifact=artifact)
89+
build_event = build.create_event(data)
9190
cdevents_command = CDeventsCommand()
92-
cdevents_command.run(new_event)
91+
cdevents_command.run(build_event)

cli/cdevents/cli/env.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from cdevents.cli.utils import add_disclaimer_text, print_function_args
1010
from cdevents.cli.cdevents_command import CDeventsCommand
1111

12-
from cdevents.core.events import Events
13-
from cdevents.core import event_type
12+
from cdevents.core.env import Env, EnvType
1413

1514
# pylint: disable=unused-argument
1615
def common_env_options(function):
@@ -57,10 +56,10 @@ def created(
5756
data: List[str] = None,
5857
):
5958
print_function_args()
60-
e = Events()
61-
new_event = e.create_environment_event(event_type.EnvironmentCreatedEventV1, id, name, repo, data)
59+
env = Env(build_type=EnvType.EnvironmentCreatedEventV1, id=id, name=name, repo=repo)
60+
env_event = env.create_event(data)
6261
cdevents_command = CDeventsCommand()
63-
cdevents_command.run(new_event)
62+
cdevents_command.run(env_event)
6463

6564

6665
@click.command(help=add_disclaimer_text("Environment Deleted CloudEvent."))
@@ -72,10 +71,10 @@ def deleted(
7271
data: List[str] = None,
7372
):
7473
print_function_args()
75-
e = Events()
76-
new_event = e.create_environment_event(event_type.EnvironmentDeletedEventV1, id, name, repo, data)
74+
env = Env(env_type=EnvType.EnvironmentDeletedEventV1, id=id, name=name, repo=repo)
75+
env_event = env.create_event(data)
7776
cdevents_command = CDeventsCommand()
78-
cdevents_command.run(new_event)
77+
cdevents_command.run(env_event)
7978

8079
@click.command(help=add_disclaimer_text("Environment Modified CloudEvent."))
8180
@common_env_options
@@ -86,7 +85,7 @@ def modified(
8685
data: List[str] = None,
8786
):
8887
print_function_args()
89-
e = Events()
90-
new_event = e.create_environment_event(event_type.EnvironmentModifiedEventV1, id, name, repo, data)
88+
env = Env(env_type=EnvType.EnvironmentModifiedEventV1, id=id, name=name, repo=repo)
89+
env_event = env.create_event(data)
9190
cdevents_command = CDeventsCommand()
92-
cdevents_command.run(new_event)
91+
cdevents_command.run(env_event)

core/cdevents/core/artifact.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ArtifactType(Enum):
99

1010

1111
class Artifact(Events):
12-
"""Events."""
12+
"""Artifact."""
1313

1414
def __init__(self, artifact_type: ArtifactType, id: str, name: str, version: str):
1515
"""Initializes class.

core/cdevents/core/branch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class BranchType(Enum):
99

1010

1111
class Branch(Events):
12-
"""Events."""
12+
"""Brach."""
1313

1414
def __init__(self, branch_type: BranchType, id: str, name: str, repoid: str):
1515
"""Initializes class.

core/cdevents/core/build.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""build"""
2+
3+
from enum import Enum
4+
from cdevents.core.events import Events
5+
6+
class BuildType(Enum):
7+
BuildStartedEventV1 :str = "cd.build.started.v1"
8+
BuildQueuedEventV1 :str = "cd.build.queued.v1"
9+
BuildFinishedEventV1 :str = "cd.build.finished.v1"
10+
11+
12+
class Build(Events):
13+
"""build."""
14+
15+
def __init__(self, build_type: BuildType, id: str, name: str, artifact: str):
16+
"""Initializes class.
17+
"""
18+
self._event_type = build_type
19+
self._id = id
20+
self._name = name
21+
self._artifact = artifact
22+
23+
def create_extensions(self):
24+
"""Create extensions.
25+
"""
26+
extensions = {
27+
"buildid": self._id,
28+
"buildname": self._name,
29+
"buildartifactid": self._artifact,
30+
}
31+
return extensions
32+
33+
def create_event(self, data: dict={}):
34+
"""Create build 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/cdevents/core/env.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""env"""
2+
3+
from enum import Enum
4+
from cdevents.core.events import Events
5+
6+
class EnvType(Enum):
7+
EnvironmentCreatedEventV1 :str = "cd.environment.created.v1"
8+
EnvironmentModifiedEventV1 :str = "cd.environment.modified.v1"
9+
EnvironmentDeletedEventV1 :str = "cd.environment.deleted.v1"
10+
11+
12+
class Env(Events):
13+
"""Env."""
14+
15+
def __init__(self, env_type: EnvType, id: str, name: str, repo: str):
16+
"""Initializes class.
17+
"""
18+
self._event_type = env_type
19+
self._id = id
20+
self._name = name
21+
self._repo = repo
22+
23+
def create_extensions(self):
24+
"""Create extensions.
25+
"""
26+
extensions = {
27+
"envId": self._id,
28+
"envname": self._name,
29+
"envrepourl": self._repo,
30+
}
31+
return extensions
32+
33+
def create_event(self, data: dict={}):
34+
"""Create env 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/cdevents/core/event_type.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,13 @@
22

33
# pylint: TODO:
44

5-
# # Artifact Events
6-
# ArtifactPackagedEventV1 :str = "cd.artifact.packaged.v1"
7-
# ArtifactPublishedEventV1 :str = "cd.artifact.published.v1"
8-
9-
# Branch Events
10-
BranchCreatedEventV1 :str = "cd.repository.branch.created.v1"
11-
BranchDeletedEventV1 :str = "cd.repository.branch.deleted.v1"
12-
13-
# Build Events
14-
BuildStartedEventV1 :str = "cd.build.started.v1"
15-
BuildQueuedEventV1 :str = "cd.build.queued.v1"
16-
BuildFinishedEventV1 :str = "cd.build.finished.v1"
17-
185
# Change Events
196
ChangeCreatedEventV1 :str = "cd.repository.change.created.v1"
207
ChangeUpdatedEventV1 :str = "cd.repository.change.updated.v1"
218
ChangeReviewedEventV1 :str = "cd.repository.change.reviewed.v1"
229
ChangeMergedEventV1 :str = "cd.repository.change.merged.v1"
2310
ChangeAbandonedEventV1 :str = "cd.repository.change.abandoned.v1"
2411

25-
# Environment Events
26-
EnvironmentCreatedEventV1 :str = "cd.environment.created.v1"
27-
EnvironmentModifiedEventV1 :str = "cd.environment.modified.v1"
28-
EnvironmentDeletedEventV1 :str = "cd.environment.deleted.v1"
29-
3012
# PipelineRun events
3113
PipelineRunStartedEventV1 :str = "cd.pipelinerun.started.v1"
3214
PipelineRunFinishedEventV1 :str = "cd.pipelinerun.finished.v1"

core/cdevents/core/events.py

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

2323
return event
2424

25-
# def create_branch_event(self, event_type: str , id: str, name: str, repoid: str, data = {}) -> CloudEvent:
26-
# """Create branch event.
27-
# """
28-
29-
# extensions = {
30-
# "branchid": id,
31-
# "branchname": name,
32-
# "branchrepositoryid": repoid,
33-
# }
34-
# event = self.create_event(event_type, extensions, data)
35-
36-
# return event
37-
38-
39-
def create_build_event(self, event_type: str , id: str, name: str, artifact: str, data = {}) -> CloudEvent:
40-
"""Create build event.
41-
"""
42-
43-
extensions = {
44-
"buildid": id,
45-
"buildname": name,
46-
"buildartifactid": artifact,
47-
}
48-
event = self.create_event(event_type, extensions, data)
49-
50-
return event
51-
52-
def create_environment_event(self, event_type: str , id: str, name: str, repo: str, data = {}) -> CloudEvent:
53-
"""Create environment event.
54-
"""
55-
56-
extensions = {
57-
"envId": id,
58-
"envname": name,
59-
"envrepourl": repo,
60-
}
61-
event = self.create_event(event_type, extensions, data)
62-
63-
return event
64-
6525
def create_pipelinerun_event(self, event_type: str , id: str, name: str, status: str, url: str, errors: str, data = {}) -> CloudEvent:
6626
"""Create pipelinerun event.
6727
"""

core/tests/test_artifact.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def test_artifact_packaged_v1():
77
artifact = Artifact(artifact_type=ArtifactType.ArtifactPackagedEventV1, id="_id", name="_name", version="_version")
88
artifact_event = artifact.create_event(data={"key1": "value1"})
99
assert artifact_event is not None
10-
assert artifact_event._attributes["type"] == "cd.artifact.packaged.v1"
10+
assert artifact_event._attributes["type"] == ArtifactType.ArtifactPackagedEventV1.value
1111
assert artifact_event._attributes["extensions"] == {"artifactid": "_id", "artifactname": "_name", "artifactversion": "_version"}
1212
assert artifact_event.data == {"key1": "value1"}
1313

@@ -16,7 +16,7 @@ def test_artifact_published_v1():
1616
artifact = Artifact(artifact_type=ArtifactType.ArtifactPublishedEventV1, id="_id", name="_name", version="_version")
1717
artifact_event = artifact.create_event(data={"key1": "value1"})
1818
assert artifact_event is not None
19-
assert artifact_event._attributes["type"] == "cd.artifact.published.v1"
19+
assert artifact_event._attributes["type"] ==ArtifactType.ArtifactPublishedEventV1.value
2020
assert artifact_event._attributes["extensions"] == {"artifactid": "_id", "artifactname": "_name", "artifactversion": "_version"}
2121
assert artifact_event.data == {"key1": "value1"}
2222

core/tests/test_branch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def test_repository_branch_created():
77
branch = Branch(branch_type=BranchType.BranchCreatedEventV1, id="_id", name="_name", repoid="_repoid")
88
branch_event = branch.create_event(data={"key1": "value1"})
99
assert branch_event is not None
10-
assert branch_event._attributes["type"] == "cd.repository.branch.created.v1"
10+
assert branch_event._attributes["type"] == BranchType.BranchCreatedEventV1.value
1111
assert branch_event._attributes["extensions"] == {"branchid": "_id", "branchname": "_name", "branchrepositoryid": "_repoid"}
1212
assert branch_event.data == {"key1": "value1"}
1313

@@ -16,6 +16,6 @@ def test_repository_branch_deleted():
1616
branch = Branch(branch_type=BranchType.BranchDeletedEventV1, id="_id", name="_name", repoid="_repoid")
1717
branch_event = branch.create_event(data={"key1": "value1"})
1818
assert branch_event is not None
19-
assert branch_event._attributes["type"] == "cd.repository.branch.deleted.v1"
19+
assert branch_event._attributes["type"] == BranchType.BranchDeletedEventV1.value
2020
assert branch_event._attributes["extensions"] == {"branchid": "_id", "branchname": "_name", "branchrepositoryid": "_repoid"}
2121
assert branch_event.data == {"key1": "value1"}

0 commit comments

Comments
 (0)