I’ve been meaning to write a Bugzilla extension to turn mentions of commits in bug messages into a link to view the commit via our web-based SVN viewer for ages – this morning I finally found the time to do it.
I needed to use the bug_format_comment hook to format comments as they’re being displayed, turning mentions of commits (e.g. “Commit 123” or “r123”) into links.
The code was pretty simple:
The following is in /var/lib/bugzilla3/extensions/LinkCommits/Extension.pm:
package Bugzilla::Extension::LinkCommits;
use strict;
use base qw(Bugzilla::Extension);
use constant NAME => 'LinkCommits';
our $VERSION = '0.1';
sub bug_format_comment {
my ($self, $args) = @_;
my $regexes = $args->{'regexes'};
my $commit_match = qr/\b( (?:Commit|r) \s? (\d+) )/x;
push(@$regexes, { match => $commit_match, replace => \&_link_commit });
}
sub _link_commit {
my $args = shift;
my $text = $args->{matches}->[0];
my $commitnum = $args->{matches}->[1];
my $url = "http://warehouse.uk2.net/repolist/?diff=uk2&rev="
. join ':', $commitnum - 1, $commitnum;
return qq{$text};
};
# This must be the last line of your extension.
__PACKAGE__->NAME;
And that is about it. Hopefully this might be of use to someone. Bugzilla comes with a comprehensive Example extension which illustrates this along with various other hooks, but I suspect I’m not the only one looking to create a simple plugin to make tweaks to bug comments when displaying (like auto-linking to commits), so this might help someone.
If you liked that post, then try these...
Welcoming ambs to the Dancer core dev team
But, how do you call the hook from the template. I mean, what will be the regex ?
Not sure I understand what you’re asking here. You say e.g. “Commit 42” in a bug message, and that will get turned into a link to that commit. Am I misunderstanding you?