Skip to content

Commit cbf6071

Browse files
committed
Refactor Auth to make GitHub Enterprise work
1 parent 0765eec commit cbf6071

File tree

67 files changed

+1402
-1278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1402
-1278
lines changed

modules/core/src/main/scala/org/scalasteward/core/application/Cli.scala

+116-68
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ import org.scalasteward.core.application.ExitCodePolicy.{
3030
SuccessOnlyIfAllReposSucceed
3131
}
3232
import org.scalasteward.core.data.Resolver
33-
import org.scalasteward.core.forge.ForgeType
34-
import org.scalasteward.core.forge.ForgeType.{AzureRepos, GitHub}
35-
import org.scalasteward.core.forge.github.GitHubApp
33+
import org.scalasteward.core.forge.Forge
34+
import org.scalasteward.core.forge.Forge._
3635
import org.scalasteward.core.git.Author
3736
import org.scalasteward.core.util.Nel
3837
import org.scalasteward.core.util.dateTime.renderFiniteDuration
39-
4038
import scala.concurrent.duration._
4139

4240
object Cli {
@@ -45,7 +43,6 @@ object Cli {
4543
object name {
4644
val forgeApiHost = "forge-api-host"
4745
val forgeLogin = "forge-login"
48-
val forgeType = "forge-type"
4946
val maxBufferSize = "max-buffer-size"
5047
val processTimeout = "process-timeout"
5148
}
@@ -71,11 +68,6 @@ object Cli {
7168
Validated.fromEither(Uri.fromString(s).leftMap(_.message)).toValidatedNel
7269
}
7370

74-
implicit val forgeTypeArgument: Argument[ForgeType] =
75-
Argument.from(name.forgeType) { s =>
76-
Validated.fromEither(ForgeType.parse(s)).toValidatedNel
77-
}
78-
7971
private val multiple = "(can be used multiple times)"
8072

8173
private val workspace: Opts[File] =
@@ -106,20 +98,7 @@ object Cli {
10698
flag("sign-commits", "Whether to sign commits; default: false").orFalse
10799

108100
private val gitCfg: Opts[GitCfg] =
109-
(gitAuthor, gitAskPass, signCommits).mapN(GitCfg.apply)
110-
111-
private val vcsType =
112-
option[ForgeType](
113-
"vcs-type",
114-
s"deprecated in favor of --${name.forgeType}",
115-
visibility = Visibility.Partial
116-
).validate(s"--vcs-type is deprecated; use --${name.forgeType} instead")(_ => false)
117-
118-
private val forgeType = {
119-
val help = ForgeType.all.map(_.asString).mkString("One of ", ", ", "") +
120-
s"; default: ${GitHub.asString}"
121-
option[ForgeType](name.forgeType, help).orElse(vcsType).withDefault(GitHub)
122-
}
101+
(gitAuthor, signCommits).mapN(GitCfg.apply)
123102

124103
private val vcsApiHost =
125104
option[Uri](
@@ -129,9 +108,7 @@ object Cli {
129108
).validate(s"--vcs-api-host is deprecated; use --${name.forgeApiHost} instead")(_ => false)
130109

131110
private val forgeApiHost: Opts[Uri] =
132-
option[Uri](name.forgeApiHost, s"API URL of the forge; default: ${GitHub.publicApiBaseUrl}")
133-
.orElse(vcsApiHost)
134-
.withDefault(GitHub.publicApiBaseUrl)
111+
option[Uri](name.forgeApiHost, s"API URL of the forge").orElse(vcsApiHost)
135112

136113
private val vcsLogin =
137114
option[String](
@@ -152,16 +129,6 @@ object Cli {
152129
"Whether to add labels on pull or merge requests (if supported by the forge)"
153130
).orFalse
154131

155-
private val forgeCfg: Opts[ForgeCfg] =
156-
(forgeType, forgeApiHost, forgeLogin, doNotFork, addPrLabels)
157-
.mapN(ForgeCfg.apply)
158-
.validate(
159-
s"${ForgeType.allNot(_.supportsForking)} do not support fork mode"
160-
)(cfg => cfg.tpe.supportsForking || cfg.doNotFork)
161-
.validate(
162-
s"${ForgeType.allNot(_.supportsLabels)} do not support pull request labels"
163-
)(cfg => cfg.tpe.supportsLabels || !cfg.addLabels)
164-
165132
private val ignoreOptsFiles: Opts[Boolean] =
166133
flag(
167134
"ignore-opts-files",
@@ -192,11 +159,11 @@ object Cli {
192159
s"Read only directory for the sandbox $multiple"
193160
).orEmpty
194161

195-
private val enableSandbox: Opts[Boolean] =
196-
flag("enable-sandbox", "Whether to use the sandbox")
197-
.map(_ => true)
198-
.orElse(flag("disable-sandbox", "Whether to not use the sandbox").map(_ => false))
199-
.orElse(Opts(false))
162+
private val enableSandbox: Opts[Boolean] = {
163+
val enable = flag("enable-sandbox", "Whether to use the sandbox").map(_ => true)
164+
val disable = flag("disable-sandbox", "Whether to not use the sandbox").map(_ => false)
165+
enable.orElse(disable).withDefault(false)
166+
}
200167

201168
private val sandboxCfg: Opts[SandboxCfg] =
202169
(whitelist, readOnly, enableSandbox).mapN(SandboxCfg.apply)
@@ -268,12 +235,6 @@ object Cli {
268235
"Whether to assign the default reviewers to a bitbucket pull request; default: false"
269236
).orFalse
270237

271-
private val bitbucketServerCfg: Opts[BitbucketServerCfg] =
272-
bitbucketServerUseDefaultReviewers.map(BitbucketServerCfg.apply)
273-
274-
private val bitbucketCfg: Opts[BitbucketCfg] =
275-
bitbucketUseDefaultReviewers.map(BitbucketCfg.apply)
276-
277238
private val gitlabMergeWhenPipelineSucceeds: Opts[Boolean] =
278239
flag(
279240
"gitlab-merge-when-pipeline-succeeds",
@@ -292,11 +253,6 @@ object Cli {
292253
"Flag indicating if a merge request should remove the source branch when merging."
293254
).orFalse
294255

295-
private val gitLabCfg: Opts[GitLabCfg] =
296-
(gitlabMergeWhenPipelineSucceeds, gitlabRequiredReviewers, gitlabRemoveSourceBranch).mapN(
297-
GitLabCfg.apply
298-
)
299-
300256
private val githubAppId: Opts[Long] =
301257
option[Long](
302258
"github-app-id",
@@ -309,17 +265,11 @@ object Cli {
309265
"GitHub application key file. Repos accessible by this app are added to the repos in repos.md. git-ask-pass is still required."
310266
)
311267

312-
private val gitHubApp: Opts[Option[GitHubApp]] =
313-
(githubAppId, githubAppKeyFile).mapN(GitHubApp.apply).orNone
314-
315-
private val azureReposOrganization: Opts[Option[String]] =
268+
private val azureReposOrganization: Opts[String] =
316269
option[String](
317270
"azure-repos-organization",
318-
s"The Azure organization (required when --${name.forgeType} is ${AzureRepos.asString})"
319-
).orNone
320-
321-
private val azureReposCfg: Opts[AzureReposCfg] =
322-
azureReposOrganization.map(AzureReposCfg.apply)
271+
s"The Azure organization (required with --azure-repos)"
272+
)
323273

324274
private val refreshBackoffPeriod: Opts[FiniteDuration] = {
325275
val default = 0.days
@@ -350,22 +300,120 @@ object Cli {
350300
if (ifAnyRepoSucceeds) SuccessIfAnyRepoSucceeds else SuccessOnlyIfAllReposSucceed
351301
}
352302

303+
private val azureRepos: Opts[Unit] =
304+
flag("azure-repos", "")
305+
306+
private val bitbucket: Opts[Unit] =
307+
flag("bitbucket", "")
308+
309+
private val bitbucketServer: Opts[Unit] =
310+
flag("bitbucket-server", "")
311+
312+
private val gitLab: Opts[Unit] =
313+
flag("gitlab", "")
314+
315+
private val gitea: Opts[Unit] =
316+
flag("gitea", "")
317+
318+
private val gitHub: Opts[Unit] =
319+
flag("github", "").withDefault(()) // With default to make it succeed as default option
320+
321+
private val forge: Opts[Forge] = {
322+
val azureReposOptions =
323+
(azureRepos, forgeApiHost, forgeLogin, gitAskPass, addPrLabels, azureReposOrganization).mapN(
324+
(_, apiUri, login, gitAskPass, addLabels, reposOrganization) =>
325+
AzureRepos(apiUri, login, gitAskPass, addLabels, reposOrganization)
326+
)
327+
val bitbucketOptions =
328+
(
329+
bitbucket,
330+
forgeApiHost.withDefault(Bitbucket.defaultApiUri),
331+
forgeLogin,
332+
gitAskPass,
333+
doNotFork,
334+
bitbucketUseDefaultReviewers
335+
).mapN((_, apiUri, login, gitAskPass, doNotFork, useDefaultReviewers) =>
336+
Bitbucket(apiUri, login, gitAskPass, doNotFork, useDefaultReviewers)
337+
)
338+
val bitbucketServerOptions =
339+
(
340+
bitbucketServer,
341+
forgeApiHost,
342+
forgeLogin,
343+
gitAskPass,
344+
bitbucketServerUseDefaultReviewers
345+
).mapN((_, apiUri, login, gitAskPass, useDefaultReviewers) =>
346+
BitbucketServer(apiUri, login, gitAskPass, useDefaultReviewers)
347+
)
348+
val gitLabOptions =
349+
(
350+
gitLab,
351+
forgeApiHost.withDefault(GitLab.defaultApiUri),
352+
forgeLogin,
353+
gitAskPass,
354+
doNotFork,
355+
addPrLabels,
356+
gitlabMergeWhenPipelineSucceeds,
357+
gitlabRequiredReviewers,
358+
gitlabRemoveSourceBranch
359+
).mapN(
360+
(
361+
_,
362+
apiUri,
363+
login,
364+
gitAskPass,
365+
doNotFork,
366+
addLabels,
367+
mergeWhenPipelineSucceeds,
368+
requiredReviewers,
369+
removeSourceBranch
370+
) =>
371+
GitLab(
372+
apiUri,
373+
login,
374+
gitAskPass,
375+
doNotFork,
376+
addLabels,
377+
mergeWhenPipelineSucceeds,
378+
requiredReviewers,
379+
removeSourceBranch
380+
)
381+
)
382+
val giteaOptions =
383+
(gitea, forgeApiHost, forgeLogin, gitAskPass, doNotFork, addPrLabels).mapN(
384+
(_, apiUri, login, gitAskPass, doNotFork, addLabels) =>
385+
Gitea(apiUri, login, gitAskPass, doNotFork, addLabels)
386+
)
387+
val gitHubOptions =
388+
(
389+
gitHub,
390+
forgeApiHost.withDefault(GitHub.defaultApiUri),
391+
doNotFork,
392+
addPrLabels,
393+
githubAppId,
394+
githubAppKeyFile
395+
).mapN((_, apiUri, doNotFork, addLabels, appId, appKeyFile) =>
396+
GitHub(apiUri, doNotFork, addLabels, appId, appKeyFile)
397+
)
398+
azureReposOptions
399+
.orElse(bitbucketOptions)
400+
.orElse(bitbucketServerOptions)
401+
.orElse(gitLabOptions)
402+
.orElse(giteaOptions)
403+
.orElse(gitHubOptions) // GitHub last as default option
404+
}
405+
353406
private val regular: Opts[Usage] = (
354407
workspace,
355408
reposFiles,
356409
gitCfg,
357-
forgeCfg,
410+
forge,
358411
ignoreOptsFiles,
359412
processCfg,
360413
repoConfigCfg,
361414
scalafixCfg,
362415
artifactCfg,
363416
cacheTtl,
364-
bitbucketCfg,
365-
bitbucketServerCfg,
366-
gitLabCfg,
367-
azureReposCfg,
368-
gitHubApp,
369417
urlCheckerTestUrls,
370418
defaultMavenRepo,
371419
refreshBackoffPeriod,

modules/core/src/main/scala/org/scalasteward/core/application/Config.scala

+3-54
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import org.http4s.Uri
2121
import org.scalasteward.core.application.Cli.EnvVar
2222
import org.scalasteward.core.application.Config._
2323
import org.scalasteward.core.data.Resolver
24-
import org.scalasteward.core.forge.ForgeType
25-
import org.scalasteward.core.forge.github.GitHubApp
24+
import org.scalasteward.core.forge.Forge
2625
import org.scalasteward.core.git.Author
2726
import org.scalasteward.core.util.Nel
2827
import scala.concurrent.duration.FiniteDuration
@@ -49,49 +48,25 @@ final case class Config(
4948
workspace: File,
5049
reposFiles: Nel[Uri],
5150
gitCfg: GitCfg,
52-
forgeCfg: ForgeCfg,
51+
forge: Forge,
5352
ignoreOptsFiles: Boolean,
5453
processCfg: ProcessCfg,
5554
repoConfigCfg: RepoConfigCfg,
5655
scalafixCfg: ScalafixCfg,
5756
artifactCfg: ArtifactCfg,
5857
cacheTtl: FiniteDuration,
59-
bitbucketCfg: BitbucketCfg,
60-
bitbucketServerCfg: BitbucketServerCfg,
61-
gitLabCfg: GitLabCfg,
62-
azureReposCfg: AzureReposCfg,
63-
githubApp: Option[GitHubApp],
6458
urlCheckerTestUrls: Nel[Uri],
6559
defaultResolver: Resolver,
6660
refreshBackoffPeriod: FiniteDuration,
6761
exitCodePolicy: ExitCodePolicy
68-
) {
69-
def forgeSpecificCfg: ForgeSpecificCfg =
70-
forgeCfg.tpe match {
71-
case ForgeType.AzureRepos => azureReposCfg
72-
case ForgeType.Bitbucket => bitbucketCfg
73-
case ForgeType.BitbucketServer => bitbucketServerCfg
74-
case ForgeType.GitHub => GitHubCfg()
75-
case ForgeType.GitLab => gitLabCfg
76-
case ForgeType.Gitea => GiteaCfg()
77-
}
78-
}
62+
)
7963

8064
object Config {
8165
final case class GitCfg(
8266
gitAuthor: Author,
83-
gitAskPass: File,
8467
signCommits: Boolean
8568
)
8669

87-
final case class ForgeCfg(
88-
tpe: ForgeType,
89-
apiHost: Uri,
90-
login: String,
91-
doNotFork: Boolean,
92-
addLabels: Boolean
93-
)
94-
9570
final case class ProcessCfg(
9671
envVars: List[EnvVar],
9772
processTimeout: FiniteDuration,
@@ -119,30 +94,4 @@ object Config {
11994
migrations: List[Uri],
12095
disableDefaults: Boolean
12196
)
122-
123-
sealed trait ForgeSpecificCfg extends Product with Serializable
124-
125-
final case class AzureReposCfg(
126-
organization: Option[String]
127-
) extends ForgeSpecificCfg
128-
129-
final case class BitbucketCfg(
130-
useDefaultReviewers: Boolean
131-
) extends ForgeSpecificCfg
132-
133-
final case class BitbucketServerCfg(
134-
useDefaultReviewers: Boolean
135-
) extends ForgeSpecificCfg
136-
137-
final case class GitHubCfg(
138-
) extends ForgeSpecificCfg
139-
140-
final case class GitLabCfg(
141-
mergeWhenPipelineSucceeds: Boolean,
142-
requiredReviewers: Option[Int],
143-
removeSourceBranch: Boolean
144-
) extends ForgeSpecificCfg
145-
146-
final case class GiteaCfg(
147-
) extends ForgeSpecificCfg
14897
}

0 commit comments

Comments
 (0)