root/trunk/plagger/lib/Plagger/Plugin/CustomFeed/SVNLog.pm

Revision 443 (checked in by pia, 15 years ago)

Added config: link, fixed POD

Line 
1 package Plagger::Plugin::CustomFeed::SVNLog;
2
3 use strict;
4 use base qw( Plagger::Plugin );
5 use SVN::Core;
6 use SVN::Client;
7 use DateTime::Format::Strptime;
8
9 our $VERSION = '0.01';
10
11 our $error_msg;
12 our @data;
13 our $current_target;
14
15 $SVN::Error::handler = \&error_handler;
16
17 sub register {
18     my($self, $context) = @_;
19     $context->register_hook(
20         $self,
21         'subscription.load' => \&load,
22     );
23 }
24
25 sub load {
26     my($self, $context) = @_;
27
28     $self->{svnlog} = SVN::Client->new();
29
30     my $feed = Plagger::Feed->new;
31     $feed->aggregator(sub { $self->aggregate(@_) });
32     $context->subscription->add($feed);
33 }
34
35 sub aggregate {
36     my($self, $context, $args) = @_;
37
38     $error_msg = '';
39     @data = ();
40     $current_target = '';
41
42     if (ref($self->conf->{target}) ne 'ARRAY') {
43         $self->conf->{target} = [$self->conf->{target}];
44     }
45
46     my $feed = Plagger::Feed->new;
47     $feed->type('svnlog');
48     $feed->title($self->conf->{title} or 'SVN Log');
49     $feed->link($self->conf->{link} or $self->conf->{target}->[0]);
50
51     my $revision_from = $self->conf->{revision_from} || 1;
52     my $revision_to   = $self->conf->{revision_to}   || 'HEAD';
53
54     for my $target (@{ $self->conf->{target} }) {
55         $context->log(debug => 'Connecting repository ' . $target);
56
57         $current_target = $target;
58         $self->{svnlog}->log(
59             $target,
60             $revision_from,
61             $revision_to,
62             1,
63             1,
64             \&log_receiver
65         );
66
67         if ($error_msg) {
68             $context->log(error => $error_msg);
69             exit;
70         }
71     }
72
73     my $format = DateTime::Format::Strptime->new(pattern => '%Y-%m-%dT%H:%M:%S.%6NZ');
74
75     if ($self->conf->{reverse}) {
76         @data = sort { $b->{revision} <=> $a->{revision} } @data;
77     }
78     else {
79         @data = sort { $a->{revision} <=> $b->{revision} } @data;
80     }
81
82     my $items_count = 0;
83     for my $data (@data) {
84         last if $items_count++ >= $self->conf->{fetch_items};
85
86         my $entry = Plagger::Entry->new;
87         $entry->title('revision ' . $data->{revision});
88         $entry->link($data->{target});
89         $entry->author($data->{author});
90         $entry->date( Plagger::Date->parse($format, $data->{date}) );
91         $entry->body($data->{message});
92
93         $feed->add_entry($entry);
94     }
95     $context->update->add($feed);
96 }
97
98 sub log_receiver {
99     my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
100
101     $message =~ s/\x0D\x0A|\x0D|\x0A/<br>/g;
102     push(@data, {
103         target => $current_target,
104         changed_paths => [sort keys %$changed_paths],
105         revision => $revision,
106         author => $author,
107         date => $date,
108         message => $message,
109     });
110 }
111
112 sub error_handler {
113     if (ref($_[0]) and UNIVERSAL::isa($_[0], '_p_svn_error_t')) {
114         $error_msg = $_[0]->message;
115     }
116 }
117
118 1;
119 __END__
120
121 =head1 NAME
122
123 Plagger::Plugin::CustomFeed::SVNLog -  Custom feed for SVN Log
124
125 =head1 SYNOPSIS
126
127     - module: CustomFeed::SVNLog
128       config:
129         target: scheme://url/to/repository
130         title: SVN Log of blah blah blah
131         link: http://url/to/repository/viewer
132         revision_from: 5
133         revision_to: 123
134         reverse: 1
135         fetch_items: 20
136
137 =head1 DESCRIPTION
138
139 This plugin fetches log from svn repository and creates a custom feed.
140
141 =head1 CONFIGURATION
142
143 =over 4
144
145 =item target
146
147 Specifies the repository url.
148
149 =item title
150
151 Specifies the feed title you want. If not spacified, default is 'SVN Log'.
152
153 =item link
154
155 Specifies the repository viewer url.
156
157 =item revision_from
158
159 Specifies a revision number you wish to start publish from.
160 default is 1.
161
162 =item revision_to
163
164 Specifies a revision number you wish to end publish to.
165 default is 'HEAD'.
166
167 =item reverse
168
169 If set to 1, this option makes feed to reverse order.
170 default is 0.
171
172 =item fetch_items
173
174 Specifies a numeric value of limit to publish.
175 This functions well with reverse, revision_from, and the revision_to option.
176
177 =back
178
179 =head1 AUTHOR
180
181 Michiya Honda <pia@cpan.org>
182
183 =head1 SEE ALSO
184
185 L<Plagger>, L<SVN::Client>
186
187 =cut
Note: See TracBrowser for help on using the browser.