Jenkins parse git change log

There is this neat plugin in Jenkins called Git Changelog which provides information about what was the git change that triggered the build. Recently I wanted to parse this change so I could send notification emails when certain things changed e.g: DB schemas.

My process for doing the same was:

  1. Parsing the changelog:
* Get the current build: 
import hudson.model.*;
import hudson.util.*;
import hudson.scm.*;
import hudson.plugins.git.*

def thr = Thread.currentThread();
def build = thr?.executable;
* Get the gitlog changeset and entries within the changeset
def changeSet= build.getChangeSet();
def entries = changeSet.getItems();
* Parse entries to capture authors, comments and files changed
def map=[:]


def filePath = ""

for (int j = 0; j < entries.length; j++) {
                                   def entry = entries[0]
                                   map.put('AUTHOR',entry.getAuthor())
                                   def files = new ArrayList(entry.getAffectedFiles())
                                   for (int k = 0; k < files.size(); k++) {
                                                                                  def file = files[k]
                                                                                  if(file.getPath().contains("Schema"))
                                                                                  {
                                                                                    filePath = file.getPath() +","+filePath
                                                                                  }
                                                                            }
                                         }
if(filePath.length()>1)
{
   filePath = filePath.substring(0, filePath.length()-1)
}

map.put('FILE', filePath )

if(changeSet.getLogs())
{
                                   if(changeSet.getLogs().get(0))
                                   {
                                        def changeComment = changeSet.getLogs().get(0).getComment().split(":")[0];
                                        map.put('CHANGE_COMMENT',changeComment)
            
                                   }
}
return map

The above code parses the changeset entries to capture authors and logs and creates an environment variable with the files changed in this changeset.

  1. Conditional action to trigger based on files changed:

Conditional Action

  1. Editable Email notification if conditional action is triggered, with attachments:

Editable email notification