Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render bugrefs as clickable links within labels #4998

Merged
merged 2 commits into from Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/OpenQA/Markdown.pm
@@ -1,11 +1,11 @@
# Copyright 2019 SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later
package OpenQA::Markdown;
use Mojo::Base -strict;
use Mojo::Base -strict, -signatures;

use Exporter 'import';
use Regexp::Common 'URI';
use OpenQA::Utils qw(BUGREF_REGEX LABEL_REGEX bugurl);
use OpenQA::Utils qw(BUGREF_REGEX UNCONSTRAINED_BUGREF_REGEX LABEL_REGEX bugurl);
use OpenQA::Constants qw(FRAGMENT_REGEX);
use CommonMark;

Expand All @@ -25,9 +25,17 @@ sub is_light_color {
return $sum > 380;
}

sub markdown_to_html {
my $text = shift;
sub _bugref_to_html ($bugref) {
my $bugurl = bugurl($bugref);
return "<a href=\"$bugurl\">$bugref</a>";
}

sub _label_to_html ($label_text) {
$label_text =~ s/${\UNCONSTRAINED_BUGREF_REGEX}/_bugref_to_html($+{match})/ge;
return "<span class=\"openqa-label\">label:$label_text<\/span>";
}

sub markdown_to_html ($text) {
$text = bugref_to_markdown($text);

# Turn all remaining URLs into links
Expand All @@ -39,16 +47,15 @@ sub markdown_to_html {
my $html = CommonMark->markdown_to_html($text);

# Make labels easy to highlight
$html =~ s/${\LABEL_REGEX}/<span class="openqa-label">label:$+{match}<\/span>/g;
$html =~ s/${\LABEL_REGEX}/_label_to_html($+{match})/ge;

# Custom markup "{{color:#ff0000|Some text}}"
$html =~ s/(\{\{([^|]+?)\|(.*?)\}\})/_custom($1, $2, $3)/ge;

return $html;
}

sub _custom {
my ($full, $rules, $text) = @_;
sub _custom ($full, $rules, $text) {
if ($rules =~ /^color:(#[a-fA-F0-9]{6})$/) {
my $color = $1;
my $bg = is_light_color($color) ? 'black' : 'white';
Expand Down
14 changes: 8 additions & 6 deletions lib/OpenQA/Utils.pm
Expand Up @@ -34,7 +34,7 @@ use Mojo::Util 'xml_escape';

my $FRAG_REGEX = FRAGMENT_REGEX;

my (%BUGREFS, %BUGURLS, $MARKER_REFS, $MARKER_URLS);
my (%BUGREFS, %BUGURLS, $MARKER_REFS, $MARKER_URLS, $BUGREF_REGEX);
BEGIN {
%BUGREFS = (
bnc => 'https://bugzilla.suse.com/show_bug.cgi?id=',
Expand Down Expand Up @@ -71,18 +71,20 @@ BEGIN {

$MARKER_REFS = join('|', keys %BUGREFS);
$MARKER_URLS = join('|', keys %BUGURLS);
}

# <marker>[#<project/repo>]#<id>
use constant BUGREF_REGEX =>
qr{(?:^|(?<=\s|,))(?<match>(?<marker>$MARKER_REFS)\#?(?<repo>[a-zA-Z/-]+)?\#(?<id>([A-Z]+-)?\d+))(?![\w\"])};
# <marker>[#<project/repo>]#<id>
$BUGREF_REGEX = qr{(?<match>(?<marker>$MARKER_REFS)\#?(?<repo>[a-zA-Z/-]+)?\#(?<id>([A-Z]+-)?\d+))};
}

use constant UNCONSTRAINED_BUGREF_REGEX => $BUGREF_REGEX;
use constant BUGREF_REGEX => qr{(?:^|(?<=\s|,))$BUGREF_REGEX(?![\w\"])};
use constant LABEL_REGEX => qr/\blabel:(?<match>([\w:#]+))\b/;

our $VERSION = sprintf "%d.%03d", q$Revision: 1.12 $ =~ /(\d+)/g;
our @EXPORT = qw(
LABEL_REGEX
UNCONSTRAINED_BUGREF_REGEX
BUGREF_REGEX
LABEL_REGEX
locate_needle
needledir
productdir
Expand Down
5 changes: 3 additions & 2 deletions t/16-markdown.t
Expand Up @@ -58,8 +58,9 @@ subtest 'bugrefs' => sub {
qq{<p>related issue: <a href="https://bugzilla.suse.com/show_bug.cgi?id=1234">bsc#1234</a>, yada yada</p>\n},
'bugref expanded';
is markdown_to_html('label:force_result:passed:bsc#1234'),
qq{<p><span class="openqa-label">label:force_result:passed:bsc#1234</span></p>\n},
'bugref not expanded because part of larger string';
qq{<p><span class="openqa-label">label:force_result:passed:}
. qq{<a href="https://bugzilla.suse.com/show_bug.cgi?id=1234">bsc#1234</a></span></p>\n},
'bugref expaned within label';
};

subtest 'openQA additions' => sub {
Expand Down