Skip to content

Commit

Permalink
refs #2: add ability to render header and footers for milestone relea…
Browse files Browse the repository at this point in the history
…se notes
  • Loading branch information
JaidenAshmore committed Aug 23, 2020
1 parent fb044ad commit 999f00e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ gitHubRelease {
gitHubUser = "$gitHubUser"
repositoryName = "$repoName"
milestoneVersion = "$milestoneVersion"
headerRenderer = { milestone -> "Appears above the issue groups: ${milestone.description}" }
groupings = {
group {
heading = "## Enhancements"
Expand All @@ -72,6 +73,7 @@ gitHubRelease {
renderer = { issue, _ -> "- [GH-${issue.number}]: ${issue.title}" }
}
}
footerRenderer = { milestone -> "Footer!" }
}
```

Expand Down
22 changes: 22 additions & 0 deletions src/main/kotlin/com/jashmore/gradle/github/GithubReleasePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
* <p>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.
*
* <p>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.
*
Expand Down Expand Up @@ -58,7 +78,9 @@ class GithubReleasePlugin : Plugin<Project> {
extension.milestoneVersion?.apply {
milestoneVersion = this
}
headerRenderer = extension.headerRenderer
groupings = extension.groupings
footerRenderer = extension.footerRenderer
extension.outputFile?.apply {
outputFile = this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,7 +43,9 @@ class GithubReleaseNotesService(private val repository: Repository,
* @return the rendered markdown for the release notes
*/
fun createReleaseNotes(milestoneVersion: String,
issueGroups: List<IssueGrouping>): String {
headerRenderer: (milestone: Milestone) -> String?,
issueGroups: List<IssueGrouping>,
footerRenderer: (milestone: Milestone) -> String?): String {
val milestone = milestoneService.getMilestones(repository, "all")
.firstOrNull { it.title == milestoneVersion }
?: throw IllegalArgumentException("No milestone found with name: $milestoneVersion")
Expand All @@ -54,7 +57,17 @@ class GithubReleaseNotesService(private val repository: Repository,

val issuesNotRendered: MutableSet<Issue> = 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) }

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
*
* <p>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.
*
* <p>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.
*/
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""${'"'}
Expand Down Expand Up @@ -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())
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -325,6 +331,8 @@ class GithubReleaseNotesTaskTest {
Stomping out the bugs
- [GH-$thirdIssueId]: Fix this bug
Footer: First milestone
""".trimIndent())
}

Expand Down

0 comments on commit 999f00e

Please sign in to comment.