Repository navigation
Bug 2086125 - [HACKBOT] Run status surfaced on the bug #2829
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
Open
dklawren
wants to merge
7
commits into
mozilla:master
Choose a base branch
from
dklawren:2086125
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,276
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
45cea1a
Bug 2086125 - [HACKBOT] Run status surfaced on the bug
dklawren fdfe8cf
Remove hackbot_enabled redundant setting
dklawren af068a7
Merge branch 'master' into 2086125
dklawren 50cc3b6
feat: Show Hackbot runs from every origin
dklawren fbb98a1
Copilot review fixes
dklawren 44b7c1b
fix: Remove use of monospace font
dklawren 75310af
Review fixes
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # 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'; | ||
|
|
||
| __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,45 @@ | ||
| # 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 parent qw(Bugzilla::Extension); | ||
|
|
||
| use Bugzilla; | ||
| use Bugzilla::Extension::Hackbot::BugRuns; | ||
| use Bugzilla::Extension::Hackbot::Constants; | ||
|
|
||
| our $VERSION = '0.01'; | ||
|
|
||
| sub config_add_panels { | ||
| my ($self, $args) = @_; | ||
| $args->{panel_modules}->{Hackbot} = 'Bugzilla::Extension::Hackbot::Config'; | ||
| } | ||
|
|
||
| # Flag the bug page to include the (initially hidden) Hackbot runs module. | ||
| # Whether a bug has runs isn't known until the browser asks the API, so the | ||
| # module's JS reveals it only when there is something to show. | ||
| sub template_before_process { | ||
| my ($self, $args) = @_; | ||
| my $file = $args->{file}; | ||
| my $vars = $args->{vars}; | ||
|
|
||
| return unless $file =~ m{bug_modal/(?:header|edit)\.html\.tmpl$}; | ||
| return unless Bugzilla::Extension::Hackbot::BugRuns->enabled; | ||
| return unless Bugzilla->user->in_group(HACKBOT_GROUP); | ||
|
|
||
| my $bug = exists $vars->{bugs} ? $vars->{bugs}[0] : $vars->{bug}; | ||
| return unless $bug && $bug->id; | ||
|
|
||
| $vars->{hackbot_runs} = 1; | ||
| } | ||
|
|
||
| __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,58 @@ | ||
| # 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::API::V1::Runs; | ||
|
|
||
| use 5.10.1; | ||
| use Mojo::Base qw( Mojolicious::Controller ); | ||
|
|
||
| use Bugzilla::Bug; | ||
| use Bugzilla::Constants; | ||
| use Bugzilla::Extension::Hackbot::BugRuns; | ||
| use Bugzilla::Extension::Hackbot::Constants; | ||
|
|
||
| sub setup_routes { | ||
| my ($class, $r) = @_; | ||
| $r->get('/hackbot/bug_runs/:bug_id')->to('Hackbot::API::V1::Runs#bug_runs'); | ||
| } | ||
|
|
||
| sub bug_runs { | ||
| my ($self) = @_; | ||
| Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); | ||
|
|
||
| my $user = $self->bugzilla->login(LOGIN_REQUIRED); | ||
| $user->id || return $self->user_error('login_required'); | ||
|
|
||
| # Kill switch: when disabled, return nothing rather than calling Hackbot. | ||
| return $self->render(json => {runs => []}) | ||
| unless Bugzilla::Extension::Hackbot::BugRuns->enabled; | ||
|
|
||
| $user->in_group(HACKBOT_GROUP) | ||
| || return $self->user_error('auth_failure', | ||
| {group => HACKBOT_GROUP, action => 'access', object => 'bugs'}); | ||
|
|
||
| my $bug_id = $self->param('bug_id'); | ||
| return $self->user_error('invalid_parameter', | ||
| {name => 'bug_id', err => 'must be a bug id'}) | ||
| unless $bug_id && $bug_id =~ /^\d+$/; | ||
|
|
||
| # Throws unless the user can see the bug, so runs on a security bug are | ||
| # never shown to someone who can't see the bug itself. | ||
| my $bug = Bugzilla::Bug->check({id => $bug_id, cache => 1}); | ||
|
|
||
| my $result = Bugzilla::Extension::Hackbot::BugRuns->for_bug($bug->id); | ||
|
|
||
| # The details are logged server-side; the table only needs to know it failed. | ||
| return $self->render( | ||
| status => 502, | ||
| json => {error => 1, message => 'Hackbot is unavailable'} | ||
| ) unless $result->{ok}; | ||
|
|
||
| return $self->render(json => {runs => $result->{runs}}); | ||
| } | ||
|
|
||
| 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,222 @@ | ||
| # 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::BugRuns; | ||
|
|
||
| # The Hackbot runs for one bug, as shown in the bug page table. | ||
| # | ||
| # Two layers: | ||
| # - for_bug() keeps a per-bug memcached copy of the run list, revalidated | ||
| # with the API's ETag once the short freshness window has passed. | ||
| # - The cached copy holds only the trimmed upstream fields (_trim). Display | ||
| # rows are built on every request (_summarize), so nicknames and the origin | ||
| # rules are never stale and changing them needs no cache-key bump. | ||
| # | ||
| # The cache is shared by every user who can see the bug. That is safe because | ||
| # the caller checks bug visibility first and the cached data holds nothing | ||
| # specific to the viewer. | ||
|
|
||
| use 5.10.1; | ||
| use strict; | ||
| use warnings; | ||
|
|
||
| use Bugzilla; | ||
| use Bugzilla::Logging; | ||
| use Bugzilla::User; | ||
| use Bugzilla::Extension::Hackbot::Client; | ||
| use Bugzilla::Extension::Hackbot::Constants; | ||
|
|
||
| # Bump the version when _trim's output gains or loses fields: a 304 reuses | ||
| # the cached runs as they are, so old entries would otherwise lack the new | ||
| # fields until they expire. | ||
| use constant CACHE_KEY_PREFIX => 'hackbot_runs.v1.'; | ||
|
|
||
| # Whether the runs table is switched on. Checked by the bug page hook and the | ||
| # API alike, so both honour the same kill switch. | ||
| sub enabled { | ||
| return Bugzilla->params->{hackbot_runs_enabled}; | ||
| } | ||
|
|
||
| # Returns {ok => 1, runs => [rows]} or {ok => 0} when the API is unreachable. | ||
| # Pass `client` to substitute the HTTP client (tests). | ||
| sub for_bug { | ||
| my ($class, $bug_id, %args) = @_; | ||
|
|
||
| my $key = CACHE_KEY_PREFIX . $bug_id; | ||
| my $cached = Bugzilla->memcached->get_data({key => $key}); | ||
| $cached = undef unless ref $cached eq 'HASH'; | ||
|
|
||
| my $entry; | ||
| if ($cached && ($cached->{fresh_until} // 0) > time()) { | ||
| $entry = $cached; | ||
| } | ||
| else { | ||
| $entry = _refresh($key, $bug_id, $cached, | ||
| $args{client} // Bugzilla::Extension::Hackbot::Client->new); | ||
| } | ||
|
|
||
| return {ok => 0} if $entry->{error}; | ||
| # Runs often share a requester, so look each email up only once. | ||
| my %nicks; | ||
| return {ok => 1, runs => [map { _summarize($_, \%nicks) } @{$entry->{runs}}]}; | ||
| } | ||
|
|
||
| # Fetch from the API, conditionally when the stale copy has an ETag, and store | ||
| # the result. Returns the new cache entry. | ||
| sub _refresh { | ||
| my ($key, $bug_id, $cached, $client) = @_; | ||
|
|
||
| # Only a good list is worth revalidating; an error entry is refetched. | ||
| my $stale = ($cached && !$cached->{error}) ? $cached : undef; | ||
|
|
||
| my $response = $client->bug_runs($bug_id, $stale && $stale->{etag}); | ||
|
|
||
| unless ($response->{ok}) { | ||
| _warn_failure($bug_id, $response); | ||
| my $retry_at = time() + HACKBOT_ERROR_CACHE_SECONDS; | ||
|
|
||
| # Keep showing the last good list, and keep its ETag so recovery can be a | ||
| # cheap 304. Only a bug with no good copy caches the error. | ||
| if ($stale) { | ||
| my $entry = {%$stale, fresh_until => $retry_at}; | ||
| _store($key, $entry, HACKBOT_REVALIDATE_SECONDS); | ||
| return $entry; | ||
| } | ||
| my $entry = {error => 1, fresh_until => $retry_at}; | ||
| _store($key, $entry, HACKBOT_ERROR_CACHE_SECONDS); | ||
| return $entry; | ||
| } | ||
|
|
||
| # A 304 is only possible when we sent an ETag, i.e. when $stale exists. | ||
| my $runs | ||
| = $response->{not_modified} | ||
| ? $stale->{runs} | ||
| : _trim_all($response->{runs}, $bug_id); | ||
|
|
||
| my $entry = { | ||
| runs => $runs, | ||
| etag => $response->{etag}, | ||
| fresh_until => time() + HACKBOT_CACHE_SECONDS, | ||
| }; | ||
| _store($key, $entry, HACKBOT_REVALIDATE_SECONDS); | ||
| return $entry; | ||
| } | ||
|
|
||
| sub _store { | ||
| my ($key, $entry, $ttl) = @_; | ||
| Bugzilla->memcached->set_data({key => $key, value => $entry, expires_in => $ttl | ||
| }); | ||
| } | ||
|
|
||
| # The valid runs for this bug, newest first (the API's order), capped at | ||
| # HACKBOT_MAX_RUNS. The API has no limit, so a busy bug is cut here. | ||
| sub _trim_all { | ||
| my ($runs, $bug_id) = @_; | ||
| my @kept = grep {defined} map { _trim($_, $bug_id) } @$runs; | ||
| splice @kept, HACKBOT_MAX_RUNS if @kept > HACKBOT_MAX_RUNS; | ||
| return \@kept; | ||
| } | ||
|
|
||
| # Reduce an API RunDoc to the fields the table needs, or undef to drop it. | ||
| sub _trim { | ||
| my ($run, $bug_id) = @_; | ||
|
|
||
| return undef unless ref $run eq 'HASH'; | ||
| return undef unless ($run->{run_id} // '') =~ HACKBOT_RUN_ID_REGEX; | ||
|
|
||
| # Only keep runs that are for this bug. The endpoint is already per bug, but | ||
| # this cache is shared by everyone who can see the bug, so a run for any | ||
| # other bug must never get into it, whatever the API sends back. | ||
| my $inputs = ref $run->{inputs} eq 'HASH' ? $run->{inputs} : {}; | ||
| return undef unless ($inputs->{bug_id} // '') =~ /^\d+$/; | ||
| return undef unless $inputs->{bug_id} == $bug_id; | ||
|
|
||
| my $created_at = $run->{created_at} // ''; | ||
| $created_at = undef | ||
| unless $created_at =~ /^\d{4}-\d\d-\d\dT[\d:.]+(?:Z|[+-]\d\d:?\d\d)?$/; | ||
|
|
||
| return { | ||
| run_id => lc $run->{run_id}, | ||
| agent => _string($run->{agent}), | ||
| status => _string($run->{status}), | ||
| created_at => $created_at, | ||
| requested_by => _string($run->{requested_by}), | ||
| requested_via => _string($run->{requested_via}), | ||
| dedupe_key => _string($run->{dedupe_key}), | ||
| needinfo => $inputs->{bugzilla_needinfo_flag_id} ? 1 : 0, | ||
| revision_id => ($inputs->{revision_id} // '') =~ /^\d+$/ | ||
| ? int($inputs->{revision_id}) | ||
| : undef, | ||
| }; | ||
| } | ||
|
|
||
| # Where a run came from, as a code the JS turns into a label. First match | ||
| # wins: the needinfo flag and dedupe keys identify the trigger precisely, | ||
| # while the service account only says which Hackbot component made the call. | ||
| sub _origin { | ||
| my ($run) = @_; | ||
| my $dedupe_key = $run->{dedupe_key} // ''; | ||
| my $requested_via = $run->{requested_via} // ''; | ||
|
|
||
| return 'needinfo' if $run->{needinfo}; | ||
| return 'phabricator' | ||
| if $dedupe_key =~ HACKBOT_PHABRICATOR_DEDUPE_REGEX && $run->{revision_id}; | ||
| return 'followup' if $dedupe_key =~ HACKBOT_FOLLOWUP_DEDUPE_REGEX; | ||
| return 'ui' if $requested_via =~ HACKBOT_UI_ACCOUNT_REGEX; | ||
| return 'ci' if $requested_via =~ HACKBOT_CI_ACCOUNT_REGEX; | ||
| return 'other'; | ||
| } | ||
|
Comment on lines
+160
to
+172
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be better to do this on Hackbot side instead of guessing it here. |
||
|
|
||
| # Build the display row for one trimmed run. | ||
| sub _summarize { | ||
| my ($run, $nicks) = @_; | ||
|
|
||
| my $origin = _origin($run); | ||
|
|
||
| return { | ||
| run_id => $run->{run_id}, | ||
| url => Bugzilla->params->{hackbot_ui_url} . 'runs/' . $run->{run_id}, | ||
| agent => $run->{agent}, | ||
| status => HACKBOT_RUN_STATUSES->{$run->{status} // ''} | ||
| ? $run->{status} | ||
| : 'unknown', | ||
| created_at => $run->{created_at}, | ||
| origin => $origin, | ||
| revision => $origin eq 'phabricator' ? $run->{revision_id} : undef, | ||
| requester => _requester_nick($run->{requested_by}, $nicks), | ||
| }; | ||
| } | ||
|
|
||
| # The BMO nickname for the requester's email, or undef when the email isn't a | ||
| # BMO account. Only the nick is sent to the browser, never the email. | ||
| # $nicks memoizes lookups, including misses, which the object cache doesn't. | ||
| sub _requester_nick { | ||
| my ($email, $nicks) = @_; | ||
| return undef unless $email; | ||
| return $nicks->{$email} if exists $nicks->{$email}; | ||
| my $user = Bugzilla::User->new({name => $email, cache => 1}); | ||
| return $nicks->{$email} = $user && $user->id ? $user->nick : undef; | ||
| } | ||
|
|
||
| sub _string { | ||
| my ($value) = @_; | ||
| return defined $value && !ref $value ? "$value" : undef; | ||
| } | ||
|
|
||
| sub _warn_failure { | ||
| my ($bug_id, $response) = @_; | ||
| my $status = $response->{status} // 0; | ||
| if ($status == 401 || $status == 403) { | ||
| WARN( "Hackbot: auth failure ($status) listing runs for bug $bug_id" | ||
| . " - check hackbot_api_key"); | ||
| } | ||
| else { | ||
| WARN("Hackbot: failed to list runs for bug $bug_id: " . $response->{errmsg}); | ||
| } | ||
| } | ||
|
|
||
| 1; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A single failed revalidation overwrites the good cached entry (and its ETag) with an error entry, so the table disappears for 60s and the next fetch is a full one. Would it be better to keep serving the stale runs on failure and only cache the error when there is no stale copy?