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

- (View) initialize(client)

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

- (Object) add_job(view_name, job_name)

Add a job to view

Parameters:

  • view_name (String)
  • job_name (String)


229
230
231
232
233
# File 'lib/jenkins_api_client/view.rb', line 229

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

- (Object) create(view_name, type = "listview")

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

- (Object) create_list_view(params)

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

- (Object) delete(view_name)

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

- (Object) delete_all!

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

- (Boolean) exists?(view_name)

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

- (Object) get_config(view_name)

Obtain the configuration stored in config.xml of a specific view

Parameters:

  • view_name (String)


250
251
252
253
# File 'lib/jenkins_api_client/view.rb', line 250

def get_config(view_name)
  @logger.info "Obtaining the configuration of view '#{view_name}'"
  @client.get_config("/view/#{path_encode view_name}")
end

- (Object) list(filter = "", ignorecase = true)

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("")
  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

- (Array) list_jobs(view_name)

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

- (Object) post_config(view_name, xml)

Post the configuration of a view given the view name and the config.xml

Parameters:

  • view_name (String)
  • xml (String)


260
261
262
263
# File 'lib/jenkins_api_client/view.rb', line 260

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

- (Object) remove_job(view_name, job_name)

Remove a job from view

Parameters:

  • view_name (String)
  • job_name (String)


240
241
242
243
244
# File 'lib/jenkins_api_client/view.rb', line 240

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

- (Object) to_s

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