Class: JenkinsApi::Client::View
- Inherits:
-
Object
- Object
- JenkinsApi::Client::View
- Includes:
- UriHelper
- Defined in:
- lib/improved_jenkins_client/view.rb
Overview
This class communicates with Jenkins “/view” API and used to create, delete, update, and various other operations permitted on the Jenkins API.
Instance Method Summary collapse
-
#add_job(view_name, job_name) ⇒ Object
Add a job to view.
-
#create(view_name, type = "listview") ⇒ Object
Creates a new empty view of the given type.
-
#create_list_view(params) ⇒ Object
Creates a listview by accepting the given parameters hash.
-
#delete(view_name) ⇒ Object
Delete a view.
-
#delete_all! ⇒ Object
Deletes all views (except the All view) in Jenkins.
-
#exists?(view_name) ⇒ Boolean
Checks if the given view exists in Jenkins.
-
#get_config(view_name) ⇒ Object
Obtain the configuration stored in config.xml of a specific view.
-
#initialize(client) ⇒ View
constructor
Initializes a new view object.
-
#list(filter = "", ignorecase = true) ⇒ Object
This method lists all views.
-
#list_jobs(view_name) ⇒ Array
List jobs in a view.
-
#list_jobs_with_details(view_name) ⇒ Array<Hash>
List jobs in view along with their details.
-
#list_views(view_name) ⇒ Array
List (sub-)views in a view.
-
#list_views_with_details(view_name) ⇒ Array<Hash>
List (sub-)views in view along with their details.
-
#post_config(view_name, xml) ⇒ Object
Post the configuration of a view given the view name and the config.xml.
-
#remove_job(view_name, job_name) ⇒ Object
Remove a job from view.
-
#to_s ⇒ Object
Return a string representation of the object.
Methods included from UriHelper
Constructor Details
#initialize(client) ⇒ View
Initializes a new view object
40 41 42 43 |
# File 'lib/improved_jenkins_client/view.rb', line 40 def initialize(client) @client = client @logger = @client.logger end |
Instance Method Details
#add_job(view_name, job_name) ⇒ Object
Add a job to view
243 244 245 246 247 |
# File 'lib/improved_jenkins_client/view.rb', line 243 def add_job(view_name, job_name) @logger.info "Adding job '#{job_name}' to view '#{view_name}'" post_msg = "/view/#{path_encode view_name}/addJobToView?name=#{form_encode job_name}" @client.api_post_request(post_msg) end |
#create(view_name, type = "listview") ⇒ Object
Creates a new empty view of the given type
listview, myview. Default: listview
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/improved_jenkins_client/view.rb', line 57 def create(view_name, type = "listview") @logger.info "Creating a view '#{view_name}' of type '#{type}'" mode = case type when "listview" "hudson.model.ListView" when "myview" "hudson.model.MyView" else raise "Type #{type} is not supported by Jenkins." end initial_post_params = { "name" => view_name, "mode" => mode, "json" => { "name" => view_name, "mode" => mode }.to_json } @client.api_post_request("/createView", initial_post_params) end |
#create_list_view(params) ⇒ Object
Creates a listview by accepting the given parameters hash
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/improved_jenkins_client/view.rb', line 94 def create_list_view(params) # Name is a required parameter. Raise an error if not specified raise ArgumentError, "Name is required for creating view" \ unless params.is_a?(Hash) && params[:name] create(params[:name], "listview") @logger.debug "Creating a list view with params: #{params.inspect}" status_filter = case params[:status_filter] when "all_selected_jobs" "" when "enabled_jobs_only" "1" when "disabled_jobs_only" "2" else "" end post_params = { "name" => params[:name], "mode" => "hudson.model.ListView", "description" => params[:description], "statusFilter" => status_filter, "json" => { "name" => params[:name], "description" => params[:description], "mode" => "hudson.model.ListView", "statusFilter" => "", "columns" => [ { "stapler-class" => "hudson.views.StatusColumn", "kind"=> "hudson.views.StatusColumn" }, { "stapler-class" => "hudson.views.WeatherColumn", "kind" => "hudson.views.WeatherColumn" }, { "stapler-class" => "hudson.views.JobColumn", "kind" => "hudson.views.JobColumn" }, { "stapler-class" => "hudson.views.LastSuccessColumn", "kind" => "hudson.views.LastSuccessColumn" }, { "stapler-class" => "hudson.views.LastFailureColumn", "kind" => "hudson.views.LastFailureColumn" }, { "stapler-class" => "hudson.views.LastDurationColumn", "kind" => "hudson.views.LastDurationColumn" }, { "stapler-class" => "hudson.views.BuildButtonColumn", "kind" => "hudson.views.BuildButtonColumn" } ] }.to_json } post_params.merge!("filterQueue" => "on") if params[:filter_queue] post_params.merge!("filterExecutors" => "on") if params[:filter_executors] post_params.merge!("useincluderegex" => "on", "includeRegex" => params[:regex]) if params[:regex] @client.api_post_request("/view/#{path_encode params[:name]}/configSubmit", post_params) end |
#delete(view_name) ⇒ Object
Delete a view
164 165 166 167 |
# File 'lib/improved_jenkins_client/view.rb', line 164 def delete(view_name) @logger.info "Deleting view '#{view_name}'" @client.api_post_request("/view/#{path_encode view_name}/doDelete") end |
#delete_all! ⇒ Object
This method deletes all views (except the All view) available in Jenkins. Please use with caution.
Deletes all views (except the All view) in Jenkins.
174 175 176 177 |
# File 'lib/improved_jenkins_client/view.rb', line 174 def delete_all! @logger.info "Deleting all views from jenkins" list.each { |view| delete(view) unless view == "All"} end |
#exists?(view_name) ⇒ Boolean
Checks if the given view exists in Jenkins
202 203 204 |
# File 'lib/improved_jenkins_client/view.rb', line 202 def exists?(view_name) list(view_name).include?(view_name) end |
#get_config(view_name) ⇒ Object
Obtain the configuration stored in config.xml of a specific view
296 297 298 299 |
# File 'lib/improved_jenkins_client/view.rb', line 296 def get_config(view_name) @logger.info "Obtaining the configuration of view '#{view_name}'" @client.get_config("/view/#{path_encode view_name}") end |
#list(filter = "", ignorecase = true) ⇒ Object
This method lists all views
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/improved_jenkins_client/view.rb', line 184 def list(filter = "", ignorecase = true) @logger.info "Obtaining views based on filter '#{filter}'" view_names = [] response_json = @client.api_get_request("", "tree=views[name]") response_json["views"].each { |view| if ignorecase view_names << view["name"] if view["name"] =~ /#{filter}/i else view_names << view["name"] if view["name"] =~ /#{filter}/ end } view_names end |
#list_jobs(view_name) ⇒ Array
List jobs in a view
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/improved_jenkins_client/view.rb', line 212 def list_jobs(view_name) @logger.info "Obtaining the jobs present in view '#{view_name}'" job_names = [] raise "The view #{view_name} doesn't exists on the server"\ unless exists?(view_name) response_json = @client.api_get_request("/view/#{path_encode view_name}") response_json["jobs"].each do |job| job_names << job["name"] end job_names end |
#list_jobs_with_details(view_name) ⇒ Array<Hash>
List jobs in view along with their details
230 231 232 233 234 235 236 |
# File 'lib/improved_jenkins_client/view.rb', line 230 def list_jobs_with_details(view_name) @logger.info "Obtaining the jobs present in view '#{view_name}'" raise "The view #{view_name} doesn't exists on the server"\ unless exists?(view_name) response_json = @client.api_get_request("/view/#{path_encode view_name}") response_json["jobs"] end |
#list_views(view_name) ⇒ Array
List (sub-)views in a view
266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/improved_jenkins_client/view.rb', line 266 def list_views(view_name) @logger.info "Obtaining the views present in view '#{view_name}'" view_names = [] raise "The view #{view_name} doesn't exists on the server"\ unless exists?(view_name) response_json = @client.api_get_request("/view/#{path_encode view_name}") response_json["views"].each do |view| view_names << view["name"] end view_names end |
#list_views_with_details(view_name) ⇒ Array<Hash>
List (sub-)views in view along with their details
284 285 286 287 288 289 290 |
# File 'lib/improved_jenkins_client/view.rb', line 284 def list_views_with_details(view_name) @logger.info "Obtaining the views present in view '#{view_name}'" raise "The view #{view_name} doesn't exists on the server"\ unless exists?(view_name) response_json = @client.api_get_request("/view/#{path_encode view_name}") response_json["views"] end |
#post_config(view_name, xml) ⇒ Object
Post the configuration of a view given the view name and the config.xml
306 307 308 309 |
# File 'lib/improved_jenkins_client/view.rb', line 306 def post_config(view_name, xml) @logger.info "Posting the configuration of view '#{view_name}'" @client.post_config("/view/#{path_encode view_name}/config.xml", xml) end |
#remove_job(view_name, job_name) ⇒ Object
Remove a job from view
254 255 256 257 258 |
# File 'lib/improved_jenkins_client/view.rb', line 254 def remove_job(view_name, job_name) @logger.info "Removing job '#{job_name}' from view '#{view_name}'" post_msg = "/view/#{path_encode view_name}/removeJobFromView?name=#{form_encode job_name}" @client.api_post_request(post_msg) end |
#to_s ⇒ Object
Return a string representation of the object
47 48 49 |
# File 'lib/improved_jenkins_client/view.rb', line 47 def to_s "#<JenkinsApi::Client::View>" end |