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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 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 8ae9171
fix: Read the Hackbot BugRun response and use its source as origin
dklawren b109438
Merge remote-tracking branch 'origin/master' into 2086125
dklawren 79337d9
Merge branch 'master' into 2086125
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
| 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,202 @@ | ||
| # 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 changing the origin or | ||
| # requester rules 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::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.v2.'; | ||
|
|
||
| # 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}; | ||
| return {ok => 1, runs => [map { _summarize($_) } @{$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 BugRun 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. | ||
| return undef unless ($run->{bug_id} // '') =~ /^\d+$/; | ||
| return undef unless $run->{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}), | ||
| source => _string($run->{source}), | ||
| created_at => $created_at, | ||
| requester => _string($run->{requester}), | ||
| revision_id => ($run->{revision_id} // '') =~ /^\d+$/ | ||
| ? int($run->{revision_id}) | ||
| : undef, | ||
| }; | ||
| } | ||
|
|
||
| # Build the display row for one trimmed run. | ||
| sub _summarize { | ||
| my ($run) = @_; | ||
|
|
||
| # The origin is the run's source as Hackbot records it, as a code the JS | ||
| # turns into a label. | ||
| my $origin | ||
| = HACKBOT_RUN_SOURCES->{$run->{source} // ''} ? $run->{source} : 'other'; | ||
|
|
||
| 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_display($run->{requester}), | ||
| }; | ||
| } | ||
|
|
||
| # The requester as shown in the table. Hackbot sends the Google account email | ||
| # for runs started through Google login, or the Bugzilla nickname for runs | ||
| # started from Bugzilla. An email loses its domain so the page can't be | ||
| # harvested for addresses. | ||
| sub _requester_display { | ||
| my ($requester) = @_; | ||
| return undef unless defined $requester; | ||
| $requester =~ s/\@.*\z//s; | ||
| return length $requester ? $requester : 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.
Uh oh!
There was an error while loading. Please reload this page.