Skip to content

Commit

Permalink
Parse also Gitlab-MR-closed event
Browse files Browse the repository at this point in the history
I need this for hardly, where we want to react when a source-git MR gets closed.
packit/hardly#62
  • Loading branch information
jpopelka committed May 9, 2022
1 parent 75a7711 commit ee42755
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 4 deletions.
7 changes: 4 additions & 3 deletions packit_service/worker/events/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class PullRequestAction(Enum):


class GitlabEventAction(Enum):
opened = "opened"
reopen = "reopen"
update = "update"
opened = "opened" # from state
closed = "closed" # from state
reopen = "reopen" # from action
update = "update" # from action


class PullRequestCommentAction(Enum):
Expand Down
8 changes: 8 additions & 0 deletions packit_service/worker/handlers/copr.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
)
from packit_service.service.urls import get_copr_build_info_url, get_srpm_build_info_url
from packit_service.utils import dump_job_config, dump_package_config
from packit_service.worker.events.enums import GitlabEventAction
from packit_service.worker.helpers.build import CoprBuildJobHelper
from packit_service.worker.handlers.abstract import (
JobHandler,
Expand Down Expand Up @@ -137,6 +138,13 @@ def run(self) -> TaskResults:
return self.copr_build_helper.run_copr_build()

def pre_check(self) -> bool:
if (
self.data.event_type == MergeRequestGitlabEvent.__name__
and self.data.action == GitlabEventAction.closed.value
):
# Not interested in closed merge requests
return False

if self.data.event_type in (
PushGitHubEvent.__name__,
PushGitlabEvent.__name__,
Expand Down
8 changes: 8 additions & 0 deletions packit_service/worker/handlers/koji.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from packit_service.service.urls import (
get_koji_build_info_url,
)
from packit_service.worker.events.enums import GitlabEventAction
from packit_service.worker.helpers.build.koji_build import KojiBuildJobHelper
from packit_service.worker.events import (
CheckRerunCommitEvent,
Expand Down Expand Up @@ -104,6 +105,13 @@ def run(self) -> TaskResults:
return self.koji_build_helper.run_koji_build()

def pre_check(self) -> bool:
if (
self.data.event_type == MergeRequestGitlabEvent.__name__
and self.data.action == GitlabEventAction.closed.value
):
# Not interested in closed merge requests
return False

if self.data.event_type in (
PushGitHubEvent.__name__,
PushGitlabEvent.__name__,
Expand Down
8 changes: 8 additions & 0 deletions packit_service/worker/handlers/testing_farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
get_testing_farm_info_url,
get_copr_build_info_url,
)
from packit_service.worker.events.enums import GitlabEventAction
from packit_service.worker.handlers import JobHandler
from packit_service.worker.handlers.abstract import (
TaskName,
Expand Down Expand Up @@ -119,6 +120,13 @@ def check_if_actor_can_run_job_and_report(self, actor: str) -> bool:
return True

def pre_check(self) -> bool:
if (
self.data.event_type == MergeRequestGitlabEvent.__name__
and self.data.action == GitlabEventAction.closed.value
):
# Not interested in closed merge requests
return False

return not (
self.testing_farm_job_helper.skip_build
and self.is_copr_build_comment_event()
Expand Down
2 changes: 1 addition & 1 deletion packit_service/worker/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def parse_mr_event(event) -> Optional[MergeRequestGitlabEvent]:
return None

state = event["object_attributes"]["state"]
if state != "opened":
if state not in {"opened", "closed"}:
return None
action = nested_get(event, "object_attributes", "action")
if action not in {"reopen", "update"}:
Expand Down
143 changes: 143 additions & 0 deletions tests/data/webhooks/gitlab/mr_closed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"object_kind": "merge_request",
"event_type": "merge_request",
"user": {
"id": 2952463,
"name": "Jiri Popelka",
"username": "jpopelka",
"avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/2952463/avatar.png",
"email": "[REDACTED]"
},
"project": {
"id": 19258958,
"name": "hello-world",
"description": "The most progressive command-line tool in the world.",
"web_url": "https://gitlab.com/packit-service/hello-world",
"avatar_url": null,
"git_ssh_url": "[email protected]:packit-service/hello-world.git",
"git_http_url": "https://gitlab.com/packit-service/hello-world.git",
"namespace": "packit-service",
"visibility_level": 20,
"path_with_namespace": "packit-service/hello-world",
"default_branch": "master",
"ci_config_path": "",
"homepage": "https://gitlab.com/packit-service/hello-world",
"url": "[email protected]:packit-service/hello-world.git",
"ssh_url": "[email protected]:packit-service/hello-world.git",
"http_url": "https://gitlab.com/packit-service/hello-world.git"
},
"object_attributes": {
"assignee_id": 2952463,
"author_id": 2952463,
"created_at": "2022-05-06 15:08:42 UTC",
"description": "",
"head_pipeline_id": null,
"id": 153884270,
"iid": 19,
"last_edited_at": null,
"last_edited_by_id": null,
"merge_commit_sha": null,
"merge_error": null,
"merge_params": {
"force_remove_source_branch": "1"
},
"merge_status": "can_be_merged",
"merge_user_id": null,
"merge_when_pipeline_succeeds": false,
"milestone_id": null,
"source_branch": "innocent-change",
"source_project_id": 35945041,
"state_id": 2,
"target_branch": "master",
"target_project_id": 19258958,
"time_estimate": 0,
"title": "ja nic, to samo",
"updated_at": "2022-05-06 15:11:26 UTC",
"updated_by_id": null,
"url": "https://gitlab.com/packit-service/hello-world/-/merge_requests/19",
"source": {
"id": 35945041,
"name": "hello-world",
"description": "The most progressive command-line tool in the world.",
"web_url": "https://gitlab.com/jpopelka/hello-world",
"avatar_url": null,
"git_ssh_url": "[email protected]:jpopelka/hello-world.git",
"git_http_url": "https://gitlab.com/jpopelka/hello-world.git",
"namespace": "Jiri Popelka",
"visibility_level": 20,
"path_with_namespace": "jpopelka/hello-world",
"default_branch": "master",
"ci_config_path": "",
"homepage": "https://gitlab.com/jpopelka/hello-world",
"url": "[email protected]:jpopelka/hello-world.git",
"ssh_url": "[email protected]:jpopelka/hello-world.git",
"http_url": "https://gitlab.com/jpopelka/hello-world.git"
},
"target": {
"id": 19258958,
"name": "hello-world",
"description": "The most progressive command-line tool in the world.",
"web_url": "https://gitlab.com/packit-service/hello-world",
"avatar_url": null,
"git_ssh_url": "[email protected]:packit-service/hello-world.git",
"git_http_url": "https://gitlab.com/packit-service/hello-world.git",
"namespace": "packit-service",
"visibility_level": 20,
"path_with_namespace": "packit-service/hello-world",
"default_branch": "master",
"ci_config_path": "",
"homepage": "https://gitlab.com/packit-service/hello-world",
"url": "[email protected]:packit-service/hello-world.git",
"ssh_url": "[email protected]:packit-service/hello-world.git",
"http_url": "https://gitlab.com/packit-service/hello-world.git"
},
"last_commit": {
"id": "769beb0807cf0bafeb3357e787f8c2f8fd3ea42b",
"message": "the best improvement ever seen to mankind\n",
"title": "the best improvement ever seen to mankind",
"timestamp": "2022-05-06T17:06:07+02:00",
"url": "https://gitlab.com/packit-service/hello-world/-/commit/769beb0807cf0bafeb3357e787f8c2f8fd3ea42b",
"author": {
"name": "Jiri Popelka",
"email": "[email protected]"
}
},
"work_in_progress": false,
"total_time_spent": 0,
"time_change": 0,
"human_total_time_spent": null,
"human_time_change": null,
"human_time_estimate": null,
"assignee_ids": [2952463],
"labels": [],
"state": "closed",
"blocking_discussions_resolved": true,
"action": "close"
},
"labels": [],
"changes": {
"state_id": {
"previous": 1,
"current": 2
},
"updated_at": {
"previous": "2022-05-06 15:08:42 UTC",
"current": "2022-05-06 15:11:26 UTC"
}
},
"repository": {
"name": "hello-world",
"url": "[email protected]:packit-service/hello-world.git",
"description": "The most progressive command-line tool in the world.",
"homepage": "https://gitlab.com/packit-service/hello-world"
},
"assignees": [
{
"id": 2952463,
"name": "Jiri Popelka",
"username": "jpopelka",
"avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/2952463/avatar.png",
"email": "[REDACTED]"
}
]
}
10 changes: 10 additions & 0 deletions tests/unit/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ def merge_request_update(self):
with open(DATA_DIR / "webhooks" / "gitlab" / "mr_update_event.json") as outfile:
return json.load(outfile)

@pytest.fixture()
def merge_request_closed(self):
with open(DATA_DIR / "webhooks" / "gitlab" / "mr_closed.json") as outfile:
return json.load(outfile)

@pytest.fixture()
def github_pr_comment_empty(self):
with open(
Expand Down Expand Up @@ -334,6 +339,11 @@ def test_parse_mr_action(self, merge_request_update):
).once()
assert event_object.package_config

def test_parse_mr_closed(self, merge_request_closed):
event_object = Parser.parse_event(merge_request_closed)
assert isinstance(event_object, MergeRequestGitlabEvent)
assert event_object.action == GitlabEventAction.closed

def test_parse_pr(self, github_pr_webhook):
event_object = Parser.parse_event(github_pr_webhook)

Expand Down

0 comments on commit ee42755

Please sign in to comment.