Ejemplo n.º 1
0
 def __init__(self, config: Config, event: PullRequestCommentEvent):
     super().__init__(config=config, event=event)
     self.config = config
     self.event = event
     self.project: GitProject = self.github_service.get_project(
         repo=event.base_repo_name, namespace=event.base_repo_namespace)
     # Get the latest pull request commit
     self.event.commit_sha = self.project.get_all_pr_commits(
         self.event.pr_id)[-1]
     self.package_config: PackageConfig = get_package_config_from_repo(
         self.project, self.event.commit_sha)
     self.package_config.upstream_project_url = event.https_url
Ejemplo n.º 2
0
 def __init__(
     self, config: ServiceConfig, job: JobConfig, pr_event: PullRequestEvent
 ):
     super().__init__(config=config, job=job, event=pr_event)
     self.pr_event = pr_event
     self.project: GitProject = pr_event.get_project()
     self.package_config: PackageConfig = get_package_config_from_repo(
         self.project, pr_event.base_ref
     )
     if not self.package_config:
         raise ValueError(f"No config file found in {self.project.full_repo_name}")
     self.package_config.upstream_project_url = pr_event.project_url
Ejemplo n.º 3
0
def test_get_package_config_from_repo(content):
    flexmock(GitProject).should_receive("get_file_content").and_return(content)
    git_project = GitProject(repo="", service=GitService(), namespace="")
    config = get_package_config_from_repo(sourcegit_project=git_project,
                                          ref="")
    assert isinstance(config, PackageConfig)
    assert Path(config.specfile_path).name == "packit.spec"
    assert config.synced_files == SyncFilesConfig(files_to_sync=[
        SyncFilesItem(src="packit.spec", dest="packit.spec"),
        SyncFilesItem(src=".packit.yaml", dest=".packit2.yaml"),
    ])
    assert not config.create_pr
Ejemplo n.º 4
0
    def get_package_config(self) -> Optional[PackageConfig]:
        project = self.get_project()
        if not project:
            return None

        package_config: PackageConfig = get_package_config_from_repo(
            project, self.ref)
        if not package_config:
            return None

        package_config.upstream_project_url = self.project_url
        return package_config
Ejemplo n.º 5
0
    def __init__(self, config: Config, event: IssueCommentEvent):
        super().__init__(config=config, event=event)

        self.config = config
        self.event = event
        self.project: GitProject = self.github_service.get_project(
            repo=event.base_repo_name, namespace=event.base_repo_namespace)
        # Get the latest tag release
        self.event.tag_name = self.project.get_latest_release().tag_name
        self.package_config: PackageConfig = get_package_config_from_repo(
            self.project, self.event.tag_name)
        self.package_config.upstream_project_url = event.https_url
Ejemplo n.º 6
0
    def get_package_config_from_repo(
        project: GitProject,
        reference: Optional[str] = None,
        base_project: Optional[GitProject] = None,
        pr_id: int = None,
        fail_when_missing: bool = True,
        spec_file_path: Optional[str] = None,
    ):
        """
        Get the package config and catch the invalid config scenario and possibly no-config scenario
        """

        if not base_project and not project:
            return None

        project_to_search_in = base_project or project
        try:
            package_config: PackageConfig = get_package_config_from_repo(
                project=project_to_search_in,
                ref=reference,
                spec_file_path=spec_file_path,
            )
            if not package_config and fail_when_missing:
                raise PackitConfigException(
                    f"No config file found in {project_to_search_in.full_repo_name} "
                    "on ref '{reference}'"
                )
        except PackitConfigException as ex:
            if pr_id:
                project.pr_comment(
                    pr_id, f"Failed to load packit config file:\n```\n{str(ex)}\n```"
                )
            else:
                # TODO: filter when https://github.com/packit/ogr/issues/308 fixed
                issues = project.get_issue_list()
                if "Invalid packit config" not in [x.title for x in issues]:
                    # TODO: store in DB
                    message = (
                        f"Failed to load packit config file:\n```\n{str(ex)}\n```\n"
                        "For more info, please check out the documentation: "
                        "http://packit.dev/packit-as-a-service/ or contact us - "
                        "[Packit team]"
                        "(https://github.com/orgs/packit/teams/the-packit-team)"
                    )

                    i = project.create_issue(
                        title="[packit] Invalid config", body=message
                    )
                    logger.debug(f"Created issue for invalid packit config: {i.url}")
            raise ex
        return package_config
Ejemplo n.º 7
0
    def __init__(self, config: ServiceConfig, event: IssueCommentEvent):
        super().__init__(config=config, event=event)

        self.config = config
        self.event = event
        self.project = self.event.get_project()
        # Get the latest tag release
        self.event.tag_name = self.project.get_latest_release().tag_name
        self.package_config: PackageConfig = get_package_config_from_repo(
            self.project, self.event.tag_name)
        if not self.package_config:
            raise ValueError(
                f"No config file found in {self.project.full_repo_name}")
        self.package_config.upstream_project_url = event.project_url
Ejemplo n.º 8
0
 def __init__(self, config: ServiceConfig, event: PullRequestCommentEvent):
     super().__init__(config=config, event=event)
     self.config = config
     self.event = event
     self.project: GitProject = event.get_project()
     # Get the latest pull request commit
     self.event.commit_sha = self.project.get_all_pr_commits(self.event.pr_id)[-1]
     self.event.base_ref = self.event.commit_sha
     self.package_config: PackageConfig = get_package_config_from_repo(
         self.project, self.event.commit_sha
     )
     if not self.package_config:
         raise ValueError(f"No config file found in {self.project.full_repo_name}")
     self.package_config.upstream_project_url = event.project_url
Ejemplo n.º 9
0
 def get_package_config(self) -> Optional[PackageConfig]:
     if not self.base_ref:
         self.base_ref = self.get_project().get_pr_info(
             self.pr_id).source_branch
     package_config: PackageConfig = get_package_config_from_repo(
         self.get_project(), self.base_ref)
     if not package_config:
         logger.info(
             f"no packit config found for "
             f"{self.base_repo_namespace}/{self.base_repo_name}, #{self.pr_id}"
         )
         return None
     package_config.upstream_project_url = self.https_url
     return package_config
Ejemplo n.º 10
0
 def __init__(
     self,
     config: ServiceConfig,
     job: JobConfig,
     test_results_event: TestingFarmResultsEvent,
 ):
     super().__init__(config=config, job=job, event=test_results_event)
     self.project: GitProject = test_results_event.get_project()
     self.package_config: PackageConfig = get_package_config_from_repo(
         self.project, test_results_event.ref
     )
     if not self.package_config:
         raise ValueError(f"No config file found in {self.project.full_repo_name}")
     self.package_config.upstream_project_url = test_results_event.project_url
Ejemplo n.º 11
0
    def get_package_config_from_repo(
        project: GitProject,
        reference: Optional[str] = None,
        base_project: Optional[GitProject] = None,
        pr_id: int = None,
        fail_when_missing: bool = True,
        spec_file_path: Optional[str] = None,
    ) -> Optional[PackageConfig]:
        """
        Get the package config and catch the invalid config scenario and possibly no-config scenario
        """

        if not base_project and not project:
            return None

        project_to_search_in = base_project or project
        try:
            package_config: PackageConfig = get_package_config_from_repo(
                project=project_to_search_in,
                ref=reference,
                spec_file_path=spec_file_path,
            )
            if not package_config and fail_when_missing:
                raise PackitMissingConfigException(
                    f"No config file for packit (e.g. `.packit.yaml`) found in "
                    f"{project_to_search_in.full_repo_name} on commit {reference}"
                )
        except PackitConfigException as ex:
            message = (
                f"{str(ex)}\n\n"
                if isinstance(ex, PackitMissingConfigException)
                else f"Failed to load packit config file:\n```\n{str(ex)}\n```\n"
            )
            message += (
                "For more info, please check out the documentation: "
                "https://packit.dev/docs/packit-service or contact us - "
                "[Packit team]"
                "(https://github.com/orgs/packit/teams/the-packit-team)"
            )

            if pr_id:
                project.get_pr(pr_id).comment(message)
            elif created_issue := PackageConfigGetter.create_issue_if_needed(
                project, title="Invalid config", message=message
            ):
                logger.debug(
                    f"Created issue for invalid packit config: {created_issue.url}"
                )
            raise ex
Ejemplo n.º 12
0
def test_get_package_config_from_repo_spec_file_not_defined(content):
    specfile_path = "packit.spec"
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_file_content").and_return(content)
    gp.should_receive("get_files").and_return([specfile_path])
    git_project = GitProject(repo="", service=GitService(), namespace="")
    config = get_package_config_from_repo(project=git_project, ref="")
    assert isinstance(config, PackageConfig)
    assert Path(config.specfile_path).name == specfile_path
    assert config.create_pr
    for j in config.jobs:
        assert j.specfile_path == specfile_path
        assert j.downstream_package_name == config.downstream_package_name
        assert j.upstream_package_name == config.upstream_package_name
Ejemplo n.º 13
0
    def get_package_config(self) -> Optional[PackageConfig]:
        releases = self.get_project().get_releases()

        if releases:
            self.tag_name = releases[0].tag_name
        package_config: PackageConfig = get_package_config_from_repo(
            self.get_project(), self.tag_name)
        if not package_config:
            logger.info(
                f"no packit config found for "
                f"{self.base_repo_namespace}/{self.base_repo_name}, #{self.issue_id}"
            )
            return None
        package_config.upstream_project_url = self.https_url
        return package_config
Ejemplo n.º 14
0
 def __init__(
     self,
     config: Config,
     job: JobConfig,
     test_results_event: TestingFarmResultsEvent,
 ):
     super().__init__(config=config, job=job, event=test_results_event)
     self.project: GitProject = self.github_service.get_project(
         repo=test_results_event.repo_name,
         namespace=test_results_event.repo_namespace,
     )
     self.package_config: PackageConfig = get_package_config_from_repo(
         self.project, test_results_event.ref
     )
     self.package_config.upstream_project_url = test_results_event.https_url
Ejemplo n.º 15
0
    def setUp(self):
        super().setUp()
        self.conf = self.get_test_config()
        self.static_tmp = "/tmp/packit_tmp"
        os.makedirs(self.static_tmp, exist_ok=True)
        TempFile.root = self.static_tmp
        self.project_ogr = self.conf.get_project(
            url="https://github.com/packit-service/ogr")

        self.pc = get_package_config_from_repo(project=self.project_ogr,
                                               ref="master")
        if not self.pc:
            raise RuntimeError("Package config not found.")
        self.dg = packit.distgit.DistGit(self.conf, self.pc)
        self.lp = LocalProject(git_project=self.project_ogr)
        self.upstream = packit.upstream.Upstream(self.conf,
                                                 self.pc,
                                                 local_project=self.lp)
Ejemplo n.º 16
0
    def setUp(self):
        self.conf = self.get_test_config()
        response_file = self.get_datafile_filename()
        PersistentObjectStorage().storage_file = response_file
        PersistentObjectStorage().dump_after_store = True

        self.project_ogr = self.conf.get_project(
            url="https://github.com/packit-service/ogr")

        self.pc = get_package_config_from_repo(
            sourcegit_project=self.project_ogr, ref="master")
        if not self.pc:
            raise RuntimeError("Package config not found.")
        self.dg = packit.distgit.DistGit(self.conf, self.pc)
        self.lp = LocalProject(git_project=self.project_ogr)
        self.upstream = packit.upstream.Upstream(self.conf,
                                                 self.pc,
                                                 local_project=self.lp)
Ejemplo n.º 17
0
    def get_package_config_from_repo(
        self,
        project: GitProject,
        reference: str,
        pr_id: int = None,
        fail_when_missing: bool = True,
    ):
        """
        Get the package config and catch the invalid config scenario and possibly no-config scenario
        Static because of the easier mocking.
        """
        try:
            package_config: PackageConfig = get_package_config_from_repo(
                project, reference
            )
            if not package_config and fail_when_missing:
                raise PackitConfigException(
                    f"No config file found in {project.full_repo_name}"
                )
        except PackitConfigException as ex:
            if pr_id:
                project.pr_comment(
                    pr_id, f"Failed to load packit config file:\n```\n{str(ex)}\n```"
                )
            else:
                # TODO: filter when https://github.com/packit-service/ogr/issues/308 fixed
                issues = project.get_issue_list()
                if "Invalid packit config" not in [x.title for x in issues]:
                    # TODO: store in DB
                    message = (
                        f"Failed to load packit config file:\n```\n{str(ex)}\n```\n"
                        "For more info, please check out the documentation: "
                        "http://packit.dev/packit-as-a-service/ or contact us - "
                        "[Packit team]"
                        "(https://github.com/orgs/packit-service/teams/the-packit-team)"
                    )

                    i = project.create_issue(
                        title="[packit] Invalid config", body=message
                    )
                    logger.debug(f"Created issue for invalid packit config: {i.url}")
            raise ex
        return package_config
Ejemplo n.º 18
0
def test_get_package_config_from_repo(
    content,
    project: GitProject,
    mock_spec_search: bool,
    spec_path: Optional[str],
    spec_path_option: Optional[str],
):
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_file_content").and_return(content)
    if mock_spec_search:
        gp.should_receive("get_files").and_return(["packit.spec"]).once()
    config = get_package_config_from_repo(project=project,
                                          spec_file_path=spec_path_option)
    assert isinstance(config, PackageConfig)
    assert config.specfile_path == spec_path
    assert config.files_to_sync == [
        SyncFilesItem(src=["packit.spec"], dest="packit.spec"),
        SyncFilesItem(src=[".packit.yaml"], dest=".packit2.yaml"),
    ]
    assert config.create_pr
Ejemplo n.º 19
0
    def get_job_input_from_github_pr(
        self, event: dict
    ) -> Optional[Tuple[JobTriggerType, PackageConfig, GitProject]]:
        """ look into the provided event and see if it's one for a new github pr """
        action = nested_get(event, "action")
        logger.debug(f"action = {action}")
        pr_id = nested_get(event, "number")
        is_pr = nested_get(event, "pull_request")
        if not is_pr:
            logger.info("Not a pull request event.")
            return None
        if action in ["opened", "reopened", "synchronize"] and pr_id:
            # we can't use head repo here b/c the app is set up against the upstream repo
            # and not the fork, on the other hand, we don't process packit.yaml from
            # the PR but what's in the upstream
            base_repo_namespace = nested_get(event, "pull_request", "base",
                                             "repo", "owner", "login")
            base_repo_name = nested_get(event, "pull_request", "base", "repo",
                                        "name")

            if not (base_repo_name and base_repo_namespace):
                logger.warning(
                    "We could not figure out the full name of the repository.")
                return None
            base_ref = nested_get(event, "pull_request", "head", "sha")
            if not base_ref:
                logger.warning("Ref where the PR is coming from is not set.")
                return None
            target_repo = nested_get(event, "repository", "full_name")
            logger.info(
                f"GitHub pull request {pr_id} event for repo {target_repo}.")
            gh_proj = get_github_project(self.config,
                                         repo=base_repo_name,
                                         namespace=base_repo_namespace)
            package_config = get_package_config_from_repo(gh_proj, base_ref)
            https_url = event["repository"]["html_url"]
            package_config.upstream_project_url = https_url
            return JobTriggerType.pull_request, package_config, gh_proj
        return None
Ejemplo n.º 20
0
    def __init__(
        self,
        config: Config,
        job: JobConfig,
        pr_event: Union[PullRequestEvent, PullRequestCommentEvent],
    ):
        super().__init__(config=config, job=job, event=pr_event)
        self.project: GitProject = self.github_service.get_project(
            repo=pr_event.base_repo_name,
            namespace=pr_event.base_repo_namespace)
        if isinstance(pr_event, PullRequestEvent):
            self.base_ref = pr_event.base_ref
        elif isinstance(pr_event, PullRequestCommentEvent):
            self.base_ref = pr_event.commit_sha
        self.package_config: PackageConfig = get_package_config_from_repo(
            self.project, self.base_ref)
        self.package_config.upstream_project_url = pr_event.https_url

        self.session = requests.session()
        adapter = requests.adapters.HTTPAdapter(max_retries=5)
        self.insecure = False
        self.session.mount("https://", adapter)
        self.header: dict = {"Content-Type": "application/json"}
Ejemplo n.º 21
0
    def __init__(
        self,
        config: ServiceConfig,
        job: JobConfig,
        event: Union[PullRequestEvent, ReleaseEvent],
    ):
        super().__init__(config=config, job=job, event=event)

        if isinstance(event, PullRequestEvent):
            base_ref = event.base_ref
        elif isinstance(event, ReleaseEvent):
            base_ref = event.tag_name
        else:
            raise PackitException(
                "Unknown event, only PREvent and ReleaseEvent are accepted.")

        self.project: GitProject = event.get_project()
        self.package_config: PackageConfig = get_package_config_from_repo(
            self.project, base_ref)
        if not self.package_config:
            raise ValueError(
                f"No config file found in {self.project.full_repo_name}")
        self.package_config.upstream_project_url = event.project_url
Ejemplo n.º 22
0
    def __init__(
        self,
        config: ServiceConfig,
        job: JobConfig,
        event: Union[CoprBuildEvent, PullRequestCommentEvent],
    ):
        super().__init__(config=config, job=job, event=event)
        self.project: GitProject = event.get_project()
        if isinstance(event, CoprBuildEvent):
            self.base_ref = event.ref
        elif isinstance(event, PullRequestCommentEvent):
            self.base_ref = event.commit_sha
        self.package_config: PackageConfig = get_package_config_from_repo(
            self.project, self.base_ref)
        if not self.package_config:
            raise ValueError(
                f"No config file found in {self.project.full_repo_name}")
        self.package_config.upstream_project_url = event.project_url

        self.session = requests.session()
        adapter = requests.adapters.HTTPAdapter(max_retries=5)
        self.insecure = False
        self.session.mount("https://", adapter)
        self.header: dict = {"Content-Type": "application/json"}
Ejemplo n.º 23
0
 def pc(self):
     if not self._pc:
         self._pc = get_package_config_from_repo(project=self.project)
     if not self._pc:
         raise RuntimeError("Package config not found.")
     return self._pc
Ejemplo n.º 24
0
 def get_package_config(self):
     package_config: PackageConfig = get_package_config_from_repo(
         self.get_project(), self.ref)
     package_config.upstream_project_url = self.https_url
     return package_config
Ejemplo n.º 25
0
 def package_config(self):
     if not self._package_config:
         self._package_config = get_package_config_from_repo(
             self.project, self.git_ref)
     return self._package_config
Ejemplo n.º 26
0
 def get_package_config(self):
     return get_package_config_from_repo(self.get_project(), self.git_ref)