Skip to content

Commit

Permalink
Make most methods that access tickets only work.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Dec 3, 2010
1 parent 9079b95 commit bf5d809
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 171 deletions.
1 change: 1 addition & 0 deletions INSTALL
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,4 @@
* git update --init * git update --init
* sudo gem19 install sequel sqlite3-ruby
* ruby19 ./bin/init * ruby19 ./bin/init
* ruby19 ./bin/update-active-tickets * ruby19 ./bin/update-active-tickets
71 changes: 71 additions & 0 deletions lib/macruby_bugmash_bot/ticket.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,71 @@
# encoding: UTF-8

$:.unshift File.expand_path("../../../vendor/simple-rss/lib", __FILE__)
require "simple-rss"
require "net/http"
require "cgi"

require "rubygems"
require "sequel"

class Ticket
def self.db_path
db = File.expand_path('../../data/db.sqlite3', __FILE__)
end

def self.connection
@db ||= Sequel.sqlite(db_path)
end

def self.create!
puts "CREATE #{db_path}"
connection.create_table :tickets do
Integer :id, :primary_key => true
String :link
String :summary, :text => true
String :assigned_to
FalseClass :marked_for_review
FalseClass :closed
end
end

def self.table
connection[:tickets]
end

OPEN_TICKETS_RSS_FEED = URI.parse("http://www.macruby.org/trac/query?status=new&status=reopened&format=rss&col=id&col=summary&col=status&col=time&order=priority&max=1000")

def self.raw_open_tickets_feed
Net::HTTP.get(ACTIVE_TICKETS_RSS_FEED)
rescue Exception => e
# obviously this is a bad thing to do, but I really don't want the bot to break this weekend due to HTTP problems...
puts "[!] FETCHING THE MACRUBY TICKET FEED FAILED DUE TO: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
return nil
end

def self.update_open_tickets!
if raw_feed = raw_open_tickets_feed
# should not be too bad to force a ASCII string to UTF-8 afaik
raw_feed.force_encoding('UTF-8') if raw_feed.respond_to?(:force_encoding)

rss = SimpleRSS.parse(raw_feed)
open_ids = Ticket.table.filter(:closed => false).select(:id).all.map(&:id)
seen = []

rss.entries.each do |entry|
id = File.basename(entry[:link]).to_i
seen << id
unless Ticket.table.filter(:id => id).first
Ticket.table.insert(:id => id, :link => entry[:link], :summary => CGI.unescapeHTML(entry[:title]), :marked_for_review => false, :closed => false)
end
end

closed = open_ids - seen
#p closed

closed.each do |id|
Ticket.table.filter(:id => id).update(:closed => true)
end
end
end
end
66 changes: 27 additions & 39 deletions lib/macruby_bugmash_bot/trac.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,21 +1,8 @@
# encoding: UTF-8 # encoding: UTF-8


$:.unshift File.expand_path("../../../vendor/simple-rss/lib", __FILE__) require "macruby_bugmash_bot/ticket"
require "simple-rss"
require "net/http"
require "cgi"


class Trac class Trac
ACTIVE_TICKETS_RSS_FEED = URI.parse("http://www.macruby.org/trac/query?status=new&status=reopened&format=rss&col=id&col=summary&col=status&col=time&order=priority&max=1000")

def self.raw_active_tickets_feed
Net::HTTP.get(ACTIVE_TICKETS_RSS_FEED)
rescue Exception => e
# obviously this is a bad thing to do, but I really don't want the bot to break this weekend due to HTTP problems...
puts "[!] FETCHING THE MACRUBY TICKET FEED FAILED DUE TO: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
return nil
end

# Defines an instance method that takes: ID, ticket, user # Defines an instance method that takes: ID, ticket, user
# This method, however, is wrapped inside a method that first checks if the # This method, however, is wrapped inside a method that first checks if the
# ticket is available at all. # ticket is available at all.
Expand All @@ -32,33 +19,34 @@ def self.define_ticket_method(name, &block)
end end
end end


attr_reader :active_tickets, :users #attr_reader :active_tickets, :users
attr_reader :users


def initialize def initialize
@active_tickets = {} #@active_tickets = {}
@users = {} @users = {}
load_tickets! #load_tickets!
end end


