diff --git a/packit_service/worker/events/pagure.py b/packit_service/worker/events/pagure.py index 0c4ca2231..a733b4e67 100644 --- a/packit_service/worker/events/pagure.py +++ b/packit_service/worker/events/pagure.py @@ -194,7 +194,7 @@ def __init__( base_repo_namespace: str, base_repo_name: str, base_repo_owner: str, - base_ref: str, + base_ref: Optional[str], target_repo: str, project_url: str, commit_sha: str, @@ -228,6 +228,15 @@ def get_base_project(self) -> GitProject: logger.debug(f"Base project: {fork} owned by {self.base_repo_owner}") return fork + def get_packages_config(self) -> Optional[PackageConfig]: + return PackageConfigGetter.get_package_config_from_repo( + base_project=self.base_project, + project=self.project, + reference=self.commit_sha, + pr_id=self.pr_id, + fail_when_missing=self.fail_when_config_file_missing, + ) + class PullRequestFlagPagureEvent(AbstractPagureEvent): def __init__( diff --git a/packit_service/worker/parser.py b/packit_service/worker/parser.py index dc1dd4c08..9d77676db 100644 --- a/packit_service/worker/parser.py +++ b/packit_service/worker/parser.py @@ -71,7 +71,10 @@ AnityaVersionUpdateEvent, NewHotnessUpdateEvent, ) -from packit_service.worker.events.pagure import PullRequestFlagPagureEvent +from packit_service.worker.events.pagure import ( + PullRequestFlagPagureEvent, + PullRequestPagureEvent, +) from packit_service.worker.handlers.abstract import MAP_CHECK_PREFIX_TO_HANDLER from packit_service.worker.helpers.build import CoprBuildJobHelper, KojiBuildJobHelper from packit_service.worker.helpers.testing_farm import TestingFarmJobHelper @@ -112,6 +115,7 @@ def parse_event( PipelineGitlabEvent, PullRequestFlagPagureEvent, PullRequestCommentPagureEvent, + PullRequestPagureEvent, PushPagureEvent, CheckRerunCommitEvent, CheckRerunPullRequestEvent, @@ -173,6 +177,7 @@ def parse_event( Parser.parse_openscanhub_task_finished_event, Parser.parse_openscanhub_task_started_event, Parser.parse_commit_comment_event, + Parser.parse_pagure_pull_request_event, ) ): if response: @@ -1703,6 +1708,48 @@ def parse_pagure_pull_request_comment_event( comment_id=comment_id, ) + @staticmethod + def parse_pagure_pull_request_event( + event, + ) -> Optional[PullRequestPagureEvent]: + if (topic := event.get("topic", "")) not in ( + "org.fedoraproject.prod.pagure.pull-request.new", + "org.fedoraproject.prod.pagure.pull-request.updated", + "org.fedoraproject.prod.pagure.pull-request.rebased", + ): + return None + + logger.info(f"Pagure PR event, topic: {topic}") + + action = ( + PullRequestAction.opened.value + if topic.endswith("new") + else PullRequestAction.synchronize.value + ) + pr_id = event["pullrequest"]["id"] + pagure_login = event["agent"] + + base_repo_namespace = event["pullrequest"]["project"]["namespace"] + base_repo_name = event["pullrequest"]["project"]["name"] + repo_from = event["pullrequest"]["repo_from"] + base_repo_owner = repo_from["user"]["name"] if repo_from else pagure_login + target_repo = repo_from["name"] if repo_from else base_repo_name + https_url = event["pullrequest"]["project"]["full_url"] + commit_sha = event["pullrequest"]["commit_stop"] + + return PullRequestPagureEvent( + action=PullRequestAction[action], + pr_id=pr_id, + base_repo_namespace=base_repo_namespace, + base_repo_name=base_repo_name, + base_repo_owner=base_repo_owner, + base_ref=None, + target_repo=target_repo, + project_url=https_url, + commit_sha=commit_sha, + user_login=pagure_login, + ) + @staticmethod def parse_new_hotness_update_event(event) -> Optional[NewHotnessUpdateEvent]: if "hotness.update.bug.file" not in event.get("topic", ""): @@ -1848,6 +1895,9 @@ def parse_openscanhub_task_started_event( "pagure.pull-request.flag.added": parse_pagure_pr_flag_event.__func__, # type: ignore "pagure.pull-request.flag.updated": parse_pagure_pr_flag_event.__func__, # type: ignore "pagure.pull-request.comment.added": parse_pagure_pull_request_comment_event.__func__, # type: ignore + "pagure.pull-request.new": parse_pagure_pull_request_event.__func__, # type: ignore + "pagure.pull-request.updated": parse_pagure_pull_request_event.__func__, # type: ignore + "pagure.pull-request.rebased": parse_pagure_pull_request_event.__func__, # type: ignore "pagure.git.receive": parse_pagure_push_event.__func__, # type: ignore "copr.build.start": parse_copr_event.__func__, # type: ignore "copr.build.end": parse_copr_event.__func__, # type: ignore diff --git a/tests/data/fedmsg/pagure_pr_new.json b/tests/data/fedmsg/pagure_pr_new.json new file mode 100644 index 000000000..7676f610e --- /dev/null +++ b/tests/data/fedmsg/pagure_pr_new.json @@ -0,0 +1,140 @@ +{ + "topic": "org.fedoraproject.prod.pagure.pull-request.new", + "agent": "zbyszek", + "pullrequest": { + "assignee": null, + "branch": "rawhide", + "branch_from": "build-only-on-aarch64", + "cached_merge_status": "unknown", + "closed_at": null, + "closed_by": null, + "comments": [], + "commit_start": "889f07af35d27bbcaf9c535c17a63b974aa42ee3", + "commit_stop": "889f07af35d27bbcaf9c535c17a63b974aa42ee3", + "date_created": "1729687175", + "full_url": "https://src.fedoraproject.org/rpms/optee_os/pull-request/2", + "id": 2, + "initial_comment": "Previously the package was \"built\" on all architectures, but no output\r\nwas produced. Only on aarch64, a noarch package was produced, which\r\nthen was distributed to all architectures. Mock/koji allow this, but\r\nit's rather pointless and confusing. We can achieve the same result by\r\nonly building on aarch64.\r\n\r\nI think this DTRT, but please don't merge without waiting for the scratch build result.", + "last_updated": "1729687175", + "project": { + "access_groups": { + "admin": [], + "collaborator": [], + "commit": [], + "ticket": [] + }, + "access_users": { + "admin": [], + "collaborator": [], + "commit": [], + "owner": ["eballetbo"], + "ticket": [] + }, + "close_status": [], + "custom_keys": [], + "date_created": "1695137284", + "date_modified": "1704875184", + "description": "The optee_os package\n", + "full_url": "https://src.fedoraproject.org/rpms/optee_os", + "fullname": "rpms/optee_os", + "id": 66326, + "milestones": {}, + "name": "optee_os", + "namespace": "rpms", + "parent": null, + "priorities": {}, + "tags": [], + "url_path": "rpms/optee_os", + "user": { + "full_url": "https://src.fedoraproject.org/user/eballetbo", + "fullname": "Enric Balletbo i Serra", + "name": "eballetbo", + "url_path": "user/eballetbo" + } + }, + "remote_git": null, + "repo_from": { + "access_groups": { + "admin": [], + "collaborator": [], + "commit": [], + "ticket": [] + }, + "access_users": { + "admin": [], + "collaborator": [], + "commit": [], + "owner": ["zbyszek"], + "ticket": [] + }, + "close_status": [], + "custom_keys": [], + "date_created": "1729687100", + "date_modified": "1729687100", + "description": "The optee_os package\n", + "full_url": "https://src.fedoraproject.org/fork/zbyszek/rpms/optee_os", + "fullname": "forks/zbyszek/rpms/optee_os", + "id": 79602, + "milestones": {}, + "name": "optee_os", + "namespace": "rpms", + "parent": { + "access_groups": { + "admin": [], + "collaborator": [], + "commit": [], + "ticket": [] + }, + "access_users": { + "admin": [], + "collaborator": [], + "commit": [], + "owner": ["eballetbo"], + "ticket": [] + }, + "close_status": [], + "custom_keys": [], + "date_created": "1695137284", + "date_modified": "1704875184", + "description": "The optee_os package\n", + "full_url": "https://src.fedoraproject.org/rpms/optee_os", + "fullname": "rpms/optee_os", + "id": 66326, + "milestones": {}, + "name": "optee_os", + "namespace": "rpms", + "parent": null, + "priorities": {}, + "tags": [], + "url_path": "rpms/optee_os", + "user": { + "full_url": "https://src.fedoraproject.org/user/eballetbo", + "fullname": "Enric Balletbo i Serra", + "name": "eballetbo", + "url_path": "user/eballetbo" + } + }, + "priorities": {}, + "tags": [], + "url_path": "fork/zbyszek/rpms/optee_os", + "user": { + "full_url": "https://src.fedoraproject.org/user/zbyszek", + "fullname": "Zbigniew J\u0119drzejewski-Szmek", + "name": "zbyszek", + "url_path": "user/zbyszek" + } + }, + "status": "Open", + "tags": [], + "threshold_reached": null, + "title": "Only build on aarch64", + "uid": "6bce8aea08204d12a74c2d17ecc505df", + "updated_on": "1729687175", + "user": { + "full_url": "https://src.fedoraproject.org/user/zbyszek", + "fullname": "Zbigniew J\u0119drzejewski-Szmek", + "name": "zbyszek", + "url_path": "user/zbyszek" + } + } +} diff --git a/tests/data/fedmsg/pagure_pr_rebased.json b/tests/data/fedmsg/pagure_pr_rebased.json new file mode 100644 index 000000000..2f0d59fbe --- /dev/null +++ b/tests/data/fedmsg/pagure_pr_rebased.json @@ -0,0 +1,140 @@ +{ + "topic": "org.fedoraproject.prod.pagure.pull-request.rebased", + "agent": "pagure", + "pullrequest": { + "assignee": null, + "branch": "rawhide", + "branch_from": "fedora_plans", + "cached_merge_status": "unknown", + "closed_at": null, + "closed_by": null, + "comments": [], + "commit_start": "196f3c99b21d75bf441331e1a82fb76d243e82d5", + "commit_stop": "196f3c99b21d75bf441331e1a82fb76d243e82d5", + "date_created": "1727942209", + "full_url": "https://src.fedoraproject.org/rpms/ftp/pull-request/6", + "id": 6, + "initial_comment": "Update plans and gating", + "last_updated": "1729685933", + "project": { + "access_groups": { + "admin": [], + "collaborator": [], + "commit": [], + "ticket": [] + }, + "access_users": { + "admin": ["mruprich"], + "collaborator": [], + "commit": [], + "owner": ["mruprich"], + "ticket": [] + }, + "close_status": [], + "custom_keys": [], + "date_created": "1501867757", + "date_modified": "1507273635", + "description": "The ftp rpms", + "full_url": "https://src.fedoraproject.org/rpms/ftp", + "fullname": "rpms/ftp", + "id": 4014, + "milestones": {}, + "name": "ftp", + "namespace": "rpms", + "parent": null, + "priorities": {}, + "tags": [], + "url_path": "rpms/ftp", + "user": { + "full_url": "https://src.fedoraproject.org/user/mruprich", + "fullname": "Michal Ruprich", + "name": "mruprich", + "url_path": "user/mruprich" + } + }, + "remote_git": null, + "repo_from": { + "access_groups": { + "admin": [], + "collaborator": [], + "commit": [], + "ticket": [] + }, + "access_users": { + "admin": [], + "collaborator": [], + "commit": [], + "owner": ["omejzlik"], + "ticket": [] + }, + "close_status": [], + "custom_keys": [], + "date_created": "1727942177", + "date_modified": "1727942177", + "description": "The ftp rpms", + "full_url": "https://src.fedoraproject.org/fork/omejzlik/rpms/ftp", + "fullname": "forks/omejzlik/rpms/ftp", + "id": 79045, + "milestones": {}, + "name": "ftp", + "namespace": "rpms", + "parent": { + "access_groups": { + "admin": [], + "collaborator": [], + "commit": [], + "ticket": [] + }, + "access_users": { + "admin": ["mruprich"], + "collaborator": [], + "commit": [], + "owner": ["mruprich"], + "ticket": [] + }, + "close_status": [], + "custom_keys": [], + "date_created": "1501867757", + "date_modified": "1507273635", + "description": "The ftp rpms", + "full_url": "https://src.fedoraproject.org/rpms/ftp", + "fullname": "rpms/ftp", + "id": 4014, + "milestones": {}, + "name": "ftp", + "namespace": "rpms", + "parent": null, + "priorities": {}, + "tags": [], + "url_path": "rpms/ftp", + "user": { + "full_url": "https://src.fedoraproject.org/user/mruprich", + "fullname": "Michal Ruprich", + "name": "mruprich", + "url_path": "user/mruprich" + } + }, + "priorities": {}, + "tags": [], + "url_path": "fork/omejzlik/rpms/ftp", + "user": { + "full_url": "https://src.fedoraproject.org/user/omejzlik", + "fullname": "Ond\u0159ej Mejzl\u00edk", + "name": "omejzlik", + "url_path": "user/omejzlik" + } + }, + "status": "Open", + "tags": [], + "threshold_reached": null, + "title": "Update plans and gating", + "uid": "0e726e2c759b46a4beee5824dd8e9f39", + "updated_on": "1729589491", + "user": { + "full_url": "https://src.fedoraproject.org/user/omejzlik", + "fullname": "Ond\u0159ej Mejzl\u00edk", + "name": "omejzlik", + "url_path": "user/omejzlik" + } + } +} diff --git a/tests/data/fedmsg/pagure_pr_updated.json b/tests/data/fedmsg/pagure_pr_updated.json new file mode 100644 index 000000000..308afc9ab --- /dev/null +++ b/tests/data/fedmsg/pagure_pr_updated.json @@ -0,0 +1,140 @@ +{ + "topic": "org.fedoraproject.prod.pagure.pull-request.updated", + "agent": "pagure", + "pullrequest": { + "assignee": null, + "branch": "f40", + "branch_from": "rawhide", + "cached_merge_status": "unknown", + "closed_at": null, + "closed_by": null, + "comments": [], + "commit_start": "d1f60630c7181d40a211ead45699c8c14df280f5", + "commit_stop": "f2f041328d629719c5ff31a08e800638d5df497f", + "date_created": "1729677025", + "full_url": "https://src.fedoraproject.org/rpms/marshalparser/pull-request/32", + "id": 32, + "initial_comment": null, + "last_updated": "1729679207", + "project": { + "access_groups": { + "admin": ["python-packagers-sig"], + "collaborator": [], + "commit": [], + "ticket": [] + }, + "access_users": { + "admin": [], + "collaborator": [], + "commit": [], + "owner": ["lbalhar"], + "ticket": ["zuul"] + }, + "close_status": [], + "custom_keys": [], + "date_created": "1595253234", + "date_modified": "1599647214", + "description": "The marshalparser package", + "full_url": "https://src.fedoraproject.org/rpms/marshalparser", + "fullname": "rpms/marshalparser", + "id": 44239, + "milestones": {}, + "name": "marshalparser", + "namespace": "rpms", + "parent": null, + "priorities": {}, + "tags": [], + "url_path": "rpms/marshalparser", + "user": { + "full_url": "https://src.fedoraproject.org/user/lbalhar", + "fullname": "Lum\u00edr Balhar", + "name": "lbalhar", + "url_path": "user/lbalhar" + } + }, + "remote_git": null, + "repo_from": { + "access_groups": { + "admin": [], + "collaborator": [], + "commit": [], + "ticket": [] + }, + "access_users": { + "admin": [], + "collaborator": [], + "commit": [], + "owner": ["lbalhar"], + "ticket": [] + }, + "close_status": [], + "custom_keys": [], + "date_created": "1596098091", + "date_modified": "1596098091", + "description": "The marshalparser package", + "full_url": "https://src.fedoraproject.org/fork/lbalhar/rpms/marshalparser", + "fullname": "forks/lbalhar/rpms/marshalparser", + "id": 44436, + "milestones": {}, + "name": "marshalparser", + "namespace": "rpms", + "parent": { + "access_groups": { + "admin": ["python-packagers-sig"], + "collaborator": [], + "commit": [], + "ticket": [] + }, + "access_users": { + "admin": [], + "collaborator": [], + "commit": [], + "owner": ["lbalhar"], + "ticket": ["zuul"] + }, + "close_status": [], + "custom_keys": [], + "date_created": "1595253234", + "date_modified": "1599647214", + "description": "The marshalparser package", + "full_url": "https://src.fedoraproject.org/rpms/marshalparser", + "fullname": "rpms/marshalparser", + "id": 44239, + "milestones": {}, + "name": "marshalparser", + "namespace": "rpms", + "parent": null, + "priorities": {}, + "tags": [], + "url_path": "rpms/marshalparser", + "user": { + "full_url": "https://src.fedoraproject.org/user/lbalhar", + "fullname": "Lum\u00edr Balhar", + "name": "lbalhar", + "url_path": "user/lbalhar" + } + }, + "priorities": {}, + "tags": [], + "url_path": "fork/lbalhar/rpms/marshalparser", + "user": { + "full_url": "https://src.fedoraproject.org/user/lbalhar", + "fullname": "Lum\u00edr Balhar", + "name": "lbalhar", + "url_path": "user/lbalhar" + } + }, + "status": "Open", + "tags": [], + "threshold_reached": null, + "title": "[f40] Update to 0.4.0 (rhbz#2321237)", + "uid": "4adeb164731e45f6a852bc16f41ecbf3", + "updated_on": "1729677338", + "user": { + "full_url": "https://src.fedoraproject.org/user/lbalhar", + "fullname": "Lum\u00edr Balhar", + "name": "lbalhar", + "url_path": "user/lbalhar" + } + } +} diff --git a/tests/unit/events/test_pagure.py b/tests/unit/events/test_pagure.py index 8c580b08c..fa0f91995 100644 --- a/tests/unit/events/test_pagure.py +++ b/tests/unit/events/test_pagure.py @@ -8,9 +8,11 @@ from ogr.services.pagure import PagureProject from packit_service.config import PackageConfigGetter +from packit_service.worker.events.enums import PullRequestAction from packit_service.worker.events.pagure import ( PullRequestCommentPagureEvent, PullRequestFlagPagureEvent, + PullRequestPagureEvent, PushPagureEvent, ) from packit_service.worker.parser import Parser @@ -23,6 +25,24 @@ def pagure_pr_flag_updated(): return json.load(outfile) +@pytest.fixture() +def pagure_pr_new(): + with open(DATA_DIR / "fedmsg" / "pagure_pr_new.json") as outfile: + return json.load(outfile) + + +@pytest.fixture() +def pagure_pr_updated(): + with open(DATA_DIR / "fedmsg" / "pagure_pr_updated.json") as outfile: + return json.load(outfile) + + +@pytest.fixture() +def pagure_pr_rebased(): + with open(DATA_DIR / "fedmsg" / "pagure_pr_rebased.json") as outfile: + return json.load(outfile) + + @pytest.fixture() def distgit_commit(): with open(DATA_DIR / "fedmsg" / "distgit_commit.json") as outfile: @@ -97,3 +117,114 @@ def test_distgit_pagure_push(distgit_commit): assert event_object.commit_sha == "abcd" assert event_object.git_ref == "main" assert event_object.project_url == "https://src.fedoraproject.org/rpms/buildah" + + +def test_parse_pagure_pull_request_new(pagure_pr_new): + event_object = Parser.parse_event(pagure_pr_new) + + assert isinstance(event_object, PullRequestPagureEvent) + assert event_object.action == PullRequestAction.opened + assert event_object.pr_id == 2 + assert event_object.base_repo_namespace == "rpms" + assert event_object.base_repo_name == "optee_os" + assert event_object.base_repo_owner == "zbyszek" + assert event_object.base_ref is None + assert event_object.target_repo == "optee_os" + assert event_object.commit_sha == "889f07af35d27bbcaf9c535c17a63b974aa42ee3" + assert event_object.user_login == "zbyszek" + assert event_object.project_url == "https://src.fedoraproject.org/rpms/optee_os" + + assert isinstance(event_object.project, PagureProject) + assert event_object.project.full_repo_name == "rpms/optee_os" + assert isinstance(event_object.base_project, PagureProject) + assert event_object.base_project.full_repo_name == "fork/zbyszek/rpms/optee_os" + + flexmock(PackageConfigGetter).should_receive( + "get_package_config_from_repo", + ).with_args( + base_project=event_object.base_project, + project=event_object.project, + reference="889f07af35d27bbcaf9c535c17a63b974aa42ee3", + pr_id=2, + fail_when_missing=False, + ).and_return( + flexmock(get_package_config_views=dict), + ).once() + flexmock(PagureProject).should_receive("get_web_url").and_return( + "https://src.fedoraproject.org/rpms/optee_os", + ) + assert event_object.packages_config + + +def test_parse_pagure_pull_request_updated(pagure_pr_updated): + event_object = Parser.parse_event(pagure_pr_updated) + + assert isinstance(event_object, PullRequestPagureEvent) + assert event_object.action == PullRequestAction.synchronize + assert event_object.pr_id == 32 + assert event_object.base_repo_namespace == "rpms" + assert event_object.base_repo_name == "marshalparser" + assert event_object.base_repo_owner == "lbalhar" + assert event_object.base_ref is None + assert event_object.target_repo == "marshalparser" + assert event_object.commit_sha == "f2f041328d629719c5ff31a08e800638d5df497f" + assert event_object.user_login == "pagure" + assert event_object.project_url == "https://src.fedoraproject.org/rpms/marshalparser" + + assert isinstance(event_object.project, PagureProject) + assert event_object.project.full_repo_name == "rpms/marshalparser" + assert isinstance(event_object.base_project, PagureProject) + assert event_object.base_project.full_repo_name == "fork/lbalhar/rpms/marshalparser" + + flexmock(PackageConfigGetter).should_receive( + "get_package_config_from_repo", + ).with_args( + base_project=event_object.base_project, + project=event_object.project, + reference="f2f041328d629719c5ff31a08e800638d5df497f", + pr_id=32, + fail_when_missing=False, + ).and_return( + flexmock(get_package_config_views=dict), + ).once() + flexmock(PagureProject).should_receive("get_web_url").and_return( + "https://src.fedoraproject.org/rpms/marshalparser", + ) + assert event_object.packages_config + + +def test_parse_pagure_pull_request_rebased(pagure_pr_rebased): + event_object = Parser.parse_event(pagure_pr_rebased) + + assert isinstance(event_object, PullRequestPagureEvent) + assert event_object.action == PullRequestAction.synchronize + assert event_object.pr_id == 6 + assert event_object.base_repo_namespace == "rpms" + assert event_object.base_repo_name == "ftp" + assert event_object.base_repo_owner == "omejzlik" + assert event_object.base_ref is None + assert event_object.target_repo == "ftp" + assert event_object.commit_sha == "196f3c99b21d75bf441331e1a82fb76d243e82d5" + assert event_object.user_login == "pagure" + assert event_object.project_url == "https://src.fedoraproject.org/rpms/ftp" + + assert isinstance(event_object.project, PagureProject) + assert event_object.project.full_repo_name == "rpms/ftp" + assert isinstance(event_object.base_project, PagureProject) + assert event_object.base_project.full_repo_name == "fork/omejzlik/rpms/ftp" + + flexmock(PackageConfigGetter).should_receive( + "get_package_config_from_repo", + ).with_args( + base_project=event_object.base_project, + project=event_object.project, + reference="196f3c99b21d75bf441331e1a82fb76d243e82d5", + pr_id=6, + fail_when_missing=False, + ).and_return( + flexmock(get_package_config_views=dict), + ).once() + flexmock(PagureProject).should_receive("get_web_url").and_return( + "https://src.fedoraproject.org/rpms/ftp", + ) + assert event_object.packages_config