Skip to content

Commit

Permalink
Externally create release
Browse files Browse the repository at this point in the history
  • Loading branch information
kpgalligan committed Dec 8, 2024
1 parent 6d03a75 commit 7bd3322
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ SONATYPE_HOST=DEFAULT
RELEASE_SIGNING_ENABLED=true

GROUP=co.touchlab.kmmbridge
VERSION_NAME=1.1.1
VERSION_NAME=1.1.2-a1
VERSION_NAME_3x=0.3.7

POM_URL=https://github.com/touchlab/KMMBridge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package co.touchlab.kmmbridge.github
import co.touchlab.kmmbridge.KmmBridgeExtension
import co.touchlab.kmmbridge.artifactmanager.ArtifactManager
import co.touchlab.kmmbridge.github.internal.GithubCalls
import co.touchlab.kmmbridge.github.internal.githubArtifactIdentifierName
import co.touchlab.kmmbridge.github.internal.githubArtifactReleaseId
import co.touchlab.kmmbridge.github.internal.githubPublishToken
import co.touchlab.kmmbridge.github.internal.githubRepo
import org.gradle.api.GradleException
Expand All @@ -12,9 +14,14 @@ import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.getByType
import java.io.File

internal class GithubReleaseArtifactManager(
private val repository: String?, private val releaseString: String?, private val useExistingRelease: Boolean
import kotlin.properties.Delegates

open class GithubReleaseArtifactManager(
private val repository: String?,
private val releaseString: String?,
@Deprecated("Releases should be created externally. This parameter controls the flow for releases created " +
"by this class, which will eventually be unsupported.")
private val useExistingRelease: Boolean
) : ArtifactManager {

@get:Input
Expand All @@ -26,52 +33,69 @@ internal class GithubReleaseArtifactManager(
@get:Input
lateinit var frameworkName: String

@get:Input
lateinit var artifactIdentifierName: String

@get:Input
var artifactReleaseId by Delegates.notNull<Int>()

// TODO: This value is stored in the config cache. It is encrypted, but this still feels insecure. Review alternatives.
// https://docs.gradle.org/current/userguide/configuration_cache.html#config_cache:secrets
lateinit var githubPublishToken: String

override fun configure(
project: Project,
version: String,
uploadTask: TaskProvider<Task>,
kmmPublishTask: TaskProvider<Task>
project: Project, version: String, uploadTask: TaskProvider<Task>, kmmPublishTask: TaskProvider<Task>
) {
this.releaseVersion = releaseString ?: project.version.toString()
this.repoName = this.repository ?: project.githubRepo
this.githubPublishToken = project.githubPublishToken
this.frameworkName = project.kmmBridgeExtension.frameworkName.get()
artifactReleaseId = project.githubArtifactReleaseId?.toInt() ?: 0
this.artifactIdentifierName = project.githubArtifactIdentifierName ?: ""
}

override fun deployArtifact(task: Task, zipFilePath: File, version: String): String {
val existingReleaseId = GithubCalls.findReleaseId(
githubPublishToken, repoName, releaseVersion
)
val uploadReleaseId = if (artifactReleaseId == 0) {
val existingReleaseId = GithubCalls.findReleaseId(
githubPublishToken, repoName, releaseVersion
)

task.logger.info("existingReleaseId: $existingReleaseId")
task.logger.info("existingReleaseId: $existingReleaseId")

if (existingReleaseId != null && !useExistingRelease) {
throw GradleException("Release for '$releaseVersion' exists. Set 'useExistingRelease = true' to update existing releases.")
}
if (existingReleaseId != null && !useExistingRelease) {
throw GradleException("Release for '$releaseVersion' exists. Set 'useExistingRelease = true' to update existing releases.")
}

val idReply: Int = existingReleaseId ?: GithubCalls.createRelease(
githubPublishToken, repoName, releaseVersion, null
)
val idReply: Int = existingReleaseId ?: GithubCalls.createRelease(
githubPublishToken, repoName, releaseVersion, null
)

task.logger.info("GitHub Release created with id: $idReply")
task.logger.info("GitHub Release created with id: $idReply")

idReply
} else {
artifactReleaseId
}

val fileName = artifactName(version, useExistingRelease)

val uploadUrl = GithubCalls.uploadZipFile(githubPublishToken, zipFilePath, repoName, idReply, fileName)
val uploadUrl = GithubCalls.uploadZipFile(githubPublishToken, zipFilePath, repoName, uploadReleaseId, fileName)
return "${uploadUrl}.zip"
}

private fun artifactName(versionString: String, useExistingRelease: Boolean): String {
return if (useExistingRelease) {
"$frameworkName-${versionString}-${(System.currentTimeMillis() / 1000)}.xcframework.zip"
} else {
"$frameworkName.xcframework.zip"
uploadZipFileName(versionString)
}
}

open fun uploadZipFileName(versionString: String) = if (artifactIdentifierName.isNotEmpty()) {
"$frameworkName-${artifactIdentifierName}.xcframework.zip"
} else {
"$frameworkName.xcframework.zip"
}
}

internal val Project.kmmBridgeExtension get() = extensions.getByType<KmmBridgeExtension>()
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ internal val Project.githubPublishToken
get() = (project.property("GITHUB_PUBLISH_TOKEN")
?: throw IllegalArgumentException("KMMBridge Github operations need property GITHUB_PUBLISH_TOKEN")) as String

internal val Project.githubArtifactReleaseId
get() = project.findStringProperty("GITHUB_ARTIFACT_RELEASE_ID")

internal val Project.githubArtifactIdentifierName
get() = project.findStringProperty("GITHUB_ARTIFACT_IDENTIFIER_NAME")

internal val Project.githubRepo: String
get() = githubRepoOrNull
?: throw IllegalArgumentException("KMMBridge Github operations need a repo param or property GITHUB_REPO")

0 comments on commit 7bd3322

Please sign in to comment.