複数のTracのチケットを一度に見たい!

Trac 好きの私は数えてみたら、5 カ所の Trac を使っていました。 Tracが分散していると ついついタスク(チケット)を忘れてしまいます ^^);
そこで、複数Tracのチケットを一度に見られる CGI を作ってみました。


画面のイメージ

TracRSSで情報が取得できるので Plagger 等を使うのもよいかもしれませんが、Tracのチケット表示画面の表を切り出してきて並べるだけなら簡単そうなので、ついつい作ってしまいました。

コード
#!/usr/local/bin/ruby
#
#  複数のTracのチケットを表示するCGI
#
require 'yaml'
require 'uri'
require 'net/http'

def load_config(conf_path)
  YAML.load_file conf_path
end

def get_ticket_list(conf)
  Net::HTTP.version_1_2
  host, port, path = URI.split(conf['url']).values_at(2,3,5)
  Net::HTTP.start(host, port ? port : 80) {|http|
    #$stderr.puts "GET #{host} #{port.to_s} #{path} (#{conf['login']})" 
    req = Net::HTTP::Get.new(path)
    req.basic_auth(conf['login'], conf['password'])   if (conf['login'])
    response = http.request(req)
    return response.body
  }
end

def get_table_body(html)
  html =~ /<tbody>(.*)<\/tbody>/m
  $1.to_s.gsub(/(\n *)+/, "\n")
end

def add_title(title, body)
  %Q!<tr><td colspan="9" style="">#{title}</td></tr>\n! + body
end

def adjust_href_url(url, html)
  url =~ /(https?:\/\/.*?)\//
  http_server = $1
  html.gsub(/href=\"\//, "href=\"#{http_server}/")
end

def put_http_header
  puts "Content-type: text/html; charset=UTF-8"
  puts ""
end

def put_page_header
  puts <<HEADER
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>{1} 未解決チケット </title>
  <link rel="stylesheet" href="/css/trac.css" type="text/css" />
  <link rel="stylesheet" href="/css/report.css" type="text/css" />
</head>
<body>

  <table class="listing tickets">
    <thead>
    <tr>
      <th>Ticket</th> <th>Summary</th> <th>Component</th> <th>Version</th> <th>Milestone</th> <th>Type</th> <th>Owner</th> <th>Created</th>
    </tr>
    </thead>
    <tbody>
HEADER
end

def put_page_footer
  puts <<FOOTER
    </tbody>
  </table>
</body>
</html>
FOOTER
end


configs = load_config 'tracs.yaml'
table_bodys = configs.map {|conf|
  add_title(conf['title'], 
            get_table_body(adjust_href_url(conf['url'], get_ticket_list(conf)))) }

put_http_header
put_page_header
table_bodys.each {|t| puts t}
put_page_footer
設定ファイル
- title: Y社 Mプロジェクト
  url: http://XXXX.YYYY.ZZZZj:4443/trac/PPPPPPP/report/1
  login: FOO
  password: BAR
- title: Award on Rails
  url: http://UUUU.VVVV.ZZZZ/trac/tsuuji/report/1
  login: FOO
  password: BAR
- title: EY-Office社内
  url: http://GoGoGoGo/trac/EY/report/1


注意: cssTracの code.css, report.css, trac.css を使っています。パス等は適当に調整して下さい。