3
votes

I'm aware of the Dynamic Parameter and Dynamic Extended Choice Parameter plugins. I'm looking for a way to list of text files in a directory in github as drop down list parameter. is there a groovy script or similar that can populate the dropdown with file names?

1

1 Answers

2
votes

You can use the github api to fetch a list of files for a given path on a given repo.

So for example, to look inside the ratpack-groovy/src/main/java/ratpack/groovy folder of the ratpack project on github, you can do:

import groovy.json.*

def contents = new JsonSlurper().parse('https://api.github.com/repos/ratpack/ratpack/contents/ratpack-groovy/src/main/java/ratpack/groovy'.toURL())
    .sort { it.type } // Directories first
    .collect { it.name + (it.type == 'dir' ? '/' : '') } // Put a slash on the end of directories

assert contents = ['handling/', 
                   'internal/', 
                   'render/', 
                   'script/', 
                   'server/', 
                   'sql/', 
                   'template/', 
                   'Groovy.java', 
                   'GroovyRatpackMain.java', 
                   'package-info.java']

Obviously you can only make 60 requests per hour