Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbrarSyed/dd333c2585ad056912e9 to your computer and use it in GitHub Desktop.
Save AbrarSyed/dd333c2585ad056912e9 to your computer and use it in GitHub Desktop.
/**
* Generates a TeamCity XML changelog via the REST API.
*/
task("createChangelog").doLast {
def teamCityURL = "http://ci.calclavia.com/"
/**
* Create a new file
*/
def file = new FileOutputStream("output/changelog.xml")
def out = new BufferedOutputStream(file)
/**
* Grab the build first, parse the XML to find the changelog XML URL
*/
def changesXML = new XmlSlurper().parse(teamCityURL + "guestAuth/app/rest/changes?locator=build:(id:" + teamcity["teamcity.build.id"] + ")")
def changes = changesXML.change
/**
* Add the XML definition header in the front of the file and remove all other occurrences of the XML header
*/
out << ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><changes>")
println("createChangelog: Identified " + changes.size() + " changes to be written into the changelog.")
for (int i = 0; i < changes.size(); i++) {
/**
* Write each changelog XML into the URL
*/
def changelogURL = teamCityURL + "httpAuth/app/rest/changes/id:" + changes[i].@id.text()
out << get(new URL(changelogURL), "username", "pass").replaceAll("<\\?xml version=\"1\\.0\" encoding=\"UTF-8\" standalone=\"yes\"\\?>", "")
}
out << "</changes>"
out.close()
}
String get(URL url, String username, String pass) {
dev connect = url.openConnection()
connect.setRequestProperty("Authorization", "Basic "+ (username + ":" + pass).bytes.encodeBase64().toString())
def stream = connect.getInputStream()
def out = stream.getText();
stream.close();
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment