Skip to content

Commit

Permalink
Add support for github 'bugrefs'
Browse files Browse the repository at this point in the history
Add support for generic github repo markers as 'bugrefs
Using the generic profile
'<marker>[#<project/repo>]#<id>'
for all bugrefs, especially for github, allows to
track all issues reported on github and especially
test fixing pull requests to be recorded as 'bugrefs'
with the same icon as for "test issues", i.e. poo#.
This way there is no need to create a ticket just
to track a test issue for which already a github
pull request exist because otherwise the URL to the
github PR would not mean anything special to github
and therefore not mark a job as "labeled".

Also, pasting full github URLs is supported, same as
for all other bugrefs, meaning that the URL is
abbreviated into short form bugrefs.

The pattern follows the openSUSE/OBS recommendation.
  • Loading branch information
okurz committed Oct 29, 2016
1 parent 4afb9ec commit a705315
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/OpenQA/Schema/Result/Comments.pm
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ sub _DoAutoLinks {
$text =~ s@(?<!['"(<>])($RE{URI})@<$1>@gi;

# For tests make sure that references into test modules and needling steps also work
$text =~ s{(t#([\w/]+))}{<a href="/tests/$2">$1</a>}gi;
$text =~ s{\b(t#([\w/]+))}{<a href="/tests/$2">$1</a>}gi;

$text =~ s{(http://\S*\.gif$)}{<img src="$1"/>}gi;
$self->SUPER::_DoAutoLinks($text);
Expand Down
26 changes: 18 additions & 8 deletions lib/OpenQA/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -348,43 +348,53 @@ my %bugrefs = (
bsc => 'https://bugzilla.suse.com/show_bug.cgi?id=',
boo => 'https://bugzilla.opensuse.org/show_bug.cgi?id=',
poo => 'https://progress.opensuse.org/issues/',
gh => 'https://github.com/',
);
my %bugurls = (
'https://bugzilla.novell.com/show_bug.cgi?id=' => 'bsc',
$bugrefs{bsc} => 'bsc',
$bugrefs{boo} => 'boo',
$bugrefs{poo} => 'poo',
'https://github.com/' => 'gh',
);

sub bugref_regex {
my $regex = join('|', keys %bugrefs);
return qr/(?<![\(\["\>])(($regex)#(\d+))(?![\w])/;
my $marker = join('|', keys %bugrefs);
my $repo_re = qr{[a-zA-Z/-]+};
# <marker>[#<project/repo>]#<id>
return qr{(?<![\(\["\>])(?<match>(?<marker>$marker)\#?(?<repo>$repo_re)?\#(?<id>\d+))(?![\w])};
}

sub find_bugref {
my ($text) = @_;
$text =~ bugref_regex;
return $1;
return $+{match};
}

sub bugurl {
my ($bugref) = @_;
return $bugrefs{$bugref};
# in github '/pull/' and '/issues/' are interchangeable, e.g.
# calling https://github.com/os-autoinst/openQA/issues/966 will yield the
# same page as https://github.com/os-autoinst/openQA/pull/966 and vice
# versa for both an issue as well as pull request
$bugref =~ bugref_regex;
return $bugrefs{$+{marker}} . ($+{repo} ? "$+{repo}/issues/" : '') . $+{id};
}

sub bugref_to_href {
my ($text) = @_;
my $regex = bugref_regex;
$text =~ s{$regex}{<a href="@{[$bugrefs{$2}]}$3">$1</a>}gi;
$text =~ s{$regex}{<a href="@{[bugurl($+{match})]}">$+{match}</a>}gi;
return $text;
}

sub href_to_bugref {
my ($text) = @_;

my $regex = join('|', keys %bugurls) =~ s/\?/\\\?/gr;
$regex = qr/(?<!["\(\[])($regex)(\d+)(?![\w])/;
$text =~ s{$regex}{@{[$bugurls{$1}]}#$2}gi;
# <repo> is optional, e.g. for github. For github issues and pull are
# interchangeable, see comment in 'bugurl', too
$regex = qr{(?<!["\(\[])(?<url_root>$regex)((?<repo>.*)/(issues|pull)/)?(?<id>\d+)(?![\w])};
$text =~ s{$regex}{@{[$bugurls{$+{url_root}} . ($+{repo} ? '#' . $+{repo} : '')]}#$+{id}}gi;
return $text;
}

Expand Down
9 changes: 3 additions & 6 deletions lib/OpenQA/WebAPI/Plugin/Helpers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,14 @@ sub register {

$app->helper(
bugurl_for => sub {
my ($c, $text) = @_;
if ($text =~ /(.*)#(.*)/) {
return bugurl($1) . $2;
}
return;
my ($c, $bugref) = @_;
return bugurl($bugref);
});

$app->helper(
bugicon_for => sub {
my ($c, $text) = @_;
return ($text =~ /poo#/) ? 'label_bug fa fa-bolt' : 'label_bug fa fa-bug';
return ($text =~ /(poo|gh)#/) ? 'label_bug fa fa-bolt' : 'label_bug fa fa-bug';
});

$app->helper(
Expand Down
42 changes: 42 additions & 0 deletions t/16-utils.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env perl -w

# Copyright (C) 2016 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

BEGIN {
unshift @INC, 'lib';
}

use strict;
use OpenQA::Utils;
use Test::More;
use Test::Mojo;
use Test::Warnings;
use Test::Output qw/stderr_like/;

is bugurl('bsc#1234'), 'https://bugzilla.suse.com/show_bug.cgi?id=1234', 'bug url is properly expanded';
ok find_bugref('gh#os-autoinst/openQA#1234'), 'github bugref is recognized';
is bugurl('gh#os-autoinst/openQA#1234'), 'https://github.com/os-autoinst/openQA/issues/1234';
is bugurl('poo#1234'), 'https://progress.opensuse.org/issues/1234';
is href_to_bugref('https://progress.opensuse.org/issues/1234'), 'poo#1234';
is bugref_to_href('boo#9876'), '<a href="https://bugzilla.opensuse.org/show_bug.cgi?id=9876">boo#9876</a>';
is href_to_bugref('https://github.com/foo/bar/issues/1234'), 'gh#foo/bar#1234';
is href_to_bugref('https://github.com/os-autoinst/os-autoinst/pull/960'), 'gh#os-autoinst/os-autoinst#960', 'github pull are also transformed same as issues';
is bugref_to_href('gh#foo/bar#1234'), '<a href="https://github.com/foo/bar/issues/1234">gh#foo/bar#1234</a>';
like bugref_to_href('bsc#2345 poo#3456 and more'), qr{a href="https://bugzilla.suse.com/show_bug.cgi\?id=2345">bsc\#2345</a> <a href=.*3456.*> and more}, 'bugrefs in text get replaced';
like bugref_to_href('boo#2345,poo#3456'), qr{a href="https://bugzilla.opensuse.org/show_bug.cgi\?id=2345">boo\#2345</a>,<a href=.*3456.*}, 'interpunctation is not consumed by href';

done_testing();
34 changes: 20 additions & 14 deletions t/ui/15-comments.t
Original file line number Diff line number Diff line change
Expand Up @@ -170,27 +170,29 @@ subtest 'URL auto-replace' => sub {
$driver->find_element('#text', 'css')->send_keys('
foo@bar foo#bar should not be detected as bugref
bsc#2436346bla should not be detected, too
bsc#2436346bla2
bsc#2436347bla2
<a href="https://openqa.example.com/foo/bar">https://openqa.example.com/foo/bar</a>: http://localhost:9562
https://openqa.example.com/tests/181148 (reference http://localhost/foo/bar )
bsc#1234 boo#2345,poo#3456 t#4567
t#5678/modules/welcome/steps/1
https://progress.opensuse.org/issues/6789
https://bugzilla.novell.com/show_bug.cgi?id=1234
https://bugzilla.novell.com/show_bug.cgi?id=7890
[bsc#1000629](https://bugzilla.suse.com/show_bug.cgi?id=1000629)
<a href="https://bugzilla.suse.com/show_bug.cgi?id=1000629">bsc#1000629</a>
bnc#1246'
<a href="https://bugzilla.suse.com/show_bug.cgi?id=1000630">bsc#1000630</a>
bnc#1246
gh#os-autoinst/openQA#1234
https://github.com/os-autoinst/os-autoinst/pull/960'
);
$driver->find_element('#submitComment', 'css')->click();
t::ui::PhantomTest::wait_for_ajax;

# the first made comment needs to be 2nd now
my @comments = $driver->find_elements('div.media-comment p', 'css');
is($comments[1]->get_text(), $test_message, "body of first comment after adding another");
#is($comments[0]->get_text(), $test_message, "body of first comment after adding another");

like($comments[0]->get_text(), qr/bsc#1234 boo#2345,poo#3456 t#4567 .*poo#6789 bsc#1234 bsc#1000629 bsc#1000629/);
like($comments[0]->get_text(), qr/bsc#1234 boo#2345,poo#3456 t#4567 .*poo#6789 bsc#7890 bsc#1000629 bsc#1000630/);
my @urls = $driver->find_elements('div.media-comment a', 'css');
is(scalar @urls, 14);
is(scalar @urls, 16);
is((shift @urls)->get_text(), 'https://openqa.example.com/foo/bar', "url1");
is((shift @urls)->get_text(), 'http://localhost:9562', "url2");
is((shift @urls)->get_text(), 'https://openqa.example.com/tests/181148', "url3");
Expand All @@ -201,10 +203,12 @@ subtest 'URL auto-replace' => sub {
is((shift @urls)->get_text(), 't#4567', "url8");
is((shift @urls)->get_text(), 't#5678/modules/welcome/steps/1', "url9");
is((shift @urls)->get_text(), 'poo#6789', "url10");
is((shift @urls)->get_text(), 'bsc#1234', "url11");
is((shift @urls)->get_text(), 'bsc#7890', "url11");
is((shift @urls)->get_text(), 'bsc#1000629', "url12");
is((shift @urls)->get_text(), 'bsc#1000629', "url13");
is((shift @urls)->get_text(), 'bsc#1000630', "url13");
is((shift @urls)->get_text(), 'bnc#1246', "url14");
is((shift @urls)->get_text(), 'gh#os-autoinst/openQA#1234', "url15");
is((shift @urls)->get_text(), 'gh#os-autoinst/os-autoinst#960', "url16");

my @urls2 = $driver->find_elements('div.media-comment a', 'css');
is((shift @urls2)->get_attribute('href'), 'https://openqa.example.com/foo/bar', "url1-href");
Expand All @@ -216,11 +220,13 @@ subtest 'URL auto-replace' => sub {
is((shift @urls2)->get_attribute('href'), 'https://progress.opensuse.org/issues/3456', "url7-href");
like((shift @urls2)->get_attribute('href'), qr{/tests/4567}, "url8-href");
like((shift @urls2)->get_attribute('href'), qr{/tests/5678/modules/welcome/steps}, "url9-href");
is((shift @urls2)->get_attribute('href'), 'https://progress.opensuse.org/issues/6789', "url10-href");
is((shift @urls2)->get_attribute('href'), 'https://bugzilla.suse.com/show_bug.cgi?id=1234', "url11-href");
is((shift @urls2)->get_attribute('href'), 'https://bugzilla.suse.com/show_bug.cgi?id=1000629', "url12-href");
is((shift @urls2)->get_attribute('href'), 'https://bugzilla.suse.com/show_bug.cgi?id=1000629', "url13-href");
is((shift @urls2)->get_attribute('href'), 'https://bugzilla.suse.com/show_bug.cgi?id=1246', "url14-href");
is((shift @urls2)->get_attribute('href'), 'https://progress.opensuse.org/issues/6789', "url10-href");
is((shift @urls2)->get_attribute('href'), 'https://bugzilla.suse.com/show_bug.cgi?id=7890', "url11-href");
is((shift @urls2)->get_attribute('href'), 'https://bugzilla.suse.com/show_bug.cgi?id=1000629', "url12-href");
is((shift @urls2)->get_attribute('href'), 'https://bugzilla.suse.com/show_bug.cgi?id=1000630', "url13-href");
is((shift @urls2)->get_attribute('href'), 'https://bugzilla.suse.com/show_bug.cgi?id=1246', "url14-href");
is((shift @urls2)->get_attribute('href'), 'https://github.com/os-autoinst/openQA/issues/1234', "url15-href");
is((shift @urls2)->get_attribute('href'), 'https://github.com/os-autoinst/os-autoinst/issues/960', "url16-href");
};

subtest 'commenting in test results including labels' => sub {
Expand Down

0 comments on commit a705315

Please sign in to comment.