-
Notifications
You must be signed in to change notification settings - Fork 240
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
base: master
Are you sure you want to change the base?
Changes from 9 commits
9468bfa
82c78ce
9c03cc5
c9d9ba0
e65104a
ad82957
d03d141
54dc97e
e399a0c
9af3e1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# | ||
|
||
# Copyright (c) 2012-2013 Kannan Manickam <[email protected]> | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
|
@@ -653,16 +653,70 @@ def list_by_status(status, jobs = []) | |
# | ||
def list(filter, ignorecase = true) | ||
@logger.info "Obtaining jobs matching filter '#{filter}'" | ||
response_json = @client.api_get_request("") | ||
jobs = [] | ||
groovy_script_output = @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().getCanonicalName().toString(), | ||
name: path, | ||
url: Hudson.getInstance().getRootUrl().toString() + job.getUrl().toString(), | ||
] | ||
} | ||
|
||
def builder = new groovy.json.JsonBuilder(allInfo) | ||
out.println(builder.toString()) | ||
} | ||
|
||
job_list_json() | ||
EOS | ||
|
||
response_json = groovy_script_output | ||
|
||
jobs = Array.new | ||
response_json["jobs"].each do |job| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ignorecase | ||
jobs << job["name"] if job["name"] =~ /#{filter}/i | ||
else | ||
jobs << job["name"] if job["name"] =~ /#{filter}/ | ||
if job.is_a?(Hash) | ||
if job.key? :_class and job[:_class] !~ /com.cloudbees.hudson.plugins.folder.Folder/ | ||
if ignorecase | ||
jobs << job[:name] if job[:name] =~ /#{filter}/i | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is using symbols instead of strings now. When I used JSON.parse it returned strings so this did not work for me. |
||
else | ||
jobs << job[:name] if job[:name] =~ /#{filter}/ | ||
end | ||
end | ||
end | ||
end | ||
jobs | ||
jobs.to_a | ||
end | ||
|
||
# List all jobs on the Jenkins CI server along with their details | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now returns a String instead of a Hash because JSON.parse was removed