Skip to content

Commit 5aa2cc1

Browse files
tarekbadrshafrittoli
authored andcommitted
Add core pakage
1 parent aa1465d commit 5aa2cc1

File tree

19 files changed

+390
-181
lines changed

19 files changed

+390
-181
lines changed

cdevents/__init__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

cli/cdevents/cli/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import click
77
import yaml
88

9+
from cdevents.cli.constants import LOGGING_CONFIGURATION_FILE
10+
911
from cdevents.cli.artifact import packaged, published
1012
from cdevents.cli.branch import created as branch_created
1113
from cdevents.cli.branch import deleted as branch_deleted
1214
from cdevents.cli.build import finished, queued, started
13-
from cdevents.cli.constants import LOGGING_CONFIGURATION_FILE
1415
from cdevents.cli.env import created as env_created
1516
from cdevents.cli.env import deleted as env_deleted
1617
from cdevents.cli.env import modified as env_modified

cli/cdevents/cli/artifact.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
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
14+
1215
# pylint: disable=unused-argument
1316
def common_artifact_options(function):
1417
"""Decorator for common cli options for artifact."""
@@ -54,15 +57,10 @@ def packaged(
5457
data: List[str] = None,
5558
):
5659
print_function_args()
57-
artifact_packaged_event_v1 = "cd.artifact.packaged.v1"
58-
extensions = {
59-
"artifactid": id,
60-
"artifactname": name,
61-
"artifactversion": version,
62-
}
63-
60+
e = Events()
61+
new_event = e.create_artifact_event(event_type.ArtifactPackagedEventV1, id, name, version, data)
6462
cdevents_command = CDeventsCommand()
65-
cdevents_command.run(artifact_packaged_event_v1, extensions, data)
63+
cdevents_command.run(new_event)
6664

6765

6866
@click.command(help=add_disclaimer_text("Artifact Published CloudEvent."))
@@ -74,11 +72,8 @@ def published(
7472
data: List[str] = None,
7573
):
7674
print_function_args()
77-
artifact_published_event_v1 = "cd.artifact.published.v1"
78-
extensions = {
79-
"artifactid": id,
80-
"artifactname": name,
81-
"artifactversion": version,
82-
}
75+
76+
e = Events()
77+
new_event = e.create_artifact_event(event_type.ArtifactPublishedEventV1, id, name, version, data)
8378
cdevents_command = CDeventsCommand()
84-
cdevents_command.run(artifact_published_event_v1, extensions, data)
79+
cdevents_command.run(new_event)

cli/cdevents/cli/branch.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
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
14+
1215
# pylint: disable=unused-argument
1316
def common_branch_options(function):
1417
"""Decorator for common cli options for branch."""
@@ -53,16 +56,12 @@ def created(
5356
repoid: str = None,
5457
data: List[str] = None,
5558
):
56-
print_function_args()
57-
branch_created_event_v1 = "cd.repository.branch.created.v1"
58-
extensions = {
59-
"branchid": id,
60-
"branchname": name,
61-
"branchrepositoryid": repoid,
62-
}
63-
59+
print_function_args()
60+
e = Events()
61+
new_event = e.create_branch_event(event_type.BranchCreatedEventV1, id, name, repoid, data)
6462
cdevents_command = CDeventsCommand()
65-
cdevents_command.run(branch_created_event_v1, extensions, data)
63+
cdevents_command.run(new_event)
64+
6665

6766
@click.command(help=add_disclaimer_text("Branch Deleted CloudEvent."))
6867
@common_branch_options
@@ -72,13 +71,8 @@ def deleted(
7271
repoid: str = None,
7372
data: List[str] = None,
7473
):
75-
print_function_args()
76-
branch_deleted_event_v1 = "cd.repository.branch.deleted.v1"
77-
extensions = {
78-
"branchid": id,
79-
"branchname": name,
80-
"branchrepositoryid": repoid,
81-
}
82-
74+
print_function_args()
75+
e = Events()
76+
new_event = e.create_branch_event(event_type.BranchDeletedEventV1, id, name, repoid, data)
8377
cdevents_command = CDeventsCommand()
84-
cdevents_command.run(branch_deleted_event_v1, extensions, data)
78+
cdevents_command.run(new_event)

