From 1f7176a6a611f30a0d70e0f75ec90724f6302043 Mon Sep 17 00:00:00 2001 From: michael Date: Fri, 19 Mar 2010 23:27:39 -0700 Subject: [PATCH] reworked rss publisher to keep status of a configurable number recent builds. Converted over to using the ruby rss library. There are better libraries that could be used, but this one comes with ruby so it doesn't add new external dependencies. A configuration option 'keep' specifies how many total builds to retain in the feed. The default is 1 to match the previous behavior. The rss library really insists on building a valid feed, so I had to add description and link elements that weren't present in the previous code. The channel_link should be a url set in your config, or it will default to using a file url pointed at the rss file being written. The description is the string that used to be the title, and the new title is now shorter. The generated rss file will look slightly different to a human, but it should be valid and it passes the Cerberus rss_publisher_test.rb In some cases you might need to make adjustments in whatever scripts you may have that parse the rss after it's generated. --- doc/site/src/options.page | 2 ++ lib/cerberus/config.example.yml | 2 ++ lib/cerberus/publisher/rss.rb | 46 +++++++++++++++++++++------------ 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/doc/site/src/options.page b/doc/site/src/options.page index 02b3e2d..c53c12c 100644 --- a/doc/site/src/options.page +++ b/doc/site/src/options.page @@ -38,6 +38,8 @@ publisher: on_event: rss: file: + channel_link: + keep: on_event: campfire: url: diff --git a/lib/cerberus/config.example.yml b/lib/cerberus/config.example.yml index e0fa813..5ac9f7c 100644 --- a/lib/cerberus/config.example.yml +++ b/lib/cerberus/config.example.yml @@ -23,6 +23,8 @@ publisher: # url: http://someemail:password@cerberustool.campfirenow.com/room/51660 # rss: # file: /usr/www/rss.xml +# channel_link: http://example.com/rss.xml +# keep: 1 # extra_subject: "#deployment #tags" #builder: # rake: diff --git a/lib/cerberus/publisher/rss.rb b/lib/cerberus/publisher/rss.rb index 1cccb23..5de8655 100644 --- a/lib/cerberus/publisher/rss.rb +++ b/lib/cerberus/publisher/rss.rb @@ -1,29 +1,41 @@ require 'cerberus/publisher/base' require 'time' require 'builder' +require 'rss' class Cerberus::Publisher::RSS < Cerberus::Publisher::Base def self.publish(state, manager, options) config = options[:publisher, :rss] subject,body = Cerberus::Publisher::Base.formatted_message(state, manager, options) - pub_date = Time.now.iso8601 - description = "
#{body}
".to_xs - result = <<-END - - - Cerberus build feed for #{options[:application_name].to_xs} - #{pub_date} - http://rubyforge.org/projects/cerberus - - #{subject.to_xs} - #{pub_date} - #{description} - - - - END + pub_date = Time.now - IO.write(config[:file], result) + begin + feed = RSS::Parser.parse(File.read(config[:file]), false) + raise RSS::Error unless feed + keep = config[:keep] || 1 + feed.items.slice!(keep -1 ..-1) # one less than keep value, to make room for the new build + rescue RSS::Error, Errno::ENOENT + # if there's no existing file or we can't parse it, start a new one from scratch + feed = RSS::Maker.make("2.0") do |new_rss| + new_rss.channel.title = "#{options[:application_name].to_xs} build status" + new_rss.channel.description = "Cerberus build feed for #{options[:application_name].to_xs}" + new_rss.channel.generator = "http://rubyforge.org/projects/cerberus" + new_rss.channel.link = config[:channel_link] || "file://#{config[:file]}" + end + end + + # update channel link if we have it explicitly set, otherwise retain existing value + feed.channel.link = config[:channel_link] unless config[:channel_link].nil? + feed.channel.pubDate = pub_date + + new_item = RSS::Rss::Channel::Item.new() + new_item.title = subject + new_item.pubDate = pub_date + new_item.description = "
#{body}
" + + feed.items.unshift new_item + + IO.write(config[:file], feed) end end