Skip to content

Commit

Permalink
Upgrade to latest Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
cy6erGn0m committed Nov 10, 2015
1 parent 9880b2e commit 31cb189
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 108 deletions.
4 changes: 2 additions & 2 deletions github-release-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<artifactId>github-release-parent</artifactId>
<groupId>cy.github</groupId>
<version>0.1-SNAPSHOT</version>
<version>0.2.1</version>
</parent>

<groupId>cy.github</groupId>
<artifactId>github-release-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<version>0.2.1</version>
<packaging>maven-plugin</packaging>

<name>github-release-plugin Maven Plugin</name>
Expand Down
85 changes: 40 additions & 45 deletions github-release-plugin/src/main/kotlin/gh-upload.kt
Original file line number Diff line number Diff line change
@@ -1,112 +1,107 @@
package cy.github

import org.apache.maven.plugin.AbstractMojo
import org.apache.maven.plugin.MojoFailureException
import org.apache.maven.plugins.annotations.Component
import org.apache.maven.plugins.annotations.LifecyclePhase
import org.apache.maven.plugin.*
import org.apache.maven.plugins.annotations.*
import org.apache.maven.plugins.annotations.Mojo
import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.project.MavenProject
import org.apache.maven.settings.Server
import org.apache.maven.settings.Settings
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher
import kotlin.properties.Delegates

Mojo(name = "gh-upload", defaultPhase = LifecyclePhase.DEPLOY, requiresOnline = true, threadSafe = true)
import org.apache.maven.project.*
import org.apache.maven.settings.*
import org.sonatype.plexus.components.sec.dispatcher.*

