Repository navigation
Bug 2086124 - [HACKBOT] Structured metadata on comments #2819
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5305100
Bug 2086124 - [HACKBOT] Structured metadata on comments
kyoshino 05a5f15
Bug 2086124 - Fix Perl::Critic violations in the Hackbot extension
kyoshino 88e1652
Bug 2086124 - Read Hackbot metadata only from plain comments
kyoshino d63923d
[skip ci] Fix icon vertical alignment
kyoshino 820e1f1
Merge branch 'master' into 2086124
dklawren 66b3a9a
Merge branch 'master' into 2086124
dklawren 605a83c
Bug 2086124 - Render the inline edit response the way the bug page does
kyoshino 892758f
[skip ci] Match hackbot.css break point with bug_modal.css
kyoshino c4d316f
[skip ci] Restore gap between comment action buttons
kyoshino 20df447
[skip ci] Document that comment text can hide part of the stored text
kyoshino 9a20c68
Merge branch 'master' into 2086124
dklawren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| # | ||
| # This Source Code Form is "Incompatible With Secondary Licenses", as | ||
| # defined by the Mozilla Public License, v. 2.0. | ||
|
|
||
| package Bugzilla::Extension::Hackbot; | ||
|
|
||
| use 5.10.1; | ||
| use strict; | ||
| use warnings; | ||
|
|
||
| use constant NAME => 'Hackbot'; | ||
|
|
||
| use constant REQUIRED_MODULES => []; | ||
|
|
||
| use constant OPTIONAL_MODULES => []; | ||
|
|
||
| __PACKAGE__->NAME; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| # | ||
| # This Source Code Form is "Incompatible With Secondary Licenses", as | ||
| # defined by the Mozilla Public License, v. 2.0. | ||
|
|
||
| package Bugzilla::Extension::Hackbot; | ||
|
|
||
| use 5.10.1; | ||
| use strict; | ||
| use warnings; | ||
|
|
||
| use base qw(Bugzilla::Extension); | ||
|
|
||
| use Bugzilla::Extension::Hackbot::Metadata qw(parse_front_matter); | ||
| use Bugzilla::User; | ||
|
|
||
| our $VERSION = '1'; | ||
|
|
||
| BEGIN { | ||
| *Bugzilla::Comment::hackbot = \&_comment_hackbot; | ||
| } | ||
|
|
||
| # The ID of the account named by the hackbot_user param, or 0 when it is unset | ||
| # or names nobody. Bugzilla::User::login_to_id doesn't cache a miss, and this | ||
| # runs for every comment that is read. | ||
| sub _hackbot_user_id { | ||
| my $login = Bugzilla->params->{hackbot_user} or return 0; | ||
| my $cache = Bugzilla->request_cache->{hackbot_user_ids} ||= {}; | ||
| return $cache->{$login} //= Bugzilla::User::login_to_id($login) || 0; | ||
| } | ||
|
|
||
| # Anyone can type a front matter block into a comment, so only the Hackbot | ||
| # account's comments are parsed, and only plain ones: the text of other types | ||
| # (attachments, duplicates) is formatted without the comment_body_full hook, so | ||
| # their block would stay visible. Returns the same list as parse_front_matter. | ||
| # A page reads each comment several times (header, badge, tags, text), so the | ||
| # result is kept on the comment for as long as its text is unchanged. | ||
| sub _parse_comment { | ||
| my ($comment) = @_; | ||
| my $user_id = _hackbot_user_id() or return; | ||
| return if ($comment->{who} // 0) != $user_id || $comment->type; | ||
|
|
||
| my $body = $comment->body; | ||
| my $parsed = $comment->{hackbot_front_matter}; | ||
| if (!$parsed || $parsed->[0] ne $body) { | ||
| $parsed = $comment->{hackbot_front_matter} = [$body, parse_front_matter($body)]; | ||
| } | ||
| return @$parsed[1 .. $#$parsed]; | ||
| } | ||
|
|
||
| sub _comment_hackbot { | ||
| my ($comment) = @_; | ||
| my ($metadata) = _parse_comment($comment); | ||
| return $metadata; | ||
| } | ||
|
|
||
| sub comment_body_full { | ||
| my ($self, $args) = @_; | ||
| my (undef, $body) = _parse_comment($args->{comment}); | ||
| ${$args->{body}} = $body if defined $body; | ||
|
kyoshino marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| sub config_add_panels { | ||
| my ($self, $args) = @_; | ||
| $args->{panel_modules}->{Hackbot} = 'Bugzilla::Extension::Hackbot::Config'; | ||
| } | ||
|
|
||
| __PACKAGE__->NAME; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| # | ||
| # This Source Code Form is "Incompatible With Secondary Licenses", as | ||
| # defined by the Mozilla Public License, v. 2.0. | ||
|
|
||
| package Bugzilla::Extension::Hackbot::Config; | ||
|
|
||
| use 5.10.1; | ||
| use strict; | ||
| use warnings; | ||
|
|
||
| use Bugzilla::Config::Common; | ||
| use Bugzilla::User; | ||
|
|
||
| our $sortkey = 1310; | ||
|
|
||
| sub get_param_list { | ||
| my ($class) = @_; | ||
|
|
||
| return ({ | ||
| name => 'hackbot_user', | ||
| type => 't', | ||
| default => '', | ||
| checker => \&check_hackbot_user, | ||
| }); | ||
| } | ||
|
|
||
| # Empty is valid: it turns the extension off. | ||
| sub check_hackbot_user { | ||
| my ($login) = @_; | ||
| return '' if $login eq '' || Bugzilla::User::login_to_id($login); | ||
| return "There is no user named '$login'."; | ||
| } | ||
|
|
||
| 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| # | ||
| # This Source Code Form is "Incompatible With Secondary Licenses", as | ||
| # defined by the Mozilla Public License, v. 2.0. | ||
|
|
||
| package Bugzilla::Extension::Hackbot::Metadata; | ||
|
|
||
| use 5.10.1; | ||
| use strict; | ||
| use warnings; | ||
|
|
||
| use base qw(Exporter); | ||
|
|
||
| our @EXPORT_OK = qw(parse_front_matter); | ||
|
|
||
| # The block hackbot-api puts at the top of a comment: | ||
| # | ||
| # --- | ||
| # hackbot: | ||
| # run: 550e8400-e29b-41d4-a716-446655440000 | ||
| # action: 3f2a9c1e7b4d4f0a8e6c2d1b5a7f9e30 | ||
| # agent: bug-fix | ||
| # url: https://hackbot.moz.tools/runs/550e8400-e29b-41d4-a716-446655440000 | ||
| # --- | ||
| # | ||
| # It is only ever generated, so this reads that one shape rather than YAML in | ||
| # general. Anything else is an ordinary comment that happens to start with a | ||
| # rule, and unknown keys are skipped so that fields can be added. The /a flag | ||
| # keeps the character classes to ASCII. | ||
| my $FRONT_MATTER | ||
| = qr/\A---\nhackbot:\n((?: [[:lower:]_]+: [^\n]*\n)+)---(?:\n+|\z)(.*)\z/as; | ||
| my $FIELD = qr/^ ([[:lower:]_]+): ([^\n]*?)\s*$/am; | ||
| my $IDENTIFIER = qr/\A[[:alnum:]._-]+\z/a; | ||
|
|
||
| # Returns the metadata hash and the comment text without the block, or an empty | ||
| # list when the text doesn't start with a valid block. The url key is left out | ||
| # unless it is an https URL, because it ends up in a link's href. | ||
| sub parse_front_matter { | ||
| my ($text) = @_; | ||
|
|
||
| my ($block, $body) = ($text // '') =~ $FRONT_MATTER or return; | ||
|
|
||
| my %fields = $block =~ /$FIELD/g; | ||
|
|
||
| my %metadata; | ||
| foreach my $key (qw(run action agent)) { | ||
| return unless ($fields{$key} // '') =~ $IDENTIFIER; | ||
| $metadata{$key} = $fields{$key}; | ||
| } | ||
| $metadata{url} = $fields{url} if ($fields{url} // '') =~ m{\Ahttps://\S+\z}; | ||
|
|
||
| return (\%metadata, $body); | ||
| } | ||
|
|
||
| 1; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.