Skip to content

Instantly share code, notes, and snippets.

@elarib
Last active August 18, 2021 10:06
Show Gist options
  • Save elarib/d5359cc6cd2957e708d16bbf338fc775 to your computer and use it in GitHub Desktop.
Save elarib/d5359cc6cd2957e708d16bbf338fc775 to your computer and use it in GitHub Desktop.
Jenkins Branch From Dynamic Repo using Active Choice parameter
team_name = TOREPLACE
username = TOREPLACE
password = TOREPLACE
urlStr= "https://bitbucket.org/!api/2.0/repositories/${team_name}/${Repository}/refs/branches?sort=-target.date&fields=pagelen,next,page,size,values.name&pagelen=100"
branches = []
def getBranches(url) {
def baseUrl = new URL(url)
HttpURLConnection connection = (HttpURLConnection) baseUrl.openConnection();
connection.addRequestProperty("Accept", "application/json")
String encoded = Base64.getEncoder().encodeToString(("${username}:${password}").getBytes(java.nio.charset.StandardCharsets.UTF_8)); //Java 8
connection.setRequestProperty("Authorization", "Basic "+encoded);
connection.with {
doOutput = true
requestMethod = 'GET'
def jsResult = new groovy.json.JsonSlurper().parseText( content.text)
jsResult.values.each { value ->
branches.add(value.name)
}
if(jsResult.next){
getBranches(jsResult.next)
}
}
}
getBranches(urlStr)
return branches
team_name = TOREPLACE
username = TOREPLACE
password = TOREPLACE
urlStr = "https://bitbucket.org/!api/2.0/repositories/${team_name}?sort=-updated_on&fields=pagelen,next,page,size,values.name&pagelen=100"
repos = []
def getRepos(url) {
def baseUrl = new URL(url)
HttpURLConnection connection = (HttpURLConnection) baseUrl.openConnection();
connection.addRequestProperty("Accept", "application/json")
String encoded = Base64.getEncoder().encodeToString(("${username}:${password}").getBytes(java.nio.charset.StandardCharsets.UTF_8)); //Java 8
connection.setRequestProperty("Authorization", "Basic "+encoded);
connection.with {
doOutput = true
requestMethod = 'GET'
def jsResult = new groovy.json.JsonSlurper().parseText( content.text)
jsResult.values.each { value ->
repos.add(value.name)
}
if(jsResult.next){
getRepos(jsResult.next)
}
}
}
getRepos(urlStr)
return repos
@Abdulateef-Muhammad
Copy link

Actually the issue here is How to get credentials from Jenkins, there is a way to do so which is described in this link.
the code in this link is using some cloudbees plugin to fetch all the credentials. Which may cause a vulnerability by exposing the other credentials.

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