Skip to content
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
23 changes: 22 additions & 1 deletion extensions/Hackbot/Extension.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ use 5.10.1;
use strict;
use warnings;

use base qw(Bugzilla::Extension);
use parent qw(Bugzilla::Extension);

use Bugzilla;
use Bugzilla::Extension::Hackbot::BugRuns;
use Bugzilla::Extension::Hackbot::Constants;
use Bugzilla::Extension::Hackbot::Metadata qw(parse_front_matter);
use Bugzilla::User;

Expand Down Expand Up @@ -67,4 +70,22 @@ sub config_add_panels {
$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;
58 changes: 58 additions & 0 deletions extensions/Hackbot/lib/API/V1/Runs.pm
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;
202 changes: 202 additions & 0 deletions extensions/Hackbot/lib/BugRuns.pm
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}) {
Comment thread
dklawren marked this conversation as resolved.
Resolved
_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;
Loading