From 83ad43cc22b29aaf798bb8506f8e1d5b01f1727c Mon Sep 17 00:00:00 2001 From: Maja Massarini Date: Fri, 8 Apr 2022 14:34:26 +0200 Subject: [PATCH 1/3] Use downstream_branch_name option --- hardly/handlers/distgit.py | 1 + tests/integration/test_dist_git_mr.py | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/hardly/handlers/distgit.py b/hardly/handlers/distgit.py index 2a606e2..7fda699 100644 --- a/hardly/handlers/distgit.py +++ b/hardly/handlers/distgit.py @@ -89,6 +89,7 @@ def run(self) -> TaskResults: Please review the contribution and once you are comfortable with the content, you should trigger a CI pipeline run via `Pipelines → Run pipeline`.""" dg_mr = self.api.sync_release( + dist_git_branch=self.package_config.downstream_branch_name, version=self.api.up.get_specfile_version(), add_new_sources=False, title=self.mr_title, diff --git a/tests/integration/test_dist_git_mr.py b/tests/integration/test_dist_git_mr.py index 467b773..0ce8657 100644 --- a/tests/integration/test_dist_git_mr.py +++ b/tests/integration/test_dist_git_mr.py @@ -13,14 +13,16 @@ from packit_service.utils import dump_package_config from packit_service.worker.monitoring import Pushgateway from packit_service.worker.parser import Parser +from ogr.services.gitlab import GitlabProject from tests.spellbook import first_dict_value def test_dist_git_mr(mr_event): - source_git_yaml = { + source_git_yaml = """ { "upstream_project_url": "https://github.com/vmware/open-vm-tools.git", "upstream_ref": "stable-11.3.0", "downstream_package_name": "open-vm-tools", + "downstream_branch_name": "another-rawhide", "specfile_path": ".distro/open-vm-tools.spec", "patch_generation_ignore_paths": [".distro"], "patch_generation_patch_id_digits": 1, @@ -45,6 +47,7 @@ def test_dist_git_mr(mr_event): } ], } + """ version = "11.3.0" trigger = flexmock( @@ -58,6 +61,9 @@ def test_dist_git_mr(mr_event): get_file_content=lambda path, ref: source_git_yaml, full_repo_name="jpopelka/src-open-vm-tools", ) + flexmock(GitlabProject).should_receive("get_file_content").and_return( + source_git_yaml + ) lp = flexmock( LocalProject, refresh_the_arguments=lambda: None, checkout_ref=lambda ref: None ) @@ -70,6 +76,7 @@ def test_dist_git_mr(mr_event): flexmock(ServiceConfig).should_receive("get_service_config").and_return(config) flexmock(Pushgateway).should_receive("push").once().and_return() flexmock(PackitAPI).should_receive("sync_release").with_args( + dist_git_branch="another-rawhide", version=version, add_new_sources=False, title="Yet another testing MR", From 369994989dea15f8eb136fded131853ebb1e1dd8 Mon Sep 17 00:00:00 2001 From: Maja Massarini Date: Fri, 8 Apr 2022 15:02:30 +0200 Subject: [PATCH 2/3] Update release notes in pull request template --- .github/PULL_REQUEST_TEMPLATE.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index b9b3cbc..ce03c64 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -15,6 +15,8 @@ Related to Merge before/after ---- +RELEASE NOTES BEGIN - +Packit now supports automatic ordering of ☕ after all checks pass. + +RELEASE NOTES END From bbeec3a9f89ae22d884179fd79ead4bed10285b1 Mon Sep 17 00:00:00 2001 From: Maja Massarini Date: Tue, 12 Apr 2022 09:37:46 +0200 Subject: [PATCH 3/3] Use upstream target branch name or a custom branch name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When creating a downstream MR use as target branch name or the same upstream target branch name or a custom one. Co-authored-by: Hunor Csomortáni --- hardly/handlers/distgit.py | 54 ++++++++-- tests/integration/test_dist_git_mr.py | 144 ++++++++++++++++++-------- 2 files changed, 145 insertions(+), 53 deletions(-) diff --git a/hardly/handlers/distgit.py b/hardly/handlers/distgit.py index 7fda699..73fe021 100644 --- a/hardly/handlers/distgit.py +++ b/hardly/handlers/distgit.py @@ -12,6 +12,7 @@ from packit.config.job_config import JobConfig from packit.config.package_config import PackageConfig from packit.local_project import LocalProject +from packit.exceptions import PackitGitException from packit_service.models import PullRequestModel, SourceGitPRDistGitPRModel from packit_service.worker.events import MergeRequestGitlabEvent, PipelineGitlabEvent from packit_service.worker.events.pagure import PullRequestFlagPagureEvent @@ -51,6 +52,26 @@ def __init__( ) self.target_repo_branch = event["target_repo_branch"] + def get_dist_git_branch(self) -> str: + """Get the downstream repo branch name where to open the MR. + If it exist otherwise raise an exception. + + Returns: + The downstream repo branch name + Raises: + PackitGitException: if the branch is not found + """ + dist_git_branch = ( + self.package_config.downstream_branch_name or self.target_repo_branch + ) + if dist_git_branch not in self.api.dg.local_project.git_project.get_branches(): + msg = f""" +Downstream {self.target_repo}:{dist_git_branch} branch does not exist. +""" + raise PackitGitException(msg) + + return dist_git_branch + def run(self) -> TaskResults: """ If user creates a merge-request on the source-git repository, @@ -88,17 +109,28 @@ def run(self) -> TaskResults: dg_mr_info += """ Please review the contribution and once you are comfortable with the content, you should trigger a CI pipeline run via `Pipelines → Run pipeline`.""" - dg_mr = self.api.sync_release( - dist_git_branch=self.package_config.downstream_branch_name, - version=self.api.up.get_specfile_version(), - add_new_sources=False, - title=self.mr_title, - description=f"{self.mr_description}\n\n---\n{dg_mr_info}", - sync_default_files=False, - # we rely on this in PipelineHandler below - local_pr_branch_suffix=f"src-{self.mr_identifier}", - mark_commit_origin=True, - ) + + dist_git_branch = None + try: + dist_git_branch = self.get_dist_git_branch() + except PackitGitException as ex: + msg = str(ex) + self.project.get_pr(int(self.mr_identifier)).comment(msg) + logger.debug(msg) + + dg_mr = None + if dist_git_branch: + dg_mr = self.api.sync_release( + dist_git_branch=dist_git_branch, + version=self.api.up.get_specfile_version(), + add_new_sources=False, + title=self.mr_title, + description=f"{self.mr_description}\n\n---\n{dg_mr_info}", + sync_default_files=False, + # we rely on this in PipelineHandler below + local_pr_branch_suffix=f"src-{self.mr_identifier}", + mark_commit_origin=True, + ) if dg_mr: comment = f"""[Dist-git MR #{dg_mr.id}]({dg_mr.url}) diff --git a/tests/integration/test_dist_git_mr.py b/tests/integration/test_dist_git_mr.py index 0ce8657..7aff9a9 100644 --- a/tests/integration/test_dist_git_mr.py +++ b/tests/integration/test_dist_git_mr.py @@ -1,5 +1,6 @@ # Copyright Contributors to the Packit project. # SPDX-License-Identifier: MIT +import pytest from flexmock import flexmock from hardly.tasks import run_dist_git_sync_handler @@ -13,41 +14,89 @@ from packit_service.utils import dump_package_config from packit_service.worker.monitoring import Pushgateway from packit_service.worker.parser import Parser -from ogr.services.gitlab import GitlabProject +from ogr.services.gitlab import GitlabProject, GitlabPullRequest +from ogr.services.pagure import PagureProject from tests.spellbook import first_dict_value -def test_dist_git_mr(mr_event): - source_git_yaml = """ { - "upstream_project_url": "https://github.com/vmware/open-vm-tools.git", - "upstream_ref": "stable-11.3.0", - "downstream_package_name": "open-vm-tools", - "downstream_branch_name": "another-rawhide", - "specfile_path": ".distro/open-vm-tools.spec", - "patch_generation_ignore_paths": [".distro"], - "patch_generation_patch_id_digits": 1, - "sync_changelog": True, - "synced_files": [ - { - "src": ".distro/", - "dest": ".", - "delete": True, - "filters": [ - "protect .git*", - "protect sources", - "exclude source-git.yaml", - "exclude .gitignore", - ], - } - ], - "sources": [ - { - "path": "open-vm-tools-11.3.0-18090558.tar.gz", - "url": "https://sources.stream.centos.org/sources/rpms/open-vm-tools/...", - } - ], - } - """ +source_git_yaml = """ { + "upstream_project_url": "https://github.com/vmware/open-vm-tools.git", + "upstream_ref": "stable-11.3.0", + "downstream_package_name": "open-vm-tools", + "specfile_path": ".distro/open-vm-tools.spec", + "patch_generation_ignore_paths": [".distro"], + "patch_generation_patch_id_digits": 1, + "sync_changelog": True, + "synced_files": [ + { + "src": ".distro/", + "dest": ".", + "delete": True, + "filters": [ + "protect .git*", + "protect sources", + "exclude source-git.yaml", + "exclude .gitignore", + ], + } + ], + "sources": [ + { + "path": "open-vm-tools-11.3.0-18090558.tar.gz", + "url": "https://sources.stream.centos.org/sources/rpms/open-vm-tools/...", + } + ], +} +""" + + +@pytest.mark.parametrize( + "source_git_yaml, downstream_branches, expected_branch, notify_msg", + [ + pytest.param( + source_git_yaml, + ["master", "c9s"], + "c9s", + False, + id="Use upstream branch name in downstream", + ), + pytest.param( + source_git_yaml.replace( + """ + "downstream_package_name": "open-vm-tools", +""", + """ + "downstream_package_name": "open-vm-tools", + "downstream_branch_name": "another-rawhide", +""", + ), + ["master", "another-rawhide"], + "another-rawhide", + False, + id="Use custom downstream branch name", + ), + pytest.param( + source_git_yaml.replace( + """ + "downstream_package_name": "open-vm-tools", +""", + """ + "downstream_package_name": "open-vm-tools", + "downstream_branch_name": "unknown", +""", + ), + [ + "master", + ], + "unknown", + True, + id="Create new downstream branch and notify user", + ), + ], +) +def test_dist_git_mr( + mr_event, source_git_yaml, downstream_branches, expected_branch, notify_msg +): version = "11.3.0" trigger = flexmock( @@ -68,6 +117,9 @@ def test_dist_git_mr(mr_event): LocalProject, refresh_the_arguments=lambda: None, checkout_ref=lambda ref: None ) lp.git_project = source_project + flexmock(PagureProject).should_receive("get_branches").and_return( + downstream_branches + ) flexmock(Upstream).should_receive("get_specfile_version").and_return(version) config = ServiceConfig() @@ -75,12 +127,18 @@ def test_dist_git_mr(mr_event): config.gitlab_mr_targets_handled = None flexmock(ServiceConfig).should_receive("get_service_config").and_return(config) flexmock(Pushgateway).should_receive("push").once().and_return() - flexmock(PackitAPI).should_receive("sync_release").with_args( - dist_git_branch="another-rawhide", - version=version, - add_new_sources=False, - title="Yet another testing MR", - description="""DnD RpcV3: A corrupted packet received may result in an out of bounds (OOB) + if notify_msg: + flexmock(GitlabPullRequest).should_receive("comment").and_return() + else: + ( + flexmock(PackitAPI) + .should_receive("sync_release") + .with_args( + dist_git_branch=expected_branch, + version=version, + add_new_sources=False, + title="Yet another testing MR", + description="""DnD RpcV3: A corrupted packet received may result in an out of bounds (OOB) memory access if the length of the message received is less than the size of the expected packet header. @@ -88,10 +146,12 @@ def test_dist_git_mr(mr_event): ###### Info for package maintainer This MR has been automatically created from [this source-git MR](https://gitlab.com/packit-service/src/open-vm-tools/-/merge_requests/5).""", - sync_default_files=False, - local_pr_branch_suffix="src-5", - mark_commit_origin=True, - ).once() + sync_default_files=False, + local_pr_branch_suffix="src-5", + mark_commit_origin=True, + ) + .once() + ) event = Parser.parse_event(mr_event) results = run_dist_git_sync_handler(