Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoMi authored and JLLeitschuh committed May 12, 2023
1 parent 1730c09 commit b1827e9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
24 changes: 16 additions & 8 deletions github/Requester.py
Expand Up @@ -620,19 +620,27 @@ def __requestRaw(self, cnx, verb, url, requestHeaders, input):
location = responseHeaders["location"]
o = urllib.parse.urlparse(location)
if o.scheme != self.__scheme:
raise RuntimeError(f"Github server redirected from {self.__scheme} protocol to {o.scheme}, "
f"please correct your Github server URL via base_url: Github(base_url=...)")
raise RuntimeError(
f"Github server redirected from {self.__scheme} protocol to {o.scheme}, "
f"please correct your Github server URL via base_url: Github(base_url=...)"
)
if o.hostname != self.__hostname:
raise RuntimeError(f"Github server redirected from host {self.__hostname} to {o.hostname}, "
f"please correct your Github server URL via base_url: Github(base_url=...)")
raise RuntimeError(
f"Github server redirected from host {self.__hostname} to {o.hostname}, "
f"please correct your Github server URL via base_url: Github(base_url=...)"
)
if o.path == url:
port = ":" + str(self.__port) if self.__port is not None else ""
requested_location = f"{self.__scheme}://{self.__hostname}{port}{url}"
raise RuntimeError(f"Requested {requested_location} but server redirected to {location}, "
f"you may need to correct your Github server URL "
f"via base_url: Github(base_url=...)")
raise RuntimeError(
f"Requested {requested_location} but server redirected to {location}, "
f"you may need to correct your Github server URL "
f"via base_url: Github(base_url=...)"
)
if self._logger.isEnabledFor(logging.INFO):
self._logger.info(f"Following Github server redirection from {url} to {o.path}")
self._logger.info(
f"Following Github server redirection from {url} to {o.path}"
)
return self.__requestRaw(original_cnx, verb, o.path, requestHeaders, input)

return status, responseHeaders, output
Expand Down
1 change: 0 additions & 1 deletion github/__init__.py
Expand Up @@ -72,7 +72,6 @@
from .InputGitAuthor import InputGitAuthor
from .InputGitTreeElement import InputGitTreeElement


# set log level to INFO for github
logger = logging.getLogger("github")
logger.setLevel(logging.INFO)
Expand Down
36 changes: 21 additions & 15 deletions tests/Requester.py
Expand Up @@ -40,48 +40,54 @@ def tearDown(self):
super().tearDown()

def testLoggingRedirection(self):
self.assertEqual(self.g.get_repo('EnricoMi/test').name, "test-renamed")
self.assertEqual(self.g.get_repo("EnricoMi/test").name, "test-renamed")
self.logger.info.assert_called_once_with(
'Following Github server redirection from /repos/EnricoMi/test to /repositories/638123443'
"Following Github server redirection from /repos/EnricoMi/test to /repositories/638123443"
)

def testBaseUrlSchemeRedirection(self):
gh = github.Github(base_url="http://api.github.com")
with self.assertRaises(RuntimeError) as exc:
gh.get_repo('PyGithub/PyGithub')
gh.get_repo("PyGithub/PyGithub")
self.assertEqual(
exc.exception.args,
('Github server redirected from http protocol to https, please correct your '
'Github server URL via base_url: Github(base_url=...)', )
(
"Github server redirected from http protocol to https, please correct your "
"Github server URL via base_url: Github(base_url=...)",
),
)

def testBaseUrlHostRedirection(self):
gh = github.Github(base_url="https://www.github.com")
with self.assertRaises(RuntimeError) as exc:
gh.get_repo('PyGithub/PyGithub')
gh.get_repo("PyGithub/PyGithub")
self.assertEqual(
exc.exception.args,
("Github server redirected from host www.github.com to github.com, "
"please correct your Github server URL via base_url: Github(base_url=...)", )
(
"Github server redirected from host www.github.com to github.com, "
"please correct your Github server URL via base_url: Github(base_url=...)",
),
)

def testBaseUrlPortRedirection(self):
# replay data forged
gh = github.Github(base_url="https://api.github.com")
with self.assertRaises(RuntimeError) as exc:
gh.get_repo('PyGithub/PyGithub')
gh.get_repo("PyGithub/PyGithub")
self.assertEqual(
exc.exception.args,
("Requested https://api.github.com/repos/PyGithub/PyGithub but server "
"redirected to https://api.github.com:443/repos/PyGithub/PyGithub, "
"you may need to correct your Github server URL "
"via base_url: Github(base_url=...)", )
(
"Requested https://api.github.com/repos/PyGithub/PyGithub but server "
"redirected to https://api.github.com:443/repos/PyGithub/PyGithub, "
"you may need to correct your Github server URL "
"via base_url: Github(base_url=...)",
),
)

def testBaseUrlPrefixRedirection(self):
# replay data forged
gh = github.Github(base_url="https://api.github.com/api/v3")
self.assertEqual(gh.get_repo('PyGithub/PyGithub').name, "PyGithub")
self.assertEqual(gh.get_repo("PyGithub/PyGithub").name, "PyGithub")
self.logger.info.assert_called_once_with(
'Following Github server redirection from /api/v3/repos/PyGithub/PyGithub to /repos/PyGithub/PyGithub'
"Following Github server redirection from /api/v3/repos/PyGithub/PyGithub to /repos/PyGithub/PyGithub"
)

0 comments on commit b1827e9

Please sign in to comment.