Created
July 14, 2017 20:15
-
-
Save noel-yap/2baa4e974e13cc3a1e3f95ca10bed3ca to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // `-Prelease.auto-scope=true` for parameterless build jobs that publish (eg build PR jobs) | |
| if (project.hasProperty('release.auto-scope') && project.properties['release.auto-scope'] == 'true') { | |
| project.ext['release.scope'] = releaseScope(file('CHANGELOG.md') as String[]) | |
| } | |
| def releaseScope(String[] changelog) { | |
| final unreleased = (changelog | |
| .dropWhile { | |
| !(it =~ /^#+ +\[Unreleased\]/) | |
| }.takeWhile { | |
| !(it =~ /^#+ +\[\d+\.\d+\.\d+.*\]/) | |
| }) | |
| if (unreleased.count { it =~ /^#+ +Changed|Removed/ } > 0) { | |
| 'major' | |
| } else if (unreleased.count { it =~ /^#+ +Added/ } > 0) { | |
| 'minor' | |
| } else if (unreleased.count { it =~ /^#+ +Deprecated|Fixed|Security/ } > 0) { | |
| 'patch' | |
| } else { | |
| '' | |
| } | |
| } | |
| def unreleasedChangesExist(String[] changelog) { | |
| !releaseScope(changelog).isEmpty() | |
| } | |
| task checkReleaseScope() { | |
| doLast { | |
| final releaseScopeFromProperties = project.properties['release.scope'] | |
| if (releaseScopeFromProperties == null) { | |
| throw new Exception("Must specify `-Prelease.scope`.") | |
| } | |
| final releaseScopeFromChangelog = releaseScope(file('CHANGELOG.md') as String[]) | |
| if (releaseScopeFromProperties != releaseScopeFromChangelog && (!project.hasProperty('release.force') || project.properties['release.force'] != 'true')) { | |
| throw new Exception("The release scope derived from `CHANGELOG.md`, '${releaseScopeFromChangelog}', doesn't match `-Prelease.scope=${releaseScopeFromProperties}. Fix `CHANGELOG.md` or use `-Prelease.force=true`.") | |
| } | |
| } | |
| } | |
| task updateChangelog() { | |
| final outputFile = file("${rootDir}/CHANGELOG.md") | |
| doLast { | |
| final changelog = outputFile as String[] | |
| final stashUrl = 'https://stash.corp.netflix.com' | |
| final compareCommitsUri = "${stashUrl}/projects/${stash.proj}/repos/${rootProject.name}/compare/commits?targetRepoId=${stash.repo.id}" | |
| final previousVersion = (changelog | |
| .find { | |
| it =~ /^#+ +\[(\d+\.\d+\.\d+.*)\]/ | |
| }.replaceAll(/^#+ +\[(\d+\.\d+\.\d+.*)\].*$/, '$1')) | |
| outputFile.text = changelog.toList().collect { | |
| it.toString().replaceAll(/^(#+ +)(\[Unreleased\])/, { array -> | |
| final prefix = array[1] | |
| final unreleased = array[2] | |
| "${prefix}${unreleased}(${compareCommitsUri}&sourceBranch=refs%2Fheads%2Fmaster&targetBranch=refs%2Ftags%2Fv${version})\n\n${prefix}[${version}](${compareCommitsUri}&sourceBranch=refs%2Ftags%2Fv${previousVersion}&targetBranch=refs%2Ftags%2Fv${version}) - ${new Date().format("yyyy-MM-dd'T'HH:mm:ssXX")}" | |
| }) | |
| }.join('\n') | |
| } | |
| outputs.file outputFile | |
| onlyIf { unreleasedChangesExist(outputFile as String[]) } | |
| } | |
| task commitChangelog(type: Exec, dependsOn: 'updateChangelog') { | |
| final inputFile = file("${rootDir}/CHANGELOG.md") | |
| workingDir rootDir | |
| executable 'bash' | |
| args '-c', "git diff-index --quiet HEAD -- ${inputFile} || git commit -m '${inputFile} updated.' ${inputFile}" | |
| inputs.file inputFile | |
| } | |
| task pushChangelog(type: Exec, dependsOn: 'commitChangelog') { | |
| workingDir rootDir | |
| executable 'bash' | |
| args '-c', "while ! git push; do git pull; done" | |
| onlyIf { gradle.taskGraph.hasTask(':final') } | |
| } | |
| tasks.finalSetup.dependsOn(':checkReleaseScope') | |
| tasks.postRelease.dependsOn(':pushChangelog') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment