diff --git a/core/src/main/scala/codeartifact/CodeArtifact.scala b/core/src/main/scala/codeartifact/CodeArtifact.scala index e08bd71..fb18091 100644 --- a/core/src/main/scala/codeartifact/CodeArtifact.scala +++ b/core/src/main/scala/codeartifact/CodeArtifact.scala @@ -1,5 +1,6 @@ package codeartifact +import software.amazon.awssdk.regions.Region import software.amazon.awssdk.services.codeartifact.CodeartifactClient import software.amazon.awssdk.services.codeartifact.model.GetAuthorizationTokenRequest @@ -23,15 +24,17 @@ object CodeArtifact { .durationSeconds(15.minutes.toSeconds) .build() - private def getAuthTokenFromRequest(req: GetAuthorizationTokenRequest): String = + private def getAuthTokenFromRequest(region: Region, req: GetAuthorizationTokenRequest): String = CodeartifactClient - .create() + .builder() + .region(region) + .build() .getAuthorizationToken(req) .authorizationToken() def getAuthToken(repo: CodeArtifactRepo): Option[String] = try { - Some(getAuthTokenFromRequest(getAuthorizationTokenRequest(repo.domain, repo.owner))) + Some(getAuthTokenFromRequest(Region.of(repo.region), getAuthorizationTokenRequest(repo.domain, repo.owner))) } catch { case _: Throwable => None } diff --git a/core/src/main/scala/codeartifact/CodeArtifactRepo.scala b/core/src/main/scala/codeartifact/CodeArtifactRepo.scala index 5fd45b6..c71a70a 100644 --- a/core/src/main/scala/codeartifact/CodeArtifactRepo.scala +++ b/core/src/main/scala/codeartifact/CodeArtifactRepo.scala @@ -7,6 +7,7 @@ case class CodeArtifactRepo( domain: String, host: String, owner: String, + region: String, url: String ) { def realm: String = s"$domain/$name" @@ -14,34 +15,14 @@ case class CodeArtifactRepo( } object CodeArtifactRepo { + private val CodeArtifactUrl = "https://((.*)-(.*).d.codeartifact.(.*).amazonaws.com)/maven/(.*)".r def fromUrl(url: String): CodeArtifactRepo = { - if (url.isEmpty()) { - sys.error("""codeArtifactUrl not defined. assign this with codeArtifactUrl := "your-url"""") + url match { + case CodeArtifactUrl(host, domain, owner, region, repo) => + CodeArtifactRepo(name = repo, domain = domain, host = host, owner = owner, region = region, url = url) + case _ => + sys.error(s"Invalid codeArtifactUrl: $url") } - - // Url looks like: - // https://-.d.codeartifact.us-west-2.amazonaws.com/maven/ - val juri = new java.net.URI(url) - - // Split on slashes, and get the last element: . - val name = juri.getPath().split('/').last - - // Split on dots. Take the head, which is the - section. - // Split on dashes. - val host = juri.getHost() - val parts = host.split('.').head.split('-') - // Last element is . - val owner = parts.last - // The rest are the . Merge them together again. - val domain = parts.init.mkString("-") - - CodeArtifactRepo( - name = name, - domain = domain, - host = host, - owner = owner, - url = url - ) } }