git-changelog-gradle-plugin icon indicating copy to clipboard operation
git-changelog-gradle-plugin copied to clipboard

Automate changelog and versioning with conventional commits and Git.

Git Changelog Gradle Plugin

Build Status Maven Central

This is a Gradle plugin for Git Changelog Lib.

Usage

There is a complete running example here.

There are some more examples in the build.gradle.

More documentation can be found in the Git Changelog Lib.

It will use master branch by default, you can change that with something like toRef = "main".

settings.gradle

pluginManagement {
  repositories {
    gradlePluginPortal()
  }
}

build.gradle

plugin {
  id 'se.bjurr.gitchangelog.git-changelog-gradle-plugin' version 'X'
}

task gitChangelogTask(type: se.bjurr.gitchangelog.plugin.gradle.GitChangelogTask) {
 file = file("CHANGELOG.md");
 templateContent = """
  // Template here!
 """;
}

Template - Simple

Changelog of ${projectDisplayName}.

{{#tags}}
## {{name}}
 {{#issues}}
  {{#hasIssue}}
   {{#hasLink}}
### {{name}} [{{issue}}]({{link}}) {{title}} {{#hasIssueType}} *{{issueType}}* {{/hasIssueType}} {{#hasLabels}} {{#labels}} *{{.}}* {{/labels}} {{/hasLabels}}
   {{/hasLink}}
   {{^hasLink}}
### {{name}} {{issue}} {{title}} {{#hasIssueType}} *{{issueType}}* {{/hasIssueType}} {{#hasLabels}} {{#labels}} *{{.}}* {{/labels}} {{/hasLabels}}
   {{/hasLink}}
  {{/hasIssue}}
  {{^hasIssue}}
### {{name}}
  {{/hasIssue}}

  {{#commits}}
**{{{messageTitle}}}**

{{#messageBodyItems}}
 * {{.}}
{{/messageBodyItems}}

[{{hash}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}} *{{commitTime}}*

  {{/commits}}

 {{/issues}}
{{/tags}}

Template - Semantic versioning from conventional commits

If you are using conventional commits:

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]

A changelog can be rendered (using Helpers) like this:

# Changelog

{{#tags}}
{{#ifReleaseTag .}}
## [{{name}}](https://gitlab.com/html-validate/html-validate/compare/{{name}}) ({{tagDate .}})

  {{#ifContainsType commits type='feat'}}
### Features

    {{#commits}}
      {{#ifCommitType . type='feat'}}
 - {{#eachCommitScope .}} **{{.}}** {{/eachCommitScope}} {{{commitDescription .}}} ([{{hash}}](https://gitlab.com/html-validate/html-validate/commit/{{hashFull}}))
      {{/ifCommitType}}
    {{/commits}}
  {{/ifContainsType}}

  {{#ifContainsType commits type='fix'}}
### Bug Fixes

    {{#commits}}
      {{#ifCommitType . type='fix'}}
 - {{#eachCommitScope .}} **{{.}}** {{/eachCommitScope}} {{{commitDescription .}}} ([{{hash}}](https://gitlab.com/html-validate/html-validate/commit/{{hashFull}}))
      {{/ifCommitType}}
    {{/commits}}
  {{/ifContainsType}}

{{/ifReleaseTag}}
{{/tags}}

Example - custom helpers

You can add your own helpers and use them in the template. There are also built in Helpers.

apply plugin: "se.bjurr.gitchangelog.git-changelog-gradle-plugin"

import se.bjurr.gitchangelog.plugin.gradle.HelperParam;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;

task gitChangelogTask(type: se.bjurr.gitchangelog.plugin.gradle.GitChangelogTask) {
  file = new File("CHANGELOG.md");
  handlebarsHelpers = [
    new HelperParam("startsWith", new Helper<String>() {
      public Object apply(String from, Options options) throws IOException {
        def s = options.hash['s']
        if (from.matches("^" + s + ".*")) {
          return options.fn(this);
        } else {
          return options.inverse(this);
        }
      }
    }),
    new HelperParam("firstLetters", new Helper<String>() {
      public Object apply(String from, Options options) throws IOException {
        def number = new Integer(options.hash['number'])
        return from.substring(0,number);
      }
    })
  ]
  templateContent = """
  {{#commits}}
    {{#startsWith messageTitle s='feat'}}
      Starts with feat: "{{messageTitle}}"
      first 10 letters of hash is: {{firstLetters hash number='10'}}
    {{/startsWith}}
  {{/commits}}
  """;
}

Get next release

It can also be used to get the next semantic version based on commits.

classpath 'se.bjurr.gitchangelog:git-changelog-lib:1.+'

...

buildscript {
 repositories {
  mavenCentral()
  mavenLocal()
 }
 dependencies {
  ...
  classpath 'se.bjurr.gitchangelog:git-changelog-lib:1.+'
 }
}


def nextVersion = se.bjurr.gitchangelog.api.GitChangelogApi.gitChangelogApiBuilder()
  .withSemanticMajorVersionPattern("^[Bb]reaking")
  .withSemanticMinorVersionPattern("[Ff]eature")
  .getNextSemanticVersion();

println "Next version:" + nextVersion.toString();
println " Major:" + nextVersion.getMajor();
println " Minor:" + nextVersion.getMinor();
println " Patch:" + nextVersion.getPatch();