DEV Community

Alexei Moussatov
Alexei Moussatov

Posted on

1 2

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()

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay