Detect git authentication failure (#5420)

Co-authored-by: Michael Oliveira <34169552+Flame442@users.noreply.github.com>
This commit is contained in:
Jan 2023-12-24 01:23:39 +01:00 committed by GitHub
parent da8cabaf50
commit ecccea6781
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 1 deletions

View File

@ -566,6 +566,20 @@ class Downloader(commands.Cog):
await ctx.send(
_("The repo name you provided is already in use. Please choose another name.")
)
except errors.AuthenticationError as err:
await ctx.send(
_(
"Failed to authenticate or repository does not exist."
" See logs for more information."
)
)
log.exception(
"Something went wrong whilst cloning %s (to revision: %s)",
repo_url,
branch,
exc_info=err,
)
except errors.CloningError as err:
await ctx.send(
_(

View File

@ -13,6 +13,7 @@ __all__ = [
"CopyingError",
"ExistingGitRepo",
"MissingGitRepo",
"AuthenticationError",
"CloningError",
"CurrentHashError",
"HardResetError",
@ -79,6 +80,15 @@ class MissingGitRepo(DownloaderException):
pass
class AuthenticationError(GitException):
"""
Thrown when git failed to authenticate with
the server
"""
pass
class CloningError(GitException):
"""
Thrown when git clone returns a non zero exit code.

View File

@ -188,6 +188,17 @@ class Repo(RepoJSONMixin):
git_path = self.folder_path / ".git"
return git_path.exists(), git_path
def _parse_git_error(self, git_command: str, stderr: str) -> errors.GitException:
stderr = stderr.lower()
# Expected to catch:
# Could not read from remote repository
# could not read X for 'URL': terminal prompts disabled
# Authentication failed
if "could not read" in stderr or "authentication failed" in stderr:
return errors.AuthenticationError("Failed to Authenticate", git_command)
return errors.CloningError("Error when running git clone.", git_command)
async def is_ancestor(self, maybe_ancestor_rev: str, descendant_rev: str) -> bool:
"""
Check if the first is an ancestor of the second.
@ -548,13 +559,20 @@ class Repo(RepoJSONMixin):
"""
env = os.environ.copy()
env["GIT_TERMINAL_PROMPT"] = "0"
env["GIT_TRACE"] = "0"
# make sure we never enter an askpass routine
# https://github.com/git/git/blob/1424303/prompt.c#L56
env.pop("GIT_ASKPASS", None)
env.pop("SSH_ASKPASS", None)
# attempt to force all output to plain ascii english
# some methods that parse output may expect it
# according to gettext manual both variables have to be set:
# https://www.gnu.org/software/gettext/manual/gettext.html#Locale-Environment-Variables
env["LC_ALL"] = "C"
env["LANGUAGE"] = "C"
# Make sure git does not consider us smart
# https://github.com/git/git/blob/a7312d1a2/editor.c#L11-L15
env["TERM"] = "dumb"
kwargs["env"] = env
async with self._repo_lock:
p: CompletedProcess = await asyncio.get_running_loop().run_in_executor(
@ -659,7 +677,7 @@ class Repo(RepoJSONMixin):
if p.returncode:
# Try cleaning up folder
shutil.rmtree(str(self.folder_path), ignore_errors=True)
raise errors.CloningError("Error when running git clone.", git_command)
raise self._parse_git_error(git_command, p.stderr.decode(**DECODE_PARAMS))
if self.branch is None:
self.branch = await self.current_branch()