Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list matching jobs using dsl #225

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
updated to remove duplicate script.
Sahal Ansari committed Apr 3, 2017
commit ad82957ae9aa62cf7d82d13c187d8866f347a302
53 changes: 3 additions & 50 deletions lib/jenkins_api_client/job.rb
Original file line number Diff line number Diff line change
@@ -653,55 +653,8 @@ def list_by_status(status, jobs = [])
#
def list(filter, ignorecase = true)
@logger.info "Obtaining jobs matching filter '#{filter}'"
text = @client.exec_script(<<'EOS')
def Map findJobs(Object obj, String namespace = null) {
def found = [:]

// groovy apparently can't #collect on a list and return a map?
obj.items.each { job ->
// a possibly better approach would be to walk the parent chain from //
// each job
def path = job.getName()
if (namespace) {
path = "${namespace}/" + path
}
found[path] = job
// intentionally not using `instanceof` here so we don't blow up if the
// cloudbees-folder plugin is not installed
if (job.getClass().getName() == 'com.cloudbees.hudson.plugins.folder.Folder') {
found << findJobs(job, path)
}
}

found
}

void job_list_json() {
def jobs = findJobs(Jenkins.getInstance())

def allInfo = jobs.collect { path, job ->
// at least these job classes do not respond to respond to #isDisabled:
// - org.jenkinsci.plugins.workflow.job.WorkflowJob
// - com.cloudbees.hudson.plugins.folder.Folder
def enabled = false
if (job.metaClass.respondsTo(job, 'isDisabled')) {
enabled = !job.isDisabled()
}

[
_class: job.getClass().toString(),
name: path,
]
}

def builder = new groovy.json.JsonBuilder(allInfo)
out.println(builder.toPrettyString())
}

job_list_json()
EOS
groovy_script_output = @client.exec_script(<<'EOS')
def Map findJobs(Object obj, String namespace = null) {
def Map findJobs(Object obj, String namespace = null) {
def found = [:]

// groovy apparently can't #collect on a list and return a map?
@@ -736,7 +689,7 @@ def enabled = false
}

[
_class: job.getClass().toString(),
_class: job.getClass().getCanonicalName(),
name: path,
url: Hudson.getInstance().getRootUrl().toString() + job.getUrl().toString(),
]
@@ -753,7 +706,7 @@ def builder = new groovy.json.JsonBuilder(allInfo)

jobs = []
response_json["jobs"].each do |job|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above the jobs hash key was removed so this no longer works.

if job["_class"] != "class com.cloudbees.hudson.plugins.folder.Folder"
if job["_class"] !~ /com.cloudbees.hudson.plugins.folder.Folder/
if ignorecase
jobs << job["name"] if job["name"] =~ /#{filter}/i
else