@Mojo(name = "gh-upload", defaultPhase = LifecyclePhase.DEPLOY, requiresOnline = true, threadSafe = true)
public class GitHubUpload : AbstractMojo() {

Component
@Component
var security: SecDispatcher? = null

Parameter(defaultValue = "\${project}", readonly = true, required = true)
@Parameter(defaultValue = "\${project}", readonly = true, required = true)
var project: MavenProject? = null

Parameter(defaultValue = "\${reactorProjects}", required = true, readonly = true)
@Parameter(defaultValue = "\${reactorProjects}", required = true, readonly = true)
var reactorProjects: List<MavenProject>? = null

Parameter(property = "maven.deploy.skip", defaultValue = "false")
@Parameter(property = "maven.deploy.skip", defaultValue = "false")
var skip: Boolean = false

Parameter(defaultValue = "\${settings}")
@Parameter(defaultValue = "\${settings}")
var settings: Settings? = null

Parameter(defaultValue = "github", required = true, readonly = true)
@Parameter(defaultValue = "github", required = true, readonly = true)
var serverId: String = "github"

Parameter(required = false)
@Parameter(required = false)
var endpointURL: String? = null

Parameter(required = false)
@Parameter(required = false)
var owner: String? = null

Parameter(required = false)
@Parameter(required = false)
var repository: String? = null

Parameter(required = true, defaultValue = "\${project.version}")
@Parameter(required = true, defaultValue = "\${project.version}")
var tagName: String = ""

Parameter(required = false)
@Parameter(required = false)
var releaseTitle = tagName

Parameter(required = true, defaultValue = "false")
@Parameter(required = true, defaultValue = "false")
var preRelease: Boolean = false

override fun execute() {
if (skip) {
getLog().warn("GitHubUpload mojo has been skipped because maven.deploy.skip=true")
log.warn("GitHubUpload mojo has been skipped because maven.deploy.skip=true")
return
}

val repo = getRepo()
val serverSettings = findServerSettings() ?: throw MojoFailureException("GitHub upload failed: no server configuration found for $serverId in settings.xml")
val auth = Auth(
userName = serverSettings.getUsername() ?: throw MojoFailureException("No username configured for github server ${serverSettings.getId()}"),
personalAccessToken = serverSettings.getPassword()?.decryptIfNeeded() ?: throw MojoFailureException("No password/personal access token specified for github server ${serverSettings.getId()}")
userName = serverSettings.username ?: throw MojoFailureException("No username configured for github server ${serverSettings.id}"),
personalAccessToken = serverSettings.password?.decryptIfNeeded() ?: throw MojoFailureException("No password/personal access token specified for github server ${serverSettings.id}")
)

getLog().info("Contacting server ${serverSettings.getId()} @ ${repo.serverEndpoint}")
log.info("Contacting server ${serverSettings.id} @ ${repo.serverEndpoint}")
val repositoryFormat = probeGitHubRepositoryFormat(repo.serverEndpoint, auth)
val releasesFormat = probeGitHubReleasesFormat(repositoryFormat, repo, auth)
getLog().debug("Releases URL format is $releasesFormat")
log.debug("Releases URL format is $releasesFormat")

getLog().info("Lookup/create release for tag $tagName")
log.info("Lookup/create release for tag $tagName")
val release = findRelease(releasesFormat, tagName, auth) ?: createRelease(auth, releasesFormat, tagName, releaseTitle, "", preRelease)
?: throw MojoFailureException("Failed to find/create release for tag $tagName")
getLog().debug("Found release $release")
log.debug("Found release $release")

if (settings?.isOffline() ?: false) {
if (settings?.isOffline ?: false) {
throw MojoFailureException("Can't upload artifacts in offline mode")
}

val defaultArtifact = project?.getArtifact()
val defaultArtifact = project?.artifact
if (defaultArtifact != null) {
val file = defaultArtifact.getFile()
val file = defaultArtifact.file
if (file != null) {
getLog().info("Uploading $file")
log.info("Uploading $file")
upload(auth, release.uploadFormat, file)
}
}

val artifacts = project?.getAttachedArtifacts() ?: emptyList()
artifacts.map { it.getFile() }.forEach { file ->
getLog().info("Uploading $file")
val artifacts = project?.attachedArtifacts ?: emptyList()
artifacts.map { it.file }.forEach { file ->
log.info("Uploading $file")
upload(auth, release.uploadFormat, file)
}

getLog().info("Upload for project ${project!!.getArtifactId()} completed. See ${release.htmlPage}")
log.info("Upload for project ${project!!.artifactId} completed. See ${release.htmlPage}")
}

private fun String.decryptIfNeeded() = "\\{.*\\}".toRegex().match(this)?.let { m ->
private fun String.decryptIfNeeded() = "\\{.*\\}".toRegex().find(this)?.let { m ->
security?.decrypt(m.value)
} ?: this

private val interactiveMode: Boolean
get() = settings?.isInteractiveMode() ?: false
get() = settings?.isInteractiveMode ?: false

private fun findServerSettings(): Server? = settings?.getServer(serverId)

private fun getRepo(): Repo {
val repos = listOf(project?.getScm()?.getConnection(), project?.getScm()?.getDeveloperConnection())
val repos = listOf(project?.scm?.connection, project?.scm?.developerConnection)
.filterNotNull()
.map { it.parseSCMUrl() }
.filterNotNull()
Expand All @@ -119,21 +114,21 @@ public class GitHubUpload : AbstractMojo() {
val host = when {
endpointURL != null -> endpointURL!!
hosts.isEmpty() -> "https://api.github.com"
hosts.size() > 1 -> throw MojoFailureException("Ambiguous github host specification: please correct connection/developerConnection or specify plugin endpointURL configuration parameter")
hosts.size > 1 -> throw MojoFailureException("Ambiguous github host specification: please correct connection/developerConnection or specify plugin endpointURL configuration parameter")
else -> hosts.single()
}

val owner = when {
this.owner != null -> this.owner!!
owners.isEmpty() -> throw MojoFailureException("Couldn't detect github username: please configure SCM connection/developerConnection or specify plugin's owner configuration parameter")
owners.size() > 1 -> throw MojoFailureException("Ambiguous github username: please fix SCM connection/developerConnection or specify plugin's owner configuration parameter")
owners.size > 1 -> throw MojoFailureException("Ambiguous github username: please fix SCM connection/developerConnection or specify plugin's owner configuration parameter")
else -> owners.single()
}

val repoName = when {
this.repository != null -> this.repository!!
repositories.isEmpty() -> throw MojoFailureException("Couldn't detect github repository name: please configure SCM connection/developerConnection or specify plugin's repository configuration parameter")
repositories.size() > 1 -> throw MojoFailureException("Ambiguous github repository name: please fix SCM connection/developerConnection or specify plugin's repository configuration parameter")
repositories.size > 1 -> throw MojoFailureException("Ambiguous github repository name: please fix SCM connection/developerConnection or specify plugin's repository configuration parameter")
else -> repositories.single()
}

Expand Down
6 changes: 3 additions & 3 deletions github-release-plugin/src/test/kotlin/utils.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cy.github.tests

import org.junit.Test as test
import cy.github.*
import kotlin.test.assertEquals
import kotlin.test.*
import org.junit.Test as test

class TestSCMParse {
test fun testSimple() {
@test fun testSimple() {
assertEquals(
Repo("https://api.github.com", "cy6erGn0m", "github-release-plugin"),
"scm:git:git@github.com:cy6erGn0m/github-release-plugin.git".parseSCMUrl())
Expand Down
2 changes: 1 addition & 1 deletion github-release-shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>github-release-parent</artifactId>
<groupId>cy.github</groupId>
<version>0.1-SNAPSHOT</version>
<version>0.2.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
56 changes: 27 additions & 29 deletions github-release-shared/src/main/kotlin/github-api.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package cy.github

import cy.rfc6570.expandURLFormat
import org.json.simple.JSONObject
import java.io.File
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
import java.util.Base64
import javax.net.ssl.HttpsURLConnection
import cy.rfc6570.*
import org.json.simple.*
import java.io.*
import java.net.*
import java.util.*
import javax.net.ssl.*


data class Auth(val userName: String, val personalAccessToken: String)
Expand All @@ -20,11 +18,11 @@ fun connectionOf(url: String, method: String = "GET", auth: Auth? = null): HttpU
val connection = URL(url).openConnection() as HttpURLConnection
connection.setRequestProperty("User-Agent", "Kotlin")
connection.setRequestProperty("Accept", "application/vnd.github.v3+json")
connection.setInstanceFollowRedirects(true)
connection.setAllowUserInteraction(false)
connection.setDefaultUseCaches(false)
connection.setDoInput(true)
connection.setRequestMethod(method)
connection.instanceFollowRedirects = true
connection.allowUserInteraction = false
connection.defaultUseCaches = false
connection.doInput = true
connection.requestMethod = method
if (auth != null) {
connection.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("${auth.userName}:${auth.personalAccessToken}".toByteArray()))
}
Expand All @@ -40,21 +38,21 @@ fun connectionOf(url: String, method: String = "GET", auth: Auth? = null): HttpU

fun createRelease(auth: Auth, releasesFormat: String, tagName: String, releaseTitle: String, description: String, preRelease: Boolean): Release? {
val request = JSONObject()
request.set("tag_name", tagName)
request.set("target_commitish", null)
request.set("name", releaseTitle)
request.set("body", description)
request.set("draft", false)
request.set("prerelease", preRelease)
request["tag_name"] = tagName
request["target_commitish"] = null
request["name"] = releaseTitle
request["body"] = description
request["draft"] = false
request["prerelease"] = preRelease

val connection = connectionOf(releasesFormat.expandURLFormat(emptyMap<String, String>()), "POST", auth)
connection.setDoOutput(true)
connection.getOutputStream().bufferedWriter().use {
connection.doOutput = true
connection.outputStream.bufferedWriter().use {
request.writeJSONString(it)
}

//println("${connection.getResponseCode()} ${connection.getResponseMessage()}")
connection.getErrorStream()?.let { error ->
connection.errorStream?.let { error ->
error.use {
it.copyTo(System.out)
}
Expand All @@ -77,7 +75,7 @@ fun JSONObject?.parseRelease(releasesFormat: String): Release? {
return if (this == null || id == null) {
null
} else {
Release(this.get("tag_name")!!.toString(), this.getAsLong("id")!!, releasesFormat, this.get("upload_url")!!.toString(), this.get("html_url")!!.toString())
Release(this.getRaw("tag_name")!!.toString(), this.getAsLong("id")!!, releasesFormat, this.getRaw("upload_url")!!.toString(), this.getRaw("html_url")!!.toString())
}
}

Expand All @@ -104,19 +102,19 @@ fun upload(auth: Auth, uploadFormat: String, source: File, name: String = source

else -> "binary/octet-stream"
})
connection.setDoOutput(true)
connection.doOutput = true

connection.getOutputStream().use { out ->
connection.outputStream.use { out ->
source.inputStream().use { ins ->
ins.copyTo(out)
}
}

connection.getErrorStream()?.let { error ->
connection.errorStream?.let { error ->
error.use {
it.copyTo(System.out)
}
throw IOException("${connection.getResponseCode()} ${connection.getResponseMessage()}")
throw IOException("${connection.responseCode} ${connection.responseMessage}")
}

connection.withReader {
Expand All @@ -126,10 +124,10 @@ fun upload(auth: Auth, uploadFormat: String, source: File, name: String = source

fun probeGitHubRepositoryFormat(base: String = "https://api.github.com", auth: Auth? = null): String =
connectionOf(base, auth = auth).withReader {
it.toJSONObject()?.get("repository_url")?.toString() ?: throw IllegalArgumentException("No repository_url endpoint found for $base")
it.toJSONObject()?.getRaw("repository_url")?.toString() ?: throw IllegalArgumentException("No repository_url endpoint found for $base")
}

fun probeGitHubReleasesFormat(repositoryFormat: String, repo: Repo, auth: Auth? = null): String =
connectionOf(repositoryFormat.expandURLFormat(mapOf("owner" to repo.user, "repo" to repo.repoName)), auth = auth).withReader {
it.toJSONObject()?.get("releases_url")?.toString() ?: throw IllegalArgumentException("No releases_url found for $repositoryFormat ($repo)")
it.toJSONObject()?.getRaw("releases_url")?.toString() ?: throw IllegalArgumentException("No releases_url found for $repositoryFormat ($repo)")
}
Loading

0 comments on commit 31cb189

Please sign in to comment.