From 999f00e7711f6597c22aca2f600a0e1419b35926 Mon Sep 17 00:00:00 2001 From: Jaiden Ashmore Date: Sun, 23 Aug 2020 19:53:14 +1000 Subject: [PATCH] refs #2: add ability to render header and footers for milestone release notes --- README.md | 2 ++ .../gradle/github/GithubReleasePlugin.kt | 22 ++++++++++++++ .../github/notes/GithubReleaseNotesService.kt | 29 +++++++++++++++++-- .../github/notes/GithubReleaseNotesTask.kt | 21 +++++++++++++- .../GithubReleasePluginIntegrationTest.kt | 8 +++++ 5 files changed, 78 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e58b590..f9a82d1 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ gitHubRelease { gitHubUser = "$gitHubUser" repositoryName = "$repoName" milestoneVersion = "$milestoneVersion" + headerRenderer = { milestone -> "Appears above the issue groups: ${milestone.description}" } groupings = { group { heading = "## Enhancements" @@ -72,6 +73,7 @@ gitHubRelease { renderer = { issue, _ -> "- [GH-${issue.number}]: ${issue.title}" } } } + footerRenderer = { milestone -> "Footer!" } } ``` diff --git a/src/main/kotlin/com/jashmore/gradle/github/GithubReleasePlugin.kt b/src/main/kotlin/com/jashmore/gradle/github/GithubReleasePlugin.kt index 1af9a1b..861ef48 100644 --- a/src/main/kotlin/com/jashmore/gradle/github/GithubReleasePlugin.kt +++ b/src/main/kotlin/com/jashmore/gradle/github/GithubReleasePlugin.kt @@ -2,10 +2,12 @@ package com.jashmore.gradle.github import com.jashmore.gradle.github.notes.GithubReleaseNotesTask import com.jashmore.gradle.github.notes.GroupingsDsl +import org.eclipse.egit.github.core.Milestone import org.eclipse.egit.github.core.client.GitHubClient import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.plugins.ExtensionAware +import org.gradle.api.tasks.Input import org.gradle.kotlin.dsl.create import org.gradle.kotlin.dsl.register import java.io.File @@ -31,6 +33,24 @@ open class GithubReleaseExtension { */ var gitHubClient: GitHubClient? = null + /** + * Optional function for generating the header content of the release notes based on the milestone that is being + * released. + * + *

