Skip to content

Instantly share code, notes, and snippets.

@mizzy
Created January 21, 2010 12:18
Show Gist options
  • Save mizzy/282756 to your computer and use it in GitHub Desktop.
Save mizzy/282756 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use RPC::XML;
use RPC::XML::Client;
use Config::Pit;
use Data::ICal;
use Data::ICal::Entry::Event;
use DateTime::Format::ICal;
my $config = pit_get("trac_ticket_to_ical", require => {
username => "your username on trac",
password => "your password on trac",
});
my $ua = RPC::XML::Client->new('http://example.jp/trac/login/xmlrpc');
$ua->credentials('private area', $config->{username}, $config->{password});
my $res = $ua->send_request(
'ticket.milestone.getAll',
);
for my $milestone ( @{ $res->value } ) {
my $ical = Data::ICal->new;
$ical->add_properties( 'X-WR-CALNAME' => $milestone );
my $res = $ua->send_request(
'ticket.query',
RPC::XML::string->new("milestone=$milestone"),
);
for my $ticket_id ( @{ $res->value } ) {
my $res = $ua->send_request(
'ticket.get',
RPC::XML::int->new($ticket_id),
);
my $attr = $res->value->[3];
next if $attr->{due_close} eq 'YYYY/MM/DD' or !$attr->{due_close};
my $dt_start;
if ( $attr->{due_assign} eq 'YYYY/MM/DD' or !$attr->{due_assign} ) {
$dt_start = DateTime->now( time_zone => 'Asia/Tokyo' );
}
else {
my ( $year, $month, $day ) = split '/', $attr->{due_assign};
$dt_start = DateTime->new(
year => $year,
month => $month,
day => $day,
time_zone => 'Asia/Tokyo',
);
}
my ( $year, $month, $day ) = split '/', $attr->{due_close};
my $dt_end = DateTime->new(
year => $year,
month => $month,
day => $day,
time_zone => 'Asia/Tokyo',
);
$dt_start = [ DateTime::Format::ICal->format_datetime($dt_start), {} ];
$dt_end = [ DateTime::Format::ICal->format_datetime($dt_end), {} ];
for my $dt ( $dt_start, $dt_end ) {
$dt->[0] =~ s/^TZID=(.*?)://
and $dt->[1]->{TZID} = $1;
$dt->[1]->{VALUE} = 'DATE';
}
my $event = Data::ICal::Entry::Event->new;
$event->add_properties(
summary => "$attr->{summary}($attr->{owner})",
description => $attr->{description},
dtstart => $dt_start,
dtend => $dt_end,
);
$ical->add_entry($event);
}
my $data = $ical->as_string;
utf8::decode($data) unless utf8::is_utf8($data);
open my $out, '>:utf8', "$milestone.ics" or die $!;
print $out $ical->as_string;
close $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment