From 2dd27643d67794fe95f8c8c14186fccadaaf74ec Mon Sep 17 00:00:00 2001 From: Oliver Kurz Date: Sat, 29 Oct 2016 13:28:01 +0200 Subject: [PATCH 1/3] Add support for github 'bugrefs' Add support for generic github repo markers as 'bugrefs Using the generic profile '[#]#' 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. --- lib/OpenQA/Schema/Result/Comments.pm | 2 +- lib/OpenQA/Utils.pm | 26 +++++++++++++------ lib/OpenQA/WebAPI/Plugin/Helpers.pm | 9 +++---- t/16-utils.t | 39 ++++++++++++++++++++++++++++ t/ui/15-comments.t | 34 ++++++++++++++---------- 5 files changed, 81 insertions(+), 29 deletions(-) create mode 100644 t/16-utils.t diff --git a/lib/OpenQA/Schema/Result/Comments.pm b/lib/OpenQA/Schema/Result/Comments.pm index 85257bb4210..ac8c4e8717e 100644 --- a/lib/OpenQA/Schema/Result/Comments.pm +++ b/lib/OpenQA/Schema/Result/Comments.pm @@ -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/]+))}{$1}gi; + $text =~ s{\b(t#([\w/]+))}{$1}gi; $text =~ s{(http://\S*\.gif$)}{}gi; $self->SUPER::_DoAutoLinks($text); diff --git a/lib/OpenQA/Utils.pm b/lib/OpenQA/Utils.pm index ac16234adee..7f1d32793b8 100644 --- a/lib/OpenQA/Utils.pm +++ b/lib/OpenQA/Utils.pm @@ -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', + $bugrefs{gh} => '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/-]+}; + # [#]# + return qr{(?])(?(?$marker)\#?(?$repo_re)?\#(?\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}{$1}gi; + $text =~ s{$regex}{$+{match}}gi; return $text; } sub href_to_bugref { my ($text) = @_; - my $regex = join('|', keys %bugurls) =~ s/\?/\\\?/gr; - $regex = qr/(? is optional, e.g. for github. For github issues and pull are + # interchangeable, see comment in 'bugurl', too + $regex = qr{(?$regex)((?.*)/(issues|pull)/)?(?\d+)(?![\w])}; + $text =~ s{$regex}{@{[$bugurls{$+{url_root}} . ($+{repo} ? '#' . $+{repo} : '')]}#$+{id}}gi; return $text; } diff --git a/lib/OpenQA/WebAPI/Plugin/Helpers.pm b/lib/OpenQA/WebAPI/Plugin/Helpers.pm index e7386a56918..e1264bb7f0d 100644 --- a/lib/OpenQA/WebAPI/Plugin/Helpers.pm +++ b/lib/OpenQA/WebAPI/Plugin/Helpers.pm @@ -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( diff --git a/t/16-utils.t b/t/16-utils.t new file mode 100644 index 00000000000..29b4cfc0c21 --- /dev/null +++ b/t/16-utils.t @@ -0,0 +1,39 @@ +#!/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; + +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'), 'boo#9876'; +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'), 'gh#foo/bar#1234'; +like bugref_to_href('bsc#2345 poo#3456 and more'), qr{a href="https://bugzilla.suse.com/show_bug.cgi\?id=2345">bsc\#2345 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, 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 https://openqa.example.com/foo/bar: 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) - bsc#1000629 - bnc#1246' + bsc#1000630 + 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"); @@ -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"); @@ -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 { From b17c4230bac967bd4bb30299e33be2932274afde Mon Sep 17 00:00:00 2001 From: Oliver Kurz Date: Mon, 31 Oct 2016 21:10:06 +0100 Subject: [PATCH 2/3] t: Delete unused (disabled) 'use Test::Output' --- t/ui/12-needle-edit.t | 1 - 1 file changed, 1 deletion(-) diff --git a/t/ui/12-needle-edit.t b/t/ui/12-needle-edit.t index f1b549a08c8..a380434c53f 100644 --- a/t/ui/12-needle-edit.t +++ b/t/ui/12-needle-edit.t @@ -22,7 +22,6 @@ use Mojo::Base -strict; use Test::More; use Test::Mojo; use Test::Warnings ':all'; -#use Test::Output qw/stdout_like stderr_like/; use OpenQA::Test::Case; use File::Path qw/make_path remove_tree/; From 402ebd9b1be5d049117816fd783c06313ff58b9e Mon Sep 17 00:00:00 2001 From: Oliver Kurz Date: Mon, 31 Oct 2016 21:10:31 +0100 Subject: [PATCH 3/3] t: Ensure proper output catching --- t/16-utils-runcmd.t | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/t/16-utils-runcmd.t b/t/16-utils-runcmd.t index 3ecdf972fda..fd912c05e3c 100644 --- a/t/16-utils-runcmd.t +++ b/t/16-utils-runcmd.t @@ -41,15 +41,21 @@ my $res = run_cmd_with_log_return_error([qw/echo Hallo Welt/]); ok($res->{status}, 'status ok'); is($res->{stderr}, 'Hallo Welt', 'cmd output returned'); -$res = run_cmd_with_log_return_error([qw/false/]); +stderr_like { + $res = run_cmd_with_log_return_error([qw/false/]); +} +qr/.*\[ERROR\] cmd returned non-zero value/i; ok(!$res->{status}, 'status not ok (non-zero status returned)'); -$res = commit_git_return_error { - dir => '/some/dir', - cmd => 'status', - message => 'test', - user => $schema->resultset('Users')->first -}; +stderr_like { + $res = commit_git_return_error { + dir => '/some/dir', + cmd => 'status', + message => 'test', + user => $schema->resultset('Users')->first + }; +} +qr/fatal: Not a git repository.*\n.*cmd returned non-zero value/i; is($res, 'Unable to commit via Git: fatal: Not a git repository: \'/some/dir/.git\'', 'Git error message returned'); done_testing();