If this function returns null, no header will be included in the release notes. + */ + @Input + var headerRenderer: (milestone: Milestone) -> String? = { null } + + /** + * Optional function for generating the footer content of the release notes based on the milestone that is being + * released. + * + *

If this function returns null, no footer will be included in the release notes. + */ + @Input + var footerRenderer: (milestone: Milestone) -> String? = { null } + /** * Defines the groups of issues and how to render them. * @@ -58,7 +78,9 @@ class GithubReleasePlugin : Plugin { extension.milestoneVersion?.apply { milestoneVersion = this } + headerRenderer = extension.headerRenderer groupings = extension.groupings + footerRenderer = extension.footerRenderer extension.outputFile?.apply { outputFile = this } diff --git a/src/main/kotlin/com/jashmore/gradle/github/notes/GithubReleaseNotesService.kt b/src/main/kotlin/com/jashmore/gradle/github/notes/GithubReleaseNotesService.kt index cffa889..d6fc05a 100644 --- a/src/main/kotlin/com/jashmore/gradle/github/notes/GithubReleaseNotesService.kt +++ b/src/main/kotlin/com/jashmore/gradle/github/notes/GithubReleaseNotesService.kt @@ -2,6 +2,7 @@ package com.jashmore.gradle.github.notes import org.eclipse.egit.github.core.Comment import org.eclipse.egit.github.core.Issue +import org.eclipse.egit.github.core.Milestone import org.eclipse.egit.github.core.Repository import org.eclipse.egit.github.core.service.IssueService import org.eclipse.egit.github.core.service.MilestoneService @@ -42,7 +43,9 @@ class GithubReleaseNotesService(private val repository: Repository, * @return the rendered markdown for the release notes */ fun createReleaseNotes(milestoneVersion: String, - issueGroups: List): String { + headerRenderer: (milestone: Milestone) -> String?, + issueGroups: List, + footerRenderer: (milestone: Milestone) -> String?): String { val milestone = milestoneService.getMilestones(repository, "all") .firstOrNull { it.title == milestoneVersion } ?: throw IllegalArgumentException("No milestone found with name: $milestoneVersion") @@ -54,7 +57,17 @@ class GithubReleaseNotesService(private val repository: Repository, val issuesNotRendered: MutableSet = issues.toMutableSet() - return issueGroups + val releaseNotesBuilder = StringBuilder() + + headerRenderer(milestone)?.apply { + releaseNotesBuilder.append(""" + |$this + | + | + """.trimMargin()) + } + + releaseNotesBuilder.append(issueGroups .mapNotNull { grouping -> val matchingIssues = issuesNotRendered.filter { grouping.filter(it) } @@ -72,7 +85,17 @@ class GithubReleaseNotesService(private val repository: Repository, """.trimMargin() } } - .joinToString("\n") + .joinToString("\n")) + + footerRenderer(milestone)?.apply { + releaseNotesBuilder.append(""" + | + | + |$this + """.trimMargin()) + } + + return releaseNotesBuilder.toString() } private fun renderIssueInGroup(issueGrouping: IssueGrouping, issue: Issue): String { diff --git a/src/main/kotlin/com/jashmore/gradle/github/notes/GithubReleaseNotesTask.kt b/src/main/kotlin/com/jashmore/gradle/github/notes/GithubReleaseNotesTask.kt index 16ed401..c93b707 100644 --- a/src/main/kotlin/com/jashmore/gradle/github/notes/GithubReleaseNotesTask.kt +++ b/src/main/kotlin/com/jashmore/gradle/github/notes/GithubReleaseNotesTask.kt @@ -1,6 +1,7 @@ package com.jashmore.gradle.github.notes import org.eclipse.egit.github.core.Issue +import org.eclipse.egit.github.core.Milestone import org.eclipse.egit.github.core.client.GitHubClient import org.eclipse.egit.github.core.service.IssueService import org.eclipse.egit.github.core.service.MilestoneService @@ -80,6 +81,24 @@ open class GithubReleaseNotesTask : DefaultTask() { @Input var milestoneVersion: String = NO_MILESTONE + /** + * Optional function for generating the header content of the release notes based on the milestone that is being + * released. + * + *

If this function returns null, no header will be included in the release notes. + */ + @Input + var headerRenderer: (milestone: Milestone) -> String? = { null } + + /** + * Optional function for generating the footer content of the release notes based on the milestone that is being + * released. + * + *

If this function returns null, no footer will be included in the release notes. + */ + @Input + var footerRenderer: (milestone: Milestone) -> String? = { null } + /** * The client that should be used to communicate with the GitHub API. */ @@ -118,7 +137,7 @@ open class GithubReleaseNotesTask : DefaultTask() { groupingsDsl.groupings() val releaseNotes = GithubReleaseNotesService(repository, milestoneService, issueService) - .createReleaseNotes(milestoneVersion, groupingsDsl.groups) + .createReleaseNotes(milestoneVersion, headerRenderer, groupingsDsl.groups, footerRenderer) logger.debug("Printing release notes to ${outputFile.toPath()}") outputFile.writeText(releaseNotes); diff --git a/src/test/kotlin/it/com/jashmore/gradle/github/GithubReleasePluginIntegrationTest.kt b/src/test/kotlin/it/com/jashmore/gradle/github/GithubReleasePluginIntegrationTest.kt index e2af5e3..bd97fa7 100644 --- a/src/test/kotlin/it/com/jashmore/gradle/github/GithubReleasePluginIntegrationTest.kt +++ b/src/test/kotlin/it/com/jashmore/gradle/github/GithubReleasePluginIntegrationTest.kt @@ -194,6 +194,7 @@ class GithubReleaseNotesTaskTest { repositoryName = "$repoName" gitHubClient = GitHubClient("localhost", ${wireMockServer.port()}, "http") outputFile = File(project.buildDir.resolve("myfile.md").toString()) + headerRenderer = { milestone -> "Milestone release: ${"$"}{milestone.description}" } groupings = { group { heading = ""${'"'} @@ -223,6 +224,7 @@ class GithubReleaseNotesTaskTest { renderer = { issue, _ -> "- [GH-${"$"}{issue.number}]: ${"$"}{issue.title}" } } } + footerRenderer = { milestone -> "Footer: ${"$"}{milestone.description}" } milestoneVersion = project.properties["milestone"] as String } """.trimIndent()) @@ -233,10 +235,12 @@ class GithubReleaseNotesTaskTest { Milestone().apply { number = 20 title = "1.0.0" + description = "First milestone" }, Milestone().apply { number = 21 title = "2.0.0" + description = "Second milestone" } ) mockIssues( @@ -310,6 +314,8 @@ class GithubReleaseNotesTaskTest { // assert assertThat(tempDir.resolve("build").resolve("myfile.md").readText()).isEqualTo(""" + Milestone release: First milestone + ## Enhancements New features that have been added @@ -325,6 +331,8 @@ class GithubReleaseNotesTaskTest { Stomping out the bugs - [GH-$thirdIssueId]: Fix this bug + + Footer: First milestone """.trimIndent()) }