Пример #1
0
    def get_project(self):
        """
        Create ogr project instance based on provided configuration.
        Project instance is used for manipulating with Github/Pagure repo.
        :return: ogr Github/Pagure project instance or None
        """
        # Return instance for github app
        if self.github_app_id:
            github_cert = Path(self.github_app_cert_path).read_text()

            if self.github_app_installation_id:
                # github token will be used as a credential over http (commit/push)
                github_app = GitHubApp(self.github_app_id,
                                       self.github_app_cert_path)
                self.github_token = github_app.get_installation_access_token(
                    self.github_app_installation_id)

            return get_project(url=self.clone_url,
                               custom_instances=[
                                   GithubService(
                                       token=None,
                                       github_app_id=self.github_app_id,
                                       github_app_private_key=github_cert)
                               ])

        # Return instance for regular user (local machine)
        return get_project(url=self.clone_url,
                           custom_instances=[
                               GithubService(token=self.github_token),
                               PagureService(
                                   token=self.pagure_token,
                                   instance_url=self.pagure_instance_url)
                           ])
Пример #2
0
    def setUp(self):
        self.github_token = os.environ.get("GITHUB_TOKEN")
        self.github_user = os.environ.get("GITHUB_USER")
        self.pagure_token = os.environ.get("PAGURE_TOKEN")
        self.pagure_user = os.environ.get("PAGURE_USER")

        test_name = self.id() or "all"

        persistent_data_file = os.path.join(
            PERSISTENT_DATA_PREFIX, f"test_factory_data_{test_name}.yaml")
        self.persistent_object_storage = PersistentObjectStorage(
            persistent_data_file)

        if self.persistent_object_storage.is_write_mode and (
                not self.github_user or not self.github_token):
            raise EnvironmentError(
                "please set GITHUB_TOKEN GITHUB_USER env variables")

        if self.persistent_object_storage.is_write_mode and (
                not self.pagure_user or not self.pagure_token):
            raise EnvironmentError(
                "please set PAGURE_TOKEN PAGURE_USER env variables")

        self.github_service = GithubService(
            token=self.github_token,
            persistent_storage=self.persistent_object_storage)

        self.pagure_service = PagureMockAPI(
            token=self.pagure_token,
            persistent_storage=self.persistent_object_storage)
Пример #3
0
    def setUp(self):
        super().setUp()
        print(PersistentObjectStorage().storage_file)
        self.github_token = os.environ.get("GITHUB_TOKEN")
        self.pagure_token = os.environ.get("PAGURE_TOKEN")
        self.gitlab_token = os.environ.get("GITLAB_TOKEN") or "some_token"

        if PersistentObjectStorage().mode == StorageMode.write:
            if not self.github_token:
                raise EnvironmentError(
                    "You are in requre write mode, please set GITHUB_TOKEN env variables"
                )
            if not self.pagure_token:
                raise EnvironmentError(
                    "You are in requre write mode, please set PAGURE_TOKEN env variables"
                )
            if not os.environ.get("GITLAB_TOKEN"):
                raise EnvironmentError(
                    "You are in requre write mode, please set GITLAB_TOKEN env variables"
                )

        self.github_service = GithubService(token=self.github_token)
        self.pagure_service = PagureService(token=self.pagure_token)
        self.gitlab_service = GitlabService(
            token=self.gitlab_token, instance_url="https://gitlab.com"
        )
        self.custom_instances = [
            self.github_service,
            self.pagure_service,
            self.gitlab_service,
        ]
Пример #4
0
def test_github_app(upstream_instance, tmpdir):
    u, ups = upstream_instance
    t = Path(tmpdir)

    fake_cert_path = t / "fake-cert.pem"
    fake_cert_path.write_text("hello!")

    user_config_file_path = t / ".packit.yaml"
    user_config_file_path.write_text(
        "---\n"
        f"authentication:\n"
        f"    github.com:\n"
        f"        github_app_private_key_path: {fake_cert_path}\n"
        f"        github_app_id: qwe\n"
    )
    flexmock(os).should_receive("getenv").with_args("XDG_CONFIG_HOME").and_return(
        str(tmpdir)
    )
    ups.config = Config.get_user_config()
    assert (
        GithubService(
            github_app_private_key_path=str(fake_cert_path), github_app_id="qwe"
        )
        in ups.config.services
    )
Пример #5
0
    def setUp(self):
        self.token = os.environ.get("GITHUB_TOKEN")
        self.user = os.environ.get("GITHUB_USER")
        test_name = self.id() or "all"

        persistent_data_file = os.path.join(
            PERSISTENT_DATA_PREFIX, f"test_github_data_{test_name}.yaml")
        PersistentObjectStorage().storage_file = persistent_data_file

        if PersistentObjectStorage().is_write_mode and (not self.user
                                                        or not self.token):
            raise EnvironmentError(
                "please set GITHUB_TOKEN GITHUB_USER env variables")

        self.service = GithubService(token=self.token)

        self.ogr_project = self.service.get_project(namespace="packit-service",
                                                    repo="ogr")

        self.ogr_fork = self.service.get_project(namespace="packit-service",
                                                 repo="ogr",
                                                 is_fork=True)

        self.hello_world_project = self.service.get_project(
            namespace="packit-service", repo="hello-world")

        self.not_forked_project = self.service.get_project(
            namespace="fedora-modularity", repo="fed-to-brew")
Пример #6
0
def test_get_user_config_new_authentication(tmp_path):
    user_config_file_path = tmp_path / ".packit.yaml"
    user_config_file_path.write_text(
        "---\n"
        "debug: true\n"
        "fas_user: rambo\n"
        "keytab_path: './rambo.keytab'\n"
        "authentication:\n"
        "    github.com:\n"
        "        token: GITHUB_TOKEN\n"
        "    pagure:\n"
        "        token: PAGURE_TOKEN\n"
        '        instance_url: "https://my.pagure.org"\n')
    flexmock(os).should_receive("getenv").with_args(
        "XDG_CONFIG_HOME").and_return(str(tmp_path))
    config = Config.get_user_config()
    assert config.debug and isinstance(config.debug, bool)
    assert config.fas_user == "rambo"
    assert config.keytab_path == "./rambo.keytab"
    assert config.kerberos_realm == "FEDORAPROJECT.ORG"

    assert GithubService(token="GITHUB_TOKEN") in config.services
    assert (PagureService(token="PAGURE_TOKEN",
                          instance_url="https://my.pagure.org")
            in config.services)
Пример #7
0
 def test_private_key_path(self):
     with tempfile.NamedTemporaryFile() as pr_key:
         Path(pr_key.name).write_text(TESTING_PRIVATE_KEY)
         service = GithubService(
             github_app_id="123", github_app_private_key_path=pr_key.name
         )
         assert service.github_app_private_key == TESTING_PRIVATE_KEY
Пример #8
0
    def setUp(self):
        self.github_token = os.environ.get("GITHUB_TOKEN")
        self.pagure_token = os.environ.get("PAGURE_TOKEN")
        self.gitlab_token = os.environ.get("GITLAB_TOKEN") or "some_token"

        test_name = self.id() or "all"

        persistent_data_file = os.path.join(
            PERSISTENT_DATA_PREFIX, f"test_factory_data_{test_name}.yaml")
        PersistentObjectStorage().storage_file = persistent_data_file

        if PersistentObjectStorage().is_write_mode and not self.github_token:
            raise EnvironmentError("please set GITHUB_TOKEN env variables")

        if PersistentObjectStorage().is_write_mode and not self.pagure_token:
            raise EnvironmentError("please set PAGURE_TOKEN env variables")

        if PersistentObjectStorage(
        ).is_write_mode and not os.environ.get("GITLAB_TOKEN"):
            raise EnvironmentError("please set GITLAB_TOKEN env variables")

        self.github_service = GithubService(token=self.github_token)
        self.pagure_service = PagureService(token=self.pagure_token)
        self.gitlab_service = GitlabService(token=self.gitlab_token,
                                            instance_url="https://gitlab.com")
        self.custom_instances = [
            self.github_service,
            self.pagure_service,
            self.gitlab_service,
        ]
Пример #9
0
 def test_get_project_having_key_as_path(self):
     service = GithubService(
         github_app_id=self.github_app_id,
         github_app_private_key_path=self.github_app_private_key_path,
     )
     project = service.get_project(namespace="packit", repo="ogr")
     assert project
     assert project.github_repo
Пример #10
0
    def get_test_config():
        conf = Config()
        pagure_user_token = os.environ.get("PAGURE_TOKEN", "test")
        github_token = os.environ.get("GITHUB_TOKEN", "test")
        conf.services = {
            PagureService(token=pagure_user_token),
            GithubService(token=github_token),
        }

        conf.dry_run = True
        return conf
Пример #11
0
 def test_github_proj_no_app_creds(self):
     service = GithubService(github_app_id="123",
                             github_app_private_key=TESTING_PRIVATE_KEY)
     project = GithubProject(repo="packit",
                             service=service,
                             namespace="packit")
     with pytest.raises(OgrException) as exc:
         assert project.github_instance
     mes = str(exc.value)
     assert "No installation ID provided for packit/packit" in mes
     assert "make sure that you provided correct credentials" in mes
Пример #12
0
    def test_get_project(self):
        github_app_private_key = (
            Path(self.github_app_private_key_path).read_text()
            if self.github_app_private_key_path else TESTING_PRIVATE_KEY)

        self.service = GithubService(
            github_app_id=self.github_app_id or "123",
            github_app_private_key=github_app_private_key,
        )
        project = self.service.get_project(namespace="packit", repo="ogr")
        assert project
        assert project.github_repo
Пример #13
0
    def setUp(self):
        super().setUp()
        self.token = os.environ.get("GITHUB_TOKEN")
        if PersistentObjectStorage().mode == StorageMode.write and not self.token:
            raise EnvironmentError(
                "You are in Requre write mode, please set GITHUB_TOKEN env variables"
            )

        self.service = GithubService(token=self.token, read_only=True)
        self.ogr_project = self.service.get_project(
            namespace="packit-service", repo="ogr"
        )
Пример #14
0
    def load_authentication(raw_dict):
        services = set()
        deprecated_keys = [
            "github_app_id",
            "github_app_cert_path",
            "github_token",
            "pagure_user_token",
            "pagure_instance_url",
            "pagure_fork_token",
        ]
        if "authentication" in raw_dict:
            services = get_instances_from_dict(instances=raw_dict["authentication"])
        elif any(key in raw_dict for key in deprecated_keys):
            logger.warning(
                "Please, "
                "use 'authentication' key in the user configuration "
                "to set tokens for GitHub and Pagure. "
                "New method supports more services and direct keys will be removed in the future.\n"
                "Example:\n"
                "authentication:\n"
                "    github.com:\n"
                "        token: GITHUB_TOKEN\n"
                "    pagure:\n"
                "        token: PAGURE_TOKEN\n"
                '        instance_url: "https://src.fedoraproject.org"\n'
                "See our documentation for more information "
                "http://packit.dev/docs/configuration/#user-configuration-file. "
            )
            github_app_id = raw_dict.get("github_app_id")
            github_app_cert_path = raw_dict.get("github_app_cert_path")
            github_token = raw_dict.get("github_token")
            services.add(
                GithubService(
                    token=github_token,
                    github_app_id=github_app_id,
                    github_app_private_key_path=github_app_cert_path,
                )
            )
            pagure_user_token = raw_dict.get("pagure_user_token")
            pagure_instance_url = raw_dict.get(
                "pagure_instance_url", "https://src.fedoraproject.org"
            )
            if raw_dict.get("pagure_fork_token"):
                warnings.warn(
                    "packit no longer accepts 'pagure_fork_token'"
                    " value (https://github.com/packit-service/packit/issues/495)"
                )
            services.add(
                PagureService(token=pagure_user_token, instance_url=pagure_instance_url)
            )

        return services
Пример #15
0
    def setUp(self):
        self.token = os.environ.get("GITHUB_TOKEN")
        test_name = self.id() or "all"
        persistent_data_file = os.path.join(
            PERSISTENT_DATA_PREFIX, f"test_github_data_{test_name}.yaml")
        PersistentObjectStorage().storage_file = persistent_data_file
        if PersistentObjectStorage(
        ).mode == StorageMode.write and not self.token:
            raise EnvironmentError("please set GITHUB_TOKEN env variables")

        self.service = GithubService(token=self.token, read_only=True)
        self.ogr_project = self.service.get_project(namespace="packit-service",
                                                    repo="ogr")
Пример #16
0
    def setUp(self):
        super().setUp()
        self.token = os.environ.get("GITHUB_TOKEN")
        if PersistentObjectStorage().mode == StorageMode.write and (not self.token):
            raise EnvironmentError(
                "You are in Requre write mode, please set proper GITHUB_TOKEN env variables"
            )

        self.service = GithubService(token=self.token)
        self._ogr_project = None
        self._ogr_fork = None
        self._hello_world_project = None
        self._not_forked_project = None
Пример #17
0
 def get_project(self):
     """
     Create ogr project instance based on provided configuration.
     Project instance is used for manipulating with Github/Pagure repo.
     :return: ogr Github/Pagure project instance or None
     """
     return get_project(url=self.clone_url,
                        custom_instances=[
                            GithubService(token=self.github_token),
                            PagureService(
                                token=self.pagure_token,
                                instance_url=self.pagure_instance_url)
                        ])
Пример #18
0
def test_pr_id_and_ref(tmp_path: Path, merge, hops_file_content):
    """p-s passes both ref and pr_id, we want to check out PR"""
    remote = tmp_path / "remote"
    remote.mkdir()
    create_new_repo(remote, ["--bare"])
    upstream_git = tmp_path / "upstream_git"
    upstream_git.mkdir()
    initiate_git_repo(upstream_git, push=True, upstream_remote=str(remote))
    # mimic github PR
    pr_id = "123"
    ref = (
        subprocess.check_output(["git", "rev-parse", "HEAD^"], cwd=upstream_git)
        .strip()
        .decode()
    )
    local_tmp_branch = "asdqwe"
    subprocess.check_call(["git", "branch", local_tmp_branch, ref], cwd=upstream_git)
    subprocess.check_call(
        ["git", "push", "origin", f"{local_tmp_branch}:refs/pull/{pr_id}/head"],
        cwd=upstream_git,
    )
    subprocess.check_call(["git", "branch", "-D", local_tmp_branch], cwd=upstream_git)

    git_project = flexmock(repo="random_name", namespace="random_namespace")
    git_project.should_receive("get_pr").and_return(flexmock(target_branch="main"))

    LocalProject(
        working_dir=upstream_git,
        offline=True,
        pr_id=pr_id,
        ref=ref,
        git_service=GithubService(),
        git_project=git_project,
        merge_pr=merge,
    )

    assert (
        subprocess.check_output(
            ["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=upstream_git
        )
        .strip()
        .decode()
        == f"pr/{pr_id}"
    )
    if hops_file_content:
        # the changes are merged into main so the file should be there
        assert (upstream_git / "hops").read_text() == hops_file_content
    else:
        # we haven't merged into main, so there should be no "hops" file
        assert not (upstream_git / "hops").is_file()
Пример #19
0
    def setUp(self):
        self.token = os.environ.get("GITHUB_TOKEN")
        self.user = os.environ.get("GITHUB_USER")
        test_name = self.id() or "all"
        persistent_data_file = os.path.join(
            PERSISTENT_DATA_PREFIX, f"test_github_data_{test_name}.yaml")
        PersistentObjectStorage().storage_file = persistent_data_file
        if PersistentObjectStorage().is_write_mode and (not self.user
                                                        or not self.token):
            raise EnvironmentError(
                "please set GITHUB_TOKEN GITHUB_USER env variables")

        self.service = GithubService(token=self.token, read_only=True)

        self.colin_project = self.service.get_project(namespace="user-cont",
                                                      repo="colin")
Пример #20
0
def global_service_config():
    """
    This config will be used instead of the one loaded from the local config file.

    You can still mock/overwrite the service config content in your tests
    but this one will be used by default.
    """
    service_config = ServiceConfig()
    service_config.services = {
        GithubService(token="token"),
        GitlabService(token="token"),
    }
    service_config.dry_run = False
    service_config.github_requests_log_path = "/path"
    service_config.server_name = "localhost"
    ServiceConfig.service_config = service_config
Пример #21
0
    def test_get_project_having_key_as_path(self):
        github_app_private_key_path = self.github_app_private_key_path
        try:
            if not self.github_app_private_key_path:
                github_app_private_key_path = tempfile.mkstemp()[1]

            self.service = GithubService(
                github_app_id=self.github_app_id or "123",
                github_app_private_key_path=github_app_private_key_path,
            )
            project = self.service.get_project(namespace="packit-service", repo="ogr")
            assert project
            assert project.github_repo
        finally:
            if not self.github_app_private_key_path:
                Path(github_app_private_key_path).unlink()
Пример #22
0
    def setUp(self):
        self.token = os.environ.get("GITHUB_TOKEN")
        test_name = self.id() or "all"

        persistent_data_file = os.path.join(
            PERSISTENT_DATA_PREFIX, f"test_github_data_{test_name}.yaml")
        PersistentObjectStorage().storage_file = persistent_data_file

        if PersistentObjectStorage().is_write_mode and (not self.token):
            raise EnvironmentError("please set GITHUB_TOKEN env variables")

        self.service = GithubService(token=self.token)
        self._ogr_project = None
        self._ogr_fork = None
        self._hello_world_project = None
        self._not_forked_project = None
Пример #23
0
def test_bad_credentials(max_retries):

    flexmock(HTTPSConnectionPool).should_call("urlopen").times(max_retries + 1)

    flexmock(Github).should_call("get_repo").and_raise(
        BadCredentialsException,
        401,
        {
            "message": "Bad credentials",
            "documentation_url": "https://docs.github.com/rest",
        },
    )

    service = GithubService(token="invalid_token", max_retries=max_retries)
    project = service.get_project(namespace="mmuzila", repo="playground")
    project.github_repo
Пример #24
0
def test_get_user_config(tmpdir):
    user_config_file_path = Path(tmpdir) / ".packit.yaml"
    user_config_file_path.write_text("---\n"
                                     "debug: true\n"
                                     "fas_user: rambo\n"
                                     "keytab_path: './rambo.keytab'\n"
                                     "github_token: GITHUB_TOKEN\n"
                                     "pagure_user_token: PAGURE_TOKEN\n")
    flexmock(os).should_receive("getenv").with_args(
        "XDG_CONFIG_HOME").and_return(str(tmpdir))
    config = Config.get_user_config()
    assert config.debug and isinstance(config.debug, bool)
    assert config.fas_user == "rambo"
    assert config.keytab_path == "./rambo.keytab"

    assert GithubService(token="GITHUB_TOKEN") in config.services
    assert PagureService(token="PAGURE_TOKEN") in config.services
Пример #25
0
def test_pr_id_and_ref(tmp_path: Path):
    """ p-s passes both ref and pr_id, we want to check out PR """
    remote = tmp_path / "remote"
    remote.mkdir()
    subprocess.check_call(["git", "init", "--bare", "."], cwd=remote)
    upstream_git = tmp_path / "upstream_git"
    upstream_git.mkdir()
    initiate_git_repo(upstream_git, push=True, upstream_remote=str(remote))
    # mimic github PR
    pr_id = "123"
    ref = (
        subprocess.check_output(["git", "rev-parse", "HEAD^"], cwd=upstream_git)
        .strip()
        .decode()
    )
    local_tmp_branch = "asdqwe"
    subprocess.check_call(["git", "branch", local_tmp_branch, ref], cwd=upstream_git)
    subprocess.check_call(
        ["git", "push", "origin", f"{local_tmp_branch}:refs/pull/{pr_id}/head"],
        cwd=upstream_git,
    )
    subprocess.check_call(["git", "branch", "-D", local_tmp_branch], cwd=upstream_git)

    LocalProject(
        working_dir=upstream_git,
        offline=True,
        pr_id=pr_id,
        ref=ref,
        git_service=GithubService(),
    )

    assert (
        subprocess.check_output(
            ["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=upstream_git
        )
        .strip()
        .decode()
        == f"pr/{pr_id}"
    )
Пример #26
0
    def set_local_project(self):
        """ update self.local_project """
        # TODO: in order to support any git forge here, ogr should also have a method like this:
        #       get_github_service_from_url(url, **kwargs):
        #       ogr should guess the forge based on the url; kwargs should be passed to the
        #       constructor in order to support the above
        if not self.local_project.git_service:
            if self.config.github_app_cert_path:
                private_key = Path(
                    self.config.github_app_cert_path).read_text()
            else:
                private_key = None
            self.local_project.git_service = GithubService(
                token=self.config.github_token,
                github_app_id=self.config.github_app_id,
                github_app_private_key=private_key,
            )
            self.local_project.refresh_the_arguments(
            )  # get git project from newly set git service

        if not self.local_project.repo_name:
            # will this ever happen?
            self.local_project.repo_name = self.package_config.downstream_package_name
Пример #27
0
    def _pr_comments_test(self):
        token = os.environ.get("GITHUB_TOKEN")
        cassette = Cassette()
        if cassette.mode == StorageMode.write and (not token):
            raise EnvironmentError(
                f"You are in Requre write mode, please set proper GITHUB_TOKEN"
                f" env variables {cassette.storage_file}")
        # possible to check before reading values because in other case values are removed
        # and in write mode is does have sense at the end
        if cassette.mode == StorageMode.read:
            self.assertIn(self.id(), cassette.storage_file)
            self.assertIn("LGTM", str(cassette.storage_object))
            self.assertTrue(
                cassette.storage_object["requests.sessions"]["send"]["GET"]
                ["https://api.github.com:443/repos/packit-service/ogr"])
        service = GithubService(token=token)
        ogr_project = service.get_project(namespace="packit-service",
                                          repo="ogr")
        pr_comments = ogr_project.get_pr_comments(9)
        assert pr_comments
        assert len(pr_comments) == 2

        assert pr_comments[0].body.endswith("fixed")
        assert pr_comments[1].body.startswith("LGTM")
Пример #28
0
def test_get_service_class_not_found(url, mapping):
    with pytest.raises(OgrException) as ex:
        _ = get_service_class(url=url, service_mapping_update=mapping)
    assert str(ex.value) == "No matching service was found."


@pytest.mark.parametrize(
    "url,mapping,instances,force_custom_instance,result",
    [
        (
            "https://github.com/packit-service/ogr",
            None,
            None,
            True,
            GithubProject(
                namespace="packit-service", repo="ogr", service=GithubService()
            ),
        ),
        (
            "github.com/packit-service/ogr",
            None,
            None,
            True,
            GithubProject(
                namespace="packit-service", repo="ogr", service=GithubService()
            ),
        ),
        (
            "[email protected]:packit-service/ogr.git",
            None,
            None,
Пример #29
0
def test_get_service_class_not_found(url, mapping):
    with pytest.raises(OgrException) as ex:
        _ = get_service_class(url=url, service_mapping_update=mapping)
    assert str(ex.value) == "No matching service was found."


@pytest.mark.parametrize(
    "url,mapping,instances,result",
    [
        (
            "https://github.com/packit-service/ogr",
            None,
            None,
            GithubProject(namespace="packit-service",
                          repo="ogr",
                          service=GithubService()),
        ),
        (
            "github.com/packit-service/ogr",
            None,
            None,
            GithubProject(namespace="packit-service",
                          repo="ogr",
                          service=GithubService()),
        ),
        (
            "[email protected]:packit-service/ogr.git",
            None,
            None,
            GithubProject(namespace="packit-service",
                          repo="ogr",
Пример #30
0
 def test_private_key(self):
     service = GithubService(github_app_id="123",
                             github_app_private_key=TESTING_PRIVATE_KEY)
     assert service.github_app_private_key == TESTING_PRIVATE_KEY