Automating Jira with Kotlin Gradle tasks

Daniel Horowitz
3 min readNov 23, 2018

--

When working on mobile projects, we often encounter ourselves with the painful task of updating Jira tasks manually every time we want to release a new build or just deliver a new beta to our QA team.

In this article, we will use a Gradle task written in Kotlin, to update our Jira tickets automatically. This project uses the Jira Rest API, some Git commands and known REST libs like Retrofit.

Current scenario

  • The dev team is adding the Jira task ID on their commit messages
  • Releases are tagged in Git

What do we want to automate

  • Transitioning Jira tasks to another state depending on our release (Beta or Prod)
  • Updating Jira tickets with important information (i.e Fix versions, Comments, labels, etc…)

Creating a Kotlin Gradle Tasks

There are many ways to create Gradle tasks on an Android project, but for this article we will do it using the buildSrc folder.

  1. Create a new folder called buildSrc in your project’s root
  2. Create a build.gradle inside the buildSrc. This will setup the Gradle Kotlin plugin and any library dependencies that our tasks will use. In our case, we are using Retrofit

Important: We need to add an extra dependecy gradleApi(). Once the buildSrc folder is created an the project is sync with gradle, Android studio will detect this folder automatically

3. Create the kotlin source foldersrc/main/kotlin inside buildSrc

In the end our project structure looks like this

Our Gradle task looks like this. The method under @TaskAction annotation is executed when the task runs

Note that the following variables are passed as parameters

lateinit var versionName: String    
lateinit var oldTag: String
lateinit var newTag: String

This task uses git tags to update all Jira tasks found between these tags. We execute the following command to retrieve the Jira tickets

git log tag/example1...tag/example2

Which on our Kotlin task looks like this

This method parsers the commit messages between the tags and searches for the Jira ID prefix (For example if your Jira prefix is FOO you should use as prefix FOO-)

Once the task finds all tickets it iterates through them updating the fixVersion and the status (by transitioning the issue)

Executing our Gradle Task

In our app’s build.gradle we include the our task. The reason we are using the app’s build.gradle is because we can easily access the versionNameparameter as follows

task updateChangelog(type: ChangelogTask) {
versionName = android.defaultConfig.versionName
oldTag = "test/v1"
newTag = "test/v2"
}

To run the task you can either use Android studio or using this command

./gradlew updateChangelog -d 

-d shows task’s logs

That’s it! we are done. Now you just need to run this task from your CI passing the parameters versionName, oldTag, newTag and you’re good to go

The code used for this post is available here

Inside this repo you can also find a Postman collection for the Jira API used for creating this example. This is specially helpful when identifying the Jira transaction ids.

Related posts

https://android.jlelse.eu/custom-gradle-tasks-with-kotlin-e0f8659628a6

--

--