|
| 1 | +# Copyright Contributors to the Packit project. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +""" |
| 5 | +This file defines classes for job handlers specific to onboarding tasks |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | + |
| 10 | +from packit.cli.dist_git_init import ( |
| 11 | + COMMIT_MESSAGE, |
| 12 | + CONFIG_FILE_NAME, |
| 13 | + ONBOARD_BRANCH_NAME, |
| 14 | + DistGitInitializer, |
| 15 | +) |
| 16 | +from packit.config.config import Config |
| 17 | +from packit.config.package_config import PackageConfig |
| 18 | + |
| 19 | +from packit_service.constants import DG_ONBOARDING_DESCRIPTION, DG_ONBOARDING_TITLE |
| 20 | +from packit_service.events import ( |
| 21 | + onboarding, |
| 22 | +) |
| 23 | +from packit_service.worker.checker.abstract import Checker |
| 24 | +from packit_service.worker.checker.onboarding import ProjectIsNotOnboarded |
| 25 | +from packit_service.worker.handlers.abstract import ( |
| 26 | + JobHandler, |
| 27 | + TaskName, |
| 28 | + reacts_to, |
| 29 | +) |
| 30 | +from packit_service.worker.mixin import ConfigFromEventMixin, PackitAPIWithDownstreamMixin |
| 31 | +from packit_service.worker.result import TaskResults |
| 32 | + |
| 33 | +logger = logging.getLogger(__name__) |
| 34 | + |
| 35 | + |
| 36 | +@reacts_to(event=onboarding.Request) |
| 37 | +class OnboardingRequestHandler( |
| 38 | + JobHandler, |
| 39 | + ConfigFromEventMixin, |
| 40 | + PackitAPIWithDownstreamMixin, |
| 41 | +): |
| 42 | + task_name = TaskName.onboarding_request |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def get_checkers() -> tuple[type[Checker], ...]: |
| 46 | + return (ProjectIsNotOnboarded,) |
| 47 | + |
| 48 | + def _run(self) -> TaskResults: |
| 49 | + package = self.project.repo |
| 50 | + logger.debug(f"Running onboarding for {package}") |
| 51 | + |
| 52 | + # generate and load config |
| 53 | + initializer = DistGitInitializer( |
| 54 | + config=Config(), |
| 55 | + path_or_url="", |
| 56 | + upstream_git_url=None, |
| 57 | + ) |
| 58 | + self.package_config = self.job_config = PackageConfig.get_from_dict( |
| 59 | + initializer.package_config_dict | {"downstream_package_name": package}, |
| 60 | + ) |
| 61 | + |
| 62 | + self.perform_onboarding( |
| 63 | + config=initializer.package_config_content, |
| 64 | + open_pr=self.data.event_dict.get("open_pr", True), |
| 65 | + ) |
| 66 | + |
| 67 | + return TaskResults(success=True, details={}) |
| 68 | + |
| 69 | + def perform_onboarding(self, config: str, open_pr: bool) -> None: |
| 70 | + # clone the repo and fetch rawhide |
| 71 | + self.packit_api.dg.create_branch( |
| 72 | + "rawhide", |
| 73 | + base="remotes/origin/rawhide", |
| 74 | + setup_tracking=True, |
| 75 | + ) |
| 76 | + self.packit_api.dg.update_branch("rawhide") |
| 77 | + self.packit_api.dg.switch_branch("rawhide", force=True) |
| 78 | + |
| 79 | + if open_pr: |
| 80 | + self.packit_api.dg.create_branch(ONBOARD_BRANCH_NAME) |
| 81 | + self.packit_api.dg.switch_branch(ONBOARD_BRANCH_NAME, force=True) |
| 82 | + self.packit_api.dg.reset_workdir() |
| 83 | + |
| 84 | + working_dir = self.packit_api.dg.local_project.working_dir |
| 85 | + |
| 86 | + # create config file |
| 87 | + (working_dir / CONFIG_FILE_NAME).write_text(config) |
| 88 | + |
| 89 | + # create or update .gitignore |
| 90 | + with (working_dir / ".gitignore").open("a") as f: |
| 91 | + print("prepare_sources_result*/", file=f) |
| 92 | + |
| 93 | + self.packit_api.dg.commit( |
| 94 | + title=COMMIT_MESSAGE, |
| 95 | + msg="", |
| 96 | + prefix="", |
| 97 | + ) |
| 98 | + |
| 99 | + if open_pr: |
| 100 | + self.packit_api.push_and_create_pr( |
| 101 | + pr_title=DG_ONBOARDING_TITLE, |
| 102 | + pr_description=DG_ONBOARDING_DESCRIPTION, |
| 103 | + git_branch="rawhide", |
| 104 | + repo=self.packit_api.dg, |
| 105 | + ) |
| 106 | + else: |
| 107 | + self.packit_api.dg.push(refspec="HEAD:rawhide") |
0 commit comments