Skip to content

Commit bcd88fe

Browse files
committed
NIT refactor code so that isSonatype is calculated only once
1 parent b36b32c commit bcd88fe

File tree

2 files changed

+57
-70
lines changed

2 files changed

+57
-70
lines changed

modules/cli/src/main/scala/scala/cli/commands/publish/Publish.scala

Lines changed: 55 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -751,51 +751,50 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
751751

752752
val ec = builds.head.options.finalCache.ec
753753

754-
def authOpt(repo: String): Either[BuildException, Option[Authentication]] = either {
755-
val isHttps = {
756-
val uri = new URI(repo)
757-
uri.getScheme == "https"
758-
}
759-
val hostOpt = Option.when(isHttps)(new URI(repo).getHost)
760-
val maybeCredentials: Either[BuildException, Option[PublishCredentials]] = hostOpt match {
761-
case None => Right(None)
762-
case Some(host) =>
763-
configDb().get(Keys.publishCredentials).wrapConfigException.map { credListOpt =>
764-
credListOpt.flatMap { credList =>
765-
credList.find { cred =>
766-
cred.host == host &&
767-
(isHttps || cred.httpsOnly.contains(false))
754+
def authOpt(repo: String, isSonatype: Boolean): Either[BuildException, Option[Authentication]] =
755+
either {
756+
val isHttps = {
757+
val uri = new URI(repo)
758+
uri.getScheme == "https"
759+
}
760+
val hostOpt = Option.when(isHttps)(new URI(repo).getHost)
761+
val maybeCredentials: Either[BuildException, Option[PublishCredentials]] = hostOpt match {
762+
case None => Right(None)
763+
case Some(host) =>
764+
configDb().get(Keys.publishCredentials).wrapConfigException.map { credListOpt =>
765+
credListOpt.flatMap { credList =>
766+
credList.find { cred =>
767+
cred.host == host &&
768+
(isHttps || cred.httpsOnly.contains(false))
769+
}
768770
}
769771
}
770-
}
771-
}
772-
val isSonatype =
773-
hostOpt.exists(host => host == "oss.sonatype.org" || host.endsWith(".oss.sonatype.org"))
774-
val passwordOpt = publishOptions.contextual(isCi).repoPassword match {
775-
case None => value(maybeCredentials).flatMap(_.password)
776-
case other => other.map(_.toConfig)
777-
}
778-
passwordOpt.map(_.get()) match {
779-
case None => None
780-
case Some(password) =>
781-
val userOpt = publishOptions.contextual(isCi).repoUser match {
782-
case None => value(maybeCredentials).flatMap(_.user)
783-
case other => other.map(_.toConfig)
784-
}
785-
val realmOpt = publishOptions.contextual(isCi).repoRealm match {
786-
case None =>
787-
value(maybeCredentials)
788-
.flatMap(_.realm)
789-
.orElse {
790-
if (isSonatype) Some("Sonatype Nexus Repository Manager")
791-
else None
792-
}
793-
case other => other
794-
}
795-
val auth = Authentication(userOpt.fold("")(_.get().value), password.value)
796-
Some(realmOpt.fold(auth)(auth.withRealm))
772+
}
773+
val passwordOpt = publishOptions.contextual(isCi).repoPassword match {
774+
case None => value(maybeCredentials).flatMap(_.password)
775+
case other => other.map(_.toConfig)
776+
}
777+
passwordOpt.map(_.get()) match {
778+
case None => None
779+
case Some(password) =>
780+
val userOpt = publishOptions.contextual(isCi).repoUser match {
781+
case None => value(maybeCredentials).flatMap(_.user)
782+
case other => other.map(_.toConfig)
783+
}
784+
val realmOpt = publishOptions.contextual(isCi).repoRealm match {
785+
case None =>
786+
value(maybeCredentials)
787+
.flatMap(_.realm)
788+
.orElse {
789+
if (isSonatype) Some("Sonatype Nexus Repository Manager")
790+
else None
791+
}
792+
case other => other
793+
}
794+
val auth = Authentication(userOpt.fold("")(_.get().value), password.value)
795+
Some(realmOpt.fold(auth)(auth.withRealm))
796+
}
797797
}
798-
}
799798

800799
val repoParams = {
801800

@@ -831,32 +830,28 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
831830
}
832831
}
833832

833+
val isSonatype: Boolean = {
834+
val uri = new URI(repoParams.repo.snapshotRepo.root)
835+
val hostOpt = Option.when(uri.getScheme == "https")(uri.getHost)
836+
837+
hostOpt.exists(host => host == "oss.sonatype.org" || host.endsWith(".oss.sonatype.org"))
838+
}
839+
834840
val now = Instant.now()
835841
val (fileSet0, modVersionOpt) = value {
836842
it
837843
// TODO Allow to add test JARs to the main build artifacts
838844
.filter(_._1.scope != Scope.Test)
839845
.map {
840846
case (build, docBuildOpt) =>
841-
val isSonatype = {
842-
val hostOpt = {
843-
val repo = repoParams.repo.snapshotRepo.root
844-
val uri = new URI(repo)
845-
if (uri.getScheme == "https") Some(uri.getHost)
846-
else None
847-
}
848-
hostOpt.exists(host =>
849-
host == "oss.sonatype.org" || host.endsWith(".oss.sonatype.org")
850-
)
851-
}
852847
buildFileSet(
853848
build,
854849
docBuildOpt,
855850
workingDir,
856851
now,
857852
isIvy2LocalLike = repoParams.isIvy2LocalLike,
858853
isCi = isCi,
859-
isSonatype = isSonatype,
854+
isSonatype,
860855
logger
861856
)
862857
}
@@ -1024,23 +1019,24 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
10241019
else fileSet2.order(ec).unsafeRun()(ec)
10251020

10261021
val isSnapshot0 = modVersionOpt.exists(_._2.endsWith("SNAPSHOT"))
1027-
val authOpt0 = value(authOpt(repoParams.repo.repo(isSnapshot0).root))
1022+
val authOpt0 = value(authOpt(repoParams.repo.repo(isSnapshot0).root, isSonatype))
10281023
if (repoParams.shouldAuthenticate && authOpt0.isEmpty)
10291024
logger.diagnostic(
10301025
"Publishing to a repository that needs authentication, but no credentials are available.",
10311026
Severity.Warning
10321027
)
1033-
val repoParams0 = repoParams.withAuth(authOpt0)
1028+
val repoParams0: RepoParams = repoParams.withAuth(authOpt0)
10341029
val hooksDataOpt = Option.when(!dummy) {
10351030
try repoParams0.hooks.beforeUpload(finalFileSet, isSnapshot0).unsafeRun()(ec)
10361031
catch {
10371032
case NonFatal(e)
1038-
if "Failed to get .*/staging/profiles \\(http status: 403,".r.unanchored.matches(
1033+
if "Failed to get .*oss\\.sonatype\\.org.*/staging/profiles \\(http status: 403,".r.unanchored.matches(
10391034
e.getMessage
10401035
) =>
1041-
logger.exit(new WrongSonatypeServerError)
1036+
logger.exit(new WrongSonatypeServerError(
1037+
repoParams0.repo.releaseRepo.root.contains("s01")))
10421038
case NonFatal(e)
1043-
if "Failed to get .*/staging/profiles \\(http status: 401,".r.unanchored.matches(
1039+
if "Failed to get .*oss\\.sonatype\\.org.*/staging/profiles \\(http status: 401,".r.unanchored.matches(
10441040
e.getMessage
10451041
) =>
10461042
logger.exit(new InvalidPublishCredentials)
@@ -1049,15 +1045,6 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
10491045
}
10501046
}
10511047

1052-
val isHttps = {
1053-
val uri = new URI(repoParams.repo.repo(isSnapshot0).root)
1054-
uri.getScheme == "https"
1055-
}
1056-
val hostOpt = Option.when(isHttps)(new URI(repoParams.repo.repo(isSnapshot0).root).getHost)
1057-
1058-
val isSonatype =
1059-
hostOpt.exists(host => host == "oss.sonatype.org" || host.endsWith(".oss.sonatype.org"))
1060-
10611048
val retainedRepo = hooksDataOpt match {
10621049
case None => // dummy mode
10631050
repoParams0.repo.repo(isSnapshot0)

modules/cli/src/main/scala/scala/cli/errors/WrongSonatypeServerError.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package scala.cli.errors
22

33
import scala.build.errors.BuildException
44

5-
final class WrongSonatypeServerError
5+
final class WrongSonatypeServerError(legacyChosen: Boolean)
66
extends BuildException(
7-
"Make sure you're publishing to the right Sonatype server: legacy 'central' or new 'central-s01'"
7+
s"Wrong Sonatype server, try with ${if legacyChosen then "'central-s01'" else "'central'"}",
88
)

0 commit comments

Comments
 (0)