DEV Community

Alexei Moussatov
Alexei Moussatov

Posted on

Howto: Drop-down list with Gitlab projects in Jenkins if you have more them 99 projects in namespace

In the previous receipt about Drop-down list for values we learned how-to choice any component from Jira. But in Jira ofteт isn't need in real life for development process.
At this receipt I've tell about how to create drop-down list for choosing projects's names by regex from Gitlab via API.

Prerequisites:

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

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

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

import groovy.json.JsonSlurperClassic
import java.util.regex.*

def projectNamespace = JIRA_PROJECT.toLowerCase()
def regexLine = "^((${projectNamespace}-(int|sys|service|bpl|pl|stp|tst)-([a-z,0-9,-]*)))"

def gitlabUrl = "https://gitlab.domain.org"
def gitlabApiToken = "secretToken"

def page = 1
def perPage = 64

def data = []
def list = []

while (true) {  
  paging = "&per_page=${perPage}&page=${page}"
  url = "curl -k -H PRIVATE-TOKEN:${gitlabApiToken} ${gitlabUrl}/api/v4/projects?search=${projectNamespace}-${paging}"
  json = url.execute().text.replaceAll("\r\n", "")
  data = new JsonSlurperClassic().parseText(json)
  if( data == [] ) { break }  
  data.each { project ->
    def projectMatcher = (project.name =~ regexLine)    
    if(projectMatcher.count && project.namespace.path == projectNamespace) {
      list += project.name
    }
  }
  page++
}


return list.sort()

Top comments (0)