Skip to content

Instantly share code, notes, and snippets.

@thatsk
Last active August 5, 2019 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thatsk/0bd61635dc510660fa8f93975dbd8fc0 to your computer and use it in GitHub Desktop.
Save thatsk/0bd61635dc510660fa8f93975dbd8fc0 to your computer and use it in GitHub Desktop.
Groovy Script
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import groovy.json.JsonBuilder
def content = '''
{
"version": "1.0",
"repos" : [{
"name": "service-config",
"source_root": "/service-config",
"mountpoint": "test",
"url" : "http://test.example.com",
"include_branch_name" : "false",
"branches" : [""],
"hooks": [{
"type": "polling",
"interval": "15"
}]
}]
}'''
def cout = new StringBuilder(), cerr = new StringBuilder()
def gitbranch = 'git name-rev --name-only HEAD'.execute()
gitbranch.consumeProcessOutput(cout, cerr)
gitbranch.waitForOrKill(1000)
println "Command Output"
def slurped = new JsonSlurper().parseText(content)
slurped.repos[0].branches = "${cout.toString().trim()}" //array because you have an array in original json
def builder = new JsonBuilder(slurped)
println(builder.toPrettyString())
@thatsk
Copy link
Author

thatsk commented Aug 1, 2019

Command Output
{
    "version": "1.0",
    "repos": [
        {
            "name": "service-config",
            "source_root": "/service-config",
            "mountpoint": "test",
            "url": "http://test.example.com",
            "include_branch_name": "false",
            "branches": "dev-RE-15",
            "hooks": [
                {
                    "type": "polling",
                    "interval": "15"
                }
            ]
        }
    ]
}```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment