def run_build(
        self, target: Optional[str] = None
    ) -> Tuple[Optional[int], Optional[str]]:
        """
        Trigger the build and return id and web_url
        :param target: str, run for all if not set
        :return: task_id, task_url
        """

        owner = self.job_owner or self.api.copr_helper.configured_owner
        if not owner:
            raise PackitCoprException(
                f"Copr owner not set. Use Copr config file or `--owner` when calling packit CLI."
            )

        self.api.copr_helper.create_copr_project_if_not_exists(
            project=self.job_project,
            chroots=list(self.build_targets),
            owner=owner,
            description=None,
            instructions=None,
        )
        logger.debug(
            f"owner={owner}, project={self.job_project}, path={self.srpm_path}"
        )

        build = self.api.copr_helper.copr_client.build_proxy.create_from_file(
            ownername=owner, projectname=self.job_project, path=self.srpm_path
        )
        return build.id, self.api.copr_helper.copr_web_build_url(build)
Exemple #2
0
    def run_copr_build(
        self,
        project: str,
        chroots: List[str],
        owner: str = None,
        description: str = None,
        instructions: str = None,
        upstream_ref: str = None,
        list_on_homepage: bool = False,
        preserve_project: bool = False,
        additional_packages: List[str] = None,
        additional_repos: List[str] = None,
    ) -> Tuple[int, str]:
        """
        Submit a build to copr build system using an SRPM using the current checkout.

        :param project: name of the copr project to build
                        inside (defaults to something long and ugly)
        :param chroots: a list of COPR chroots (targets) e.g. fedora-rawhide-x86_64
        :param owner: defaults to username from copr config file
        :param description: description of the project
        :param instructions: installation instructions for the project
        :param upstream_ref: git ref to upstream commit
        :param list_on_homepage: if set, created copr project will be visible on copr's home-page
        :param preserve_project: if set, project will not be created as temporary
        :param list additional_packages: buildroot packages for the chroot [DOES NOT WORK YET]
        :param list additional_repos: buildroot additional additional_repos
        :return: id of the created build and url to the build web page
        """
        srpm_path = self.create_srpm(
            upstream_ref=upstream_ref,
            srpm_dir=self.up.local_project.working_dir)

        owner = owner or self.copr_helper.configured_owner
        if not owner:
            raise PackitCoprException(
                "Copr owner not set. Use Copr config file or `--owner` when calling packit CLI."
            )

        self.copr_helper.create_copr_project_if_not_exists(
            project=project,
            chroots=chroots,
            owner=owner,
            description=description,
            instructions=instructions,
            list_on_homepage=list_on_homepage,
            preserve_project=preserve_project,
            additional_packages=additional_packages,
            additional_repos=additional_repos,
        )
        logger.debug(f"Submitting a build to copr build system,"
                     f"owner={owner}, project={project}, path={srpm_path}")

        build = self.copr_helper.copr_client.build_proxy.create_from_file(
            ownername=owner, projectname=project, path=srpm_path)
        return build.id, self.copr_helper.copr_web_build_url(build)
Exemple #3
0
    def create_copr_project_if_not_exists(
        self,
        project: str,
        chroots: List[str],
        owner: str = None,
        description: str = None,
        instructions: str = None,
    ) -> None:
        """
        Create a project in copr if it does not exists.

        Raises PackitCoprException on any problems.
        """
        owner = owner or self.configured_owner

        if not owner:
            raise PackitCoprException(
                f"Copr owner not set. Use Copr config file or `--owner` when calling packit CLI."
            )

        try:
            copr_proj = self.copr_client.project_proxy.get(ownername=owner,
                                                           projectname=project)
            # make sure or project has chroots set correctly
            if set(copr_proj.chroot_repos.keys()) != set(chroots):
                logger.info(f"Updating targets on project {owner}/{project}")
                logger.debug(f"old = {set(copr_proj.chroot_repos.keys())}")
                logger.debug(f"new = {set(chroots)}")
                self.copr_client.project_proxy.edit(
                    owner,
                    project,
                    chroots=chroots,
                    description=description,
                    instructions=instructions,
                )
        except CoprNoResultException as ex:
            if owner != self.configured_owner:
                raise PackitCoprProjectException(
                    f"Copr project {owner}/{project} not found.") from ex

            logger.info(
                f"Copr project {owner}/{project} not found. Creating new.")
            self.create_copr_project(chroots, description, instructions, owner,
                                     project)
Exemple #4
0
    def create_copr_project_if_not_exists(self) -> str:
        """
        Create project in Copr.

        Returns:
            str owner
        """
        owner = self.job_owner or self.api.copr_helper.configured_owner
        if not owner:
            raise PackitCoprException(
                "Copr owner not set. Use Copr config file or `--owner` when calling packit CLI."
            )

        try:
            overwrite_booleans = owner == "packit"
            self.api.copr_helper.create_copr_project_if_not_exists(
                project=self.job_project,
                chroots=list(self.build_targets_all),
                owner=owner,
                description=None,
                instructions=None,
                list_on_homepage=self.list_on_homepage if overwrite_booleans else None,
                preserve_project=self.preserve_project if overwrite_booleans else None,
                additional_repos=self.additional_repos,
                request_admin_if_needed=True,
            )
        except PackitCoprSettingsException as ex:
            # notify user first, PR if exists, commit comment otherwise
            table = (
                "| field | old value | new value |\n"
                "| ----- | --------- | --------- |\n"
            )
            for field, (old, new) in ex.fields_to_change.items():
                table += f"| {field} | {old} | {new} |\n"

            boolean_note = ""
            if "unlisted_on_hp" in ex.fields_to_change:
                boolean_note += (
                    "The `unlisted_on_hp` field is represented as `list_on_homepage`"
                    " in the packit config."
                    "By default we create projects with `list_on_homepage: False`.\n"
                )

            if "delete_after_days" in ex.fields_to_change:
                boolean_note += (
                    "The `delete_after_days` field is represented as `preserve_project`"
                    " in the packit config (`True` is `-1` and `False` is `60`)."
                    "By default we create projects with `preserve: True` "
                    "which means `delete_after_days=60`.\n"
                )

            permissions_url = self.api.copr_helper.get_copr_settings_url(
                owner, self.job_project, section="permissions"
            )
            settings_url = self.api.copr_helper.get_copr_settings_url(
                owner, self.job_project
            )

            msg = (
                "Based on your Packit configuration the settings "
                f"of the {owner}/{self.job_project} "
                "Copr project would need to be updated as follows:\n"
                "\n"
                f"{table}"
                "\n"
                f"{boolean_note}"
                "\n"
                "Packit was unable to update the settings above as it is missing `admin` "
                f"permissions on the {owner}/{self.job_project} Copr project.\n"
                "\n"
                "To fix this you can do one of the following:\n"
                "\n"
                f"- Grant Packit `admin` permissions on the {owner}/{self.job_project} "
                f"Copr project on the [permissions page]({permissions_url}).\n"
                "- Change the above Copr project settings manually "
                f"on the [settings page]({settings_url}) "
                "to match the Packit configuration.\n"
                "- Update the Packit configuration to match the Copr project settings.\n"
                "\n"
                "Please retrigger the build, once the issue above is fixed.\n"
            )
            self.status_reporter.comment(body=msg)
            raise ex

        return owner
