DEV Community

Alexei Moussatov
Alexei Moussatov

Posted on

Howto: Drop-down list with branches from Gitlab project

In the previous receipt about Drop-down list for values we learned how-to choice any component from Jira or project name from Gitlab.
On the next step we need in branch selector for choosen project.

Prerequisites:

Step 1: In job configuration add parameter Active Choice Reactive Parameter

Step 2: Set name for your parameter (for example GIT_BRANCH)

Step 3: Set value for Referenced parameters as COMPONENT

Step 4: Select Groovy script and add next groovy-code

import groovy.json.JsonSlurperClassic

def projectName = COMPONENT
def gitlabUrl = "https://gitlab.domain.org"
def gitlabApiToken = "SecretToken"
def per_page = 128

def url = "curl -k -H PRIVATE-TOKEN:${gitlabApiToken} ${gitlabUrl}/api/v4/projects?search=$projectName"
def json = url.execute().text.replaceAll("\r\n", "")
def data = new JsonSlurperClassic().parseText(json)
def project = data.find { it.name == projectName }
def projectId = project.id

url = "curl -k -H PRIVATE-TOKEN:${gitlabApiToken} ${gitlabUrl}/api/v4/projects/${projectId}/repository/branches?per_page=${per_page}"
json = url.execute().text.replaceAll("\r\n", "")
data = new JsonSlurperClassic().parseText(json)
def list = []
data.each { branch ->
    list += branch.name
}
return list

Top comments (0)