Skip to content

Commit ee42755

Browse files
committed
Parse also Gitlab-MR-closed event
I need this for hardly, where we want to react when a source-git MR gets closed. packit/hardly#62
1 parent 75a7711 commit ee42755

File tree

7 files changed

+182
-4
lines changed

7 files changed

+182
-4
lines changed

packit_service/worker/events/enums.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ class PullRequestAction(Enum):
1111

1212

1313
class GitlabEventAction(Enum):
14-
opened = "opened"
15-
reopen = "reopen"
16-
update = "update"
14+
opened = "opened" # from state
15+
closed = "closed" # from state
16+
reopen = "reopen" # from action
17+
update = "update" # from action
1718

1819

1920
class PullRequestCommentAction(Enum):

packit_service/worker/handlers/copr.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
)
4545
from packit_service.service.urls import get_copr_build_info_url, get_srpm_build_info_url
4646
from packit_service.utils import dump_job_config, dump_package_config
47+
from packit_service.worker.events.enums import GitlabEventAction
4748
from packit_service.worker.helpers.build import CoprBuildJobHelper
4849
from packit_service.worker.handlers.abstract import (
4950
JobHandler,
@@ -137,6 +138,13 @@ def run(self) -> TaskResults:
137138
return self.copr_build_helper.run_copr_build()
138139

139140
def pre_check(self) -> bool:
141+
if (
142+
self.data.event_type == MergeRequestGitlabEvent.__name__
143+
and self.data.action == GitlabEventAction.closed.value
144+
):
145+
# Not interested in closed merge requests
146+
return False
147+
140148
if self.data.event_type in (
141149
PushGitHubEvent.__name__,
142150
PushGitlabEvent.__name__,

packit_service/worker/handlers/koji.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from packit_service.service.urls import (
2626
get_koji_build_info_url,
2727
)
28+
from packit_service.worker.events.enums import GitlabEventAction
2829
from packit_service.worker.helpers.build.koji_build import KojiBuildJobHelper
2930
from packit_service.worker.events import (
3031
CheckRerunCommitEvent,
@@ -104,6 +105,13 @@ def run(self) -> TaskResults:
104105
return self.koji_build_helper.run_koji_build()
105106

106107
def pre_check(self) -> bool:
108+
if (
109+
self.data.event_type == MergeRequestGitlabEvent.__name__
110+
and self.data.action == GitlabEventAction.closed.value
111+
):
112+
# Not interested in closed merge requests
113+
return False
114+
107115
if self.data.event_type in (
108116
PushGitHubEvent.__name__,
109117
PushGitlabEvent.__name__,

packit_service/worker/handlers/testing_farm.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
get_testing_farm_info_url,
3737
get_copr_build_info_url,
3838
)
39+
from packit_service.worker.events.enums import GitlabEventAction
3940
from packit_service.worker.handlers import JobHandler
4041
from packit_service.worker.handlers.abstract import (
4142
TaskName,
@@ -119,6 +120,13 @@ def check_if_actor_can_run_job_and_report(self, actor: str) -> bool:
119120
return True
120121

121122
def pre_check(self) -> bool:
123+
if (
124+
self.data.event_type == MergeRequestGitlabEvent.__name__
125+
and self.data.action == GitlabEventAction.closed.value
126+
):
127+
# Not interested in closed merge requests
128+
return False
129+
122130
return not (
123131
self.testing_farm_job_helper.skip_build
124132
and self.is_copr_build_comment_event()

packit_service/worker/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def parse_mr_event(event) -> Optional[MergeRequestGitlabEvent]:
151151
return None
152152

153153
state = event["object_attributes"]["state"]
154-
if state != "opened":
154+
if state not in {"opened", "closed"}:
155155
return None
156156
action = nested_get(event, "object_attributes", "action")
157157
if action not in {"reopen", "update"}:
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
{
2+
"object_kind": "merge_request",
3+
"event_type": "merge_request",
4+
"user": {
5+
"id": 2952463,
6+
"name": "Jiri Popelka",
7+
"username": "jpopelka",
8+
"avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/2952463/avatar.png",
9+
"email": "[REDACTED]"
10+
},
11+
"project": {
12+
"id": 19258958,
13+
"name": "hello-world",
14+
"description": "The most progressive command-line tool in the world.",
15+
"web_url": "https://gitlab.com/packit-service/hello-world",
16+
"avatar_url": null,
17+
"git_ssh_url": "[email protected]:packit-service/hello-world.git",
18+
"git_http_url": "https://gitlab.com/packit-service/hello-world.git",
19+
"namespace": "packit-service",
20+
"visibility_level": 20,
21+
"path_with_namespace": "packit-service/hello-world",
22+
"default_branch": "master",
23+
"ci_config_path": "",
24+
"homepage": "https://gitlab.com/packit-service/hello-world",
25+
"url": "[email protected]:packit-service/hello-world.git",
26+
"ssh_url": "[email protected]:packit-service/hello-world.git",
27+
"http_url": "https://gitlab.com/packit-service/hello-world.git"
28+
},
29+
"object_attributes": {
30+
"assignee_id": 2952463,
31+
"author_id": 2952463,
32+
"created_at": "2022-05-06 15:08:42 UTC",
33+
"description": "",
34+
"head_pipeline_id": null,
35+
"id": 153884270,
36+
"iid": 19,
37+
"last_edited_at": null,
38+
"last_edited_by_id": null,
39+
"merge_commit_sha": null,
40+
"merge_error": null,
41+
"merge_params": {
42+
"force_remove_source_branch": "1"
43+
},
44+
"merge_status": "can_be_merged",
45+
"merge_user_id": null,
46+
"merge_when_pipeline_succeeds": false,
47+
"milestone_id": null,
48+
"source_branch": "innocent-change",
49+
"source_project_id": 35945041,
50+
"state_id": 2,
51+
"target_branch": "master",
52+
"target_project_id": 19258958,
53+
"time_estimate": 0,
54+
"title": "ja nic, to samo",
55+
"updated_at": "2022-05-06 15:11:26 UTC",
56+
"updated_by_id": null,
57+
"url": "https://gitlab.com/packit-service/hello-world/-/merge_requests/19",
58+
"source": {
59+
"id": 35945041,
60+
"name": "hello-world",
61+
"description": "The most progressive command-line tool in the world.",
62+
"web_url": "https://gitlab.com/jpopelka/hello-world",
63+
"avatar_url": null,
64+
"git_ssh_url": "[email protected]:jpopelka/hello-world.git",
65+
"git_http_url": "https://gitlab.com/jpopelka/hello-world.git",
66+
"namespace": "Jiri Popelka",
67+
"visibility_level": 20,
68+
"path_with_namespace": "jpopelka/hello-world",
69+
"default_branch": "master",
70+
"ci_config_path": "",
71+
"homepage": "https://gitlab.com/jpopelka/hello-world",
72+
"url": "[email protected]:jpopelka/hello-world.git",
73+
"ssh_url": "[email protected]:jpopelka/hello-world.git",
74+
"http_url": "https://gitlab.com/jpopelka/hello-world.git"
75+
},
76+
"target": {
77+
"id": 19258958,
78+
"name": "hello-world",
79+
"description": "The most progressive command-line tool in the world.",
80+
"web_url": "https://gitlab.com/packit-service/hello-world",
81+
"avatar_url": null,
82+
"git_ssh_url": "[email protected]:packit-service/hello-world.git",
83+
"git_http_url": "https://gitlab.com/packit-service/hello-world.git",
84+
"namespace": "packit-service",
85+
"visibility_level": 20,
86+
"path_with_namespace": "packit-service/hello-world",
87+
"default_branch": "master",
88+
"ci_config_path": "",
89+
"homepage": "https://gitlab.com/packit-service/hello-world",
90+
"url": "[email protected]:packit-service/hello-world.git",
91+
"ssh_url": "[email protected]:packit-service/hello-world.git",
92+
"http_url": "https://gitlab.com/packit-service/hello-world.git"
93+
},
94+
"last_commit": {
95+
"id": "769beb0807cf0bafeb3357e787f8c2f8fd3ea42b",
96+
"message": "the best improvement ever seen to mankind\n",
97+
"title": "the best improvement ever seen to mankind",
98+
"timestamp": "2022-05-06T17:06:07+02:00",
99+
"url": "https://gitlab.com/packit-service/hello-world/-/commit/769beb0807cf0bafeb3357e787f8c2f8fd3ea42b",
100+
"author": {
101+
"name": "Jiri Popelka",
102+
"email": "[email protected]"
103+
}
104+
},
105+
"work_in_progress": false,
106+
"total_time_spent": 0,
107+
"time_change": 0,
108+
"human_total_time_spent": null,
109+
"human_time_change": null,
110+
"human_time_estimate": null,
111+
"assignee_ids": [2952463],
112+
"labels": [],
113+
"state": "closed",
114+
"blocking_discussions_resolved": true,
115+
"action": "close"
116+
},
117+
"labels": [],
118+
"changes": {
119+
"state_id": {
120+
"previous": 1,
121+
"current": 2
122+
},
123+
"updated_at": {
124+
"previous": "2022-05-06 15:08:42 UTC",
125+
"current": "2022-05-06 15:11:26 UTC"
126+
}
127+
},
128+
"repository": {
129+
"name": "hello-world",
130+
"url": "[email protected]:packit-service/hello-world.git",
131+
"description": "The most progressive command-line tool in the world.",
132+
"homepage": "https://gitlab.com/packit-service/hello-world"
133+
},
134+
"assignees": [
135+
{
136+
"id": 2952463,
137+
"name": "Jiri Popelka",
138+
"username": "jpopelka",
139+
"avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/2952463/avatar.png",
140+
"email": "[REDACTED]"
141+
}
142+
]
143+
}

tests/unit/test_events.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ def merge_request_update(self):
136136
with open(DATA_DIR / "webhooks" / "gitlab" / "mr_update_event.json") as outfile:
137137
return json.load(outfile)
138138

139+
@pytest.fixture()
140+
def merge_request_closed(self):
141+
with open(DATA_DIR / "webhooks" / "gitlab" / "mr_closed.json") as outfile:
142+
return json.load(outfile)
143+
139144
@pytest.fixture()
140145
def github_pr_comment_empty(self):
141146
with open(
@@ -334,6 +339,11 @@ def test_parse_mr_action(self, merge_request_update):
334339
).once()
335340
assert event_object.package_config
336341

342+
def test_parse_mr_closed(self, merge_request_closed):
343+
event_object = Parser.parse_event(merge_request_closed)
344+
assert isinstance(event_object, MergeRequestGitlabEvent)
345+
assert event_object.action == GitlabEventAction.closed
346+
337347
def test_parse_pr(self, github_pr_webhook):
338348
event_object = Parser.parse_event(github_pr_webhook)
339349

0 commit comments

Comments
 (0)