Last active
July 1, 2016 18:10
-
-
Save fmamud/ba9ff832544be2139ec230f51a0c8f27 to your computer and use it in GitHub Desktop.
Get all closed PR from private repository
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
@Grab('org.codehaus.gpars:gpars:1.2.1') | |
import static groovyx.gpars.GParsPool.* | |
import groovy.json.JsonSlurper | |
orgid = 0 | |
orgname = 'X' | |
username = '' | |
token = 'X' | |
def getPullsCount(name) { | |
def pulls = [] | |
fetchPulls("https://api.github.com/repos/$orgname/$name/pulls?state=closed") { count -> | |
pulls << count | |
} | |
def sum = pulls.sum() | |
println "Fetching $name pull requests: ${sum}." | |
sum | |
} | |
def fetchPulls(url, cls) { | |
def pulls = url.toURL() | |
def pullsConn = pulls.openConnection() | |
pullsConn.setRequestProperty('Authorization', "Basic " + "$username:$token".getBytes().encodeBase64().toString()) | |
def json = new JsonSlurper().parseText(pullsConn.inputStream.text) | |
cls(json.size()) | |
def links = pullsConn.headerFields.Link?.getAt(0) // get github navigation headers | |
if (links?.contains("next")) { | |
def start = links.indexOf('<') + 1 | |
def end = links.substring(start).indexOf('>') + 1 | |
def pullPage = links.substring(1, end) | |
fetchPulls(pullPage, cls) | |
} | |
} | |
withPool(50) { | |
def result = [:] | |
// I'm lazy here :) | |
1.upto(7) { page -> | |
def basic = "https://api.github.com/organizations/$orgid/repos?type=private&page=$page".toURL().getText(requestProperties: ['Authorization': "Basic " + "$username:$token".getBytes().encodeBase64().toString()]) | |
def json = new JsonSlurper().parseText(basic) | |
json.name.eachParallel { name -> | |
result[name] = getPullsCount(name) | |
} | |
} | |
println result.sort { -it.value } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment