Class: JenkinsApi::Client::View

Inherits:
Object
  • Object
show all
Includes:
UriHelper
Defined in:
lib/jenkins_api_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

Methods included from UriHelper

#form_encode, #path_encode

Constructor Details

#initialize(client) ⇒ View

Initializes a new view object

Parameters:

  • client (Client)

    the client object



40
41
42
43
# File 'lib/jenkins_api_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

Parameters:

  • view_name (String)
  • job_name (String)


243
244
245
246
247
# File 'lib/jenkins_api_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

Parameters:

  • view_name (String)

    Name of the view to be created

  • type (String) (defaults to: "listview")

    Type of view to be created. Valid options:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jenkins_api_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

Parameters:

  • params (Hash)

    options to create the new view

Options Hash (params):

  • :name (String)

    Name of the view

  • :description (String)

    Description of the view

  • :status_filter (String)

    Filter jobs based on the status. Valid options: all_selected_jobs, enabled_jobs_only, disabled_jobs_only. Default: all_selected_jobs

  • :filter_queue (Boolean)

    true or false

  • :filter_executors (Boolean)

    true or false

  • :regex (String)

    Regular expression to filter jobs that are to be added to the view

Raises:

  • (ArgumentError)

    if the required parameter :name is not specified



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/jenkins_api_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

Parameters:

  • view_name (String)


164
165
166
167
# File 'lib/jenkins_api_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

Note:

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/jenkins_api_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

Parameters:

  • view_name (String)

Returns:

  • (Boolean)


202
203
204
# File 'lib/jenkins_api_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

Parameters:

  • view_name (String)


296
297
298
299
# File 'lib/jenkins_api_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

Parameters:

  • filter (String) (defaults to: "")

    a regex to filter view names

  • ignorecase (Bool) (defaults to: true)

    whether to be case sensitive or not



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/jenkins_api_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

Parameters:

  • view_name (String)

Returns:

  • (Array)

    job_names list of jobs in the specified view



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/jenkins_api_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

Parameters:

  • view_name (String)

Returns:

  • (Array<Hash>)

    the details of jobs in the specified view



230
231
232
233
234
235
236
# File 'lib/jenkins_api_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

Parameters:

  • view_name (String)

Returns:

  • (Array)

    view_names list of (sub-)views in the specified view



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/jenkins_api_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

Parameters:

  • view_name (String)

Returns:

  • (Array<Hash>)

    the details of (sub-)views in the specified view



284
285
286
287
288
289
290
# File 'lib/jenkins_api_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

Parameters:

  • view_name (String)
  • xml (String)


306
307
308
309
# File 'lib/jenkins_api_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

Parameters:

  • view_name (String)
  • job_name (String)


254
255
256
257
258
# File 'lib/jenkins_api_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_sObject

Return a string representation of the object



47
48
49
# File 'lib/jenkins_api_client/view.rb', line 47

def to_s
  "#<JenkinsApi::Client::View>"
end