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")
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) ])
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
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
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" )
class GithubTests(unittest.TestCase): 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) self._ogr_project = None self._ogr_fork = None self._hello_world_project = None self._not_forked_project = None @property def ogr_project(self): if not self._ogr_project: self._ogr_project = self.service.get_project( namespace="packit-service", repo="ogr" ) return self._ogr_project @property def ogr_fork(self): if not self._ogr_fork: self._ogr_fork = self.service.get_project( namespace="packit-service", repo="ogr", is_fork=True ) return self._ogr_fork @property def hello_world_project(self): if not self._hello_world_project: self._hello_world_project = self.service.get_project( namespace="packit-service", repo="hello-world" ) return self._hello_world_project @property def not_forked_project(self): if not self._not_forked_project: self._not_forked_project = self.service.get_project( namespace="fedora-modularity", repo="fed-to-brew" ) return self._not_forked_project def tearDown(self): PersistentObjectStorage().dump()
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
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")
class ReadOnly(RequreTestCase): 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" ) def test_pr_comments(self): pr_comments = self.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") def test_create_pr(self): pr = self.ogr_project.pr_create( "title", "text", "master", "lbarcziova:testing_branch" ) assert pr.title == "title" def test_create_fork(self): fork = self.ogr_project.fork_create() assert not fork.is_fork
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
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()
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)
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
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")
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, ]
class ReadOnly(unittest.TestCase): 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") def tearDown(self): PersistentObjectStorage().dump() def test_pr_comments(self): pr_comments = self.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") def test_create_pr(self): pr = self.ogr_project.pr_create("title", "text", "master", "lbarcziova:testing_branch") assert pr.title == "title" def test_create_fork(self): fork = self.ogr_project.fork_create() assert not fork.is_fork
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)
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, ]
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 )
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
class GithubTests(RequreTestCase): 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 @property def ogr_project(self): if not self._ogr_project: self._ogr_project = self.service.get_project(namespace="packit", repo="ogr") return self._ogr_project @property def ogr_fork(self): if not self._ogr_fork: self._ogr_fork = self.service.get_project( namespace="packit", repo="ogr", is_fork=True ) return self._ogr_fork @property def hello_world_project(self): if not self._hello_world_project: self._hello_world_project = self.service.get_project( namespace="packit", repo="hello-world" ) return self._hello_world_project @property def not_forked_project(self): if not self._not_forked_project: self._not_forked_project = self.service.get_project( namespace="fedora-modularity", repo="fed-to-brew" ) return self._not_forked_project
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
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
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
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) ])
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()
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")
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
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
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}" )