Exemple #5
0
    def run_build(
            self,
            target: Optional[str] = None
    ) -> Tuple[Optional[int], Optional[str]]:
        """
        Trigger the build and return id and web_url
        :param target: str, run for all if not set
        :return: task_id, task_url
        """

        owner = self.job_owner or self.api.copr_helper.configured_owner
        if not owner:
            raise PackitCoprException(
                "Copr owner not set. Use Copr config file or `--owner` when calling packit CLI."
            )

        try:
            overwrite_booleans = owner == "packit"
            self.api.copr_helper.create_copr_project_if_not_exists(
                project=self.job_project,
                chroots=list(self.build_targets_all),
                owner=owner,
                description=None,
                instructions=None,
                list_on_homepage=self.list_on_homepage
                if overwrite_booleans else None,
                preserve_project=self.preserve_project
                if overwrite_booleans else None,
                additional_repos=self.additional_repos,
                request_admin_if_needed=True,
            )
        except PackitCoprSettingsException as ex:
            # notify user first, PR if exists, commit comment otherwise
            table = ("| field | old value | new value |\n"
                     "| ----- | --------- | --------- |\n")
            for field, (old, new) in ex.fields_to_change.items():
                table += f"| {field} | {old} | {new} |\n"

            boolean_note = ""
            if "unlisted_on_hp" in ex.fields_to_change:
                boolean_note += (
                    "The `unlisted_on_hp` field is represented as `list_on_homepage`"
                    " in the packit config."
                    "By default we create projects with `list_on_homepage: False`.\n"
                )

            if "delete_after_days" in ex.fields_to_change:
                boolean_note += (
                    "The `delete_after_days` field is represented as `preserve_project`"
                    " in the packit config (`True` is `-1` and `False` is `60`)."
                    "By default we create projects with `preserve: True` "
                    "which means `delete_after_days=60`.\n")

            permissions_url = self.api.copr_helper.get_copr_settings_url(
                owner, self.job_project, section="permissions")
            settings_url = self.api.copr_helper.get_copr_settings_url(
                owner, self.job_project)

            msg = (
                "Based on your Packit configuration the settings "
                f"of the {owner}/{self.job_project} "
                "Copr project would need to be updated as follows:\n"
                "\n"
                f"{table}"
                "\n"
                f"{boolean_note}"
                "\n"
                "Packit was unable to update the settings above as it is missing `admin` "
                f"permissions on the {owner}/{self.job_project} Copr project.\n"
                "\n"
                "To fix this you can do one of the following:\n"
                "\n"
                f"- Grant Packit `admin` permissions on the {owner}/{self.job_project} "
                f"Copr project on the [permissions page]({permissions_url}).\n"
                "- Change the above Copr project settings manually "
                f"on the [settings page]({settings_url}) "
                "to match the Packit configuration.\n"
                "- Update the Packit configuration to match the Copr project settings.\n"
                "\n"
                "Please retrigger the build, once the issue above is fixed.\n")
            self.status_reporter.comment(body=msg)
            raise ex

        logger.debug(
            f"owner={owner}, project={self.job_project}, path={self.srpm_path}"
        )

        try:
            build = self.api.copr_helper.copr_client.build_proxy.create_from_file(
                ownername=owner,
                projectname=self.job_project,
                path=self.srpm_path,
                buildopts={
                    "chroots": list(self.build_targets),
                },
            )
        except CoprRequestException as ex:
            if "You don't have permissions to build in this copr." in str(
                    ex) or "is not allowed to build in the copr" in str(ex):
                self.api.copr_helper.copr_client.project_proxy.request_permissions(
                    ownername=owner,
                    projectname=self.job_project,
                    permissions={"builder": True},
                )

                # notify user, PR if exists, commit comment otherwise
                permissions_url = self.api.copr_helper.get_copr_settings_url(
                    owner, self.job_project, section="permissions")
                self.status_reporter.comment(
                    body="We have requested the `builder` permissions "
                    f"for the {owner}/{self.job_project} Copr project.\n"
                    "\n"
                    "Please confirm the request on the "
                    f"[{owner}/{self.job_project} Copr project permissions page]"
                    f"({permissions_url})"
                    " and retrigger the build.", )
            raise ex

        return build.id, self.api.copr_helper.copr_web_build_url(build)