Class: JenkinsApi::Client::View

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(client) ⇒ View

Initializes a new view object

Parameters:

  • client (Client)

    the client object



37
38
39
40
# File 'lib/jenkins_api_client/view.rb', line 37

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)


226
227
228
229
230
# File 'lib/jenkins_api_client/view.rb', line 226

def add_job(view_name, job_name)
  @logger.info "Adding job '#{job_name}' to view '#{view_name}'"
  post_msg = "/view/#{view_name}/addJobToView?name=#{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:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jenkins_api_client/view.rb', line 54

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



91
92
93
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
# File 'lib/jenkins_api_client/view.rb', line 91

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/#{params[:name]}/configSubmit",
                           post_params)
end

#delete(view_name) ⇒ Object

Delete a view

Parameters:

  • view_name (String)


161
162
163
164
# File 'lib/jenkins_api_client/view.rb', line 161

def delete(view_name)
  @logger.info "Deleting view '#{view_name}'"
  @client.api_post_request("/view/#{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.



171
172
173
174
# File 'lib/jenkins_api_client/view.rb', line 171

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)


199
200
201
# File 'lib/jenkins_api_client/view.rb', line 199

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)


247
248
249
250
# File 'lib/jenkins_api_client/view.rb', line 247

def get_config(view_name)
  @logger.info "Obtaining the configuration of view '#{view_name}'"
  @client.get_config("/view/#{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



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/jenkins_api_client/view.rb', line 181

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

#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



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/jenkins_api_client/view.rb', line 209

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/#{view_name}")
  response_json["jobs"].each do |job|
    job_names << job["name"]
  end
  job_names
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)


257
258
259
260
# File 'lib/jenkins_api_client/view.rb', line 257

def post_config(view_name, xml)
  @logger.info "Posting the configuration of view '#{view_name}'"
  @client.post_config("/view/#{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)


237
238
239
240
241
# File 'lib/jenkins_api_client/view.rb', line 237

def remove_job(view_name, job_name)
  @logger.info "Removing job '#{job_name}' from view '#{view_name}'"
  post_msg = "/view/#{view_name}/removeJobFromView?name=#{job_name}"
  @client.api_post_request(post_msg)
end

#to_sObject

Return a string representation of the object



44
45
46
# File 'lib/jenkins_api_client/view.rb', line 44

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