Skip to content

Commit

Permalink
feature: Add support for updating existing GitHub releases with only … (
Browse files Browse the repository at this point in the history
#48)

* feature: Add support for updating existing GitHub releases with only the change log.

This is needed for the tool-ocpp-emulator where we create a release by uploading binary installers and we need to only update the release notes in the `changelog-cli` command - see failing build [Bump version to 1.1.2](https://github.com/monta-app/tool-ocpp-emulator/actions/runs/3886331389)

* chore: Bump version
  • Loading branch information
morten-andersen authored Jan 11, 2023
1 parent b1ff5e3 commit 8df4925
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "com.monta.gradle.changelog"
version = "1.2.0"
version = "1.3.0"

repositories {
// Use Maven Central for resolving dependencies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class GenerateChangeLogCommand : CliktCommand() {
envvar = "CHANGELOG_GITHUB_RELEASE"
).flag("--github-release", "-R", default = false)

private val update: Boolean by option(
help = "Must be set if the github release already exists and should be update with change log instead of created",
envvar = "CHANGELOG_GITHUB_UPDATE"
).flag("--github-update", default = false)

private val githubToken: String? by option(
help = "Github Token used for creating releases",
envvar = "CHANGELOG_GITHUB_TOKEN"
Expand Down Expand Up @@ -73,6 +78,7 @@ class GenerateChangeLogCommand : CliktCommand() {
serviceName = serviceName,
jiraAppName = jiraAppName,
githubRelease = githubRelease,
update = update,
githubToken = githubToken
)

Expand Down
62 changes: 62 additions & 0 deletions src/commonMain/kotlin/com.monta.changelog/github/GitHubService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.monta.changelog.util.resolve
import io.ktor.client.call.body
import io.ktor.client.request.accept
import io.ktor.client.request.header
import io.ktor.client.request.get
import io.ktor.client.request.patch
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.statement.bodyAsText
Expand Down Expand Up @@ -62,6 +64,61 @@ class GitHubService(
}
}

suspend fun updateRelease(
linkResolvers: List<LinkResolver>,
changeLog: ChangeLog,
): String {
val releaseId = getReleaseId(changeLog)

val url = "https://api.github.com/repos/${changeLog.repoOwner}/${changeLog.repoName}/releases/$releaseId"

val response = client.patch(url) {
header("Authorization", "token $githubToken")
contentType(ContentType.Application.Json)
accept(ContentType.parse("application/vnd.github.v3+json"))
setBody(
UpdateReleaseRequest(
body = buildBody(
markdownFormatter = MarkdownFormatter.GitHub,
linkResolvers = linkResolvers,
groupedCommitMap = changeLog.groupedCommitMap
),
)
)
}

if (response.status.value in 200..299) {
println("successfully updated release ${response.bodyAsText()}")
val responseBody = response.body<ReleaseResponse>()
return responseBody.html_url
} else {
DebugLogger.error("failed to update release ${response.bodyAsText()}")
DebugLogger.error("returning with code 1")
exit(1)
return ""
}
}

private suspend fun getReleaseId(changeLog: ChangeLog): Int? {
val url = "https://api.github.com/repos/${changeLog.repoOwner}/${changeLog.repoName}/releases/tags/${changeLog.tagName}"

val response = client.get(url) {
header("Authorization", "token $githubToken")
accept(ContentType.parse("application/vnd.github.v3+json"))
}

if (response.status.value == 200) {
println("found release ${response.bodyAsText()}")
val responseBody = response.body<ReleaseResponse>()
return responseBody.id
} else {
DebugLogger.error("could not find release ${response.bodyAsText()}")
DebugLogger.error("returning with code 1")
exit(1)
return null
}
}

private fun buildBody(
markdownFormatter: MarkdownFormatter,
linkResolvers: List<LinkResolver>,
Expand Down Expand Up @@ -117,6 +174,11 @@ class GitHubService(
val tag_name: String,
)

@Serializable
data class UpdateReleaseRequest(
val body: String,
)

@Serializable
data class ErrorResponse(
@SerialName("documentation_url")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ChangeLogService(
private val serviceName: String,
private val jiraAppName: String?,
private val githubRelease: Boolean,
private val update: Boolean,
githubToken: String?,
) {

Expand Down Expand Up @@ -74,7 +75,11 @@ class ChangeLogService(
)

if (githubRelease) {
changeLog.githubReleaseUrl = gitHubService.createRelease(linkResolvers, changeLog)
changeLog.githubReleaseUrl = if (update) {
gitHubService.updateRelease(linkResolvers, changeLog)
} else {
gitHubService.createRelease(linkResolvers, changeLog)
}
}

changeLogPrinter.print(linkResolvers, changeLog)
Expand Down

0 comments on commit 8df4925

Please sign in to comment.