Class: Hudkins::Job

Inherits:
Object
  • Object
show all
Extended by:
Mixin
Includes:
Comparable, Common
Defined in:
lib/hudkins/job.rb

Overview

Description

Primary class for interacting with a Hudson job

Examples

hud = Hudkins.new "http://hudson.com"
job = hud.jobs.find_by_name :job_name

job.disabled? # => true
job.disabled = false
job.post_config!

attr_accessor_from_config methods

I created custom attr_accessor like DSL methods that create accessor like methods for the Hudkins::Job object to easily interact with the xml config. Paradigm is to use method_name? for boolean values and method_name! for any methods that post updates to the server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin

attr_accessor_from_config, attr_post_from_config, attr_reader_from_config, attr_writer_from_config

Methods included from Common

#check_host_availability, #get, #host_available?, #hudkins, #post, #url_escape

Constructor Details

#initialize(hudkins, data) ⇒ Job

Returns a new instance of Job.



61
62
63
64
65
66
67
# File 'lib/hudkins/job.rb', line 61

def initialize hudkins, data
  @hudkins = hudkins
  @name = data["name"]
  @url = URI.parse data["url"]
  @color = data["color"]
  @path = @url.path
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



25
26
27
# File 'lib/hudkins/job.rb', line 25

def color
  @color
end

#configObject (readonly)

Description

accessor for job’s config. Initializes then caches. Use update_config if out of date.



102
103
104
# File 'lib/hudkins/job.rb', line 102

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/hudkins/job.rb', line 25

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



25
26
27
# File 'lib/hudkins/job.rb', line 25

def path
  @path
end

#urlObject (readonly)

Returns the value of attribute url.



25
26
27
# File 'lib/hudkins/job.rb', line 25

def url
  @url
end

Instance Method Details

#<=>(other) ⇒ Object

Enumerables/Comparables…



78
79
80
81
# File 'lib/hudkins/job.rb', line 78

def <=> other
  # TODO how do I implement jobs.sort(&:path) ?
  self.name <=> other.name
end

#blocked_by_upstreamObject

:attr_accessor: blocked_by_upstream



43
# File 'lib/hudkins/job.rb', line 43

attr_accessor_from_config :blocked_by_upstream, "//project//blockBuildWhenUpstreamBuilding",  :bool

#build!Object



113
114
115
# File 'lib/hudkins/job.rb', line 113

def build!
  get path + "/build"
end

#can_roamObject

:attr_accessor: can_roam



37
# File 'lib/hudkins/job.rb', line 37

attr_accessor_from_config :can_roam,            "//project//canRoam",                         :bool

#concurrent_buildsObject

:attr_accessor: concurrent_builds



46
# File 'lib/hudkins/job.rb', line 46

attr_accessor_from_config :concurrent_builds,   "//project//concurrentBuild",                 :bool

#copy(new_job_name) ⇒ Object

Description

Copy job to new_job_name

Example

new_job = job.copy "new-job-name"
new_job.scm_url = "http://svn/new/job/path"
new_job.post_config!


158
159
160
161
162
163
164
# File 'lib/hudkins/job.rb', line 158

def copy new_job_name
  # post /createItem?name=NEWJOBNAME&mode=copy&from=FROMJOBNAME
  response = post "/createItem?" +
    url_escape(:name => new_job_name, :mode => "copy", :from => name)
  # return new job object
  hudkins.update_jobs.find_by_name new_job_name if response.success?
end

#delete!Object



117
118
119
# File 'lib/hudkins/job.rb', line 117

def delete!
  post path + "/doDelete"
end

#descriptionObject

:attr_accessor: description



34
# File 'lib/hudkins/job.rb', line 34

attr_accessor_from_config :description,         "//project//descriptoin"

#disable!Object



125
126
127
# File 'lib/hudkins/job.rb', line 125

def disable!
  post path + "/disable"
end

#disabledObject

:attr_accessor: disabled



40
# File 'lib/hudkins/job.rb', line 40

attr_accessor_from_config :disabled,            "//project//disabled",                        :bool

#enable!Object



129
130
131
# File 'lib/hudkins/job.rb', line 129

def enable!
  post path + "/enable"
end

#inspectObject



69
70
71
# File 'lib/hudkins/job.rb', line 69

def inspect
  super "@name=#{@name}"
end

#post_config!Object

Description

Post the job’s config back to the server to update it.



109
110
111
# File 'lib/hudkins/job.rb', line 109

def post_config!
  post path + "/config.xml", @config
end

#quick_description!(msg = nil) ⇒ Object

Description

The remote api allows for updating just the description. I had to tweak the name because I still wanted description to be an attr_accessor_from_config

Example

job.quick_description! # => "this is the description for job"
job.quick_description! = "this is the new desc." # => Response obj


141
142
143
144
145
146
147
148
# File 'lib/hudkins/job.rb', line 141

def quick_description! msg = nil
  # another yuck!
  if msg
    post path + "/description?" + url_escape(:description => msg)
  else
    get( path + "/description" ).body
  end
end

#recreate!Object



121
122
123
# File 'lib/hudkins/job.rb', line 121

def recreate!
  hudkins.add_job name, config
end

#rotate_logs_daysObject

:attr_accessor: :rotate_logs_days Number of days to keep builds -1 == infinite



59
# File 'lib/hudkins/job.rb', line 59

attr_accessor_from_config :rotate_logs_days,    "//logRotator/daysToKeep",                    Integer

#rotate_logs_numObject

:attr_accessor: :rotate_logs_num Number of builds to keep -1 == infinite



54
# File 'lib/hudkins/job.rb', line 54

attr_accessor_from_config :rotate_logs_num,     "//logRotator/numToKeep",                     :int

#scm_urlObject

:attr_accessor: scm_url



31
# File 'lib/hudkins/job.rb', line 31

attr_accessor_from_config :scm_url,             "//scm//remote"

#update_config(config = nil) ⇒ Object

Description

get the job’s config takes string (xml) or other’s config



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hudkins/job.rb', line 87

def update_config config = nil
  config = case config
  when String then
    hudkins.parse_string( config, :xml )
  when Nokogiri::XML::Document, NilClass then
    config
  else
    raise "unknown config type #{config.class}"
  end
  @config = config || hudkins.get_parsed( path + "/config.xml", :accept => "application/xml" )
end