Skip to content

Commit

Permalink
Add Coursier support
Browse files Browse the repository at this point in the history
Closes #23
  • Loading branch information
Alex Zolotko committed Sep 15, 2021
1 parent 713759b commit f63374b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ project/metals.sbt
#vscode
.vscode/

.bsp/
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "sbt-gitlab"
organization := "com.gilcloud"
version := "0.0.6"
version := "0.0.7-SNAPSHOT"
description := "publishing and dependency resolution for gitlab both private and hosted using header auth"
sbtPlugin := true

Expand All @@ -17,7 +17,7 @@ publishMavenStyle := false
bintrayRepository := "sbt-plugins"
bintrayOrganization := Some("gilcloud")

initialCommands in console := "import com.gilcloud.sbt.gitlab._"
console / initialCommands := "import com.gilcloud.sbt.gitlab._"

enablePlugins(ScriptedPlugin)
// set up 'scripted; sbt plugin for testing sbt plugins
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.3.10
sbt.version=1.5.5
58 changes: 40 additions & 18 deletions src/main/scala/com/gilcloud/sbt/gitlab/GitlabPlugin.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gilcloud.sbt.gitlab

import lmcoursier.definitions.Authentication
import okhttp3.OkHttpClient
import org.apache.ivy.util.url.{
URLHandler,
Expand All @@ -14,7 +15,6 @@ import sbt._
import scala.util.Try
object GitlabPlugin extends AutoPlugin {


lazy val headerAuthHandler =
taskKey[Unit]("perform auth using header credentials")

Expand Down Expand Up @@ -61,11 +61,23 @@ object GitlabPlugin extends AutoPlugin {
downloader: URLHandler
): Unit = {}
}

override def projectSettings: Seq[Def.Setting[_]] =
inScope(publish.scopedKey.scope)(gitLabProjectSettings)

val gitLabProjectSettings : Seq[Def.Setting[_]] =
private val gitlabCredentialsHandler = Def.task {
gitlabCredentials.value.orElse {
Credentials
.allDirect(credentials.value.filter {
case f: FileCredentials => f.path.exists()
case _ => true
})
.find(_.realm == "gitlab")
.map { GitlabCredentials(_) }
}
}

val gitLabProjectSettings : Seq[Def.Setting[_]] =
Seq(
publishMavenStyle := true,
gitlabDomain := sys.env.getOrElse("CI_SERVER_HOST", "gitlab.com"),
Expand All @@ -81,15 +93,7 @@ object GitlabPlugin extends AutoPlugin {
.map(GitlabCredentials("Job-Token", _))
},
headerAuthHandler := {
val cred = gitlabCredentials.value.orElse {
Credentials
.allDirect(credentials.value.filter {
case f: FileCredentials => f.path.exists()
case _ => true
})
.find(_.realm == "gitlab")
.map { GitlabCredentials(_) }
}
val cred = gitlabCredentialsHandler.value
val logger = streams.value.log
val client = headerEnrichingClientBuilder(
CustomHttp.okhttpClientBuilder.value,
Expand All @@ -110,13 +114,31 @@ object GitlabPlugin extends AutoPlugin {
gitlabProjectId.value.map(gitlabProjectRepository(gitlabDomain.value, _)) orElse
gitlabGroupId.value.map(gitlabGroupRepository(gitlabDomain.value, _))
},
resolvers ++= gitlabProjectId.value.map(gitlabProjectRepository(gitlabDomain.value, _)) orElse
gitlabGroupId.value.map(gitlabGroupRepository(gitlabDomain.value, _))
resolvers ++= gitlabProjectId.value
.map(gitlabProjectRepository(gitlabDomain.value, _)) orElse
gitlabGroupId.value.map(gitlabGroupRepository(gitlabDomain.value, _)),
csrConfiguration := {
val current = csrConfiguration.value
gitlabCredentialsHandler.value.fold(current) { token =>
current.addRepositoryAuthentication(
repositoryName,
Authentication(Seq(token.key -> token.value))
)
}
}
)

private def gitlabProjectRepository(gitlabDomain: String, projectId: Int): MavenRepository =
"gitlab-maven" at s"https://$gitlabDomain/api/v4/projects/$projectId/packages/maven"
private val repositoryName = "gitlab-maven"

private def gitlabProjectRepository(
gitlabDomain: String,
projectId: Int
): MavenRepository =
repositoryName at s"https://$gitlabDomain/api/v4/projects/$projectId/packages/maven"

private def gitlabGroupRepository(gitlabDomain: String, groupId: Int): MavenRepository =
"gitlab-maven" at s"https://$gitlabDomain/api/v4/groups/$groupId/-/packages/maven"
private def gitlabGroupRepository(
gitlabDomain: String,
groupId: Int
): MavenRepository =
repositoryName at s"https://$gitlabDomain/api/v4/groups/$groupId/-/packages/maven"
}

0 comments on commit f63374b

Please sign in to comment.