cli/cdevents/cli/build.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
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
14+
1215
# pylint: disable=unused-argument
1316
def common_build_options(function):
1417
"""Decorator for common cli options for build."""
@@ -55,15 +58,10 @@ def started(
5558
data: List[str] = None,
5659
):
5760
print_function_args()
58-
build_started_event_v1 = "cd.build.started.v1"
59-
extensions = {
60-
"buildid": id,
61-
"buildname": name,
62-
"buildartifactid": artifact,
63-
}
64-
61+
e = Events()
62+
new_event = e.create_build_event(event_type.BuildStartedEventV1, id, name, artifact, data)
6563
cdevents_command = CDeventsCommand()
66-
cdevents_command.run(build_started_event_v1, extensions, data)
64+
cdevents_command.run(new_event)
6765

6866
@click.command(help=add_disclaimer_text("Build Finished CloudEvent."))
6967
@common_build_options
@@ -74,16 +72,10 @@ def finished(
7472
data: List[str] = None,
7573
):
7674
print_function_args()
77-
build_finished_event_v1 = "cd.build.finished.v1"
78-
extensions = {
79-
"buildid": id,
80-
"buildname": name,
81-
"buildartifactid": artifact,
82-
}
83-
75+
e = Events()
76+
new_event = e.create_build_event(event_type.BuildFinishedEventV1, id, name, artifact, data)
8477
cdevents_command = CDeventsCommand()
85-
cdevents_command.run(build_finished_event_v1, extensions, data)
86-
78+
cdevents_command.run(new_event)
8779

8880
@click.command(help=add_disclaimer_text("PipelineRun Queued CloudEvent."))
8981
@common_build_options
@@ -94,12 +86,7 @@ def queued(
9486
data: List[str] = None,
9587
):
9688
print_function_args()
97-
build_queued_event_v1 = "cd.build.queued.v1"
98-
extensions = {
99-
"buildid": id,
100-
"buildname": name,
101-
"buildartifactid": artifact,
102-
}
103-
89+
e = Events()
90+
new_event = e.create_build_event(event_type.BuildQueuedEventV1, id, name, artifact, data)
10491
cdevents_command = CDeventsCommand()
105-
cdevents_command.run(build_queued_event_v1, extensions, data)
92+
cdevents_command.run(new_event)

cli/cdevents/cli/cdevents_command.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
from abc import ABC
44
import requests
55

6+
from cloudevents.http import CloudEvent
7+
8+
from cdevents.core.event_sender import EventSender
9+
610
from cdevents.cli.configuration_handler import ConfigurationHandler
7-
from cloudevents.http import CloudEvent, to_structured
811
from cdevents.cli.configuration_handler import new_default_configuration_handler
912

13+
1014
class CDeventsCommand(ABC):
1115
"""Abstract base class for all Hostlog commands."""
1216

@@ -21,21 +25,24 @@ def __init__(self, config_handler: ConfigurationHandler = None):
2125
if config_handler is None:
2226
self._config_handler = new_default_configuration_handler()
2327

24-
def run(self, type, extensions, data):
28+
def run(self, event: CloudEvent):
2529
"""run command.
2630
"""
27-
attributes = {
28-
"type": type,
29-
"source": self.config_handler.source.name,
30-
"extensions": extensions,
31-
}
32-
event = CloudEvent(attributes, dict(data))
33-
headers, body = to_structured(event)
34-
cde_link = self.config_handler.client.host
35-
36-
# send and print event
37-
result = requests.post(cde_link, headers=headers, data=body)
38-
self._log.info(f"Response with state code {result.status_code}")
31+
# attributes = {
32+
# "type": type,
33+
# "source": self.config_handler.source.name,
34+
# "extensions": extensions,
35+
# }
36+
# event = CloudEvent(attributes, dict(data))
37+
# headers, body = to_structured(event)
38+
# cde_link = self.config_handler.client.host
39+
40+
# # send and print event
41+
# result = requests.post(cde_link, headers=headers, data=body)
42+
# self._log.info(f"Response with state code {result.status_code}")
43+
44+
e = EventSender(cde_link=self.config_handler.client.host)
45+
e.send(event)
3946

4047

4148
@property

cli/cdevents/cli/env.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
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
14+
1215
# pylint: disable=unused-argument
1316
def common_env_options(function):
1417
"""Decorator for common cli options for environment."""
@@ -54,15 +57,10 @@ def created(
5457
data: List[str] = None,
5558
):
5659
print_function_args()
57-
environment_created_event_v1 = "cd.environment.created.v1"
58-
extensions = {
59-
"envId": id,
60-
"envname": name,
61-
"envrepourl": repo,
62-
}
63-
60+
e = Events()
61+
new_event = e.create_environment_event(event_type.EnvironmentCreatedEventV1, id, name, repo, data)
6462
cdevents_command = CDeventsCommand()
65-
cdevents_command.run(environment_created_event_v1, extensions, data)
63+
cdevents_command.run(new_event)
6664

6765

6866
@click.command(help=add_disclaimer_text("Environment Deleted CloudEvent."))
@@ -74,16 +72,10 @@ def deleted(
7472
data: List[str] = None,
7573
):
7674
print_function_args()
77-
environment_deleted_event_v1 = "cd.environment.deleted.v1"
78-
extensions = {
79-
"envId": id,
80-
"envname": name,
81-
"envrepourl": repo,
82-
}
83-
75+
e = Events()
76+
new_event = e.create_environment_event(event_type.EnvironmentDeletedEventV1, id, name, repo, data)
8477
cdevents_command = CDeventsCommand()
85-
cdevents_command.run(environment_deleted_event_v1, extensions, data)
86-
78+
cdevents_command.run(new_event)
8779

8880
@click.command(help=add_disclaimer_text("Environment Modified CloudEvent."))
8981
@common_env_options
@@ -94,12 +86,7 @@ def modified(
9486
data: List[str] = None,
9587
):
9688
print_function_args()
97-
environment_modified_event_v1 = "cd.environment.modified.v1"
98-
extensions = {
99-
"envId": id,
100-
"envname": name,
101-
"envrepourl": repo,
102-
}
103-
89+
e = Events()
90+
new_event = e.create_environment_event(event_type.EnvironmentModifiedEventV1, id, name, repo, data)
10491
cdevents_command = CDeventsCommand()
105-
cdevents_command.run(environment_modified_event_v1, extensions, data)
92+
cdevents_command.run(new_event)

cli/cdevents/cli/pipelinerun.py

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
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
14+
1215
# pylint: disable=unused-argument
1316
def common_pipelinerun_options(function):
1417
"""Decorator for common cli options for pipelinerun."""
@@ -70,17 +73,10 @@ def started(
7073
data: List[str] = None,
7174
):
7275
print_function_args()
73-
pipeline_run_started_event_v1 = "cd.pipelinerun.started.v1"
74-
extensions = {
75-
"pipelinerunid": id,
76-
"pipelinerunname": name,
77-
"pipelinerunstatus": status,
78-
"pipelinerunurl": url,
79-
"pipelinerunerrors": errors,
80-
}
81-
76+
e = Events()
77+
new_event = e.create_pipelinerun_event(event_type.PipelineRunStartedEventV1, id, name, status, url, errors, data)
8278
cdevents_command = CDeventsCommand()
83-
cdevents_command.run(pipeline_run_started_event_v1, extensions, data)
79+
cdevents_command.run(new_event)
8480

8581

8682
@click.command(help=add_disclaimer_text("PipelineRun Finished CloudEvent."))
@@ -94,17 +90,10 @@ def finished(
9490
data: List[str] = None,
9591
):
9692
print_function_args()
97-
pipeline_run_finished_event_v1 = "cd.pipelinerun.finished.v1"
98-
extensions = {
99-
"pipelinerunid": id,
100-
"pipelinerunname": name,
101-
"pipelinerunstatus": status,
102-
"pipelinerunurl": url,
103-
"pipelinerunerrors": errors,
104-
}
105-
93+
e = Events()
94+
new_event = e.create_pipelinerun_event(event_type.PipelineRunFinishedEventV1, id, name, status, url, errors, data)
10695
cdevents_command = CDeventsCommand()
107-
cdevents_command.run(pipeline_run_finished_event_v1, extensions, data)
96+
cdevents_command.run(new_event)
10897

10998
@click.command(help=add_disclaimer_text("PipelineRun Queued CloudEvent."))
11099
@common_pipelinerun_options
@@ -117,14 +106,8 @@ def queued(
117106
data: List[str] = None,
118107
):
119108
print_function_args()
120-
pipeline_run_queued_event_v1 = "cd.pipelinerun.queued.v1"
121-
extensions = {
122-
"pipelinerunid": id,
123-
"pipelinerunname": name,
124-
"pipelinerunstatus": status,
125-
"pipelinerunurl": url,
126-
"pipelinerunerrors": errors,
127-
}
128-
109+
e = Events()
110+
new_event = e.create_pipelinerun_event(event_type.PipelineRunQueuedEventV1, id, name, status, url, errors, data)
129111
cdevents_command = CDeventsCommand()
130-
cdevents_command.run(pipeline_run_queued_event_v1, extensions, data)
112+
cdevents_command.run(new_event)
113+

0 commit comments

Comments
 (0)