Last active
March 18, 2018 10:26
-
-
Save rinekri/55ee74f671db22906c14f4d0464760f6 to your computer and use it in GitHub Desktop.
Gradle task to changelog posting using a Mattermost Hook
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
| apply from: rootProject.file('tools/changelog-task.gradle') |
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
| task('sendChangelog') { | |
| // TODO: use correct key to extract changelog | |
| def changelogMessageKey = "CHANGELOG" | |
| // TODO: use correct project name! | |
| def projectName = "AppName Android" | |
| // TODO: use correct icon for your project! | |
| def iconUrl = "http://i.imgur.com/HQTF5FK.png" | |
| // TODO: use correct webhook url (the one below will post to #builds channel on our mattermost) | |
| def webHookUrl = "https://mm.appkode.ru/hooks/7hh7szbq8frimdj4x5cei99g4e" | |
| // TODO: use correct @-mention-names for your project! | |
| def userMentions = ["@name1", "@name2", "@name3"] | |
| // TODO: use actual version name for your project | |
| def versionName = "0.0000" | |
| doLast { | |
| def changeLog = getChangelog(changelogMessageKey) | |
| if (!changeLog.isEmpty()) { | |
| def json = "\"{" + | |
| "\\\"icon_url\\\": \\\"$iconUrl\\\"," + | |
| "\\\"username\\\": \\\"buildBot\\\", " + | |
| "\\\"text\\\": \\\"**$projectName $versionName** $userMentions\\n$changeLog\\\"" + | |
| "}\"" | |
| def sendChangelogToMattermost = executeOnShell("curl -i -X POST " + | |
| "-H 'Content-Type: application/json' " + | |
| "-d $json $webHookUrl") | |
| sendChangelogToMattermost.waitForOrKill(1000) | |
| } else { | |
| println "changelog is empty or error occurred" | |
| } | |
| } | |
| } | |
| private def getChangelog(String changelogMessageKey) { | |
| def lastTag = returnFromShell("git tag --sort=-v:refname " + | |
| "| grep '\\(production\\|alpha\\|prerelease\\|dev\\)' " + | |
| "| head -n 1 ").trim() | |
| println "changelog lastTag = $lastTag" | |
| def changelog = returnFromShell("git log $lastTag..HEAD " + | |
| "| grep \"$changelogMessageKey\" " + | |
| "| sed 's/.*$changelogMessageKey: /* /'") | |
| println "changelog text: \n$changelog" | |
| return changelog | |
| } | |
| private def returnFromShell(String command) { | |
| def killProcessTimeout = 3000 | |
| def process = executeOnShell(command) | |
| def result = new StringBuffer() | |
| def error = new StringBuffer() | |
| process.consumeProcessOutput(result, error) | |
| process.waitForOrKill(killProcessTimeout) | |
| if (result.size() > 0) { | |
| println "shell command $command executed successfully" | |
| } | |
| if (error.size() > 0) { | |
| println "shell executrion error: \n${error.toString()}" | |
| } | |
| return result.toString() | |
| } | |
| private def executeOnShell(String command) { | |
| return ["sh", "-c", command].execute([], rootDir) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment