diff --git a/poetry/console/commands/publish.py b/poetry/console/commands/publish.py index bd777bb65d2..6ec2cf3480a 100644 --- a/poetry/console/commands/publish.py +++ b/poetry/console/commands/publish.py @@ -75,10 +75,27 @@ def handle(self) -> Optional[int]: Path(self.option("client-cert")) if self.option("client-cert") else None ) + repository_name = self.option("repository") + username, password = self.option("username"), self.option("password") + if repository_name and not username and not password: + try: + repository = self.poetry.pool.repository(repository_name) + auth = repository.auth + except AttributeError as error: + raise AttributeError( + f"No credentials available for {repository_name}" + ) from error + except ValueError: + raise ValueError(f"No repository named {repository_name} found") + else: + if auth: + username = auth.username + password = auth.password + publisher.publish( - self.option("repository"), - self.option("username"), - self.option("password"), + repository_name, + username, + password, cert, client_cert, self.option("dry-run"), diff --git a/poetry/repositories/legacy_repository.py b/poetry/repositories/legacy_repository.py index 60fd8c8b6fa..4dbb0ec8b38 100644 --- a/poetry/repositories/legacy_repository.py +++ b/poetry/repositories/legacy_repository.py @@ -217,6 +217,10 @@ def __init__( self._disable_cache = disable_cache + @property + def auth(self) -> Optional[requests.auth.HTTPBasicAuth]: + return self._authenticator.session.auth + @property def cert(self) -> Optional[Path]: return self._cert