Class: Hudson::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/hudson-remote-api/job.rb

Overview

This class provides an interface to Hudson jobs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config = nil) ⇒ Job

Instance methods



39
40
41
42
43
44
45
# File 'lib/hudson-remote-api/job.rb', line 39

def initialize(name, config=nil)
  name.strip!
  # Creates the job in Hudson if it doesn't already exist
  @name = Job.list.include?(name) ? name : Job.create(name, config).name
  load_config
  self
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/hudson-remote-api/job.rb', line 4

def config
  @config
end

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/hudson-remote-api/job.rb', line 4

def description
  @description
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/hudson-remote-api/job.rb', line 4

def name
  @name
end

#parameterized_jobObject

Returns the value of attribute parameterized_job.



4
5
6
# File 'lib/hudson-remote-api/job.rb', line 4

def parameterized_job
  @parameterized_job
end

#repository_browser_locationObject

Returns the value of attribute repository_browser_location.



4
5
6
# File 'lib/hudson-remote-api/job.rb', line 4

def repository_browser_location
  @repository_browser_location
end

#repository_urlObject

Returns the value of attribute repository_url.



4
5
6
# File 'lib/hudson-remote-api/job.rb', line 4

def repository_url
  @repository_url
end

#repository_urlsObject

Returns the value of attribute repository_urls.



4
5
6
# File 'lib/hudson-remote-api/job.rb', line 4

def repository_urls
  @repository_urls
end

Class Method Details

.create(name, config = nil) ⇒ Object

Raises:



29
30
31
32
33
34
# File 'lib/hudson-remote-api/job.rb', line 29

def create(name, config=nil)
  config ||= File.open(File.dirname(__FILE__) + '/new_job_config.xml').read
  response = Hudson.client.create_item!({:name=>name, :mode=>"hudson.model.FreeStyleProject", :config=>config})
  raise(APIError, "Error creating job #{name}: #{response.body}") if response.class != Net::HTTPFound
  Job.get(name)
end

.get(job_name) ⇒ Object



24
25
26
27
# File 'lib/hudson-remote-api/job.rb', line 24

def get(job_name)
  job_name.strip!
  list.include?(job_name) ? Job.new(job_name) : nil
end

.listObject

List all Hudson jobs



9
10
11
12
13
14
# File 'lib/hudson-remote-api/job.rb', line 9

def list()
  xml = Hudson.client.server_info
  server_info_parser = Hudson::Parser::ServerInfo.new(xml)

  server_info_parser.jobs
end

.list_activeObject

List all jobs in active execution



17
18
19
20
21
22
# File 'lib/hudson-remote-api/job.rb', line 17

def list_active
  xml = Hudson.client.server_info
  server_info_parser = Hudson::Parser::ServerInfo.new(xml)

  server_info_parser.active_jobs
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/hudson-remote-api/job.rb', line 105

def active?
  Job.list_active.include?(self.name)
end

#build(params = {}) ⇒ Object

Start building this job on Hudson server



171
172
173
174
175
176
177
178
# File 'lib/hudson-remote-api/job.rb', line 171

def build(params={})
  if @parameterized_job
    response = Hudson.client.build_job_with_parameters!(self.name, params)
  else
    response = Hudson.client.build_job!(self.name)
  end
  response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPRedirection)
end

#builds_listObject



101
102
103
# File 'lib/hudson-remote-api/job.rb', line 101

def builds_list
  @job_info_parser.builds
end

#colorObject



69
70
71
# File 'lib/hudson-remote-api/job.rb', line 69

def color
  @color ||= @job_info_parser.color
end

#copy(new_job = nil) ⇒ Object

Create a new job on Hudson server based on the current job object

Raises:



118
119
120
121
122
123
# File 'lib/hudson-remote-api/job.rb', line 118

def copy(new_job=nil)
  new_job = "copy_of_#{@name}" if new_job.nil?
  response = Hudson.client.create_item!({:name=>new_job, :mode=>"copy", :from=>@name})
  raise(APIError, "Error copying job #{@name}: #{response.body}") if response.class != Net::HTTPFound
  Job.new(new_job)
end

#deleteObject

Delete this job from Hudson server



191
192
193
194
# File 'lib/hudson-remote-api/job.rb', line 191

def delete()
  response = Hudson.client.delete_job!(self.name)
  response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPRedirection)
end

#disableObject



180
181
182
183
# File 'lib/hudson-remote-api/job.rb', line 180

def disable()
  response = Hudson.client.disable_job!(self.name)
  response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPRedirection)
end

#enableObject



185
186
187
188
# File 'lib/hudson-remote-api/job.rb', line 185

def enable()
  response = Hudson.client.enable_job!(self.name)
  response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPRedirection)
end

#free_style_project?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/hudson-remote-api/job.rb', line 65

def free_style_project?
  @free_style_project ||= @job_info_parser.free_style_project?
end

#last_buildObject



73
74
75
# File 'lib/hudson-remote-api/job.rb', line 73

def last_build
  @job_info_parser.last_build
end

#last_completed_buildObject



77
78
79
# File 'lib/hudson-remote-api/job.rb', line 77

def last_completed_build
  @job_info_parser.last_completed_build
end

#last_failed_buildObject



81
82
83
# File 'lib/hudson-remote-api/job.rb', line 81

def last_failed_build
  @job_info_parser.last_failed_build
end

#last_stable_buildObject



85
86
87
# File 'lib/hudson-remote-api/job.rb', line 85

def last_stable_build
  @job_info_parser.last_stable_build
end

#last_successful_buildObject



89
90
91
# File 'lib/hudson-remote-api/job.rb', line 89

def last_successful_build
  @job_info_parser.last_successful_build
end

#last_unsuccessful_buildObject



93
94
95
# File 'lib/hudson-remote-api/job.rb', line 93

def last_unsuccessful_build
  @job_info_parser.last_unsuccessful_build
end

#load_configObject Also known as: reload_config

Load data from Hudson’s Job configuration settings into class variables



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hudson-remote-api/job.rb', line 48

def load_config
  @config = Hudson.client.job_config_info(self.name)
  @config_info_parser = Hudson::Parser::JobConfigInfo.new(@config)
  @config_writer = Hudson::XmlWriter::JobConfigInfo.new(self.name, @config)

  @info = Hudson.client.job_info(self.name)
  @job_info_parser = Hudson::Parser::JobInfo.new(@info)

  @description = @config_info_parser.description
  @parameterized_job = @config_info_parser.parameterized?
  @git = @config_info_parser.git_repo?
  @repository_url = @git ? @config_info_parser.git_repository : @config_info_parser.svn_repository
  @repostory_urls = @config_info_parser.svn_repository_urls
  @repository_browser_location = @config_info_parser.scm_broswer_location
end

#next_build_numberObject



97
98
99
# File 'lib/hudson-remote-api/job.rb', line 97

def next_build_number
  @job_info_parser.next_build_number
end

#triggersObject



162
163
164
# File 'lib/hudson-remote-api/job.rb', line 162

def triggers
  @config_info_parser.triggers
end

#triggers=(opts = {}) ⇒ Object



157
158
159
160
# File 'lib/hudson-remote-api/job.rb', line 157

def triggers= opts={}
  @config_writer.triggers = opts
  reload_config
end

#wait_for_build_to_finish(poll_freq = 10) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/hudson-remote-api/job.rb', line 109

def wait_for_build_to_finish(poll_freq=10)
  loop do
    puts "waiting for all #{@name} builds to finish"
    sleep poll_freq # wait
    break if !active? and !BuildQueue.list.include?(@name)
  end
end

#wipe_out_workspaceObject



196
197
198
199
200
201
202
203
204
205
# File 'lib/hudson-remote-api/job.rb', line 196

def wipe_out_workspace()
  wait_for_build_to_finish

  if !active?
    response = Hudson.client.wipeout_job_workspace!(self.name)
  else
    response = false
  end
  response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPRedirection)
end