From 45cea1ac0bd4ea05b6938a065f694190880dc62e Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Thu, 12 Nov 2026 21:14:48 -0500 Subject: [PATCH 1/4] Bug 2086125 - [HACKBOT] Run status surfaced on the bug --- extensions/Hackbot/Config.pm | 16 ++ extensions/Hackbot/Extension.pm | 45 ++++ extensions/Hackbot/lib/API/V1/Runs.pm | 58 +++++ extensions/Hackbot/lib/BugRuns.pm | 184 +++++++++++++ extensions/Hackbot/lib/Client.pm | 117 +++++++++ extensions/Hackbot/lib/Config.pm | 38 +++ extensions/Hackbot/lib/Constants.pm | 66 +++++ extensions/Hackbot/t/bmo/bug-runs.t | 245 ++++++++++++++++++ .../en/default/admin/params/hackbot.html.tmpl | 31 +++ .../en/default/hackbot/table.html.tmpl | 20 ++ .../hook/bug_modal/edit-module.html.tmpl | 20 ++ .../hook/bug_modal/header-end.html.tmpl | 14 + extensions/Hackbot/web/js/hackbot_runs.js | 109 ++++++++ extensions/Hackbot/web/style/hackbot_runs.css | 46 ++++ qa/t/rest_hackbot_runs.t | 118 +++++++++ scripts/generate_bmo_data.pl | 2 + 16 files changed, 1129 insertions(+) create mode 100644 extensions/Hackbot/Config.pm create mode 100644 extensions/Hackbot/Extension.pm create mode 100644 extensions/Hackbot/lib/API/V1/Runs.pm create mode 100644 extensions/Hackbot/lib/BugRuns.pm create mode 100644 extensions/Hackbot/lib/Client.pm create mode 100644 extensions/Hackbot/lib/Config.pm create mode 100644 extensions/Hackbot/lib/Constants.pm create mode 100644 extensions/Hackbot/t/bmo/bug-runs.t create mode 100644 extensions/Hackbot/template/en/default/admin/params/hackbot.html.tmpl create mode 100644 extensions/Hackbot/template/en/default/hackbot/table.html.tmpl create mode 100644 extensions/Hackbot/template/en/default/hook/bug_modal/edit-module.html.tmpl create mode 100644 extensions/Hackbot/template/en/default/hook/bug_modal/header-end.html.tmpl create mode 100644 extensions/Hackbot/web/js/hackbot_runs.js create mode 100644 extensions/Hackbot/web/style/hackbot_runs.css create mode 100644 qa/t/rest_hackbot_runs.t diff --git a/extensions/Hackbot/Config.pm b/extensions/Hackbot/Config.pm new file mode 100644 index 0000000000..ef1ec72c24 --- /dev/null +++ b/extensions/Hackbot/Config.pm @@ -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; diff --git a/extensions/Hackbot/Extension.pm b/extensions/Hackbot/Extension.pm new file mode 100644 index 0000000000..30be230be7 --- /dev/null +++ b/extensions/Hackbot/Extension.pm @@ -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; diff --git a/extensions/Hackbot/lib/API/V1/Runs.pm b/extensions/Hackbot/lib/API/V1/Runs.pm new file mode 100644 index 0000000000..d2103fe8f7 --- /dev/null +++ b/extensions/Hackbot/lib/API/V1/Runs.pm @@ -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; diff --git a/extensions/Hackbot/lib/BugRuns.pm b/extensions/Hackbot/lib/BugRuns.pm new file mode 100644 index 0000000000..9f2b013b9f --- /dev/null +++ b/extensions/Hackbot/lib/BugRuns.pm @@ -0,0 +1,184 @@ +# 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; + +use constant CACHE_KEY_PREFIX => 'hackbot_runs.v1.'; + +# Whether the runs table is switched on: both the Hackbot master switch and +# the runs feature itself. Checked by the bug page hook and the API alike. +sub enabled { + my $params = Bugzilla->params; + return $params->{hackbot_enabled} && $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} + : [grep {defined} map { _trim($_, $bug_id) } @{$response->{runs}}]; + + 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 + }); +} + +# 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 API filters by bug_id, but an + # API that ignored the filter (an older deploy treats it as an unknown query + # parameter) would return every run, including runs on bugs this user may + # not be allowed to see. + 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}), + needinfo => $inputs->{bugzilla_needinfo_flag_id} ? 1 : 0, + }; +} + +# Build the display row for one trimmed run. +sub _summarize { + my ($run, $nicks) = @_; + + my $origin + = $run->{needinfo} ? 'needinfo' + : ($run->{requested_via} // '') =~ HACKBOT_UI_ACCOUNT_REGEX ? 'ui' + : '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, + 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; diff --git a/extensions/Hackbot/lib/Client.pm b/extensions/Hackbot/lib/Client.pm new file mode 100644 index 0000000000..95988a9467 --- /dev/null +++ b/extensions/Hackbot/lib/Client.pm @@ -0,0 +1,117 @@ +# 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::Client; + +# The HTTP side of talking to the Hackbot API, and nothing else: no caching, +# no shaping of the data. Every request goes through _get(), so changing how +# BMO authenticates (e.g. a Google ID token instead of X-API-Key) is a change +# in one place. + +use 5.10.1; +use strict; +use warnings; + +use Bugzilla; +use Bugzilla::Extension::Hackbot::Constants; + +use JSON qw(decode_json); +use LWP::UserAgent; + +sub new { + my ($class, %args) = @_; + my $self = bless { + base_url => $args{base_url} // Bugzilla->params->{hackbot_api_url}, + api_key => $args{api_key} // Bugzilla->params->{hackbot_api_key}, + ua => $args{ua}, + }, $class; + $self->{ua} //= $self->_build_ua; + return $self; +} + +# Fetch the runs recorded for a bug, newest first. Pass the ETag from a cached +# copy to make the request conditional. +# +# Returns a hashref: +# {ok => 1, not_modified => 1, etag => ...} - 304, cache is current +# {ok => 1, not_modified => 0, etag => ..., runs => [...]} +# {ok => 0, status => , errmsg => ...} +sub bug_runs { + my ($self, $bug_id, $etag) = @_; + + return {ok => 0, status => 0, errmsg => 'hackbot_api_url is not set'} + unless $self->{base_url}; + + # $bug_id is a validated integer; the base URL ends in a slash (check_urlbase). + my $url + = $self->{base_url} + . 'runs?bug_id=' + . int($bug_id) + . '&limit=' + . HACKBOT_MAX_RUNS; + + my $result = $self->_get($url, $etag); + return $result if !$result->{ok} || $result->{not_modified}; + + unless (ref $result->{data} eq 'ARRAY') { + return { + ok => 0, + status => $result->{status}, + errmsg => 'response is not a list' + }; + } + + return { + ok => 1, + not_modified => 0, + etag => $result->{etag}, + runs => $result->{data}, + }; +} + +sub _get { + my ($self, $url, $etag) = @_; + + my @headers = ('Accept' => 'application/json'); + push @headers, ('X-API-Key' => $self->{api_key}) if $self->{api_key}; + push @headers, ('If-None-Match' => $etag) if defined $etag; + + my $response = $self->{ua}->get($url, @headers); + + # LWP counts a 304 as a failure, but for a conditional request it means the + # cached copy is still valid. Keep the ETag the server echoes, if any. + if ($response->code == 304) { + return {ok => 1, not_modified => 1, etag => $response->header('ETag') // $etag}; + } + + unless ($response->is_success) { + return {ok => 0, status => $response->code, errmsg => $response->status_line}; + } + + my $data = eval { decode_json($response->decoded_content) }; + if ($@) { + return {ok => 0, status => $response->code, errmsg => "JSON parse error: $@"}; + } + + return { + ok => 1, + status => $response->code, + etag => $response->header('ETag'), + data => $data, + }; +} + +sub _build_ua { + my $ua = LWP::UserAgent->new(timeout => HACKBOT_API_TIMEOUT); + $ua->agent('BMO-Bugzilla/1.0'); + if (my $proxy = Bugzilla->params->{proxy_url}) { + $ua->proxy('https', $proxy); + } + return $ua; +} + +1; diff --git a/extensions/Hackbot/lib/Config.pm b/extensions/Hackbot/lib/Config.pm new file mode 100644 index 0000000000..e79714e431 --- /dev/null +++ b/extensions/Hackbot/lib/Config.pm @@ -0,0 +1,38 @@ +# 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; + +our $sortkey = 1310; + +sub get_param_list { + return ( + {name => 'hackbot_enabled', type => 'b', default => 0}, + {name => 'hackbot_runs_enabled', type => 'b', default => 0}, + { + name => 'hackbot_api_url', + type => 't', + default => '', + checker => \&check_urlbase + }, + {name => 'hackbot_api_key', type => 'p', default => ''}, + { + name => 'hackbot_ui_url', + type => 't', + default => 'https://hackbot.moz.tools/', + checker => \&check_urlbase + }, + ); +} + +1; diff --git a/extensions/Hackbot/lib/Constants.pm b/extensions/Hackbot/lib/Constants.pm new file mode 100644 index 0000000000..2e0fcf20c3 --- /dev/null +++ b/extensions/Hackbot/lib/Constants.pm @@ -0,0 +1,66 @@ +# 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::Constants; + +use 5.10.1; +use strict; +use warnings; + +use base 'Exporter'; + +our @EXPORT = qw( + HACKBOT_GROUP + HACKBOT_API_TIMEOUT + HACKBOT_MAX_RUNS + HACKBOT_CACHE_SECONDS + HACKBOT_REVALIDATE_SECONDS + HACKBOT_ERROR_CACHE_SECONDS + HACKBOT_RUN_STATUSES + HACKBOT_RUN_ID_REGEX + HACKBOT_UI_ACCOUNT_REGEX +); + +# Only members of this group see Hackbot runs, on the bug page or via the API. +use constant HACKBOT_GROUP => 'editbugs'; + +# Seconds to wait for the Hackbot API before giving up. The table is loaded +# asynchronously, so a slow API delays only the table, never the bug page. +use constant HACKBOT_API_TIMEOUT => 10; + +# The most runs shown for one bug (sent as the API's `limit`; newest first). +use constant HACKBOT_MAX_RUNS => 50; + +# Freshness window: how long a cached run list is served without contacting +# Hackbot. Kept short because a run moves pending -> running -> done within +# minutes, and the point of the table is to show that progress. +use constant HACKBOT_CACHE_SECONDS => 30; + +# Hard memcached TTL. Much longer than the freshness window so the stored ETag +# outlives it and a stale entry can be revalidated with If-None-Match (a cheap +# 304) instead of a full fetch. +use constant HACKBOT_REVALIDATE_SECONDS => 86_400; + +# Failed lookups are cached briefly so a Hackbot outage doesn't add an API +# call to every bug view, while still recovering quickly. +use constant HACKBOT_ERROR_CACHE_SECONDS => 60; + +# The run states defined by the Hackbot API (RunStatus in hackbot-api). +use constant HACKBOT_RUN_STATUSES => + {map { $_ => 1 } qw(pending running succeeded failed timed_out)}; + +# Run ids are UUIDs. Anything else is dropped rather than put into a link. +use constant HACKBOT_RUN_ID_REGEX => qr{ + ^ [[:xdigit:]]{8} - [[:xdigit:]]{4} - [[:xdigit:]]{4} + - [[:xdigit:]]{4} - [[:xdigit:]]{12} $ +}x; + +# `requested_via` holds the service account that created the run. Runs +# started from the Hackbot UI come from its Cloud Run service account. +use constant HACKBOT_UI_ACCOUNT_REGEX => qr/^hackbot-ui-run@/; + +1; diff --git a/extensions/Hackbot/t/bmo/bug-runs.t b/extensions/Hackbot/t/bmo/bug-runs.t new file mode 100644 index 0000000000..275c2f8a13 --- /dev/null +++ b/extensions/Hackbot/t/bmo/bug-runs.t @@ -0,0 +1,245 @@ +#!/usr/bin/env perl +# 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. + +use strict; +use warnings; +use 5.10.1; +use lib qw( . lib local/lib/perl5 ); + +BEGIN { + $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf'; + $ENV{BUGZILLA_DISABLE_HOSTAGE} = 1; +} + +use Test::More; +use HTTP::Response; +use JSON qw(encode_json); + +use Bugzilla; +BEGIN { Bugzilla->extensions } + +use Bugzilla::User; +use Bugzilla::Extension::Hackbot::BugRuns; +use Bugzilla::Extension::Hackbot::Client; + +# A bug id no fixture uses, varied per process so re-runs don't share cache. +my $BUG_ID = 1_900_000_000 + ($$ % 100_000); +my $CACHE_KEY + = Bugzilla::Extension::Hackbot::BugRuns::CACHE_KEY_PREFIX . $BUG_ID; + +Bugzilla->params->{hackbot_ui_url} = 'https://hackbot.example.test/'; + +my $requester = Bugzilla::User->new({name => 'automation@bmo.tld'}); + +sub run_doc { + my (%over) = @_; + my $inputs = delete $over{inputs} // {bug_id => $BUG_ID}; + return { + run_id => '53bbcc6e-014c-487b-b0e8-8a6b7b0ec273', + agent => 'bug-fix', + status => 'running', + inputs => $inputs, + requested_by => undef, + requested_via => undef, + created_at => '2026-11-12T16:24:39.123456Z', + %over, + }; +} + +# Records each request and answers from a queue of HTTP::Response objects. +package FakeUA { + sub new { bless {responses => [@_[1 .. $#_]], requests => []}, $_[0] } + + sub get { + my ($self, $url, %headers) = @_; + push @{$self->{requests}}, {url => $url, headers => \%headers}; + return shift @{$self->{responses}}; + } +} + +# Stands in for the Client: returns queued results and records the ETag sent. +package FakeClient { + sub new { bless {results => [@_[1 .. $#_]], etags => []}, $_[0] } + + sub bug_runs { + my ($self, $bug_id, $etag) = @_; + push @{$self->{etags}}, $etag; + return shift @{$self->{results}}; + } + sub calls { scalar @{$_[0]->{etags}} } +} + +package main; + +sub response { + my ($code, $body, %headers) = @_; + return HTTP::Response->new($code, undef, + ['Content-Type' => 'application/json', %headers], $body); +} + +sub client_with { + my $ua = FakeUA->new(@_); + my $client = Bugzilla::Extension::Hackbot::Client->new( + base_url => 'https://hackbot-api.example.test/', + api_key => 'secret-key', + ua => $ua, + ); + return ($client, $ua); +} + +sub reset_cache { + Bugzilla->memcached->set_data({key => $CACHE_KEY, value => undef}); +} + +# Age the cached entry past its freshness window. +sub expire_cache { + my $entry = Bugzilla->memcached->get_data({key => $CACHE_KEY}); + $entry->{fresh_until} = time() - 1; + Bugzilla->memcached->set_data({key => $CACHE_KEY, value => $entry}); +} + +sub fetch { + my ($client) = @_; + return Bugzilla::Extension::Hackbot::BugRuns->for_bug($BUG_ID, + client => $client); +} + +subtest 'client: request shape and conditional requests' => sub { + my ($client, $ua) = client_with( + response(200, encode_json([run_doc()]), ETag => '"v1"'), + response(304, '', ETag => '"v1"'), + ); + + my $result = $client->bug_runs($BUG_ID); + ok($result->{ok}, '200 is ok'); + is($result->{etag}, '"v1"', 'ETag is returned'); + is(scalar @{$result->{runs}}, 1, 'runs are returned'); + + my $request = $ua->{requests}[0]; + is( + $request->{url}, + "https://hackbot-api.example.test/runs?bug_id=$BUG_ID&limit=50", + 'lists runs filtered by bug id' + ); + is($request->{headers}{'X-API-Key'}, 'secret-key', 'sends the API key'); + ok(!exists $request->{headers}{'If-None-Match'}, + 'first request is unconditional'); + + $result = $client->bug_runs($BUG_ID, '"v1"'); + ok($result->{ok} && $result->{not_modified}, '304 means not modified'); + is($ua->{requests}[1]{headers}{'If-None-Match'}, '"v1"', 'sends If-None-Match'); +}; + +subtest 'client: failures' => sub { + my ($client) = client_with( + response(500, 'oops'), + response(200, '{"not":"a list"}'), + response(200, 'not json'), + ); + ok(!$client->bug_runs($BUG_ID)->{ok}, 'HTTP error is not ok'); + ok(!$client->bug_runs($BUG_ID)->{ok}, 'non-list body is not ok'); + ok(!$client->bug_runs($BUG_ID)->{ok}, 'invalid JSON is not ok'); + + my $unconfigured = Bugzilla::Extension::Hackbot::Client->new( + base_url => '', + ua => FakeUA->new() + ); + ok(!$unconfigured->bug_runs($BUG_ID)->{ok}, 'missing API URL is not ok'); +}; + +subtest 'runs are trimmed to valid runs for this bug' => sub { + reset_cache(); + my $client = FakeClient->new({ + ok => 1, + etag => '"t"', + runs => [ + run_doc(), + run_doc(run_id => 'not-a-uuid'), + run_doc(inputs => {bug_id => $BUG_ID + 1}), + run_doc(inputs => {}), + 'not a hash', + ], + }); + + my $result = fetch($client); + ok($result->{ok}, 'ok'); + is(scalar @{$result->{runs}}, 1, 'only the valid run for this bug is kept'); + + my $run = $result->{runs}[0]; + is( + $run->{url}, + 'https://hackbot.example.test/runs/53bbcc6e-014c-487b-b0e8-8a6b7b0ec273', + 'run link points at the Hackbot UI' + ); + is($run->{agent}, 'bug-fix', 'agent'); + is($run->{status}, 'running', 'status'); + is($run->{created_at}, '2026-11-12T16:24:39.123456Z', 'created_at'); +}; + +subtest 'origin and requester' => sub { + reset_cache(); + my $client = FakeClient->new({ + ok => 1, + runs => [ + run_doc( + run_id => '00000000-0000-0000-0000-000000000001', + inputs => {bug_id => $BUG_ID, bugzilla_needinfo_flag_id => 42}, + requested_by => $requester->login, + ), + run_doc( + run_id => '00000000-0000-0000-0000-000000000002', + requested_via => 'hackbot-ui-run@some-project.iam.gserviceaccount.com', + requested_by => 'not-a-bmo-user@example.test', + status => 'something-new', + ), + run_doc(run_id => '00000000-0000-0000-0000-000000000003'), + ], + }); + + my ($needinfo, $ui, $other) = @{fetch($client)->{runs}}; + is($needinfo->{origin}, 'needinfo', 'needinfo run'); + is($needinfo->{requester}, $requester->nick, 'requester shown by nick'); + is($ui->{origin}, 'ui', 'Hackbot UI run'); + is($ui->{requester}, undef, 'unknown email has no requester'); + is($ui->{status}, 'unknown', 'unrecognized status'); + is($other->{origin}, 'other', 'anything else'); + ok(!grep({ exists $_->{requested_by} } $needinfo, $ui, $other), + 'requester emails are not exposed'); +}; + +SKIP: { + skip 'memcached is not enabled', 1 unless Bugzilla->memcached->enabled; + + subtest 'ETag caching' => sub { + reset_cache(); + my $client = FakeClient->new( + {ok => 1, etag => '"v1"', runs => [run_doc()]}, + {ok => 1, not_modified => 1, etag => '"v1"'}, + {ok => 0, status => 503, errmsg => 'down'}, + ); + + is(scalar @{fetch($client)->{runs}}, 1, 'first load fetches'); + fetch($client); + is($client->calls, 1, 'fresh cache is served without a request'); + + expire_cache(); + + my $result = fetch($client); + is($client->{etags}[1], '"v1"', 'stale cache revalidates with its ETag'); + is(scalar @{$result->{runs}}, 1, '304 reuses the cached runs'); + + expire_cache(); + + ok(!fetch($client)->{ok}, 'API failure is reported'); + ok(!fetch($client)->{ok}, 'failure is cached'); + is($client->calls, 3, 'cached failure makes no further request'); + }; +} + +reset_cache(); +done_testing(); diff --git a/extensions/Hackbot/template/en/default/admin/params/hackbot.html.tmpl b/extensions/Hackbot/template/en/default/admin/params/hackbot.html.tmpl new file mode 100644 index 0000000000..c88b1aaed3 --- /dev/null +++ b/extensions/Hackbot/template/en/default/admin/params/hackbot.html.tmpl @@ -0,0 +1,31 @@ +[%# 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. + #%] + +[% + title = "Hackbot" + desc = "Configure Hackbot Integration" +%] + +[% + param_descs = { + hackbot_enabled => + 'Master switch for the Hackbot integration. When off, every Hackbot' + _ ' feature is disabled and BMO never contacts the Hackbot API.', + hackbot_runs_enabled => + 'Show the Hackbot runs for a bug on the bug page (editbugs members only).' + _ ' Has no effect unless hackbot_enabled is on.', + hackbot_api_url => + 'Base URL of the Hackbot API, ending with a slash' + _ ' (for example https://hackbot-api.example.com/).', + hackbot_api_key => + 'API key BMO sends to the Hackbot API in the X-API-Key header.', + hackbot_ui_url => + 'Base URL of the Hackbot UI, ending with a slash. Run links are' + _ ' built from this URL followed by runs/ and the run id.', + } +%] diff --git a/extensions/Hackbot/template/en/default/hackbot/table.html.tmpl b/extensions/Hackbot/template/en/default/hackbot/table.html.tmpl new file mode 100644 index 0000000000..58e5b0dd0f --- /dev/null +++ b/extensions/Hackbot/template/en/default/hackbot/table.html.tmpl @@ -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. + #%] + + + + + + + + + + + + +
AgentStatusOriginStarted
diff --git a/extensions/Hackbot/template/en/default/hook/bug_modal/edit-module.html.tmpl b/extensions/Hackbot/template/en/default/hook/bug_modal/edit-module.html.tmpl new file mode 100644 index 0000000000..191b5b23fb --- /dev/null +++ b/extensions/Hackbot/template/en/default/hook/bug_modal/edit-module.html.tmpl @@ -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. + #%] + +[% RETURN UNLESS hackbot_runs %] + +[%# Hidden until hackbot_runs.js finds at least one run for the bug. %] +
+ [% WRAPPER bug_modal/module.html.tmpl + title = "Hackbot" + subtitle = [{ unfiltered => '' }] + collapsed = 0 + %] + [% INCLUDE hackbot/table.html.tmpl %] + [% END %] +
diff --git a/extensions/Hackbot/template/en/default/hook/bug_modal/header-end.html.tmpl b/extensions/Hackbot/template/en/default/hook/bug_modal/header-end.html.tmpl new file mode 100644 index 0000000000..495f586c11 --- /dev/null +++ b/extensions/Hackbot/template/en/default/hook/bug_modal/header-end.html.tmpl @@ -0,0 +1,14 @@ +[%# 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. + #%] + +[% + IF hackbot_runs; + style_urls.push('extensions/Hackbot/web/style/hackbot_runs.css'); + javascript_urls.push('extensions/Hackbot/web/js/hackbot_runs.js'); + END; +%] diff --git a/extensions/Hackbot/web/js/hackbot_runs.js b/extensions/Hackbot/web/js/hackbot_runs.js new file mode 100644 index 0000000000..80340481a8 --- /dev/null +++ b/extensions/Hackbot/web/js/hackbot_runs.js @@ -0,0 +1,109 @@ +/* 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/. */ + +"use strict"; + +const HackbotRuns = { + STATUS_LABELS: { + pending: { label: "pending", cls: "hb-status-pending" }, + running: { label: "running", cls: "hb-status-running" }, + succeeded: { label: "succeeded", cls: "hb-status-succeeded" }, + failed: { label: "failed", cls: "hb-status-failed" }, + timed_out: { label: "timed out", cls: "hb-status-failed" }, + unknown: { label: "unknown", cls: "hb-status-pending" }, + }, + + ORIGIN_LABELS: { + needinfo: "needinfo", + ui: "Hackbot UI", + other: "Hackbot", + }, + + cell(className, content) { + const td = document.createElement("td"); + td.className = className; + if (content instanceof Node) { + td.appendChild(content); + } else { + td.textContent = content ?? "—"; + } + return td; + }, + + statusBadge(status) { + const info = this.STATUS_LABELS[status] || this.STATUS_LABELS.unknown; + const badge = document.createElement("span"); + badge.className = `hb-status-badge ${info.cls}`; + badge.textContent = info.label; + return badge; + }, + + originText(run) { + const label = this.ORIGIN_LABELS[run.origin] || this.ORIGIN_LABELS.other; + return run.requester ? `${label} by :${run.requester}` : label; + }, + + // A relative time ("4 minutes ago") that bug_modal.js keeps up to date via + // the rel-time class, with the absolute time as a tooltip. + startedAt(createdAt) { + const date = createdAt ? new Date(createdAt) : null; + if (!date || isNaN(date)) return null; + const span = document.createElement("span"); + span.className = "rel-time"; + span.dataset.time = Math.floor(date.getTime() / 1000); + span.title = date.toLocaleString(); + span.textContent = timeAgo(date); + return span; + }, + + viewLink(url) { + const link = document.createElement("a"); + link.href = url; + link.target = "_blank"; + link.rel = "noopener noreferrer"; + link.textContent = "view run"; + return link; + }, + + buildRow(run) { + const tr = document.createElement("tr"); + tr.appendChild(this.cell("hb-col-agent", run.agent)); + tr.appendChild(this.cell("hb-col-status", this.statusBadge(run.status))); + tr.appendChild(this.cell("hb-col-origin", this.originText(run))); + tr.appendChild(this.cell("hb-col-started", this.startedAt(run.created_at))); + tr.appendChild(this.cell("hb-col-link", this.viewLink(run.url))); + return tr; + }, + + async onLoad() { + const module = document.getElementById("hackbot-runs-module"); + const tbody = module?.querySelector("tbody.hackbot-runs-body"); + if (!tbody) return; + + try { + const { runs } = await Bugzilla.API.get( + `hackbot/bug_runs/${BUGZILLA.bug_id}` + ); + + // Like the other integrations, the module only appears when there is + // something to show. A Hackbot outage keeps it hidden as well, rather + // than putting an error box on every bug page (it's logged server-side). + if (!runs || runs.length === 0) return; + + for (const run of runs) { + tbody.appendChild(this.buildRow(run)); + } + module.querySelector(".hackbot-run-count")?.replaceChildren( + `${runs.length} run${runs.length === 1 ? "" : "s"} for this bug` + ); + module.classList.remove("bz_default_hidden"); + } catch (e) { + console.error("Error loading Hackbot runs:", e); + } + }, +}; + +window.addEventListener("DOMContentLoaded", () => { + HackbotRuns.onLoad(); +}); diff --git a/extensions/Hackbot/web/style/hackbot_runs.css b/extensions/Hackbot/web/style/hackbot_runs.css new file mode 100644 index 0000000000..cf8bcd73cb --- /dev/null +++ b/extensions/Hackbot/web/style/hackbot_runs.css @@ -0,0 +1,46 @@ +/* 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/. */ + +.hackbot-runs { + width: 100%; + border-collapse: collapse; + font-size: 13px; +} + +.hackbot-runs th, +.hackbot-runs td { + padding: 4px 8px; + text-align: left; + vertical-align: middle; + border-bottom: 1px solid var(--grid-border-color); +} + +.hackbot-runs thead th { + background-color: var(--grid-header-background-color); + color: var(--primary-text-color); + font-weight: bold; + white-space: nowrap; +} + +/* Status badge */ +.hb-status-badge { + display: inline-block; + padding: 2px 8px; + border-radius: 10px; + font-size: 11px; + font-weight: bold; + white-space: nowrap; +} + +.hb-status-pending { background-color: #eaeef2; color: #57606a; } +.hb-status-running { background-color: #fff8c5; color: #7d4e00; } +.hb-status-succeeded { background-color: #dafbe1; color: #1a7f37; } +.hb-status-failed { background-color: #ffebe9; color: #cf222e; } + +/* Column sizing hints */ +.hb-col-agent { white-space: nowrap; font-family: monospace; } +.hb-col-status { white-space: nowrap; width: 7em; } +.hb-col-origin { width: 40%; } +.hb-col-started { white-space: nowrap; } +.hb-col-link { white-space: nowrap; text-align: right; } diff --git a/qa/t/rest_hackbot_runs.t b/qa/t/rest_hackbot_runs.t new file mode 100644 index 0000000000..5b51de3498 --- /dev/null +++ b/qa/t/rest_hackbot_runs.t @@ -0,0 +1,118 @@ +#!/usr/bin/env perl +# 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. +use strict; +use warnings; +use 5.10.1; +use lib qw(lib ../../lib ../../local/lib/perl5); + +use Bugzilla; +use Bugzilla::Logging; + +use QA::Util qw(get_config); +use QA::Tests qw(create_bug_fields PRIVATE_BUG_USER); +use Test::Mojo; +use Test::More; + +my $config = get_config(); +my $url = Bugzilla->localconfig->urlbase; +my $admin_key = $config->{admin_user_api_key}; +my $editbugs_key = $config->{editbugs_user_api_key}; +my $unprivileged_key = $config->{unprivileged_user_api_key}; +my $private_key = $config->{PRIVATE_BUG_USER . '_user_api_key'}; + +my $t = Test::Mojo->new(); + +# The endpoint reads the run list from memcached before it would ever call the +# Hackbot API, so seeding a fresh cache entry lets us test it with no network. +sub seed_runs { + my ($bug_id) = @_; + Bugzilla->memcached->set_data({ + key => "hackbot_runs.v1.$bug_id", + value => { + etag => '"seeded"', + fresh_until => time() + 300, + runs => [{ + run_id => '53bbcc6e-014c-487b-b0e8-8a6b7b0ec273', + agent => 'bug-fix', + status => 'running', + created_at => '2026-11-12T16:24:39Z', + requested_by => undef, + requested_via => undef, + needinfo => 1, + }], + }, + expires_in => 300, + }); +} + +sub create_bug { + my ($api_key, $summary) = @_; + my $bug_data = create_bug_fields($config); + delete $bug_data->{cc}; + $bug_data->{summary} = $summary; + $t->post_ok( + $url . 'rest/bug' => {'X-Bugzilla-API-Key' => $api_key} => json => $bug_data) + ->status_is(200) + ->json_has('/id'); + return $t->tx->res->json->{id}; +} + +my $bug_id = create_bug($editbugs_key, 'Test Hackbot runs'); +seed_runs($bug_id); + +# Login is required. +$t->get_ok($url . "rest/hackbot/bug_runs/$bug_id")->status_is(401); + +# Only editbugs members may see Hackbot runs. +$t->get_ok($url + . "rest/hackbot/bug_runs/$bug_id" => + {'X-Bugzilla-API-Key' => $unprivileged_key}) + ->json_is('/error', 1) + ->json_hasnt('/runs'); + +# An editbugs member gets the runs, with no requester email exposed. +$t->get_ok($url + . "rest/hackbot/bug_runs/$bug_id" => {'X-Bugzilla-API-Key' => $editbugs_key}) + ->status_is(200) + ->json_is('/runs/0/run_id', '53bbcc6e-014c-487b-b0e8-8a6b7b0ec273') + ->json_like('/runs/0/url', qr{/runs/53bbcc6e-014c-487b-b0e8-8a6b7b0ec273$}) + ->json_is('/runs/0/agent', 'bug-fix') + ->json_is('/runs/0/status', 'running') + ->json_is('/runs/0/origin', 'needinfo') + ->json_hasnt('/runs/0/requested_by') + ->json_hasnt('/runs/1'); + +# A non-numeric bug id is rejected. +$t->get_ok( + $url . 'rest/hackbot/bug_runs/abc' => {'X-Bugzilla-API-Key' => $editbugs_key}) + ->json_is('/error', 1); + +# Runs on a bug the user can't see are not shown, even to editbugs members. +# The editbugs user files the bug and, as its reporter, can still see it once +# it is in QA-Selenium-TEST. The admin is in editbugs but neither in that group +# (it is created so admin doesn't inherit it) nor the reporter, so is denied. +my $private_bug_id + = create_bug($editbugs_key, 'Test Hackbot runs on a private bug'); +$t->put_ok($url + . "rest/bug/$private_bug_id" => {'X-Bugzilla-API-Key' => $private_key} => + json => {groups => {add => ['QA-Selenium-TEST']}})->status_is(200); +seed_runs($private_bug_id); + +$t->get_ok($url + . "rest/hackbot/bug_runs/$private_bug_id" => + {'X-Bugzilla-API-Key' => $admin_key}) + ->json_is('/error', 1) + ->json_hasnt('/runs'); + +$t->get_ok($url + . "rest/hackbot/bug_runs/$private_bug_id" => + {'X-Bugzilla-API-Key' => $editbugs_key}) + ->status_is(200) + ->json_is('/runs/0/origin', 'needinfo'); + +done_testing(); diff --git a/scripts/generate_bmo_data.pl b/scripts/generate_bmo_data.pl index 269a3f1501..d86842ccde 100755 --- a/scripts/generate_bmo_data.pl +++ b/scripts/generate_bmo_data.pl @@ -627,6 +627,8 @@ github_pr_linking_enabled => 1, github_push_comment_enabled => 1, github_pr_status_enabled => 1, + hackbot_enabled => 1, + hackbot_runs_enabled => 1, insidergroup => 'core-security-release', last_change_time_non_bot_skip_list => 'automation@bmo.tld', last_visit_keep_days => '28', From fdfe8cfae9e08ec3867aba53a7e938c1dcc69def Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Thu, 12 Nov 2026 21:19:11 -0500 Subject: [PATCH 2/4] Remove hackbot_enabled redundant setting --- extensions/Hackbot/lib/BugRuns.pm | 7 +++---- extensions/Hackbot/lib/Config.pm | 1 - .../template/en/default/admin/params/hackbot.html.tmpl | 6 +----- scripts/generate_bmo_data.pl | 1 - 4 files changed, 4 insertions(+), 11 deletions(-) diff --git a/extensions/Hackbot/lib/BugRuns.pm b/extensions/Hackbot/lib/BugRuns.pm index 9f2b013b9f..82b4882191 100644 --- a/extensions/Hackbot/lib/BugRuns.pm +++ b/extensions/Hackbot/lib/BugRuns.pm @@ -32,11 +32,10 @@ use Bugzilla::Extension::Hackbot::Constants; use constant CACHE_KEY_PREFIX => 'hackbot_runs.v1.'; -# Whether the runs table is switched on: both the Hackbot master switch and -# the runs feature itself. Checked by the bug page hook and the API alike. +# 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 { - my $params = Bugzilla->params; - return $params->{hackbot_enabled} && $params->{hackbot_runs_enabled}; + return Bugzilla->params->{hackbot_runs_enabled}; } # Returns {ok => 1, runs => [rows]} or {ok => 0} when the API is unreachable. diff --git a/extensions/Hackbot/lib/Config.pm b/extensions/Hackbot/lib/Config.pm index e79714e431..19abb1b639 100644 --- a/extensions/Hackbot/lib/Config.pm +++ b/extensions/Hackbot/lib/Config.pm @@ -17,7 +17,6 @@ our $sortkey = 1310; sub get_param_list { return ( - {name => 'hackbot_enabled', type => 'b', default => 0}, {name => 'hackbot_runs_enabled', type => 'b', default => 0}, { name => 'hackbot_api_url', diff --git a/extensions/Hackbot/template/en/default/admin/params/hackbot.html.tmpl b/extensions/Hackbot/template/en/default/admin/params/hackbot.html.tmpl index c88b1aaed3..8320812c77 100644 --- a/extensions/Hackbot/template/en/default/admin/params/hackbot.html.tmpl +++ b/extensions/Hackbot/template/en/default/admin/params/hackbot.html.tmpl @@ -13,12 +13,8 @@ [% param_descs = { - hackbot_enabled => - 'Master switch for the Hackbot integration. When off, every Hackbot' - _ ' feature is disabled and BMO never contacts the Hackbot API.', hackbot_runs_enabled => - 'Show the Hackbot runs for a bug on the bug page (editbugs members only).' - _ ' Has no effect unless hackbot_enabled is on.', + 'Show the Hackbot runs for a bug on the bug page (editbugs members only).', hackbot_api_url => 'Base URL of the Hackbot API, ending with a slash' _ ' (for example https://hackbot-api.example.com/).', diff --git a/scripts/generate_bmo_data.pl b/scripts/generate_bmo_data.pl index d86842ccde..ef0c687f4b 100755 --- a/scripts/generate_bmo_data.pl +++ b/scripts/generate_bmo_data.pl @@ -627,7 +627,6 @@ github_pr_linking_enabled => 1, github_push_comment_enabled => 1, github_pr_status_enabled => 1, - hackbot_enabled => 1, hackbot_runs_enabled => 1, insidergroup => 'core-security-release', last_change_time_non_bot_skip_list => 'automation@bmo.tld', From 50cc3b6997369fc4176ce9c3b848a08cafef1807 Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Mon, 16 Nov 2026 09:48:18 -0500 Subject: [PATCH 3/4] feat: Show Hackbot runs from every origin Bug 2086125. Label each run by what started it: needinfo, @hackbot on a Phabricator revision, Hackbot UI, CI failure, or a follow-up run, falling back to "Hackbot". Decode API responses as UTF-8 exactly once, and handle responses without an ETag, which previously broke the result hash. --- extensions/Hackbot/lib/BugRuns.pm | 30 +++++++++-- extensions/Hackbot/lib/Client.pm | 9 +++- extensions/Hackbot/lib/Constants.pm | 16 +++++- extensions/Hackbot/t/bmo/bug-runs.t | 61 +++++++++++++++++++++-- extensions/Hackbot/web/js/hackbot_runs.js | 12 +++-- 5 files changed, 111 insertions(+), 17 deletions(-) diff --git a/extensions/Hackbot/lib/BugRuns.pm b/extensions/Hackbot/lib/BugRuns.pm index 82b4882191..82de68ac79 100644 --- a/extensions/Hackbot/lib/BugRuns.pm +++ b/extensions/Hackbot/lib/BugRuns.pm @@ -30,6 +30,9 @@ 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 @@ -126,18 +129,36 @@ sub _trim { 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 - = $run->{needinfo} ? 'needinfo' - : ($run->{requested_via} // '') =~ HACKBOT_UI_ACCOUNT_REGEX ? 'ui' - : 'other'; + my $origin = _origin($run); return { run_id => $run->{run_id}, @@ -148,6 +169,7 @@ sub _summarize { : 'unknown', created_at => $run->{created_at}, origin => $origin, + revision => $origin eq 'phabricator' ? $run->{revision_id} : undef, requester => _requester_nick($run->{requested_by}, $nicks), }; } diff --git a/extensions/Hackbot/lib/Client.pm b/extensions/Hackbot/lib/Client.pm index 95988a9467..ccd2fb51c5 100644 --- a/extensions/Hackbot/lib/Client.pm +++ b/extensions/Hackbot/lib/Client.pm @@ -92,15 +92,20 @@ sub _get { return {ok => 0, status => $response->code, errmsg => $response->status_line}; } - my $data = eval { decode_json($response->decoded_content) }; + # decode_json expects UTF-8 bytes, so undo only the transfer encoding (gzip) + # here. Letting LWP also decode the charset would hand decode_json + # characters, and any non-ASCII text (e.g. a run's comment) would fail. + my $data = eval { decode_json($response->decoded_content(charset => 'none')) }; if ($@) { return {ok => 0, status => $response->code, errmsg => "JSON parse error: $@"}; } + # scalar: with no ETag header, header() in list context returns an empty + # list, which would shift every later pair in this hash. return { ok => 1, status => $response->code, - etag => $response->header('ETag'), + etag => scalar $response->header('ETag'), data => $data, }; } diff --git a/extensions/Hackbot/lib/Constants.pm b/extensions/Hackbot/lib/Constants.pm index 2e0fcf20c3..260157f4df 100644 --- a/extensions/Hackbot/lib/Constants.pm +++ b/extensions/Hackbot/lib/Constants.pm @@ -23,6 +23,9 @@ our @EXPORT = qw( HACKBOT_RUN_STATUSES HACKBOT_RUN_ID_REGEX HACKBOT_UI_ACCOUNT_REGEX + HACKBOT_CI_ACCOUNT_REGEX + HACKBOT_PHABRICATOR_DEDUPE_REGEX + HACKBOT_FOLLOWUP_DEDUPE_REGEX ); # Only members of this group see Hackbot runs, on the bug page or via the API. @@ -59,8 +62,17 @@ use constant HACKBOT_RUN_ID_REGEX => qr{ - [[:xdigit:]]{4} - [[:xdigit:]]{12} $ }x; -# `requested_via` holds the service account that created the run. Runs -# started from the Hackbot UI come from its Cloud Run service account. +# How a run's origin is recognised. Hackbot has no explicit origin field, so +# these follow its conventions (see BugRuns::_origin for the order applied): +# - `requested_via` holds the service account that created the run, so +# Hackbot UI and pulse listener (CI) runs are told apart by their Cloud +# Run service accounts. +# - `dedupe_key` is set by each trigger: "phab-txn:" for an @hackbot +# mention on a Phabricator revision, "parent-" for a run started +# by another run. use constant HACKBOT_UI_ACCOUNT_REGEX => qr/^hackbot-ui-run@/; +use constant HACKBOT_CI_ACCOUNT_REGEX => qr/^hackbot-pulse-listener-run@/; +use constant HACKBOT_PHABRICATOR_DEDUPE_REGEX => qr/^phab-txn:/; +use constant HACKBOT_FOLLOWUP_DEDUPE_REGEX => qr/^parent-/; 1; diff --git a/extensions/Hackbot/t/bmo/bug-runs.t b/extensions/Hackbot/t/bmo/bug-runs.t index 275c2f8a13..b40583341e 100644 --- a/extensions/Hackbot/t/bmo/bug-runs.t +++ b/extensions/Hackbot/t/bmo/bug-runs.t @@ -135,6 +135,27 @@ subtest 'client: request shape and conditional requests' => sub { is($ua->{requests}[1]{headers}{'If-None-Match'}, '"v1"', 'sends If-None-Match'); }; +subtest 'client: response without an ETag' => sub { + my ($client) = client_with(response(200, encode_json([run_doc()]))); + my $result = $client->bug_runs($BUG_ID); + ok($result->{ok}, 'ok without an ETag'); + is(scalar @{$result->{runs} // []}, 1, 'runs are returned'); + is($result->{etag}, undef, 'no ETag to revalidate with'); +}; + +subtest 'client: UTF-8 response with a charset' => sub { + my $comment = "Caf\x{e9} \x{2014} \x{2713}"; + my $body + = encode_json([run_doc(inputs => {bug_id => $BUG_ID, comment => $comment})]); + my ($client) = client_with(HTTP::Response->new( + 200, undef, ['Content-Type' => 'application/json; charset=utf-8'], $body + )); + + my $result = $client->bug_runs($BUG_ID); + ok($result->{ok}, 'non-ASCII JSON is parsed'); + is($result->{runs}[0]{inputs}{comment}, $comment, 'text is decoded once'); +}; + subtest 'client: failures' => sub { my ($client) = client_with( response(500, 'oops'), @@ -197,19 +218,49 @@ subtest 'origin and requester' => sub { requested_by => 'not-a-bmo-user@example.test', status => 'something-new', ), - run_doc(run_id => '00000000-0000-0000-0000-000000000003'), + run_doc( + run_id => '00000000-0000-0000-0000-000000000003', + dedupe_key => 'phab-txn:PHID-XACT-DREV-abc123', + inputs => {bug_id => $BUG_ID, revision_id => 218404}, + ), + run_doc( + run_id => '00000000-0000-0000-0000-000000000004', + requested_via => 'hackbot-pulse-listener-run@p.iam.gserviceaccount.com', + ), + run_doc( + run_id => '00000000-0000-0000-0000-000000000005', + dedupe_key => 'parent-0f6a2c1e-7d1b-4c3e-9a55-2b8e6f1d4c90', + + # Whichever component started it, a follow-up is labelled as one. + requested_via => 'hackbot-api@p.iam.gserviceaccount.com', + ), + run_doc( + run_id => '00000000-0000-0000-0000-000000000006', + dedupe_key => 'phab-txn:PHID-XACT-DREV-def456', # no revision_id + ), + run_doc(run_id => '00000000-0000-0000-0000-000000000007'), ], }); - my ($needinfo, $ui, $other) = @{fetch($client)->{runs}}; + my ($needinfo, $ui, $phab, $ci, $followup, $phab_no_rev, $other) + = @{fetch($client)->{runs}}; is($needinfo->{origin}, 'needinfo', 'needinfo run'); is($needinfo->{requester}, $requester->nick, 'requester shown by nick'); is($ui->{origin}, 'ui', 'Hackbot UI run'); is($ui->{requester}, undef, 'unknown email has no requester'); is($ui->{status}, 'unknown', 'unrecognized status'); - is($other->{origin}, 'other', 'anything else'); - ok(!grep({ exists $_->{requested_by} } $needinfo, $ui, $other), - 'requester emails are not exposed'); + is($phab->{origin}, 'phabricator', 'Phabricator @hackbot run'); + is($phab->{revision}, 218404, 'with its revision'); + is($ci->{origin}, 'ci', 'CI (pulse listener) run'); + is($followup->{origin}, 'followup', 'follow-up run'); + is($phab_no_rev->{origin}, 'other', 'Phabricator key without a revision'); + is($other->{origin}, 'other', 'anything else'); + is($other->{revision}, undef, 'revision only for Phabricator'); + ok( + !grep({ exists $_->{requested_by} || exists $_->{dedupe_key} } $needinfo, + $ui, $phab, $ci, $followup, $other), + 'requester emails and dedupe keys are not exposed' + ); }; SKIP: { diff --git a/extensions/Hackbot/web/js/hackbot_runs.js b/extensions/Hackbot/web/js/hackbot_runs.js index 80340481a8..10ede98b23 100644 --- a/extensions/Hackbot/web/js/hackbot_runs.js +++ b/extensions/Hackbot/web/js/hackbot_runs.js @@ -14,10 +14,14 @@ const HackbotRuns = { unknown: { label: "unknown", cls: "hb-status-pending" }, }, + // Labels for the origin codes set by BugRuns::_origin on the server. ORIGIN_LABELS: { - needinfo: "needinfo", - ui: "Hackbot UI", - other: "Hackbot", + needinfo: () => "needinfo", + phabricator: (run) => `@hackbot on D${run.revision}`, + ui: () => "Hackbot UI", + ci: () => "CI failure", + followup: () => "follow-up run", + other: () => "Hackbot", }, cell(className, content) { @@ -40,7 +44,7 @@ const HackbotRuns = { }, originText(run) { - const label = this.ORIGIN_LABELS[run.origin] || this.ORIGIN_LABELS.other; + const label = (this.ORIGIN_LABELS[run.origin] || this.ORIGIN_LABELS.other)(run); return run.requester ? `${label} by :${run.requester}` : label; }, From fbb98a10e0504483e6be82181cb98dd42fbc1d70 Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Tue, 17 Nov 2026 10:56:13 -0500 Subject: [PATCH 4/4] Copilot review fixes --- extensions/Hackbot/lib/BugRuns.pm | 18 ++++++++++---- extensions/Hackbot/lib/Client.pm | 16 ++++++------- extensions/Hackbot/lib/Constants.pm | 3 ++- extensions/Hackbot/t/bmo/bug-runs.t | 37 ++++++++++++++++++++++------- 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/extensions/Hackbot/lib/BugRuns.pm b/extensions/Hackbot/lib/BugRuns.pm index 82de68ac79..abcec443d4 100644 --- a/extensions/Hackbot/lib/BugRuns.pm +++ b/extensions/Hackbot/lib/BugRuns.pm @@ -86,7 +86,7 @@ sub _refresh { my $runs = $response->{not_modified} ? $stale->{runs} - : [grep {defined} map { _trim($_, $bug_id) } @{$response->{runs}}]; + : _trim_all($response->{runs}, $bug_id); my $entry = { runs => $runs, @@ -103,6 +103,15 @@ sub _store { }); } +# 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) = @_; @@ -110,10 +119,9 @@ sub _trim { 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 API filters by bug_id, but an - # API that ignored the filter (an older deploy treats it as an unknown query - # parameter) would return every run, including runs on bugs this user may - # not be allowed to see. + # 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; diff --git a/extensions/Hackbot/lib/Client.pm b/extensions/Hackbot/lib/Client.pm index ccd2fb51c5..c27401956d 100644 --- a/extensions/Hackbot/lib/Client.pm +++ b/extensions/Hackbot/lib/Client.pm @@ -47,21 +47,19 @@ sub bug_runs { unless $self->{base_url}; # $bug_id is a validated integer; the base URL ends in a slash (check_urlbase). - my $url - = $self->{base_url} - . 'runs?bug_id=' - . int($bug_id) - . '&limit=' - . HACKBOT_MAX_RUNS; + # The endpoint returns every run for the bug as {runs => [...]}; it takes no + # limit, so BugRuns caps the list. + my $url = $self->{base_url} . 'bugs/' . int($bug_id) . '/runs'; my $result = $self->_get($url, $etag); return $result if !$result->{ok} || $result->{not_modified}; - unless (ref $result->{data} eq 'ARRAY') { + my $data = $result->{data}; + unless (ref $data eq 'HASH' && ref $data->{runs} eq 'ARRAY') { return { ok => 0, status => $result->{status}, - errmsg => 'response is not a list' + errmsg => 'response has no runs list' }; } @@ -69,7 +67,7 @@ sub bug_runs { ok => 1, not_modified => 0, etag => $result->{etag}, - runs => $result->{data}, + runs => $data->{runs}, }; } diff --git a/extensions/Hackbot/lib/Constants.pm b/extensions/Hackbot/lib/Constants.pm index 260157f4df..37c312c9dd 100644 --- a/extensions/Hackbot/lib/Constants.pm +++ b/extensions/Hackbot/lib/Constants.pm @@ -35,7 +35,8 @@ use constant HACKBOT_GROUP => 'editbugs'; # asynchronously, so a slow API delays only the table, never the bug page. use constant HACKBOT_API_TIMEOUT => 10; -# The most runs shown for one bug (sent as the API's `limit`; newest first). +# The most runs shown for one bug. The API returns them all, newest first, and +# BugRuns keeps this many. use constant HACKBOT_MAX_RUNS => 50; # Freshness window: how long a cached run list is served without contacting diff --git a/extensions/Hackbot/t/bmo/bug-runs.t b/extensions/Hackbot/t/bmo/bug-runs.t index b40583341e..7a82f3dfc4 100644 --- a/extensions/Hackbot/t/bmo/bug-runs.t +++ b/extensions/Hackbot/t/bmo/bug-runs.t @@ -111,8 +111,8 @@ sub fetch { subtest 'client: request shape and conditional requests' => sub { my ($client, $ua) = client_with( - response(200, encode_json([run_doc()]), ETag => '"v1"'), - response(304, '', ETag => '"v1"'), + response(200, encode_json({runs => [run_doc()]}), ETag => '"v1"'), + response(304, '', ETag => '"v1"'), ); my $result = $client->bug_runs($BUG_ID); @@ -123,8 +123,8 @@ subtest 'client: request shape and conditional requests' => sub { my $request = $ua->{requests}[0]; is( $request->{url}, - "https://hackbot-api.example.test/runs?bug_id=$BUG_ID&limit=50", - 'lists runs filtered by bug id' + "https://hackbot-api.example.test/bugs/$BUG_ID/runs", + 'lists the runs for the bug' ); is($request->{headers}{'X-API-Key'}, 'secret-key', 'sends the API key'); ok(!exists $request->{headers}{'If-None-Match'}, @@ -136,7 +136,7 @@ subtest 'client: request shape and conditional requests' => sub { }; subtest 'client: response without an ETag' => sub { - my ($client) = client_with(response(200, encode_json([run_doc()]))); + my ($client) = client_with(response(200, encode_json({runs => [run_doc()]}))); my $result = $client->bug_runs($BUG_ID); ok($result->{ok}, 'ok without an ETag'); is(scalar @{$result->{runs} // []}, 1, 'runs are returned'); @@ -145,8 +145,8 @@ subtest 'client: response without an ETag' => sub { subtest 'client: UTF-8 response with a charset' => sub { my $comment = "Caf\x{e9} \x{2014} \x{2713}"; - my $body - = encode_json([run_doc(inputs => {bug_id => $BUG_ID, comment => $comment})]); + my $body = encode_json( + {runs => [run_doc(inputs => {bug_id => $BUG_ID, comment => $comment})]}); my ($client) = client_with(HTTP::Response->new( 200, undef, ['Content-Type' => 'application/json; charset=utf-8'], $body )); @@ -159,11 +159,13 @@ subtest 'client: UTF-8 response with a charset' => sub { subtest 'client: failures' => sub { my ($client) = client_with( response(500, 'oops'), - response(200, '{"not":"a list"}'), + response(200, '{"not":"runs"}'), + response(200, encode_json([run_doc()])), response(200, 'not json'), ); ok(!$client->bug_runs($BUG_ID)->{ok}, 'HTTP error is not ok'); - ok(!$client->bug_runs($BUG_ID)->{ok}, 'non-list body is not ok'); + ok(!$client->bug_runs($BUG_ID)->{ok}, 'body without runs is not ok'); + ok(!$client->bug_runs($BUG_ID)->{ok}, 'bare list body is not ok'); ok(!$client->bug_runs($BUG_ID)->{ok}, 'invalid JSON is not ok'); my $unconfigured = Bugzilla::Extension::Hackbot::Client->new( @@ -202,6 +204,23 @@ subtest 'runs are trimmed to valid runs for this bug' => sub { is($run->{created_at}, '2026-11-12T16:24:39.123456Z', 'created_at'); }; +subtest 'runs are capped at the newest HACKBOT_MAX_RUNS' => sub { + reset_cache(); + my $max = Bugzilla::Extension::Hackbot::Constants::HACKBOT_MAX_RUNS; + my @docs = map { + run_doc(run_id => sprintf('00000000-0000-0000-0000-%012d', $_)) + } 1 .. $max + 5; + + # Another bug's run is dropped and does not count towards the cap. + unshift @docs, run_doc(inputs => {bug_id => $BUG_ID + 1}); + my $client = FakeClient->new({ok => 1, runs => \@docs}); + + my $runs = fetch($client)->{runs}; + is(scalar @$runs, $max, 'list is capped'); + like($runs->[0]{url}, qr/-000000000001$/, 'newest run is kept'); + like($runs->[-1]{url}, qr/-0*\Q$max\E$/, 'cut after the cap'); +}; + subtest 'origin and requester' => sub { reset_cache(); my $client = FakeClient->new({