Skip to content

Commit

Permalink
VCS selection from the Config
Browse files Browse the repository at this point in the history
  • Loading branch information
daddykotex committed Jun 6, 2019
1 parent 5cda862 commit 7ab87af
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ object Cli {
reposFile: String,
gitAuthorName: String,
gitAuthorEmail: String,
vcsType: SupportedVCS = SupportedVCS.GitHub,
vcsApiHost: Uri = Uri.uri("https://api.github.com"),
vcsLogin: String,
gitAskPass: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ final case class Config(
workspace: File,
reposFile: File,
gitAuthor: Author,
vcsType: SupportedVCS,
vcsApiHost: Uri,
vcsLogin: String,
gitAskPass: File,
Expand Down Expand Up @@ -77,6 +78,7 @@ object Config {
workspace = args.workspace.toFile,
reposFile = args.reposFile.toFile,
gitAuthor = Author(args.gitAuthorName, args.gitAuthorEmail),
vcsType = args.vcsType,
vcsApiHost = args.vcsApiHost,
vcsLogin = args.vcsLogin,
gitAskPass = args.gitAskPass.toFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import org.scalasteward.core.dependency.json.JsonDependencyRepository
import org.scalasteward.core.dependency.{DependencyRepository, DependencyService}
import org.scalasteward.core.git.GitAlg
import org.scalasteward.core.vcs.data.AuthenticatedUser
import org.scalasteward.core.github.http4s.Http4sGitHubApiAlg
import org.scalasteward.core.github.http4s.authentication.addCredentials
import org.scalasteward.core.io.{FileAlg, ProcessAlg, WorkspaceAlg}
import org.scalasteward.core.nurture.json.JsonPullRequestRepo
import org.scalasteward.core.nurture.{EditAlg, NurtureAlg, PullRequestRepository}
Expand All @@ -35,10 +33,9 @@ import org.scalasteward.core.sbt.SbtAlg
import org.scalasteward.core.update.json.JsonUpdateRepository
import org.scalasteward.core.update.{FilterAlg, UpdateRepository, UpdateService}
import org.scalasteward.core.util.{DateTimeAlg, HttpJsonClient, LogAlg}
import org.scalasteward.core.vcs.{VCSApiAlg, VCSRepoAlg, VCSSpecifics}
import org.scalasteward.core.vcs.{VCSApiAlg, VCSRepoAlg, VCSSelection, VCSSpecifics}

import scala.concurrent.ExecutionContext
import org.scalasteward.core.github.GitHubSpecifics

final case class Context[F[_]](
config: Config,
Expand Down Expand Up @@ -74,9 +71,9 @@ object Context {
implicit val editAlg: EditAlg[F] = EditAlg.create[F]
implicit val gitAlg: GitAlg[F] = GitAlg.create[F]
implicit val httpJsonClient: HttpJsonClient[F] = new HttpJsonClient[F]
implicit val vcsSpecifics: VCSSpecifics = new GitHubSpecifics(config)
implicit val vcsApiAlg: VCSApiAlg[F] =
new Http4sGitHubApiAlg[F](config.vcsApiHost, _ => addCredentials(user)) //TODO SELECT GIVEN A CONFIG
val vcsSelection = new VCSSelection[F]
implicit val (vcsApiAlg: VCSApiAlg[F], vcsSpecifics: VCSSpecifics) =
vcsSelection.build(config)
implicit val vcsRepoAlg: VCSRepoAlg[F] = VCSRepoAlg.create[F](config, gitAlg)
implicit val pullRequestRepo: PullRequestRepository[F] = new JsonPullRequestRepo[F]
implicit val sbtAlg: SbtAlg[F] = SbtAlg.create[F]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2018-2019 scala-steward contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.scalasteward.core.application

import cats.Eq
import cats.implicits._
import caseapp.core.Error.MalformedValue
import caseapp.core.argparser.ArgParser

sealed trait SupportedVCS {
import SupportedVCS.{GitHub, Gitlab}
val asString = this match {
case GitHub => "github"
case Gitlab => "gitlab"
}
}

object SupportedVCS {
case object GitHub extends SupportedVCS
case object Gitlab extends SupportedVCS

implicit val supportedVCSEq: Eq[SupportedVCS] =
Eq.fromUniversalEquals

def parse(value: String): Either[String, SupportedVCS] = value match {
case "github" => Right(GitHub)
case "gitlab" => Right(Gitlab)
case unknown => Left(s"Unexpected string '$unknown'")
}

implicit val supportedVCSParser: ArgParser[SupportedVCS] =
ArgParser[String].xmapError(
_.asString,
s => SupportedVCS.parse(s).leftMap(error => MalformedValue("SupportedVCS", error))
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2018-2019 scala-steward contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.scalasteward.core.vcs

import org.scalasteward.core.application.Config
import org.scalasteward.core.util.HttpJsonClient
import org.scalasteward.core.github.GitHubSpecifics
import org.scalasteward.core.github.http4s.Http4sGitHubApiAlg
import org.scalasteward.core.gitlab.http4s.Http4sGitLabApiAlg
import org.scalasteward.core.gitlab.GitlabSpecifics
import org.scalasteward.core.vcs.data.AuthenticatedUser
import cats.effect.Sync
import org.scalasteward.core.application.SupportedVCS.GitHub
import org.scalasteward.core.application.SupportedVCS.Gitlab

class VCSSelection[F[_]: Sync](implicit client: HttpJsonClient[F], user: AuthenticatedUser) {
private def github(config: Config): (Http4sGitHubApiAlg[F], GitHubSpecifics) = {
import org.scalasteward.core.github.http4s.authentication.addCredentials

val alg = new Http4sGitHubApiAlg[F](config.vcsApiHost, _ => addCredentials(user))
val specifics = new GitHubSpecifics(config)
(alg, specifics)
}
private def gitlab(config: Config): (Http4sGitLabApiAlg[F], GitlabSpecifics) = {
import org.scalasteward.core.gitlab.http4s.authentication.addCredentials

val alg = new Http4sGitLabApiAlg[F](config.vcsApiHost, user, _ => addCredentials(user))
val specifics = new GitlabSpecifics()
(alg, specifics)
}
def build(config: Config): (VCSApiAlg[F], VCSSpecifics) = config.vcsType match {
case GitHub => github(config)
case Gitlab => gitlab(config)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CliTest extends FunSuite with Matchers {
List("--repos-file", "b"),
List("--git-author-name", "c"),
List("--git-author-email", "d"),
List("--vcs-type", "gitlab"),
List("--vcs-api-host", "http://example.com"),
List("--vcs-login", "e"),
List("--git-ask-pass", "f"),
Expand All @@ -27,6 +28,7 @@ class CliTest extends FunSuite with Matchers {
Cli.Args(
workspace = "a",
reposFile = "b",
vcsType = SupportedVCS.Gitlab,
gitAuthorName = "c",
gitAuthorEmail = "d",
vcsApiHost = Uri.uri("http://example.com"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import org.scalasteward.core.sbt.SbtAlg
import org.scalasteward.core.update.FilterAlg
import org.scalasteward.core.util.{DateTimeAlg, LogAlg}
import org.scalasteward.core.vcs.VCSRepoAlg
import org.scalasteward.core.application.SupportedVCS

object MockContext {
implicit val config: Config = Config(
workspace = File.temp / "ws",
reposFile = File.temp / "repos.md",
gitAuthor = Author("Bot Doe", "bot@example.org"),
vcsType = SupportedVCS.GitHub,
vcsApiHost = Uri.uri(""),
vcsLogin = "bot-doe",
gitAskPass = File.temp / "askpass.sh",
Expand Down

0 comments on commit 7ab87af

Please sign in to comment.