Created
June 4, 2018 11:05
-
-
Save nikolauskrismer/61c6703181e8b6fe5075faa0a38f0c56 to your computer and use it in GitHub Desktop.
Gradle script creating a changelog out of git commit messages
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
buildscript { | |
repositories { | |
maven { | |
url "https://plugins.gradle.org/m2/" | |
} | |
} | |
dependencies { | |
classpath "org.ajoberstar:grgit:+" | |
} | |
} | |
import org.ajoberstar.grgit.* | |
task generateChangelog { | |
doLast { | |
def grgit = Grgit.open(dir: '.') | |
def fileChangelog = new File('CHANGELOG.md') | |
def excludeCommitters = ['Jenkins'] | |
def skipEmptyVersion = true | |
// if we build a release we do not need to look at the HEAD (only until the tag which is built) | |
def tags = (project.version.endsWith('-SNAPSHOT')) ? ["HEAD"] : [] | |
grgit.tag.list().reverse().each{ t -> | |
tags += t.getName() | |
} | |
fileChangelog.delete() | |
def builder | |
def commits | |
def numTags = tags.size() | |
for (int i = 0; i < numTags; ++i) { | |
if (tags[i+1] == null) { | |
commits = grgit.log(includes: [tags[i]]) | |
} else { | |
commits = grgit.log { | |
range tags[i+1], tags[i]; | |
} | |
} | |
commits = commits.grep { commit -> | |
!excludeCommitters*.toLowerCase().contains(commit.committer.name.toLowerCase()) | |
} | |
if (skipEmptyVersion && commits.isEmpty()) { | |
continue | |
} | |
builder = new StringBuilder() | |
builder.append("Version ${tags[i]}:\n".replaceAll('Version HEAD', 'Upcoming version')) | |
def tagLength = builder.size() - 1 // -1 because of newline character | |
for (int j = 0; j < tagLength; ++j) { | |
builder.append('-') | |
} | |
builder.append('\n') | |
commits.inject(builder) { bldr, commit -> | |
bldr.append(' - ') | |
bldr.append(commit.shortMessage.trim().replaceAll(' ', ' ')) | |
bldr.append(' (' + commit.committer.name + ')') | |
bldr.append('\n') | |
} | |
builder.append('\n') | |
fileChangelog << builder.toString() | |
} | |
} | |
} | |
task commitChangelog(dependsOn: [generateChangelog]) { | |
doLast { | |
def fileChangelog = new File('CHANGELOG.md') | |
def grgit = Grgit.open(dir: '.') | |
def status = grgit.status(); | |
def staged = status.staged | |
def unstaged = status.unstaged | |
def allChanges = staged.added + staged.modified + staged.removed + unstaged.added + unstaged.modified + unstaged.removed | |
if (allChanges.contains(fileChangelog.name)) { | |
println "Commiting $fileChangelog.name" | |
def msgCommit = (project.version.endsWith('-SNAPSHOT')) ? 'Updating changelog' : "Releasing new version ${project.version}" | |
grgit.add(patterns: [fileChangelog.name]) | |
grgit.commit(message: msgCommit, amend: false) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment