Class: Hudson::Job

Inherits:
HudsonObject
  • 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

Methods inherited from HudsonObject

#get_xml, get_xml, load_xml_api, #send_post_request, #send_xml_post_request

Constructor Details

#initialize(name) ⇒ Job

Returns a new instance of Job



34
35
36
37
38
39
# File 'lib/hudson-remote-api/job.rb', line 34

def initialize(name)
    @name = name
    load_xml_api
    load_config
    load_info
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color



6
7
8
# File 'lib/hudson-remote-api/job.rb', line 6

def color
  @color
end

#configObject

Returns the value of attribute config



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

def config
  @config
end

#descriptionObject

Returns the value of attribute description



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

def description
  @description
end

#last_buildObject (readonly)

Returns the value of attribute last_build



6
7
8
# File 'lib/hudson-remote-api/job.rb', line 6

def last_build
  @last_build
end

#last_completed_buildObject (readonly)

Returns the value of attribute last_completed_build



6
7
8
# File 'lib/hudson-remote-api/job.rb', line 6

def last_completed_build
  @last_completed_build
end

#last_failed_buildObject (readonly)

Returns the value of attribute last_failed_build



6
7
8
# File 'lib/hudson-remote-api/job.rb', line 6

def last_failed_build
  @last_failed_build
end

#last_stable_buildObject (readonly)

Returns the value of attribute last_stable_build



6
7
8
# File 'lib/hudson-remote-api/job.rb', line 6

def last_stable_build
  @last_stable_build
end

#last_successful_buildObject (readonly)

Returns the value of attribute last_successful_build



6
7
8
# File 'lib/hudson-remote-api/job.rb', line 6

def last_successful_build
  @last_successful_build
end

#last_unsuccessful_buildObject (readonly)

Returns the value of attribute last_unsuccessful_build



6
7
8
# File 'lib/hudson-remote-api/job.rb', line 6

def last_unsuccessful_build
  @last_unsuccessful_build
end

#nameObject

Returns the value of attribute name



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

def name
  @name
end

#next_build_numberObject (readonly)

Returns the value of attribute next_build_number



6
7
8
# File 'lib/hudson-remote-api/job.rb', line 6

def next_build_number
  @next_build_number
end

#repository_browser_locationObject

Returns the value of attribute repository_browser_location



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

def repository_browser_location
  @repository_browser_location
end

#repository_urlObject

Returns the value of attribute repository_url



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

def repository_url
  @repository_url
end

#repository_urlsObject

Returns the value of attribute repository_urls



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

def repository_urls
  @repository_urls
end

Class Method Details

.listObject

List all Hudson jobs



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

def self.list()
    xml = get_xml(@@hudson_xml_api_path)
    
    jobs = []
    jobs_doc = REXML::Document.new(xml)
    jobs_doc.each_element("hudson/job") do |job|
        jobs << job.elements["name"].text
    end
    jobs
end

.list_activeObject

List all jobs in active execution



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hudson-remote-api/job.rb', line 21

def self.list_active
    xml = get_xml(@@hudson_xml_api_path)
    
    active_jobs = []
    jobs_doc = REXML::Document.new(xml)
    jobs_doc.each_element("hudson/job") do |job|
        if job.elements["color"].text.include?("anime")
            active_jobs << job.elements["name"].text
        end
    end
    active_jobs
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

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

#buildObject

Start building this job on Hudson server (can't build parameterized jobs)



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

def build()
    response = send_post_request(@xml_api_build_path, {:delay => '0sec'})
    response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPRedirection)
end

#copy(new_job = nil) ⇒ Object

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

Raises:

  • (APIError)


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

def copy(new_job=nil)
    new_job = "copy_of_#{@name}" if new_job.nil?
    
    response = send_post_request(@@xml_api_create_item_path, {: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



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

def delete()
    response = send_post_request(@xml_api_delete_path)
    response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPRedirection)
end

#disableObject



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

def disable()            
    response = send_post_request(@xml_api_disable_path)
    puts response.class
    response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPRedirection)
end

#enableObject



167
168
169
170
171
# File 'lib/hudson-remote-api/job.rb', line 167

def enable()            
    response = send_post_request(@xml_api_enable_path)
    puts response.class
    response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPRedirection)
end

#load_configObject

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hudson-remote-api/job.rb', line 52

def load_config()
    @config = get_xml(@xml_api_config_path)
    @config_doc = REXML::Document.new(@config)
    
    @config_doc = REXML::Document.new(@config)
    if !@config_doc.elements["/project/scm/locations/hudson.scm.SubversionSCM_-ModuleLocation/remote"].nil?
        @repository_url = @config_doc.elements["/project/scm/locations/hudson.scm.SubversionSCM_-ModuleLocation/remote"].text || ""
    end
    @repository_urls = []
    if !@config_doc.elements["/project/scm/locations"].nil?
        @config_doc.elements.each("/project/scm/locations/hudson.scm.SubversionSCM_-ModuleLocation"){|e| @repository_urls << e.elements["remote"].text }
    end
    if !@config_doc.elements["/project/scm/browser/location"].nil?
        @repository_browser_location = @config_doc.elements["/project/scm/browser/location"].text
    end
    if !@config_doc.elements["/project/description"].nil?
        @description = @config_doc.elements["/project/description"].text || ""
    end
end

#load_infoObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hudson-remote-api/job.rb', line 72

def load_info()
    @info = get_xml(@xml_api_path)
    @info_doc = REXML::Document.new(@info)
    
    if @info_doc.elements["/freeStyleProject"]
      @color = @info_doc.elements["/freeStyleProject/color"].text if @info_doc.elements["/freeStyleProject/color"]
      @last_build = @info_doc.elements["/freeStyleProject/lastBuild/number"].text if @info_doc.elements["/freeStyleProject/lastBuild/number"]
      @last_completed_build = @info_doc.elements["/freeStyleProject/lastCompletedBuild/number"].text if @info_doc.elements["/freeStyleProject/lastCompletedBuild/number"]
      @last_failed_build = @info_doc.elements["/freeStyleProject/lastFailedBuild/number"].text if @info_doc.elements["/freeStyleProject/lastFailedBuild/number"]
      @last_stable_build = @info_doc.elements["/freeStyleProject/lastStableBuild/number"].text if @info_doc.elements["/freeStyleProject/lastStableBuild/number"]
      @last_successful_build = @info_doc.elements["/freeStyleProject/lastSuccessfulBuild/number"].text if @info_doc.elements["/freeStyleProject/lastSuccessfulBuild/number"]
      @last_unsuccessful_build = @info_doc.elements["/freeStyleProject/lastUnsuccessfulBuild/number"].text if @info_doc.elements["/freeStyleProject/lastUnsuccessfulBuild/number"]
      @next_build_number = @info_doc.elements["/freeStyleProject/nextBuildNumber"].text if @info_doc.elements["/freeStyleProject/nextBuildNumber"]
    end
end

#load_xml_apiObject



41
42
43
44
45
46
47
48
49
# File 'lib/hudson-remote-api/job.rb', line 41

def load_xml_api
  @xml_api_path = File.join(Hudson[:url], "job/#{@name}/api/xml")
  @xml_api_config_path = File.join(Hudson[:url], "job/#{@name}/config.xml")
  @xml_api_build_path = File.join(Hudson[:url], "job/#{@name}/build")
  @xml_api_disable_path = File.join(Hudson[:url], "job/#{@name}/disable")
  @xml_api_enable_path = File.join(Hudson[:url], "job/#{@name}/enable")
  @xml_api_delete_path  = File.join(Hudson[:url], "job/#{@name}/doDelete")
  @xml_api_wipe_out_workspace_path = File.join(Hudson[:url], "job/#{@name}/doWipeOutWorkspace")
end

#update(config = nil) ⇒ Object

Update the job configuration on Hudson server



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

def update(config=nil)
    @config = config if !config.nil?
    response = send_xml_post_request(@xml_api_config_path, @config)
    response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPRedirection)
end

#wait_for_build_to_finish(poll_freq = 10) ⇒ Object



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

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



179
180
181
182
183
184
185
186
187
188
# File 'lib/hudson-remote-api/job.rb', line 179

def wipe_out_workspace()
    wait_for_build_to_finish
    
    if !active?
        response = send_post_request(@xml_api_wipe_out_workspace_path)
    else
        response = false
    end
    response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPRedirection)
end