Skip to content

Commit 5d95b1e

Browse files
authored
Default to publish repository configured for local machine when infering publish.ci.repository (#2571)
* Default to repository configured for publishing from local machine when computing the repository for CI publishing * Fix repo user and repo password checks since they ignored the CLI options when looking for repository, tests local Github and CI Github were checking nothing. * Mention repo name when username or password has not been found * Refactor duplicated code to CheckUtils.scala
1 parent 8df7956 commit 5d95b1e

File tree

5 files changed

+277
-85
lines changed

5 files changed

+277
-85
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package scala.cli.commands.publish.checks
2+
3+
import java.net.URI
4+
5+
import scala.build.Logger
6+
import scala.build.options.PublishOptions as BPublishOptions
7+
import scala.cli.commands.publish.{PublishSetupOptions, RepoParams}
8+
9+
object CheckUtils {
10+
11+
/** Keep in mind that combinedOptions do not contain all options from cliOptions, e.g.
12+
* publishRepo.publishRepository is not propagated
13+
*/
14+
def getRepoOpt(
15+
cliOptions: PublishSetupOptions,
16+
combinedOptions: BPublishOptions
17+
): Option[String] =
18+
cliOptions.publishRepo.publishRepository
19+
.orElse {
20+
combinedOptions.contextual(cliOptions.publishParams.setupCi).repository
21+
}
22+
.orElse {
23+
if (cliOptions.publishParams.setupCi)
24+
combinedOptions.contextual(isCi = false).repository
25+
else
26+
None
27+
}
28+
29+
def getHostOpt(
30+
options: PublishSetupOptions,
31+
pubOpt: BPublishOptions,
32+
workspace: os.Path,
33+
logger: Logger
34+
): Option[String] =
35+
getRepoOpt(options, pubOpt).flatMap { repo =>
36+
RepoParams(
37+
repo,
38+
pubOpt.versionControl.map(_.url),
39+
workspace,
40+
None,
41+
false,
42+
null,
43+
logger
44+
) match {
45+
case Left(ex) =>
46+
logger.debug("Caught exception when trying to compute host to check user credentials")
47+
logger.debug(ex)
48+
None
49+
case Right(params) =>
50+
Some(new URI(params.repo.snapshotRepo.root).getHost)
51+
}
52+
}
53+
}

modules/cli/src/main/scala/scala/cli/commands/publish/checks/PasswordCheck.scala

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,27 @@ final case class PasswordCheck(
2121
def fieldName = "password"
2222
def directivePath = "publish" + (if (options.publishParams.setupCi) ".ci" else "") + ".password"
2323

24-
private def hostOpt(pubOpt: BPublishOptions): Option[String] = {
25-
val repo = pubOpt.contextual(options.publishParams.setupCi).repository.getOrElse(
26-
RepositoryCheck.defaultRepository
27-
)
28-
RepoParams(
29-
repo,
30-
pubOpt.versionControl.map(_.url),
24+
private def passwordOpt(pubOpt: BPublishOptions) =
25+
CheckUtils.getHostOpt(
26+
options,
27+
pubOpt,
3128
workspace,
32-
None,
33-
false,
34-
null,
3529
logger
3630
) match {
37-
case Left(ex) =>
38-
logger.debug("Caught exception when trying to compute host to check user credentials")
39-
logger.debug(ex)
40-
None
41-
case Right(params) =>
42-
Some(new URI(params.repo.snapshotRepo.root).getHost)
43-
}
44-
}
45-
46-
private def passwordOpt(pubOpt: BPublishOptions) = hostOpt(pubOpt) match {
47-
case None => Right(None)
48-
case Some(host) =>
49-
configDb().get(Keys.publishCredentials).wrapConfigException.map { credListOpt =>
50-
credListOpt.flatMap { credList =>
51-
credList
52-
.iterator
53-
.filter(_.host == host)
54-
.map(_.password)
55-
.collectFirst {
56-
case Some(p) => p
57-
}
31+
case None => Right(None)
32+
case Some(host) =>
33+
configDb().get(Keys.publishCredentials).wrapConfigException.map { credListOpt =>
34+
credListOpt.flatMap { credList =>
35+
credList
36+
.iterator
37+
.filter(_.host == host)
38+
.map(_.password)
39+
.collectFirst {
40+
case Some(p) => p
41+
}
42+
}
5843
}
59-
}
60-
}
44+
}
6145

6246
def check(pubOpt: BPublishOptions): Boolean =
6347
pubOpt.retained(options.publishParams.setupCi).repoPassword.nonEmpty || {
@@ -106,7 +90,12 @@ final case class PasswordCheck(
10690
)
10791
}
10892
else
109-
hostOpt(pubOpt) match {
93+
CheckUtils.getHostOpt(
94+
options,
95+
pubOpt,
96+
workspace,
97+
logger
98+
) match {
11099
case None =>
111100
logger.debug("No host, not checking for publish repository password")
112101
OptionCheck.DefaultValue.empty
@@ -118,17 +107,22 @@ final case class PasswordCheck(
118107
)
119108
OptionCheck.DefaultValue.empty
120109
}
121-
else
110+
else {
111+
val optionName =
112+
CheckUtils.getRepoOpt(options, pubOpt).map(r =>
113+
s"publish password for $r"
114+
).getOrElse("publish password")
122115
value {
123116
Left {
124117
new MissingPublishOptionError(
125-
"publish password",
118+
optionName,
126119
"",
127120
"publish.credentials",
128121
configKeys = Seq(Keys.publishCredentials.fullName)
129122
)
130123
}
131124
}
125+
}
132126
}
133127
}
134128
}

modules/cli/src/main/scala/scala/cli/commands/publish/checks/RepositoryCheck.scala

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package scala.cli.commands.publish.checks
22

33
import scala.build.Logger
44
import scala.build.errors.BuildException
5-
import scala.build.options.{PublishOptions => BPublishOptions}
5+
import scala.build.options.PublishOptions as BPublishOptions
66
import scala.cli.commands.publish.{OptionCheck, PublishSetupOptions}
7+
import scala.cli.errors.MissingPublishOptionError
78

89
final case class RepositoryCheck(
910
options: PublishSetupOptions,
@@ -15,16 +16,31 @@ final case class RepositoryCheck(
1516
def check(pubOpt: BPublishOptions): Boolean =
1617
pubOpt.retained(options.publishParams.setupCi).repository.nonEmpty
1718
def defaultValue(pubOpt: BPublishOptions): Either[BuildException, OptionCheck.DefaultValue] = {
18-
val repo = options.publishRepo.publishRepository.getOrElse {
19-
logger.message("repository:")
20-
logger.message(s" using ${RepositoryCheck.defaultRepositoryDescription}")
21-
RepositoryCheck.defaultRepository
22-
}
23-
Right(OptionCheck.DefaultValue.simple(repo, Nil, Nil))
19+
val maybeRepo = options.publishRepo.publishRepository
20+
.toRight(RepositoryCheck.missingValueError)
21+
.orElse {
22+
if (options.publishParams.setupCi) {
23+
val repoFromLocal = pubOpt.retained(isCi = false)
24+
.repository
25+
.toRight(RepositoryCheck.missingValueError)
26+
repoFromLocal.foreach { repoName =>
27+
logger.message("repository:")
28+
logger.message(s" using repository from local configuration: $repoName")
29+
}
30+
repoFromLocal
31+
}
32+
else Left(RepositoryCheck.missingValueError)
33+
}
34+
35+
maybeRepo.map(repo => OptionCheck.DefaultValue.simple(repo, Nil, Nil))
2436
}
2537
}
2638

2739
object RepositoryCheck {
28-
def defaultRepository = "central-s01"
29-
def defaultRepositoryDescription = "Maven Central via its s01 server"
40+
def missingValueError = new MissingPublishOptionError(
41+
"repository",
42+
"--publish-repository",
43+
"publish.repository",
44+
extraMessage = "use 'central' or 'central-s01' to publish to Maven Central via Sonatype."
45+
)
3046
}

modules/cli/src/main/scala/scala/cli/commands/publish/checks/UserCheck.scala

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,28 @@ final case class UserCheck(
2121
def fieldName = "user"
2222
def directivePath = "publish" + (if (options.publishParams.setupCi) ".ci" else "") + ".user"
2323

24-
private def hostOpt(pubOpt: BPublishOptions): Option[String] = {
25-
val repo = pubOpt.contextual(options.publishParams.setupCi).repository.getOrElse(
26-
RepositoryCheck.defaultRepository
27-
)
28-
RepoParams(
29-
repo,
30-
pubOpt.versionControl.map(_.url),
24+
private def userOpt(pubOpt: BPublishOptions) =
25+
CheckUtils.getHostOpt(
26+
options,
27+
pubOpt,
3128
workspace,
32-
None,
33-
false,
34-
null,
3529
logger
3630
) match {
37-
case Left(ex) =>
38-
logger.debug("Caught exception when trying to compute host to check user credentials")
39-
logger.debug(ex)
40-
None
41-
case Right(params) =>
42-
Some(new URI(params.repo.snapshotRepo.root).getHost)
43-
}
44-
}
45-
46-
private def userOpt(pubOpt: BPublishOptions) = hostOpt(pubOpt) match {
47-
case None => Right(None)
48-
case Some(host) =>
49-
configDb().get(Keys.publishCredentials).wrapConfigException.map { credListOpt =>
50-
credListOpt.flatMap { credList =>
51-
credList
52-
.iterator
53-
.filter(_.host == host)
54-
.map(_.user)
55-
.collectFirst {
56-
case Some(p) =>
57-
p
58-
}
31+
case None => Right(None)
32+
case Some(host) =>
33+
configDb().get(Keys.publishCredentials).wrapConfigException.map { credListOpt =>
34+
credListOpt.flatMap { credList =>
35+
credList
36+
.iterator
37+
.filter(_.host == host)
38+
.map(_.user)
39+
.collectFirst {
40+
case Some(p) =>
41+
p
42+
}
43+
}
5944
}
60-
}
61-
}
45+
}
6246

6347
def check(pubOpt: BPublishOptions): Boolean =
6448
pubOpt.retained(options.publishParams.setupCi).repoUser.nonEmpty || {
@@ -106,7 +90,12 @@ final case class UserCheck(
10690
)
10791
}
10892
else
109-
hostOpt(pubOpt) match {
93+
CheckUtils.getHostOpt(
94+
options,
95+
pubOpt,
96+
workspace,
97+
logger
98+
) match {
11099
case None =>
111100
logger.debug("No host, not checking for publish repository user")
112101
OptionCheck.DefaultValue.empty
@@ -118,17 +107,22 @@ final case class UserCheck(
118107
)
119108
OptionCheck.DefaultValue.empty
120109
}
121-
else
110+
else {
111+
val optionName =
112+
CheckUtils.getRepoOpt(options, pubOpt).map(r => s"publish user for $r").getOrElse(
113+
"publish user"
114+
)
122115
value {
123116
Left {
124117
new MissingPublishOptionError(
125-
"publish user",
118+
optionName,
126119
"",
127120
"publish.credentials",
128121
configKeys = Seq(Keys.publishCredentials.fullName)
129122
)
130123
}
131124
}
125+
}
132126
}
133127
}
134128
}

0 commit comments

Comments
 (0)