Skip to content

Commit 6632fc6

Browse files
tarekbadrshafrittoli
authored andcommitted
add PipelineRun obj
1 parent e02ed75 commit 6632fc6

File tree

7 files changed

+73
-57
lines changed

7 files changed

+73
-57
lines changed

cli/cdevents/cli/env.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
"""Module for cli environment 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

cli/cdevents/cli/pipelinerun.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
"""Module for cli pipelinerun 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.pipelinerun import Pipelinerun, PipelinerunType
1410

1511
# pylint: disable=unused-argument
1612
def common_pipelinerun_options(function):
@@ -73,11 +69,10 @@ def started(
7369
data: List[str] = None,
7470
):
7571
print_function_args()
76-
e = Events()
77-
new_event = e.create_pipelinerun_event(event_type.PipelineRunStartedEventV1, id, name, status, url, errors, data)
72+
pipelinerun = Pipelinerun(PipelinerunType.PipelineRunStartedEventV1, id=id, name=name, status=status, url=url, errors=errors)
73+
pipelinerun_event = pipelinerun.create_event(data)
7874
cdevents_command = CDeventsCommand()
79-
cdevents_command.run(new_event)
80-
75+
cdevents_command.run(pipelinerun_event)
8176

8277
@click.command(help=add_disclaimer_text("PipelineRun Finished CloudEvent."))
8378
@common_pipelinerun_options
@@ -90,10 +85,11 @@ def finished(
9085
data: List[str] = None,
9186
):
9287
print_function_args()
93-
e = Events()
94-
new_event = e.create_pipelinerun_event(event_type.PipelineRunFinishedEventV1, id, name, status, url, errors, data)
88+
pipelinerun = Pipelinerun(PipelinerunType.PipelineRunFinishedEventV1, id=id, name=name, status=status, url=url, errors=errors)
89+
pipelinerun_event = pipelinerun.create_event(data)
9590
cdevents_command = CDeventsCommand()
96-
cdevents_command.run(new_event)
91+
cdevents_command.run(pipelinerun_event)
92+
9793

9894
@click.command(help=add_disclaimer_text("PipelineRun Queued CloudEvent."))
9995
@common_pipelinerun_options
@@ -106,8 +102,9 @@ def queued(
106102
data: List[str] = None,
107103
):
108104
print_function_args()
109-
e = Events()
110-
new_event = e.create_pipelinerun_event(event_type.PipelineRunQueuedEventV1, id, name, status, url, errors, data)
105+
pipelinerun = Pipelinerun(PipelinerunType.PipelineRunQueuedEventV1, id=id, name=name, status=status, url=url, errors=errors)
106+
pipelinerun_event = pipelinerun.create_event(data)
111107
cdevents_command = CDeventsCommand()
112-
cdevents_command.run(new_event)
108+
cdevents_command.run(pipelinerun_event)
109+
113110

core/cdevents/core/event_type.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
ChangeMergedEventV1 :str = "cd.repository.change.merged.v1"
1010
ChangeAbandonedEventV1 :str = "cd.repository.change.abandoned.v1"
1111

12-
# PipelineRun events
13-
PipelineRunStartedEventV1 :str = "cd.pipelinerun.started.v1"
14-
PipelineRunFinishedEventV1 :str = "cd.pipelinerun.finished.v1"
15-
PipelineRunQueuedEventV1 :str = "cd.pipelinerun.queued.v1"
16-
1712
# Repository events
1813
RepositoryCreatedEventV1 :str = "cd.repository.created.v1"
1914
RepositoryModifiedEventV1 :str = "cd.repository.modified.v1"

core/cdevents/core/events.py

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

2323
return event
2424

25-
def create_pipelinerun_event(self, event_type: str , id: str, name: str, status: str, url: str, errors: str, data = {}) -> CloudEvent:
26-
"""Create pipelinerun event.
27-
"""
28-
29-
extensions = {
30-
"pipelinerunid": id,
31-
"pipelinerunname": name,
32-
"pipelinerunstatus": status,
33-
"pipelinerunurl": url,
34-
"pipelinerunerrors": errors,
35-
}
36-
event = self.create_event(event_type, extensions, data)
37-
38-
return event
39-
4025
def create_repository_event(self, event_type: str , id: str, name: str, url: str, data = {}) -> CloudEvent:
4126
"""Create repository event.
4227
"""

core/cdevents/core/pipelinerun.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""pipelinerun"""
2+
3+
from enum import Enum
4+
from cdevents.core.events import Events
5+
6+
class PipelinerunType(Enum):
7+
PipelineRunStartedEventV1 :str = "cd.pipelinerun.started.v1"
8+
PipelineRunFinishedEventV1 :str = "cd.pipelinerun.finished.v1"
9+
PipelineRunQueuedEventV1 :str = "cd.pipelinerun.queued.v1"
10+
11+
12+
class Pipelinerun(Events):
13+
"""Pipelinerun."""
14+
15+
def __init__(self, pipelinerun_type: PipelinerunType, id: str, name: str, status: str, url: str, errors: str):
16+
"""Initializes class.
17+
"""
18+
self._event_type = pipelinerun_type
19+
self._id = id
20+
self._name = name
21+
self._status = status
22+
self._url = url
23+
self._errors = errors
24+
25+
def create_extensions(self):
26+
"""Create extensions.
27+
"""
28+
extensions = {
29+
"pipelinerunid": self._id,
30+
"pipelinerunname": self._name,
31+
"pipelinerunstatus": self._status,
32+
"pipelinerunurl": self._url,
33+
"pipelinerunerrors": self._errors,
34+
}
35+
return extensions
36+
37+
def create_event(self, data: dict={}):
38+
"""Create pipelinerun event.
39+
"""
40+
extensions = self.create_extensions()
41+
event = super().create_event(event_type=self._event_type.value, extensions=extensions, data=data)
42+
return event

core/tests/test_environment.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from cdevents.core import event_type
21
import pytest
32

43
from cdevents.core.env import Env, EnvType

core/tests/test_pipelinerun.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
from pickle import NONE
2-
from cdevents.core import event_type
31
import pytest
42

5-
from cdevents.core.events import Events
3+
from cdevents.core.pipelinerun import Pipelinerun, PipelinerunType
64

75
@pytest.mark.unit
86
def test_pipelinerun_started():
9-
event = Events().create_pipelinerun_event(event_type.PipelineRunStartedEventV1, id="_id", name="_name", status="_status", url="_url", errors="_errors", data={"pipelinerun": "_pipelinerun"})
10-
assert event is not None
11-
assert event._attributes["type"] == event_type.PipelineRunStartedEventV1
12-
assert event._attributes["extensions"] == {"pipelinerunid": "_id", "pipelinerunname": "_name", "pipelinerunstatus": "_status", "pipelinerunurl": "_url", "pipelinerunerrors": "_errors"}
13-
assert event.data == {"pipelinerun": "_pipelinerun"}
7+
pipelinerun = Pipelinerun(pipelinerun_type=PipelinerunType.PipelineRunStartedEventV1, id="_id", name="_name", status="_status", url="_url", errors="_errors")
8+
pipelinerun_event = pipelinerun.create_event(data={"key1": "value1"})
9+
assert pipelinerun_event is not None
10+
assert pipelinerun_event._attributes["type"] == PipelinerunType.PipelineRunStartedEventV1.value
11+
assert pipelinerun_event._attributes["extensions"] == {"pipelinerunid": "_id", "pipelinerunname": "_name", "pipelinerunstatus": "_status", "pipelinerunurl": "_url", "pipelinerunerrors": "_errors"}
12+
assert pipelinerun_event.data == {"key1": "value1"}
1413

1514
@pytest.mark.unit
1615
def test_pipelinerun_finished():
17-
event = Events().create_pipelinerun_event(event_type.PipelineRunFinishedEventV1, id="_id", name="_name", status="_status", url="_url", errors="_errors", data={"pipelinerun": "_pipelinerun"})
18-
assert event is not None
19-
assert event._attributes["type"] == event_type.PipelineRunFinishedEventV1
20-
assert event._attributes["extensions"] == {"pipelinerunid": "_id", "pipelinerunname": "_name", "pipelinerunstatus": "_status", "pipelinerunurl": "_url", "pipelinerunerrors": "_errors"}
21-
assert event.data == {"pipelinerun": "_pipelinerun"}
16+
pipelinerun = Pipelinerun(pipelinerun_type=PipelinerunType.PipelineRunFinishedEventV1, id="_id", name="_name", status="_status", url="_url", errors="_errors")
17+
pipelinerun_event = pipelinerun.create_event(data={"key1": "value1"})
18+
assert pipelinerun_event is not None
19+
assert pipelinerun_event._attributes["type"] == PipelinerunType.PipelineRunFinishedEventV1.value
20+
assert pipelinerun_event._attributes["extensions"] == {"pipelinerunid": "_id", "pipelinerunname": "_name", "pipelinerunstatus": "_status", "pipelinerunurl": "_url", "pipelinerunerrors": "_errors"}
21+
assert pipelinerun_event.data == {"key1": "value1"}
2222

2323
@pytest.mark.unit
2424
def test_pipelinerun_queued():
25-
event = Events().create_pipelinerun_event(event_type.PipelineRunQueuedEventV1, id="_id", name="_name", status="_status", url="_url", errors="_errors", data={"pipelinerun": "_pipelinerun"})
26-
assert event is not None
27-
assert event._attributes["type"] == event_type.PipelineRunQueuedEventV1
28-
assert event._attributes["extensions"] == {"pipelinerunid": "_id", "pipelinerunname": "_name", "pipelinerunstatus": "_status", "pipelinerunurl": "_url", "pipelinerunerrors": "_errors"}
29-
assert event.data == {"pipelinerun": "_pipelinerun"}
25+
pipelinerun = Pipelinerun(pipelinerun_type=PipelinerunType.PipelineRunQueuedEventV1, id="_id", name="_name", status="_status", url="_url", errors="_errors")
26+
pipelinerun_event = pipelinerun.create_event(data={"key1": "value1"})
27+
assert pipelinerun_event is not None
28+
assert pipelinerun_event._attributes["type"] == PipelinerunType.PipelineRunQueuedEventV1.value
29+
assert pipelinerun_event._attributes["extensions"] == {"pipelinerunid": "_id", "pipelinerunname": "_name", "pipelinerunstatus": "_status", "pipelinerunurl": "_url", "pipelinerunerrors": "_errors"}
30+
assert pipelinerun_event.data == {"key1": "value1"}

0 commit comments

Comments
 (0)