<commit_before>/**
 * Changelog Generator using Git and Github.
 * Create by <NAME> on 2016-06-24.
 */

println ""
println "--------------------------------"
println "Executing ChangeLog Generator..."
println "--------------------------------"
println ""

def user = System.getenv("GITHUB_USERNAME")
def token = System.getenv("GITHUB_TOKEN")

if(!user || !token) {
    println "You need to define the following environment variables before using this script: GITHUB_USERNAME, GITHUB_TOKEN"
    return
}

if(args.length != 3) {
    println "Usage: groovy changelog-generator.groovy GITHUB_REPO_URL TAG_START TAG_END"
    return
}

def repoUrl = args[0]
def tagStart = args[1]
def tagEnd = args[2]


println "Github Repo URL: ${repoUrl}"
println "Start Tag: ${tagStart}"
println "End Tag: ${tagEnd}"
println ""


println "Getting commits from current local Git working copy..."
def logCmd = "git log ${tagStart}..${tagEnd} --pretty=format:%H --merges"
def logResult = logCmd.execute().text
def commits = logResult.split("\\r?\\n")
println "Done."



println "Getting Pull Requests from Github..."
def slurper = new groovy.json.JsonSlurper()
def prsUrl = new URL("${repoUrl}/pulls?state=all")
def encodedAuth = "${user}:${token}".getBytes().encodeBase64().toString()
URLConnection connection = prsUrl.openConnection();
connection.setRequestProperty("Authorization", "Basic ${encodedAuth}");
def prResponse = slurper.parse(new BufferedReader(new InputStreamReader(connection.getInputStream())))
println "Done."



println "Processing changelog..."
StringBuilder sb = new StringBuilder()

sb.append("# Change Log")
sb.append("\n\n")

sb.append("## Version ${tagEnd}:")
sb.append("\n\n")

sb.append("**Merged Pull Requests:**")
sb.append("\n\n")

for(def commitSha : commits) {
    for(def pr in prResponse) {
	if(commitSha.equals(pr.merge_commit_sha)) {
	    sb.append("- ${pr.title} [\\#${pr.number}](${pr.html_url}) ([${pr.user.login}](${pr.user.html_url}))")
	    sb.append("\n")
	}
    }<commit_msg>Adding compare diff to changelog generator<commit_after>/**
 * Changelog Generator using Git and Github.
 * Create by <NAME> on 2016-06-24.
 */

println ""
println "--------------------------------"
println "Executing ChangeLog Generator..."
println "--------------------------------"
println ""

def user = System.getenv("GITHUB_USERNAME")
def token = System.getenv("GITHUB_TOKEN")
def githubApi = System.getenv("GITHUB_API")

if(!user || !token) {
    println "You need to define the following environment variables before using this script: GITHUB_USERNAME, GITHUB_TOKEN and optionally GITHUB_API."
    return
}

if(!githubApi) {
    githubApi = "https://api.github.com"
}

//Verify Arguments
if(args.length != 3) {
    println "Usage: groovy changelog-generator.groovy GITHUB_REPO_NAME TAG_START TAG_END"
    return
}

//Arguments
def githubRepoName = args[0]
def tagStart = args[1]
def tagEnd = args[2]
def repoApiUrl = "${githubApi}/repos/${githubRepoName}"

//Print environment data
println "Repo API URL: ${repoApiUrl}"
println "Start Tag: ${tagStart}"
println "End Tag: ${tagEnd}"
println ""


println "Getting commits from current local Git working copy..."
def logCmd = "git log ${tagStart}..${tagEnd} --pretty=format:%H --merges"
def logResult = logCmd.execute().text
def commits = logResult.split("\\r?\\n")
println "Done."


def slurper = new groovy.json.JsonSlurper()
def encodedAuth = "${user}:${token}".getBytes().encodeBase64().toString()


println "Getting Repo Information from Github..."
URLConnection repoInfoConnection = new URL("${repoApiUrl}").openConnection();
repoInfoConnection.setRequestProperty("Authorization", "Basic ${encodedAuth}");
def repoInfoResponse = slurper.parse(new BufferedReader(new InputStreamReader(repoInfoConnection.getInputStream())))
println "Done."



println "Getting Pull Requests from Github..."
URLConnection prConnection = new URL("${repoApiUrl}/pulls?state=all").openConnection();
prConnection.setRequestProperty("Authorization", "Basic ${encodedAuth}");
def prResponse = slurper.parse(new BufferedReader(new InputStreamReader(prConnection.getInputStream())))
println "Done."



println "Processing changelog..."
StringBuilder sb = new StringBuilder()

sb.append("# Change Log")
sb.append("\n\n")

sb.append("## Version ${tagEnd}:")
sb.append("\n")

sb.append("[Full Changelog](${repoInfoResponse.html_url}/compare/${tagStart}...${tagEnd})")
sb.append("\n\n")

sb.append("**Merged Pull Requests:**")
sb.append("\n\n")

for(def commitSha : commits) {
    for(def pr in prResponse) {
	if(commitSha.equals(pr.merge_commit_sha)) {
	    sb.append("- ${pr.title} [\\#${pr.number}](${pr.html_url}) ([${pr.user.login}](${pr.user.html_url}))")
	    sb.append("\n")
	}
    }
{'fraction_non_alphanumeric': 0.18838737020555202, 'fraction_numerical': 0.005933460478915025, 'mean_word_length': 4.3881278538812785, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4861502', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1487, 'n_words': 369}