Skip to content
Open
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
16 changes: 16 additions & 0 deletions extensions/Hackbot/Config.pm
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;
45 changes: 45 additions & 0 deletions extensions/Hackbot/Extension.pm
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;
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;
213 changes: 213 additions & 0 deletions extensions/Hackbot/lib/BugRuns.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
# 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 $entry = {error => 1, fresh_until => time() + HACKBOT_ERROR_CACHE_SECONDS};
_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';
}

# 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;
Loading
Loading