This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gitoriousparser.pm
298 lines (226 loc) · 8.12 KB
/
gitoriousparser.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package modules::local::gitoriousparser;
use strict;
use warnings;
use XML::Atom::Client;
use HTML::Entities;
use base 'modules::local::karmalog';
=head1 NAME
modules::local::gitoriousparser
=head1 DESCRIPTION
This module is responsible for parsing ATOM feeds generated by gitorious.org.
=cut
# When new feeds are configured, this number is incremented and added to the
# base timer interval in an attempt to stagger their occurance in time.
our $feed_number = 1;
# This is a map of $self objects. Because botnix does not use full class
# instances and instead calls package::name->function() to call methods, the
# actual OO storage for those modules ends up in here.
our %objects_by_package;
=head1 METHODS
=head2 fetch_feed
This is a pseudomethod called as a timer callback. It fetches the feed, parses
it into an XML::Atom::Feed object and passes that to process_feed().
This is the main entry point to this module. Botnix does not use full class
instances, instead it just calls by package name. This function maps from the
function name to a real $self object (stored in %objects_by_package).
=cut
sub fetch_feed {
my $pkg = shift;
my $self = $objects_by_package{$pkg};
my $atom = XML::Atom::Client->new();
my $feed = $atom->getFeed($$self{url});
$pkg->process_feed($feed);
}
=head2 process_feed
$self->process_feed($feed);
Enumerates the commits in the feed, emitting any events it hasn't seen before.
This subroutine manages a "seen" cache in $self, and will take care not to
announce any commit more than once.
The first time through, nothing is emitted. This is because we assume the bot
was just restarted ungracefully and the users have already seen all the old
events. So it just populates the seen-cache silently.
=cut
sub process_feed {
my ($pkg, $feed) = @_;
my $self = $objects_by_package{$pkg};
my @items = $feed->entries;
@items = sort { $a->updated cmp $b->updated } @items; # ascending order
my $newest = $items[-1];
my $latest = $newest->updated;
# skip the first run, to prevent new installs from flooding the channel
foreach my $item (@items) {
my @revs = $item->content->body =~ m|/commit/([a-z0-9]{40})|g;
for my $rev (reverse @revs) {
$self->output_item($item, $rev)
if !$$self{seen}{$rev}++ && $$self{not_first_time};
}
}
$$self{not_first_time} = 1;
}
=head2 longest_common_prefix
my $prefix = longest_common_prefix(@files);
Given a list of filenames, like ("src/ops/perl6.ops", "src/classes/IO.pir"),
returns the common prefix portion. For the example I just gave, the common
prefix would be "src/".
=cut
sub longest_common_prefix {
my $prefix = shift;
for (@_) {
chop $prefix while (! /^\Q$prefix\E/);
}
return $prefix;
}
=head2 try_link
modules::local::gitoriousparser->try_link(
$url,
['network', '#channel']
);
This is called by autofeed.pm. Given a gitorious.org URL, try to determine
the project name and canonical path. Then configure a feed reader for it if
one doesn't already exist.
The array reference containing network and channel are optional. If not
specified, magnet/#parrot is assumed. If the feed already exists but didn't
have the specified target, the existing feed is extended.
Currently supports the following URL format:
http://gitorious.org/projectname/
...with or without the "/" suffix.
=cut
sub try_link {
my ($pkg, $url, $target) = @_;
$target = ['magnet', '#parrot'] unless defined $target;
my $project;
if($url =~ m|http://gitorious.org/([^/]+)/?|) {
$project = $1;
} else {
# whatever it is, we can't handle it. Log and return.
main::lprint("gitorious try_link(): I can't handle $url");
return;
}
my $parsername = $project . "log";
my $modulename = "modules::local::" . $parsername;
$modulename =~ s/-/_/g;
if(exists($objects_by_package{$modulename})) {
# extend existing feed if necessary
my $self = $objects_by_package{$modulename};
my $already_have_target = 0;
foreach my $this (@{$$self{targets}}) {
$already_have_target++
if($$target[0] eq $$this[0] && $$target[1] eq $$this[1]);
}
push(@{$$self{targets}}, $target) unless $already_have_target;
return;
}
# create new feed
# url, feed_name, targets, objects_by_package
my $rss_link = "http://gitorious.org/$project.atom";
my $self = {
url => $rss_link,
feed_name => $project,
modulename => $modulename,
targets => [ $target ],
};
# create a dynamic subclass to get the timer callback back to us
eval "package $modulename; use base 'modules::local::gitoriousparser';";
$objects_by_package{$modulename} = bless($self, $modulename);
main::lprint("$parsername gitorious ATOM parser autoloaded.");
main::create_timer($parsername."_fetch_feed_timer", $modulename,
"fetch_feed", 300 + $feed_number++);
}
=head2 output_item
$self->output_item($item, $revision);
Takes an XML::Atom::Entry object, extracts the useful bits from it and calls
put() to emit the karma message.
The karma message is typically as follows:
feedname: $revision | username++ | :
feedname: One or more lines of commit log message
feedname: review: http://link/to/diff
=cut
sub output_item {
my ($self, $item, $rev) = @_;
my $link;
my $creator = $item->author;
if(defined($creator)) {
$creator = $creator->name;
} else {
$creator = 'unknown';
}
my $desc = $item->content;
if(defined($desc)) {
$desc = $desc->body;
} else {
$desc = '(no commit message)';
}
$desc = decode_entities($desc);
$desc =~ s,(<ul>),$1\n,g;
$desc =~ s,(</li>),$1\n,g;
my @lines = split "\n", $desc;
for my $line (@lines) {
my ($item) = $line =~ m,\s*<li>(.*)</li>$,;
next unless $item;
my ($name, $link)
= $item =~ m,^([^<]+)<a href="([^"]+)">[[:xdigit:]]{7}</a>:\s*[^<]+$,;
next unless $link;
my ($commit) = $link =~ m,/commit/([[:xdigit:]]{40}),;
next unless $commit && $commit eq $rev;
$link = "http://gitorious.org$link"
unless $link =~ m,^https?://,;
my $patch = $self->fetch_url("$link.patch");
my (@tmp, @log, @files, $this);
@tmp = split(/\n+/, $patch);
while(defined($this = shift(@tmp))) {
last if $this =~ /^Subject:/;
}
if(defined($this) && $this =~ /^Subject:\s*(?:\[PATCH\] ?)?(.+)/) {
push(@log, $1);
$this = shift(@tmp);
while(defined($this) && $this ne '---') {
$this =~ s/^\s+//;
push(@log, $this) if length $this;
$this = shift(@tmp);
}
}
$this = shift(@tmp);
while(defined($this)) {
if($this =~ /^\s+(\S+)\s+\|\s+\d+/) {
push(@files, $1);
} else {
last;
}
$this = shift(@tmp);
}
my $prefix = longest_common_prefix(@files);
$prefix //= '/';
$prefix =~ s|^/||; # cut off the leading slash
if(scalar @files > 1) {
$prefix .= " (" . scalar(@files) . " files)";
}
$commit = substr($commit, 0, 7);
$self->emit_karma_message(
feed => $$self{feed_name},
rev => $commit,
user => $creator,
log => \@log,
link => $link,
prefix => $prefix,
targets => $$self{targets},
);
main::lprint($$self{feed_name}.": output_item: output rev $commit");
}
}
=head2 implements
This is a pseudo-method called by botnix to determine which event callbacks
this module supports. It is only called when explicitly subclassed (rakudo
does this). Returns an empty array.
=cut
sub implements {
return qw();
}
=head2 get_self
This is a helper method used by the test suite to fetch a feed's local state.
It isn't used in production.
=cut
sub get_self {
my $pkg = shift;
return $objects_by_package{$pkg};
}
1;