This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add new util class ReleaseNotesGenerator * Set lazy github release body value with custom ReleaseBodyStrategy The new helper class `ReleaseBodyStrategy` implements `PublishBodyStrategy` from the `net.wooga.atlas-github` plugin. The strategy method `getBody` will be called with the `GHRepository` object for the current release. The method will call `ReleaseNotesGenerator`'s method `generateReleaseNotes`
- Loading branch information
Showing
6 changed files
with
492 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/groovy/wooga/gradle/release/utils/ReleaseBodyStrategy.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package wooga.gradle.release.utils | ||
|
||
import org.ajoberstar.gradle.git.release.base.ReleaseVersion | ||
import org.ajoberstar.grgit.Grgit | ||
import org.kohsuke.github.GHRepository | ||
import wooga.gradle.github.publish.PublishBodyStrategy | ||
|
||
class ReleaseBodyStrategy implements PublishBodyStrategy { | ||
|
||
private Grgit git | ||
private ReleaseVersion version | ||
|
||
ReleaseBodyStrategy(ReleaseVersion version, Grgit git) { | ||
this.git = git | ||
this.version = version | ||
} | ||
|
||
@Override | ||
String getBody(GHRepository repository) { | ||
return new ReleaseNotesGenerator(git, repository).generateReleaseNotes(version) | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
src/main/groovy/wooga/gradle/release/utils/ReleaseNotesGenerator.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright 2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package wooga.gradle.release.utils | ||
|
||
import org.ajoberstar.gradle.git.release.base.ReleaseVersion | ||
import org.ajoberstar.grgit.Commit | ||
import org.ajoberstar.grgit.Grgit | ||
import org.kohsuke.github.GHPullRequest | ||
import org.kohsuke.github.GHRepository | ||
|
||
/** | ||
* A generator class to create release notes from git log and pull request bodies. | ||
*/ | ||
class ReleaseNotesGenerator { | ||
|
||
public static final String INITAL_RELEASE_MSG = "* ![NEW] Initial Release\n" | ||
public static final String ICON_IDS = """ | ||
<!-- START icon Id's --> | ||
[NEW]:http://resources.atlas.wooga.com/icons/icon_new.svg "New" | ||
[ADD]:http://resources.atlas.wooga.com/icons/icon_add.svg "Add" | ||
[IMPROVE]:http://resources.atlas.wooga.com/icons/icon_improve.svg "IMPROVE" | ||
[CHANGE]:http://resources.atlas.wooga.com/icons/icon_change.svg "Change" | ||
[FIX]:http://resources.atlas.wooga.com/icons/icon_fix.svg "Fix" | ||
[UPDATE]:http://resources.atlas.wooga.com/icons/icon_update.svg "Update" | ||
[BREAK]:http://resources.atlas.wooga.com/icons/icon_break.svg "Remove" | ||
[REMOVE]:http://resources.atlas.wooga.com/icons/icon_remove.svg "Remove" | ||
[IOS]:http://resources.atlas.wooga.com/icons/icon_iOS.svg "iOS" | ||
[ANDROID]:http://resources.atlas.wooga.com/icons/icon_android.svg "Android" | ||
[WEBGL]:http://resources.atlas.wooga.com/icons/icon_webGL.svg "Web:GL" | ||
<!-- END icon Id's --> | ||
""".stripIndent() | ||
|
||
private Grgit git | ||
private GHRepository hub | ||
|
||
ReleaseNotesGenerator(Grgit git, GHRepository hub) { | ||
this.git = git | ||
this.hub = hub | ||
} | ||
|
||
/** | ||
* Generates a release note body message with given <code>version</code>. | ||
* The generator will parse the git log from <code>HEAD</code> to previous version | ||
* and reads change lists from referenced pull requests. If no pull requests commits | ||
* can be found, it will list the git log. | ||
* @param version The <code>ReleaseVersion</code> to create the release note for | ||
* @return a <code>String</code> containing the generated release notes | ||
*/ | ||
String generateReleaseNotes(ReleaseVersion version) { | ||
StringBuilder builder = new StringBuilder() | ||
List<String> includes = ['HEAD'] | ||
List<String> excludes = createExcludes(version) | ||
|
||
if (!version.previousVersion) { | ||
builder << INITAL_RELEASE_MSG | ||
} | ||
|
||
List<Commit> log = git.log(includes: includes, excludes: excludes) | ||
List<GHPullRequest> pullRequests = fetchPullRequestsFromLog(log) | ||
|
||
List<String> changeList = [] | ||
pullRequests.inject(changeList) { ch, pr -> | ||
def changes = pr.body.readLines().findAll { it.trim().startsWith("* ![") } | ||
changes = changes.collect { it + " [#${pr.number}]" } | ||
ch << changes.join("\n") | ||
} | ||
|
||
if (changeList.size() == 0) { | ||
changeList << log.collect({ "* ${it.shortMessage}" }).join("\n") | ||
} | ||
|
||
changeList.removeAll([""]) | ||
|
||
if (changeList.size() > 0) { | ||
builder << changeList.join("\n") | ||
builder << "\n" | ||
builder << ICON_IDS | ||
} | ||
|
||
builder.toString().trim() | ||
} | ||
|
||
protected List<GHPullRequest> fetchPullRequestsFromLog(List<Commit> log) { | ||
String pattern = /#(\d+)/ | ||
def prCommits = log.findAll { it.shortMessage =~ pattern } | ||
def prNumbers = prCommits.collect { | ||
def m = (it.shortMessage =~ pattern) | ||
m[0][1].toInteger() | ||
} | ||
def prs = prNumbers.collect { hub.getPullRequest(it) } | ||
prs.removeAll([null]) | ||
prs | ||
} | ||
|
||
private List<String> createExcludes(ReleaseVersion version) { | ||
List<String> excludes = [] | ||
if (version.previousVersion) { | ||
String previousVersion = "v${version.previousVersion}^{commit}" | ||
if (tagExists(previousVersion)) { | ||
excludes << previousVersion | ||
} | ||
} | ||
excludes | ||
} | ||
|
||
private boolean tagExists(String revStr) { | ||
try { | ||
git.resolve.toCommit(revStr) | ||
return true | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/test/groovy/wooga/gradle/release/utils/ReleaseBodyStrategySpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package wooga.gradle.release.utils | ||
|
||
import org.ajoberstar.gradle.git.release.base.ReleaseVersion | ||
import org.ajoberstar.grgit.Grgit | ||
import org.kohsuke.github.GHPullRequest | ||
import org.kohsuke.github.GHRepository | ||
import spock.lang.Specification | ||
|
||
class ReleaseBodyStrategySpec extends Specification { | ||
|
||
ReleaseBodyStrategy releaseBodyStrategy | ||
Grgit git | ||
GHRepository repository | ||
ReleaseVersion version | ||
|
||
def mockPullRequest(int number, Boolean changeSet = true) { | ||
def bodyOut = new StringBuilder() | ||
|
||
bodyOut << """ | ||
## Description | ||
Yada Yada Yada Yada Yada | ||
Yada Yada Yada Yada Yada | ||
Yada Yada Yada Yada Yada | ||
""".stripIndent() | ||
|
||
if(changeSet) { | ||
bodyOut << """ | ||
## Changes | ||
* ![ADD] some stuff | ||
* ![REMOVE] some stuff | ||
* ![FIX] some stuff | ||
Yada Yada Yada Yada Yada | ||
Yada Yada Yada Yada Yada | ||
Yada Yada Yada Yada Yada | ||
""".stripIndent() | ||
} | ||
|
||
def pr = Mock(GHPullRequest) | ||
pr.body >> bodyOut.toString() | ||
pr.number >> number | ||
return pr | ||
} | ||
|
||
def setup() { | ||
git = Grgit.init(dir: File.createTempDir()) | ||
git.commit(message: 'initial commit') | ||
|
||
repository = Mock(GHRepository) | ||
version = new ReleaseVersion("1.1.0", "1.0.0", false) | ||
|
||
releaseBodyStrategy = new ReleaseBodyStrategy(version, git) | ||
} | ||
|
||
def "verify calls ReleaseNotesGenerator to generate release notes body"() { | ||
given: "a git log with pull requests commits and tags" | ||
|
||
git.commit(message: 'commit') | ||
git.commit(message: 'commit (#1)') | ||
git.tag.add(name: 'v1.0.0') | ||
git.commit(message: 'commit') | ||
git.commit(message: 'commit (#2)') | ||
git.commit(message: 'commit (#3)') | ||
git.commit(message: 'commit') | ||
|
||
and: "mocked pull requests" | ||
repository.getPullRequest(1) >> mockPullRequest(1) | ||
repository.getPullRequest(2) >> mockPullRequest(2) | ||
repository.getPullRequest(3) >> mockPullRequest(3) | ||
|
||
when: | ||
def body = releaseBodyStrategy.getBody(repository) | ||
|
||
then: | ||
body == (""" | ||
* ![ADD] some stuff [#3] | ||
* ![REMOVE] some stuff [#3] | ||
* ![FIX] some stuff [#3] | ||
* ![ADD] some stuff [#2] | ||
* ![REMOVE] some stuff [#2] | ||
* ![FIX] some stuff [#2] | ||
""".stripIndent().stripMargin() + ReleaseNotesGenerator.ICON_IDS).trim() | ||
} | ||
} |
Oops, something went wrong.