def load_tickets! #def load_tickets!
if raw_feed = self.class.raw_active_tickets_feed #if raw_feed = self.class.raw_active_tickets_feed
# should not be too bad to force a ASCII string to UTF-8 afaik ## should not be too bad to force a ASCII string to UTF-8 afaik
raw_feed.force_encoding('UTF-8') if raw_feed.respond_to?(:force_encoding) #raw_feed.force_encoding('UTF-8') if raw_feed.respond_to?(:force_encoding)
rss = SimpleRSS.parse(raw_feed) #rss = SimpleRSS.parse(raw_feed)
@active_tickets = rss.entries.inject({}) do |h, entry| #@active_tickets = rss.entries.inject({}) do |h, entry|
id = File.basename(entry[:link]).to_i #id = File.basename(entry[:link]).to_i
h[id] = { :id => id, :link => entry[:link], :summary => CGI.unescapeHTML(entry[:title]) } #h[id] = { :id => id, :link => entry[:link], :summary => CGI.unescapeHTML(entry[:title]) }
h #h
end #end
# clean assigned/marked tickets ## clean assigned/marked tickets
@users.each do |name, tickets| #@users.each do |name, tickets|
tickets.reject! { |t| @active_tickets[t[:id]].nil? } #tickets.reject! { |t| @active_tickets[t[:id]].nil? }
end #end
end #end
end #end


def ticket(id) def ticket(id)
@active_tickets[id.to_i] Ticket.table.filter(:id => id).first
end end


def ticket_message(id) def ticket_message(id)
Expand Down Expand Up @@ -115,9 +103,9 @@ def open_ticket
"Ticket ##{id} can't be assigned to `#{user}', as it is already assigned to `#{assigned_to}'." "Ticket ##{id} can't be assigned to `#{user}', as it is already assigned to `#{assigned_to}'."
end end
else else
ticket[:assigned_to] = user Ticket.table.filter(:id => id).update(:assigned_to => user)
@users[user] ||= [] @users[user] ||= []
@users[user] << ticket @users[user] << ticket(id)
@users[user] = @users[user].sort_by { |x| x[:id] } @users[user] = @users[user].sort_by { |x| x[:id] }
"Ticket ##{id} is now assigned to `#{user}'." "Ticket ##{id} is now assigned to `#{user}'."
end end
Expand All @@ -126,7 +114,7 @@ def open_ticket
define_ticket_method :resign_from_ticket do |id, ticket, user| define_ticket_method :resign_from_ticket do |id, ticket, user|
if assigned_to = ticket[:assigned_to] if assigned_to = ticket[:assigned_to]
if assigned_to == user if assigned_to == user
ticket[:assigned_to] = nil Ticket.table.filter(:id => id).update(:assigned_to => nil)
"Ticket ##{id} was resigned by `#{user}'." "Ticket ##{id} was resigned by `#{user}'."
else else
"Ticket #19 can't be unassigned by `#{user}', as it is assigned to `#{assigned_to}'." "Ticket #19 can't be unassigned by `#{user}', as it is assigned to `#{assigned_to}'."
Expand All @@ -147,7 +135,7 @@ def open_ticket
define_ticket_method :mark_for_review do |id, ticket, user| define_ticket_method :mark_for_review do |id, ticket, user|
assigned_to = ticket[:assigned_to] assigned_to = ticket[:assigned_to]
if assigned_to == user if assigned_to == user
ticket[:marked_for_review] = true Ticket.table.filter(:id => id).update(:marked_for_review => true)
"Ticket ##{id} is marked for review by `#{assigned_to}'." "Ticket ##{id} is marked for review by `#{assigned_to}'."
elsif !assigned_to.nil? elsif !assigned_to.nil?
"Ticket ##{id} can't be marked for review by `#{user}', as it is assigned to `#{assigned_to}'." "Ticket ##{id} can't be marked for review by `#{user}', as it is assigned to `#{assigned_to}'."
Expand All @@ -160,7 +148,7 @@ def open_ticket
if ticket[:marked_for_review] if ticket[:marked_for_review]
assigned_to = ticket[:assigned_to] assigned_to = ticket[:assigned_to]
if assigned_to == user if assigned_to == user
ticket[:marked_for_review] = nil Ticket.table.filter(:id => id).update(:marked_for_review => false)
"Ticket ##{id} is unmarked for review by `#{assigned_to}'." "Ticket ##{id} is unmarked for review by `#{assigned_to}'."
else else
"Ticket ##{id} can't be unmarked for review by `#{user}', as it is assigned to `#{assigned_to}'." "Ticket ##{id} can't be unmarked for review by `#{user}', as it is assigned to `#{assigned_to}'."
Expand Down
Loading

0 comments on commit bf5d809

Please sign in to comment.