Class: Guard::Jenkins

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/jenkins.rb

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Jenkins

Returns a new instance of Jenkins.



8
9
10
11
12
13
14
15
16
# File 'lib/guard/jenkins.rb', line 8

def initialize( watchers=[], options={} )
  puts "watchers param for initialize:\n#{watchers.to_s}"
  super

  @jenkins_path = options[:jenkins].nil? ? '/var/lib/jenkins/' : options[:jenkins]
  @skip_check = options[:skip_check].nil? ? false : true
  @fail_img = options[:fail_img].nil? ? @jenkins_path + 'userContent/images/fail.png' : options[:fail_img]
  @success_img = options[:success_img].nil? ? @jenkins_path + 'userContent/images/success.png' : options[:success_img]
end

Instance Method Details

#check_present_jobsObject



40
41
42
43
44
# File 'lib/guard/jenkins.rb', line 40

def check_present_jobs
  job_names.each do |job|
    update_image_for job
  end
end

#current_build_num(job_name) ⇒ Object



102
103
104
# File 'lib/guard/jenkins.rb', line 102

def current_build_num(job_name)
  next_build_num(job_name) - 1
end

#ensure_dir_for(job_name) ⇒ Object



115
116
117
118
119
# File 'lib/guard/jenkins.rb', line 115

def ensure_dir_for(job_name)
  unless Dir.new(@jenkins_path + 'userContent/jobs').include? job_name
    Dir.mkdir(@jenkins_path + 'userContent/jobs/' + job_name)
  end
end

#get_job_name_from(path) ⇒ Object



54
55
56
57
58
# File 'lib/guard/jenkins.rb', line 54

def get_job_name_from(path)
  p = path.gsub(/\/builds.*$/, '')
  q = p.gsub(/^jobs\//, '')
  q
end

#job_namesObject



67
68
69
70
71
72
# File 'lib/guard/jenkins.rb', line 67

def job_names
  names = Dir.new(@jenkins_path + 'jobs')
  names = names.reject {|dir| dir == '.' }
  names = names.reject {|dir| dir == '..' }
  names
end

#last_success_file(job_name) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/guard/jenkins.rb', line 74

def last_success_file(job_name)
  if File.exists?( @jenkins_path + 'jobs/' + job_name + '/lastSuccessful/build.xml' )
    File.new( @jenkins_path + 'jobs/' + job_name +
             '/lastSuccessful/build.xml' )
  else
    false
  end
end

#last_success_num(build_file) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/guard/jenkins.rb', line 93

def last_success_num(build_file)
  if build_file
    doc = Nokogiri::XML build_file
    doc.at_xpath('/build/number').text.to_i
  else
    0
  end
end


130
131
132
133
134
135
136
137
# File 'lib/guard/jenkins.rb', line 130

def link_fail_img_for(job_name)
  current_status = @jenkins_path + 'userContent/jobs/' + job_name + '/current_status.png'
  ensure_dir_for job_name
  if File.exists? current_status
    File.delete current_status
  end
  File.symlink( @fail_img, current_status )
end


121
122
123
124
125
126
127
128
# File 'lib/guard/jenkins.rb', line 121

def link_success_img_for(job_name)
  current_status = @jenkins_path + 'userContent/jobs/' + job_name + '/current_status.png'
  ensure_dir_for job_name
  if File.exists? current_status
    File.delete current_status
  end
  File.symlink( @success_img, current_status )
end

#next_build_num(job_name) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/guard/jenkins.rb', line 83

def next_build_num(job_name)
  if File.exists?(@jenkins_path + 'jobs/' + job_name + '/nextBuildNumber' )
    nbn_file = File.new( @jenkins_path + 'jobs/' +
                        job_name + '/nextBuildNumber' )
    nbn_file.gets.strip.to_i
  else
    100     # completely arbitrary number greater than 1
  end
end

#reloadObject



27
28
29
# File 'lib/guard/jenkins.rb', line 27

def reload
  check_present_jobs
end

#run_allObject



31
32
33
# File 'lib/guard/jenkins.rb', line 31

def run_all
  check_present_jobs
end

#run_on_change(paths = []) ⇒ Object



35
36
37
38
# File 'lib/guard/jenkins.rb', line 35

def run_on_change( paths=[] )
  puts "run on change was called with #{paths.to_s}"
  update_status_for paths
end

#start(*args) ⇒ Object



18
19
20
21
# File 'lib/guard/jenkins.rb', line 18

def start(*args)
  puts "args to #start:\n#{args.to_s}"
  check_present_jobs unless @skip_check
end

#stopObject



23
24
25
# File 'lib/guard/jenkins.rb', line 23

def stop
  true
end

#success?(job_name) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
# File 'lib/guard/jenkins.rb', line 106

def success?(job_name)
  file = last_success_file(job_name)
  if last_success_num(file) == current_build_num(job_name)
    return true
  else
    return false
  end
end

#update_image_for(job) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/guard/jenkins.rb', line 46

def update_image_for(job)
  if success? job
    link_success_img_for job
  else
    link_fail_img_for job
  end
end

#update_status_for(paths = []) ⇒ Object



60
61
62
63
64
65
# File 'lib/guard/jenkins.rb', line 60

def update_status_for( paths=[] )
  jobs = paths.map {|path| get_job_name_from path }
  jobs.each do |job|
    update_image_for job
  end
end