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

Add formatted bugref and carryover flag for comments #5357

Merged
merged 7 commits into from
Nov 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions assets/stylesheets/comments.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,44 @@
}
}

.openqa-label {
.openqa-bugref, .openqa-label, .openqa-flag {
position: relative;
background-color: #fdfd96;
color: #666666;
padding: .3em;
padding-left: 1.25em;
padding-left: .25em;
border-radius: .25rem;
white-space: nowrap;
}

.openqa-bugref {
background-color: #eee;
color: #666666;
border: 1px solid #ddd;
}

.openqa-label {
background-color: #fdfd96;
color: #666666;
border: 1px solid #ecec85;
}

.openqa-label::before {
.openqa-flag {
background-color: #fdd596;
color: #666666;
border: 1px solid #ecd785;
}
asdil12 marked this conversation as resolved.
Show resolved Hide resolved

.openqa-label, .openqa-flag {
padding-left: 1.25em;
}

.openqa-label::before, .openqa-flag::before {
display: inline-block;
font-style: normal;
font-variant: normal;
text-rendering: auto;
font-family: "ForkAwesome";
font-weight: 900;
font-size: .8em;
content: "\f02e";
position: absolute;
left: 0;
top: 50%;
Expand All @@ -75,6 +94,15 @@
padding-left: .25em;
}
asdil12 marked this conversation as resolved.
Show resolved Hide resolved

.openqa-label::before {
content: "\f02e";
}


.openqa-flag::before {
content: "\f024";
}

.well {
margin: 0px;
}
Expand Down
18 changes: 15 additions & 3 deletions docs/UsersGuide.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ image::images/review_badges.png[Review badges]

(To simplify, checking for false-negatives is not considered here.)

=== Bug references and labels
=== Bug references, labels and flags
==== Bug references
It is possible to reference a bug by writing `<bugtracker_shortname>#<bug_nr>`
in a comment, e.g. `bsc#1234`. It is also possible to spell out the full URL,
Expand All @@ -316,6 +316,8 @@ shortened automatically. A bug reference is rendered as link and a bug icon
is displayed for the job in various places as shown in the figure below.
A comment containing a bug reference will also be
<<UsersGuide.asciidoc#carry-over,carried over>> to reduce manual review work.
Refer to the Flags section below for other ways to trigger automated comment
carryover.

WARNING: If you want to reference a bug without making it count as a bug
reference you need to wrap it into a label (see subsequent section), e.g.
Expand Down Expand Up @@ -364,7 +366,15 @@ operator permissions.

NOTE: `force_result`-labels are evaluated when when a comment is
<<UsersGuide.asciidoc#carry-over,carried over>>. However, the carry over will
only happen when the comment *also* contains a bug reference.
only happen when the comment *also* contains a bug reference or `flag:carryover`.

==== Flags
Currently there is only one flag for job comments supported.

===== flag:carryover
Adding `flag:carryover` to a comment, will result in this comment being
<<UsersGuide.asciidoc#carry-over,carried over>> to a new job failing for
the same reason, without a bugref required.

=== Distinguish product and test issues bugref https://github.com/os-autoinst/openQA/pull/708[gh#708]

Expand Down Expand Up @@ -563,7 +573,9 @@ prefix the status with the build number.
Many test failures within the same scenario might be due to the same reason.
To avoid human reviewers having to add the same bug references again and
again, bug references are carried over from previous failures in the same
scenario if a job fails. This idea is inspired by the
scenario if a job fails. The same behaviour can be achieved by adding
`flag:carryover` to a comment.
This idea is inspired by the
https://wiki.jenkins-ci.org/display/JENKINS/Claim+plugin[Claim plugin] for
Jenkins.

Expand Down
44 changes: 28 additions & 16 deletions lib/OpenQA/Markdown.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@
# SPDX-License-Identifier: GPL-2.0-or-later
package OpenQA::Markdown;
use Mojo::Base -strict, -signatures;
use Mojo::Util 'xml_escape';
use OpenQA::App;

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

our @EXPORT_OK = qw(bugref_to_markdown is_light_color markdown_to_html);

sub bugref_to_markdown {
my $text = shift;
$text =~ s/${\BUGREF_REGEX}/"[$+{match}](" . bugurl($+{match}) . ')'/geio;
return $text;
}
our @EXPORT_OK = qw(bugref_to_html is_light_color markdown_to_html);

sub is_light_color {
my $color = shift;
Expand All @@ -25,30 +21,46 @@ sub is_light_color {
return $sum > 380;
}

sub _bugref_to_html ($bugref) {
my $bugurl = bugurl($bugref);
return "<a href=\"$bugurl\">$bugref</a>";
sub bugref_to_html ($bugref, $fancy = 0) {
my $app = OpenQA::App->singleton;
my $bugs = $app->schema->resultset('Bugs');
my $bug = $bugs->get_bug($bugref);
my $bugurl = $app->bugurl_for($bugref);
my $bugtitle = xml_escape($app->bugtitle_for($bugref, $bug));
my $bugicon = $app->bugicon_for($bugref, $bug);
return
qq{<span title="$bugtitle" class="openqa-bugref"><a href="$bugurl"><i class="test-label $bugicon"></i>&nbsp;$bugref</a></span>}
if ($fancy);
return qq{<a href="$bugurl" title="$bugtitle">$bugref</a>};
}

sub _label_to_html ($label_text) {
$label_text =~ s/${\UNCONSTRAINED_BUGREF_REGEX}/_bugref_to_html($+{match})/ge;
$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);
sub _flag_to_html ($flag_text) {
return "<span class=\"openqa-flag\">flag:$flag_text<\/span>";
}

# Turn all remaining URLs into links
sub markdown_to_html ($text) {
# Turn all URLs into links
$text =~ s/(?<!['"(<>])($RE{URI}${\FRAGMENT_REGEX})/<$1>/gio;

# Turn references to test modules and needling steps into links
$text =~ s!\b(t#([\w/]+))![$1](/tests/$2)!gi;

my $html = CommonMark->markdown_to_html($text);

# Make labels easy to highlight
# Turn all bugrefs into fancy bugref links
$html =~ s/${\BUGREF_REGEX}/bugref_to_html($+{match}, 1)/geio;

# Highlight labels
$html =~ s/${\LABEL_REGEX}/_label_to_html($+{match})/ge;

# Highlight flags (e.g. carryover)
$html =~ s/${\FLAG_REGEX}/_flag_to_html($+{match})/ge;

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

Expand Down
17 changes: 14 additions & 3 deletions lib/OpenQA/Schema/Result/Comments.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package OpenQA::Schema::Result::Comments;
use Mojo::Base 'DBIx::Class::Core', -signatures;

use OpenQA::Jobs::Constants;
use OpenQA::Utils qw(find_labels find_bugref find_bugrefs);
use OpenQA::Utils qw(find_labels find_flags find_bugref find_bugrefs);
use OpenQA::Markdown qw(markdown_to_html);
use List::Util qw(first);

Expand Down Expand Up @@ -107,6 +107,17 @@ sub label ($self) {
return find_labels($self->text)->[0];
}

=head2 text_flags

Returns flag values if C<$self> has flags, e.g. 'flag:carryover flag:foobar' returns a hashref with the keys 'carryover' and 'foobar'
=cut
sub text_flags ($self) {
my $flags = find_flags($self->text);
my %flag_hash;
@flag_hash{@$flags} = ();
return \%flag_hash;
}

=head2 force_result

Returns new result value if C<$self> is a special "force_result" label, e.g.
Expand Down Expand Up @@ -158,11 +169,11 @@ sub event_data ($self) {
return $data;
}

sub extended_hash ($self) {
sub extended_hash ($self, $render_markdown = 1) {
return {
id => $self->id,
text => $self->text,
renderedMarkdown => $self->rendered_markdown->to_string,
renderedMarkdown => ($render_markdown) ? $self->rendered_markdown->to_string : undef,
bugrefs => $self->bugrefs,
created => $self->t_created->strftime("%Y-%m-%d %H:%M:%S %z"),
updated => $self->t_updated->strftime("%Y-%m-%d %H:%M:%S %z"),
Expand Down
4 changes: 2 additions & 2 deletions lib/OpenQA/Schema/Result/Jobs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1713,10 +1713,10 @@ sub carry_over_bugrefs ($self) {

my $comments = $prev->comments->search({}, {order_by => {-desc => 'me.id'}});
for my $comment ($comments->all) {
next unless $comment->bugref;
next if !$comment->bugref && !exists($comment->text_flags->{carryover});
my $text = $comment->text;
my $prev_id = $prev->id;
$text .= "\n\n(Automatic takeover from t#$prev_id)" if $text !~ qr/Automatic takeover/;
$text .= "\n\n(Automatic carryover from t#$prev_id)" if $text !~ qr/Automatic (takeover|carryover)/;
$text .= "\n(The hook script will not be executed.)"
if $text !~ qr/The hook script will not be executed/ && defined $self->hook_script;
$text .= "\n" unless substr($text, -1, 1) eq "\n";
Expand Down
11 changes: 10 additions & 1 deletion lib/OpenQA/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ BEGIN {
}

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

use constant ONE_SECOND_IN_MICROSECONDS => 1_000_000;

Expand All @@ -90,6 +91,7 @@ our @EXPORT = qw(
UNCONSTRAINED_BUGREF_REGEX
BUGREF_REGEX
LABEL_REGEX
FLAG_REGEX
locate_needle
needledir
productdir
Expand All @@ -101,6 +103,7 @@ our @EXPORT = qw(
run_cmd_with_log_return_error
parse_assets_from_settings
find_labels
find_flags
find_bugref
find_bugrefs
bugurl
Expand Down Expand Up @@ -429,6 +432,12 @@ sub find_labels {
return \@labels;
}

sub find_flags ($text) {
my @flags;
push @flags, $+{match} while $text =~ /${\FLAG_REGEX}/g;
return \@flags;
}

sub find_bugref {
my ($text) = @_;
$text //= '';
Expand Down
8 changes: 6 additions & 2 deletions lib/OpenQA/WebAPI/Controller/API/V1/Comment.pm
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,21 @@ sub _comments ($self) {
=item list()

Returns a list of comments for a job or a job group given its id. For each comment the
list includes its bug references, date of creation, comment id, rendered markdown text,
list includes its bug references, date of creation, comment id,
text, date of update and the user name that created the comment.

Add the optional "render_markdown=1" parameter to include the rendered markdown text
for each comment.

=back

=cut

sub list ($self) {
my $comments = $self->_comments();
return unless $comments;
$self->render(json => [map { $_->extended_hash } $comments->all]);
my $render_markdown = $self->param('render_markdown') // 0;
$self->render(json => [map { $_->extended_hash($render_markdown) } $comments->all]);
asdil12 marked this conversation as resolved.
Show resolved Hide resolved
Martchus marked this conversation as resolved.
Show resolved Hide resolved
}


Expand Down
Loading
Loading