Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformatted sources #2085

Merged
merged 2 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ ba4e52433e07bd408fd3e52ab9b06659b687f594

# Scala Steward: Reformat with scalafmt 3.5.8
31a8c01406b77246150a8418a310132357b83e08

# Reformatted sources
a769e208cf7e276f11f859ca7e618d5c973e6d4c
9 changes: 6 additions & 3 deletions bsp/src/mill/bsp/BSP.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ object BSP extends ExternalModule {
if (os.exists(bspFile)) T.log.info(s"Overwriting BSP connection file: ${bspFile}")
else T.log.info(s"Creating BSP connection file: ${bspFile}")
val withDebug = T.log.debugEnabled
if(withDebug) T.log.debug("Enabled debug logging for the BSP server. If you want to disable it, you need to re-run this install command without the --debug option.")
if (withDebug) T.log.debug(
"Enabled debug logging for the BSP server. If you want to disable it, you need to re-run this install command without the --debug option."
)
os.write.over(bspFile, createBspConnectionJson(jobs, withDebug), createFolders = true)
}

@deprecated("Use other overload instead.", "Mill after 0.10.7")
def createBspConnectionJson(jobs: Int): String = BSP.createBspConnectionJson(jobs: Int, debug = false)
def createBspConnectionJson(jobs: Int): String =
BSP.createBspConnectionJson(jobs: Int, debug = false)

// creates a Json with the BSP connection details
def createBspConnectionJson(jobs: Int, debug: Boolean): String = {
Expand All @@ -74,7 +77,7 @@ object BSP extends ExternalModule {
"false",
"--jobs",
s"${jobs}"
) ++ (if(debug) Seq("--debug") else Seq()),
) ++ (if (debug) Seq("--debug") else Seq()),
millVersion = BuildInfo.millVersion,
bspVersion = bspProtocolVersion,
languages = languages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package mill.contrib.gitlab
case class GitlabAuthHeaders(headers: Seq[(String, String)])

object GitlabAuthHeaders {
def apply(header: String, value: String): GitlabAuthHeaders = GitlabAuthHeaders(Seq(header -> value))
def apply(header: String, value: String): GitlabAuthHeaders =
GitlabAuthHeaders(Seq(header -> value))

def privateToken(token: String): GitlabAuthHeaders = GitlabAuthHeaders("Private-Token", token)
def deployToken(token: String): GitlabAuthHeaders = GitlabAuthHeaders("Deploy-Token", token)
def jobToken(token: String): GitlabAuthHeaders = GitlabAuthHeaders("Job-Token", token)
def deployToken(token: String): GitlabAuthHeaders = GitlabAuthHeaders("Deploy-Token", token)
def jobToken(token: String): GitlabAuthHeaders = GitlabAuthHeaders("Job-Token", token)
}
16 changes: 8 additions & 8 deletions contrib/gitlab/src/mill/contrib/gitlab/GitlabHttpApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ class GitlabUploader(
connectTimeout: Int = 5000
) {
val http = requests.Session(
readTimeout = readTimeout,
connectTimeout = connectTimeout,
maxRedirects = 0,
check = false
readTimeout = readTimeout,
connectTimeout = connectTimeout,
maxRedirects = 0,
check = false
)

private val uploadTimeout = 2.minutes.toMillis.toInt

// https://docs.gitlab.com/ee/user/packages/maven_repository/#publish-a-package
def upload(uri: String, data: Array[Byte]): requests.Response = {
http.put(
uri,
readTimeout = uploadTimeout,
headers = authentication.headers,
data = data
uri,
readTimeout = uploadTimeout,
headers = authentication.headers,
data = data
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ case class ProjectRepository(baseUrl: String, projectId: Int) extends GitlabPack
// https://docs.gitlab.com/ee/api/packages/maven.html#upload-a-package-file
def uploadUrl(artifact: Artifact): String = {
val repoUrl = url()
val group = artifact.group.replace(".", "/")
val id = artifact.id
val group = artifact.group.replace(".", "/")
val id = artifact.id
val version = artifact.version
s"$repoUrl/$group/$id/$version"
}
Expand Down
35 changes: 18 additions & 17 deletions contrib/gitlab/src/mill/contrib/gitlab/GitlabPublishModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ trait GitlabPublishModule extends PublishModule {
val auth = tokenLookup.resolveGitlabToken(log, env, props)
if (auth.isEmpty) {
throw new GitlabAuthenticationException(
s"Unable to resolve authentication with $tokenLookup"
s"Unable to resolve authentication with $tokenLookup"
)
}
auth.get
}

def env: Input[Map[String, String]] = T.input(T.ctx().env)
def props: Map[String, String] = sys.props.toMap
def props: Map[String, String] = sys.props.toMap

def publishGitlab(
readTimeout: Int = 60000,
Expand All @@ -49,11 +49,11 @@ trait GitlabPublishModule extends PublishModule {
} else {
val uploader = new GitlabUploader(gitlabAuth)
new GitlabPublisher(
uploader.upload,
gitlabRepo,
readTimeout,
connectTimeout,
T.log
uploader.upload,
gitlabRepo,
readTimeout,
connectTimeout,
T.log
).publish(artifacts.map { case (a, b) => (a.path, b) }, artifactInfo)
}

Expand All @@ -73,20 +73,21 @@ object GitlabPublishModule extends ExternalModule {
val repo = ProjectRepository(gitlabRoot, projectId)
val auth = GitlabAuthHeaders.privateToken(personalToken)

val artifacts: Seq[(Seq[(os.Path, String)], Artifact)] = T.sequence(publishArtifacts.value)().map {
case PublishModule.PublishData(a, s) => (s.map { case (p, f) => (p.path, f) }, a)
}
val artifacts: Seq[(Seq[(os.Path, String)], Artifact)] =
T.sequence(publishArtifacts.value)().map {
case PublishModule.PublishData(a, s) => (s.map { case (p, f) => (p.path, f) }, a)
}

val uploader = new GitlabUploader(auth)

new GitlabPublisher(
uploader.upload,
repo,
readTimeout,
connectTimeout,
T.log
uploader.upload,
repo,
readTimeout,
connectTimeout,
T.log
).publishAll(
artifacts: _*
artifacts: _*
)
}

Expand All @@ -101,7 +102,7 @@ trait GitlabMavenRepository {

def mavenRepo(implicit context: Ctx): MavenRepository = {
val gitlabAuth = tokenLookup.resolveGitlabToken(context.log, context.env, sys.props.toMap).get
val auth = Authentication(gitlabAuth.headers)
val auth = Authentication(gitlabAuth.headers)
MavenRepository(repository.url(), Some(auth))
}
}
4 changes: 2 additions & 2 deletions contrib/gitlab/src/mill/contrib/gitlab/GitlabPublisher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class GitlabPublisher(
val publishResults = payloads.map { case (fileName, data) =>
log.info(s"Uploading $fileName")
val uploadTarget = repo.uploadUrl(artifact)
val resp = upload(s"$uploadTarget/$fileName", data)
val resp = upload(s"$uploadTarget/$fileName", data)
resp
}
artifact -> publishResults
Expand All @@ -58,7 +58,7 @@ class GitlabPublisher(
}
// Or just log? Fail later?
throw new RuntimeException(
s"Failed to publish $artifact to Gitlab. Errors: \n${errors.mkString("\n")}"
s"Failed to publish $artifact to Gitlab. Errors: \n${errors.mkString("\n")}"
)
}
}
Expand Down
98 changes: 51 additions & 47 deletions contrib/gitlab/src/mill/contrib/gitlab/GitlabTokenLookup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ trait GitlabTokenLookup {
import GitlabTokenLookup._

// Default search places for token
def personalTokenEnv: String = "GITLAB_PERSONAL_ACCESS_TOKEN"
def personalTokenEnv: String = "GITLAB_PERSONAL_ACCESS_TOKEN"
def personalTokenProperty: String = "gitlab.personal-access-token"
def personalTokenFile: os.Path = os.home / os.RelPath(".mill/gitlab/personal-access-token")
def personalTokenFileWD: os.Path = os.home / os.RelPath(".gitlab/personal-access-token")
def deployTokenEnv: String = "GITLAB_DEPLOY_TOKEN"
def deployTokenProperty: String = "gitlab.deploy-token"
def deployTokenFile: os.Path = os.home / os.RelPath(".mill/gitlab/deploy-token")
def deployTokenFileWD: os.Path = os.pwd / os.RelPath(".gitlab/deploy-token")
def jobTokenEnv: String = "CI_JOB_TOKEN"
def personalTokenFile: os.Path = os.home / os.RelPath(".mill/gitlab/personal-access-token")
def personalTokenFileWD: os.Path = os.home / os.RelPath(".gitlab/personal-access-token")
def deployTokenEnv: String = "GITLAB_DEPLOY_TOKEN"
def deployTokenProperty: String = "gitlab.deploy-token"
def deployTokenFile: os.Path = os.home / os.RelPath(".mill/gitlab/deploy-token")
def deployTokenFileWD: os.Path = os.pwd / os.RelPath(".gitlab/deploy-token")
def jobTokenEnv: String = "CI_JOB_TOKEN"

// Default token search order. Implementation picks first found and does not look for the rest.
def tokenSearchOrder: Seq[GitlabToken] = Seq(
Personal(Env(personalTokenEnv)),
Personal(Property(personalTokenProperty)),
Personal(File(personalTokenFile)),
Personal(File(personalTokenFileWD)),
Deploy(Env(deployTokenEnv)),
Deploy(Property(deployTokenProperty)),
Deploy(File(deployTokenFile)),
Deploy(File(deployTokenFileWD)),
CIJob(Env(jobTokenEnv))
Personal(Env(personalTokenEnv)),
Personal(Property(personalTokenProperty)),
Personal(File(personalTokenFile)),
Personal(File(personalTokenFileWD)),
Deploy(Env(deployTokenEnv)),
Deploy(Property(deployTokenProperty)),
Deploy(File(deployTokenFile)),
Deploy(File(deployTokenFileWD)),
CIJob(Env(jobTokenEnv))
)

// Finds gitlab token from this environment. Overriding this is not generally necessary.
Expand All @@ -53,16 +53,18 @@ trait GitlabTokenLookup {
): Either[String, GitlabAuthHeaders] = {

def readSource(source: TokenSource): Either[String, String] = source match {
case Env(name) => env.get(name).toRight(s"Could not read environment variable $name")
case Property(prop) => props.get(prop).toRight(s"Could not read system property variable $prop")
case File(path) => Try(os.read(path)).map(_.trim).toEither.left.map(e => s"failed to read file $e")
case Custom(f) => f()
case Env(name) => env.get(name).toRight(s"Could not read environment variable $name")
case Property(prop) =>
props.get(prop).toRight(s"Could not read system property variable $prop")
case File(path) =>
Try(os.read(path)).map(_.trim).toEither.left.map(e => s"failed to read file $e")
case Custom(f) => f()
}

token match {
case Personal(source) => readSource(source).map(GitlabAuthHeaders.privateToken)
case Deploy(source) => readSource(source).map(GitlabAuthHeaders.deployToken)
case CIJob(source) => readSource(source).map(GitlabAuthHeaders.jobToken)
case Personal(source) => readSource(source).map(GitlabAuthHeaders.privateToken)
case Deploy(source) => readSource(source).map(GitlabAuthHeaders.deployToken)
case CIJob(source) => readSource(source).map(GitlabAuthHeaders.jobToken)
case CustomHeader(header, source) => readSource(source).map(GitlabAuthHeaders(header, _))
}
}
Expand All @@ -73,34 +75,36 @@ trait GitlabTokenLookup {

object GitlabTokenLookup {

/** Possible types of a Gitlab authentication header.
* - Personal = "Private-Token" ->
* - Deploy = "Deploy-Token"->
* - CIJob = "Job-Token" ->
* - CustomHeader = Use with TokenSource/Custom to produce anything you like
*
* Currently only one custom header is supported. If you need multiple override gitlabToken from GitlabPublishModule
* directly
*/
/**
* Possible types of a Gitlab authentication header.
* - Personal = "Private-Token" ->
* - Deploy = "Deploy-Token"->
* - CIJob = "Job-Token" ->
* - CustomHeader = Use with TokenSource/Custom to produce anything you like
*
* Currently only one custom header is supported. If you need multiple override gitlabToken from GitlabPublishModule
* directly
*/
trait GitlabToken {
def source: TokenSource
}
case class Personal(source: TokenSource) extends GitlabToken
case class Deploy(source: TokenSource) extends GitlabToken
case class CIJob(source: TokenSource) extends GitlabToken
case class Personal(source: TokenSource) extends GitlabToken
case class Deploy(source: TokenSource) extends GitlabToken
case class CIJob(source: TokenSource) extends GitlabToken
case class CustomHeader(header: String, source: TokenSource) extends GitlabToken

/** Possible source of token value. Either an
* - Env = Environment variable
* - Property = Javas system property
* - File =Contents of a file on local disk.
* - Custom = Own function
*
* Possible additions, that can now be supported with Custom: KeyVault, Yaml, etc..
*/
/**
* Possible source of token value. Either an
* - Env = Environment variable
* - Property = Javas system property
* - File =Contents of a file on local disk.
* - Custom = Own function
*
* Possible additions, that can now be supported with Custom: KeyVault, Yaml, etc..
*/
sealed trait TokenSource
case class Env(name: String) extends TokenSource
case class File(path: os.Path) extends TokenSource
case class Property(property: String) extends TokenSource
case class Env(name: String) extends TokenSource
case class File(path: os.Path) extends TokenSource
case class Property(property: String) extends TokenSource
case class Custom(f: () => Either[String, String]) extends TokenSource
}
34 changes: 21 additions & 13 deletions contrib/gitlab/test/src/mill/contrib/gitlab/GitlabTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ object GitlabTests extends TestSuite {
test("Token search returns first applicable") {
object testLookup extends GitlabTokenLookup {
override def tokenSearchOrder: Seq[GitlabToken] = Seq(
Personal(Property("gl.token1")),
Personal(Property("gl.token2"))
Personal(Property("gl.token1")),
Personal(Property("gl.token2"))
)
}

val none = testLookup.resolveGitlabToken(DummyLogger, Map.empty, Map.empty)
val first = testLookup.resolveGitlabToken(DummyLogger, Map.empty, Map("gl.token1" -> "1"))
val none = testLookup.resolveGitlabToken(DummyLogger, Map.empty, Map.empty)
val first = testLookup.resolveGitlabToken(DummyLogger, Map.empty, Map("gl.token1" -> "1"))
val second = testLookup.resolveGitlabToken(DummyLogger, Map.empty, Map("gl.token2" -> "2"))
val both = testLookup.resolveGitlabToken(DummyLogger, Map.empty, Map("gl.token1" -> "1", "gl.token2" -> "2"))
val both = testLookup.resolveGitlabToken(
DummyLogger,
Map.empty,
Map("gl.token1" -> "1", "gl.token2" -> "2")
)

assert(none.isEmpty)
assertMatch(first) { case Some(GitlabAuthHeaders(Seq(("Private-Token", "1")))) => }
Expand All @@ -33,16 +37,20 @@ object GitlabTests extends TestSuite {

test("Token from environment variable") {
val token =
defaultLookup.resolveGitlabToken(DummyLogger, Map("GITLAB_PERSONAL_ACCESS_TOKEN" -> "t"), Map.empty)
defaultLookup.resolveGitlabToken(
DummyLogger,
Map("GITLAB_PERSONAL_ACCESS_TOKEN" -> "t"),
Map.empty
)

assertMatch(token) { case Some(GitlabAuthHeaders(Seq(("Private-Token", "t")))) => }
}

test("Token from property") {
val token = defaultLookup.resolveGitlabToken(
DummyLogger,
Map("GITLAB_DEPLOY_TOKEN" -> "t"),
Map("gitlab.personal-access-token" -> "pt")
DummyLogger,
Map("GITLAB_DEPLOY_TOKEN" -> "t"),
Map("gitlab.personal-access-token" -> "pt")
)

// personal access token property resolves before deploy token in default lookup
Expand All @@ -55,7 +63,7 @@ object GitlabTests extends TestSuite {

object fileEnv extends GitlabTokenLookup {
override def tokenSearchOrder: Seq[GitlabToken] = Seq(
Deploy(File(tokenFile))
Deploy(File(tokenFile))
)
}

Expand All @@ -69,7 +77,7 @@ object GitlabTests extends TestSuite {
test("Custom token source.") {
object customEnv extends GitlabTokenLookup {
override def tokenSearchOrder: Seq[GitlabToken] = Seq(
Deploy(Custom(() => Right("tok")))
Deploy(Custom(() => Right("tok")))
)
}

Expand All @@ -88,7 +96,7 @@ object GitlabTests extends TestSuite {
val urls: ListBuffer[String] = ListBuffer[String]()
}

val repo = ProjectRepository("https://gitlab.local", 10)
val repo = ProjectRepository("https://gitlab.local", 10)
val publisher = new GitlabPublisher(uploader, repo, 1, 1, DummyLogger)

val fakeFile = os.pwd / "dummy.data"
Expand All @@ -102,7 +110,7 @@ object GitlabTests extends TestSuite {

assert(uploader.urls.size == 1)
assert(
uploader.urls.head == "https://gitlab.local/api/v4/projects/10/packages/maven/test/group/id/0.0.0/data.file"
uploader.urls.head == "https://gitlab.local/api/v4/projects/10/packages/maven/test/group/id/0.0.0/data.file"
)
}
}
Expand Down
Loading