Created
August 14, 2020 04:29
-
-
Save anhvt52/f0c4efffcda9cb90c72dd890d1939f0a to your computer and use it in GitHub Desktop.
Groovy script to parse changelog from merge commit message
This file contains 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
def parseCommitTxt(commit) { | |
def commitPattern = /^(feat|fix|docs|ci|style|refactor|test|chore)(?:\((.+)\))?: (.+)$/ | |
def commitBody = "git show -s ${commit} --pretty=format:%b".execute().text | |
def commitMsg = commitBody.split("See merge request")[0].trim() | |
def type, scope, subject | |
try { | |
def (_, t, sc, s) = (commitMsg =~ commitPattern)[0] | |
type = t | |
scope = sc | |
subject = s | |
} catch (ignored) { | |
type = "other" | |
subject = commitMsg | |
} | |
// parse MR number | |
def mrInfo = "" | |
try { | |
def mrNumberPattern = "(![0-9]+)\$" | |
def mrNumber = commitBody.find(mrNumberPattern).replace('!', "") | |
mrInfo = "[[MR!${mrNumber}](https://yourgitrepo/merge_requests/${mrNumber})]" | |
} catch (ignore) { | |
} | |
def jiraInfo = "" | |
if (scope) { | |
scope.split(',') | |
.each { token -> | |
jiraInfo += "|[[Jira!${token.toString().trim()}](http://jira.com/browse/${token.toString().trim()})]" | |
} | |
} | |
def info = [:] | |
info["type"] = type | |
info["text"] = "* ${subject.capitalize()} ${mrInfo}${jiraInfo}\n" | |
return info | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment