Skip to content

Commit ff1b4b8

Browse files
authored
Bug 2002639 - Need to add see_also links to Jira in the REST API if available and the request is coming from JBI
1 parent ef83e7c commit ff1b4b8

File tree

5 files changed

+68
-12
lines changed

5 files changed

+68
-12
lines changed

Bugzilla/WebService/Bug.pm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ sub get {
503503
}
504504
}
505505

506+
Bugzilla::Hook::process('webservice_bug_get',
507+
{webservice => $self, user => $user, params => $params, bug_data => \@hashes,});
508+
506509
return {bugs => \@hashes, faults => \@faults};
507510
}
508511

extensions/JiraWebhookSync/Extension.pm

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,8 @@ sub webhook_before_send {
136136

137137
INFO("Processing webhook for bug $bug_id to Jira host $hostname");
138138

139-
# Check if there's a Jira mapping for this bug
140-
if (my $jira_map
141-
= Bugzilla::Extension::JiraWebhookSync::JiraBugMap->get_by_bug_id($bug_id))
142-
{
143-
INFO('Adding Jira see_also to webhook payload: ' . $jira_map->jira_url);
144-
145-
# Add the Jira URL to the see_also array in the payload if not already present
146-
$payload->{bug}->{see_also} ||= [];
147-
if (none { $_ eq $jira_map->jira_url } @{$payload->{bug}->{see_also}}) {
148-
push @{$payload->{bug}->{see_also}}, $jira_map->jira_url;
149-
}
150-
}
139+
# Check if there's a Jira mapping for this bug and add to see also
140+
$payload->{bug}->{see_also} = _populate_jira_see_also($bug_id, $payload->{bug}->{see_also});
151141

152142
# Make copy of the current whiteboard value
153143
my $whiteboard = $payload->{bug}->{whiteboard};
@@ -167,6 +157,26 @@ sub webhook_before_send {
167157
$payload->{bug}->{whiteboard} = _add_whiteboard_tags($whiteboard, \@new_tags);
168158
}
169159

160+
sub webservice_bug_get {
161+
my ($self, $args) = @_;
162+
my $bug_data = $args->{bug_data};
163+
my $user = $args->{user};
164+
my $params = Bugzilla->params;
165+
166+
# Only add see also values for the Jira webhook sync user
167+
if (!$params->{jira_webhook_sync_user} || $user->login ne $params->{jira_webhook_sync_user}) {
168+
INFO('Not the jira_webhook_sync_user so skipping see also population');
169+
return;
170+
}
171+
172+
INFO('Jira sync user is accessing REST get bug. Looking for see also values');
173+
174+
foreach my $bug (@{$bug_data}) {
175+
INFO('Processing bug id ' . $bug->{id} . ' for Jira see also population');
176+
$bug->{see_also} = _populate_jira_see_also($bug->{id}, $bug->{see_also});
177+
}
178+
}
179+
170180
# Adds a whiteboard tag to the whiteboard string if it doesn't already exist.
171181
# Returns the whiteboard value with the tag in [brackets] format.
172182
# If the tag already exists, returns the whiteboard unchanged.
@@ -203,4 +213,22 @@ sub _bug_matches_rule {
203213
return 0;
204214
}
205215

216+
# Adds the Jira issue URL associated with the given Bugzilla bug ID to the see_also list,
217+
# ensuring no duplicates. Returns the updated list.
218+
sub _populate_jira_see_also {
219+
my ($bug_id, $see_also_list) = @_;
220+
221+
$see_also_list ||= [];
222+
223+
if (my $jira_map
224+
= Bugzilla::Extension::JiraWebhookSync::JiraBugMap->get_by_bug_id($bug_id))
225+
{
226+
INFO('Adding Jira see_also to webhook payload: ' . $jira_map->jira_url);
227+
push @{$see_also_list}, $jira_map->jira_url
228+
if none { $_ eq $jira_map->jira_url } @{$see_also_list};
229+
}
230+
231+
return $see_also_list;
232+
}
233+
206234
__PACKAGE__->NAME;

extensions/JiraWebhookSync/lib/Config.pm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ sub get_param_list {
2121

2222
my @params = (
2323
{name => 'jira_webhook_sync_hostname', type => 't', default => ''},
24+
{
25+
name => 'jira_webhook_sync_user',
26+
type => 't',
27+
default => '',
28+
},
2429
{
2530
name => 'jira_webhook_sync_config',
2631
type => 'l',

extensions/JiraWebhookSync/t/bmo/jira_webhook_sync.t

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ set_parameters(
3131
},
3232
'Jira Webhook Sync' => {
3333
'jira_webhook_sync_hostname' => {type => 'text', value => 'externalapi.test'},
34+
'jira_webhook_sync_user' => {type => 'text', value => $config->{editbugs_user_login}},
3435
'jira_webhook_sync_config' => {
3536
type => 'text',
3637
value => '{"BZFF": {"product": "Firefox", "component": "General"}}'
@@ -139,6 +140,24 @@ $t->get_ok('http://externalapi.test:8001/webhooks/last_payload')
139140
->json_is('/bug/whiteboard', '[BZFF]')
140141
->json_is('/bug/see_also/0', $jira_url);
141142

143+
# When using the REST API to get bug details, and the calling user
144+
# is the jira sync user, then we need to make sure the hidden see
145+
# also values are included in the response.
146+
$t->get_ok($config->{browser_url}
147+
. "/rest/bug/$bug_id_1" =>
148+
{'X-Bugzilla-API-Key' => $config->{editbugs_user_api_key}})
149+
->status_is(200)
150+
->json_is('/bugs/0/id', $bug_id_1)
151+
->json_is('/bugs/0/see_also/0', $jira_url);
152+
153+
# And make sure the see also values are not visible to other users
154+
$t->get_ok($config->{browser_url}
155+
. "/rest/bug/$bug_id_1" =>
156+
{'X-Bugzilla-API-Key' => $config->{admin_user_api_key}})
157+
->status_is(200)
158+
->json_is('/bugs/0/id', $bug_id_1)
159+
->json_hasnt('/bugs/0/see_also/0');
160+
142161
# Turn off webhooks and jira sync.
143162
log_in($sel, $config, 'admin');
144163
set_parameters(

extensions/JiraWebhookSync/template/en/default/admin/params/jirawebhooksync.html.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ JBI config files</a>.
3737
[%
3838
param_descs = {
3939
jira_webhook_sync_hostname => "Only add whiteboard tag if the Jira hostname matches. Leave this blank to disable this feature.",
40+
jira_webhook_sync_user => "The Bugzilla account used by Jira Bugzilla Integration service.",
4041
jira_webhook_sync_config => jira_webhook_sync_config_desc,
4142
jira_webhook_sync_project_keys => 'List of Jira project keys for which see_also values should be stored in a mapping table instead of as regular see_also links.'
4243
}

0 commit comments

Comments
 (0)