Skip to content

Commit

Permalink
feat(extension): replace builder with lambda property
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Kesselring authored and Marcel Kesselring committed Aug 15, 2024
1 parent 07e8ea5 commit 8e4b2ad
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class OctopusDeployPlugin : Plugin<Project> {
)
extension.commitLinkBaseUrl.convention("https://git.example.com/repo/commits/")
extension.generateChangelogSinceLastTag.convention(false)
extension.gitlabCi.convention(false)
extension.buildInformationAddition.convention({ /* no-op */ })
extension.useShortCommitHashes.convention(true)

val getFirstCommitHashTask = project.tasks.create("firstCommitHash", FirstCommitHashTask::class.java).apply {
Expand Down Expand Up @@ -58,8 +58,7 @@ class OctopusDeployPlugin : Plugin<Project> {
parseCommitsForJiraIssues.set(extension.parseCommitsForJiraIssues)
jiraBaseBrowseUrl.set(extension.jiraBaseBrowseUrl)
version.set(extension.version)
buildInformationAddition.set(extension.buildInformationAdditionData)
gitlabCi.set(extension.gitlabCi)
buildInformationAddition.set(extension.buildInformationAddition)
outputFile.set(extension.outputDir.file("build-information.json"))
commits.set(commitsSinceLastTagTask.commits)
dependsOn(commitsSinceLastTagTask)
Expand All @@ -75,8 +74,7 @@ class OctopusDeployPlugin : Plugin<Project> {
parseCommitsForJiraIssues.set(extension.parseCommitsForJiraIssues)
jiraBaseBrowseUrl.set(extension.jiraBaseBrowseUrl)
version.set(extension.version)
buildInformationAddition.set(extension.buildInformationAdditionData)
gitlabCi.set(extension.gitlabCi)
buildInformationAddition.set(extension.buildInformationAddition)
outputFile.set(extension.outputDir.file("build-information.md"))
dependsOn(commitsSinceLastTagTask)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,155 +3,25 @@ package com.liftric.octopusdeploy.extensions
import com.liftric.octopusdeploy.api.CommitCli
import com.liftric.octopusdeploy.api.LinksCli
import com.liftric.octopusdeploy.api.WorkItem
import org.gradle.api.Project
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional

data class BuildInformationCli(
var Id: String?,
var PackageId: String?,
var Version: String?,
var BuildEnvironment: String?,
var BuildNumber: String?,
var BuildUrl: String?,
var Branch: String?,
var VcsType: String?,
var VcsRoot: String?,
var VcsCommitNumber: String?,
var VcsCommitUrl: String?,
var IssueTrackerName: String?,
var WorkItems: List<WorkItem>,
var Commits: List<CommitCli>,
var IncompleteDataWarning: String?,
var Created: String?,
var LastModifiedOn: String?,
var LastModifiedBy: String?,
var Links: LinksCli?,
var Id: String? = null,
var PackageId: String? = null,
var Version: String? = null,
var BuildEnvironment: String? = null,
var BuildNumber: String? = null,
var BuildUrl: String? = null,
var Branch: String? = null,
var VcsType: String? = null,
var VcsRoot: String? = null,
var VcsCommitNumber: String? = null,
var VcsCommitUrl: String? = null,
var IssueTrackerName: String? = null,
var WorkItems: List<WorkItem> = emptyList(),
var Commits: List<CommitCli> = emptyList(),
var IncompleteDataWarning: String? = null,
var Created: String? = null,
var LastModifiedOn: String? = null,
var LastModifiedBy: String? = null,
var Links: LinksCli? = null,
)

@Suppress("MemberVisibilityCanBePrivate")
class BuildInformationAdditionBuilder(@get:Internal val proj: Project) {
@get:Input
@get:Optional
val Id: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val PackageId: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val Version: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val BuildEnvironment: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val BuildNumber: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val BuildUrl: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val Branch: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val VcsType: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val VcsRoot: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val VcsCommitNumber: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val VcsCommitUrl: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val IssueTrackerName: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val WorkItems: ListProperty<WorkItem> = proj.objects.listProperty(WorkItem::class.java)

@get:Input
@get:Optional
var Commits: ListProperty<CommitCli> = proj.objects.listProperty(CommitCli::class.java)

@get:Input
@get:Optional
val IncompleteDataWarning: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val Created: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val LastModifiedOn: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val LastModifiedBy: Property<String?> = proj.objects.property(String::class.java)

@get:Input
@get:Optional
val Links: Property<LinksCli?> = proj.objects.property(LinksCli::class.java)


fun build(): BuildInformationCli = BuildInformationCli(
Id = this.Id.orNull,
PackageId = this.PackageId.orNull,
Version = this.Version.orNull,
BuildEnvironment = this.BuildEnvironment.orNull,
BuildNumber = this.BuildNumber.orNull,
BuildUrl = this.BuildUrl.orNull,
Branch = this.Branch.orNull,
VcsType = this.VcsType.orNull,
VcsRoot = this.VcsRoot.orNull,
VcsCommitNumber = this.VcsCommitNumber.orNull,
VcsCommitUrl = this.VcsCommitUrl.orNull,
IssueTrackerName = this.IssueTrackerName.orNull,
WorkItems = this.WorkItems.getOrElse(listOf()),
Commits = this.Commits.getOrElse(listOf()),
IncompleteDataWarning = this.IncompleteDataWarning.orNull,
Created = this.Created.orNull,
LastModifiedOn = this.LastModifiedOn.orNull,
LastModifiedBy = this.LastModifiedBy.orNull,
Links = this.Links.orNull,
)

fun buildForGitlabCi(): BuildInformationCli = BuildInformationCli(
Id = this.Id.orNull,
PackageId = this.PackageId.orNull,
Version = this.Version.orNull,
BuildEnvironment = "GitLabCI",
BuildNumber = System.getenv("CI_PIPELINE_IID"),
BuildUrl = System.getenv("CI_PIPELINE_URL"),
Branch = System.getenv("CI_COMMIT_REF_NAME"),
VcsType = "Git",
VcsRoot = System.getenv("CI_PROJECT_URL"),
VcsCommitNumber = System.getenv("CI_COMMIT_SHORT_SHA"),
VcsCommitUrl = "${System.getenv("CI_PROJECT_URL")?.removeSuffix("/")}/commit/${System.getenv("CI_COMMIT_SHA")}",
IssueTrackerName = this.IssueTrackerName.orNull,
WorkItems = this.WorkItems.getOrElse(listOf()),
Commits = this.Commits.getOrElse(listOf()),
IncompleteDataWarning = this.IncompleteDataWarning.orNull,
Created = this.Created.orNull,
LastModifiedOn = this.LastModifiedOn.orNull,
LastModifiedBy = System.getenv("GITLAB_USER_NAME"),
Links = this.Links.orNull,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ abstract class OctopusDeployExtension(val project: Project) {
/**
* Customize the final octopus build-information before uploading
*/
abstract val buildInformationAdditionData: Property<BuildInformationAdditionBuilder?>
abstract val buildInformationAddition: Property<BuildInformationCli.() -> Unit>

/**
* Configures the http logging of the underlying okhttp client used for octopus api requests
Expand All @@ -93,12 +93,24 @@ abstract class OctopusDeployExtension(val project: Project) {
*/
abstract val useShortCommitHashes: Property<Boolean>

/**
* Default `buildInformationAddition` implementation adding context from the CI environment for Gitlab CI.
*/
abstract val gitlabCi: Property<Boolean>
fun gitlab(additional: BuildInformationCli.() -> Unit = {}) {
buildInformationAddition.set({
BuildEnvironment = if (System.getenv("CI") != null) {
"GitLabCI"
} else {
"gradle"
}
BuildNumber = System.getenv("CI_PIPELINE_IID")
BuildUrl = System.getenv("CI_PIPELINE_URL")
Branch = System.getenv("CI_COMMIT_REF_NAME")
VcsType = "Git"
VcsRoot = System.getenv("CI_PROJECT_URL")
VcsCommitNumber = System.getenv("CI_COMMIT_SHORT_SHA")
VcsCommitUrl =
"${System.getenv("CI_PROJECT_URL")
?.removeSuffix("/")}/commit/${System.getenv("CI_COMMIT_SHA")}"
LastModifiedBy = System.getenv("GITLAB_USER_NAME")
additional()
})
}
}

fun OctopusDeployExtension.buildInformationAddition(action: BuildInformationAdditionBuilder.() -> Unit) {
buildInformationAdditionData.set(BuildInformationAdditionBuilder(project).apply(action))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.liftric.octopusdeploy.task

import com.liftric.octopusdeploy.api.CommitCli
import com.liftric.octopusdeploy.api.WorkItem
import com.liftric.octopusdeploy.extensions.BuildInformationAdditionBuilder
import com.liftric.octopusdeploy.extensions.BuildInformationCli
import com.liftric.octopusdeploy.parseCommitsForJira
import org.gradle.api.DefaultTask
Expand All @@ -28,7 +27,7 @@ abstract class CreateBuildInformationMarkdownTask : DefaultTask() {

@get:Nested
@get:Optional
abstract val buildInformationAddition: Property<BuildInformationAdditionBuilder>
abstract val buildInformationAddition: Property<BuildInformationCli.() -> Unit>

@get:Optional
@get:Input
Expand All @@ -42,10 +41,6 @@ abstract class CreateBuildInformationMarkdownTask : DefaultTask() {
@get:Optional
abstract val jiraBaseBrowseUrl: Property<String>

@get:Input
@get:Optional
abstract val gitlabCi: Property<Boolean>

@TaskAction
fun execute() {
val commits = commits.getOrElse(listOf())
Expand All @@ -54,14 +49,7 @@ abstract class CreateBuildInformationMarkdownTask : DefaultTask() {
} else {
null
}
val gitlabCi = gitlabCi.getOrElse(false)
val buildInformationCli: BuildInformationCli = if (gitlabCi) {
buildInformationAddition.orNull?.buildForGitlabCi()
?: BuildInformationAdditionBuilder(project).buildForGitlabCi()
} else {
buildInformationAddition.orNull?.build() ?: BuildInformationAdditionBuilder(project).build()
}

val buildInformationCli = BuildInformationCli().apply(buildInformationAddition.get())
outputFile.get().asFile.apply {
writeText("")
appendText("# ${packageName.get()}: ${version.get()}\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.fasterxml.jackson.databind.PropertyNamingStrategies
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.liftric.octopusdeploy.api.CommitCli
import com.liftric.octopusdeploy.api.WorkItem
import com.liftric.octopusdeploy.extensions.BuildInformationAdditionBuilder
import com.liftric.octopusdeploy.extensions.BuildInformationCli
import com.liftric.octopusdeploy.parseCommitsForJira
import org.gradle.api.DefaultTask
Expand All @@ -32,7 +31,7 @@ abstract class CreateBuildInformationTask : DefaultTask() {

@get:Nested
@get:Optional
abstract val buildInformationAddition: Property<BuildInformationAdditionBuilder>
abstract val buildInformationAddition: Property<BuildInformationCli.() -> Unit>

@get:Optional
@get:Input
Expand All @@ -46,20 +45,11 @@ abstract class CreateBuildInformationTask : DefaultTask() {
@get:Optional
abstract val jiraBaseBrowseUrl: Property<String>

@get:Input
@get:Optional
abstract val gitlabCi: Property<Boolean>

@TaskAction
fun execute() {
val commits = commits.getOrElse(listOf())
val gitlabCi = gitlabCi.getOrElse(false)
val buildInformationCli: BuildInformationCli = if (gitlabCi) {
buildInformationAddition.orNull?.buildForGitlabCi()
?: BuildInformationAdditionBuilder(project).buildForGitlabCi()
} else {
buildInformationAddition.orNull?.build() ?: BuildInformationAdditionBuilder(project).build()
}

val buildInformationCli = BuildInformationCli().apply(buildInformationAddition.get())

val workItems: List<WorkItem>? = if (parseCommitsForJiraIssues.getOrElse(false)) {
parseCommitsForJira(commits, jiraBaseBrowseUrl.getOrElse(""))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package com.liftric.octopusdeploy.task

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.liftric.octopusdeploy.extensions.BuildInformationCli
import com.liftric.octopusdeploy.api.CommitCli
import com.liftric.octopusdeploy.api.WorkItem
import com.liftric.octopusdeploy.extensions.BuildInformationAdditionBuilder
import com.liftric.octopusdeploy.extensions.BuildInformationCli
import junit.framework.TestCase.*
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
Expand All @@ -32,9 +31,7 @@ class CreateBuildInformationBuilderTaskTest {
packageName.set("test-package")
commits.set(createTestCommits())
version.set("2.1.4")
buildInformationAddition.set(BuildInformationAdditionBuilder(project).apply{
Id.set("foo")
})
buildInformationAddition.set({Id = "foo"})
outputFile.set(outputFileBuildInformation)
issueTrackerName.set("Jira")
parseCommitsForJiraIssues.set(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CreateBuildInformationGitlabCiTaskTest {
val taskResult = jacksonObjectMapper().readValue<BuildInformationCli>(readText())
println(taskResult)
assertEquals("foo", taskResult.Id)
assertEquals("GitLabCI", taskResult.BuildEnvironment)
assertEquals("gradle", taskResult.BuildEnvironment)
assertEquals(envVars["CI_PIPELINE_IID"], taskResult.BuildNumber)
assertEquals(envVars["CI_PROJECT_URL"], taskResult.VcsRoot)
assertEquals(envVars["CI_PIPELINE_URL"], taskResult.BuildUrl)
Expand All @@ -73,13 +73,12 @@ octopus {
gitRoot.set(file("${File("").absolutePath}"))
packageName.set("whatever")
serverUrl.set("whatever")
gitlabCi.set(true)
buildInformationAddition {
Id.set("foo")
gitlab {
Id = "foo"
}
}
"""
)
}
}
}
}

0 comments on commit 8e4b2ad

Please sign in to comment.