Skip to content

Commit

Permalink
Bug 1769284 - Show phabricator links in user_profile
Browse files Browse the repository at this point in the history
  • Loading branch information
arai-a committed May 18, 2022
1 parent c1e3a33 commit 8603e78
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 1 deletion.
3 changes: 3 additions & 0 deletions extensions/PhabBugz/Extension.pm
Expand Up @@ -33,6 +33,9 @@ sub template_before_process {
return unless Bugzilla->user->id;
return unless Bugzilla->params->{phabricator_enabled};
return unless Bugzilla->params->{phabricator_base_uri};

$vars->{phabricator_available} = 1;

return unless $file =~ /bug_modal\/(header|edit).html.tmpl$/;

if (my $bug = exists $vars->{'bugs'} ? $vars->{'bugs'}[0] : $vars->{'bug'}) {
Expand Down
60 changes: 60 additions & 0 deletions extensions/PhabBugz/lib/WebService.pm
Expand Up @@ -205,6 +205,57 @@ sub bug_revisions {
return {revisions => \@revisions};
}

sub user {
state $check = compile(Object, Dict [user_id => Int]);
my ($self, $params) = $check->(@_);

$self->_check_phabricator();

Bugzilla->login(LOGIN_REQUIRED);

my $bmo_user_id = $params->{user_id};

my $response = request(
'bmoexternalaccount.search',
{
accountids => [$bmo_user_id],
}
);
if (scalar(@{$response->{result}}) == 0) {
return {};
}

my $phid = $response->{result}[0]{phid};

$response = request(
'user.query',
{
phids => [$phid],
}
);
if (scalar(@{$response->{result}}) == 0) {
return {};
}

my $user_name = $response->{result}[0]{userName};
my $real_name = $response->{result}[0]{realName};

my $base_url = Bugzilla->params->{phabricator_base_uri};
$base_url =~ s{/$}{};
my $user_url = "$base_url/p/$user_name/";
my $revisions_url = "$base_url/differential/?responsiblePHIDs%5B0%5D=$phid&statuses%5B0%5D=open()&order=newest&bucket=action";

return {
user => {
phid => $phid,
userName => $user_name,
realName => $real_name,
userURL => $user_url,
revisionsURL => $revisions_url,
},
};
}

sub rest_resources {
return [
# Bug permission checks
Expand Down Expand Up @@ -235,6 +286,15 @@ sub rest_resources {
},
},
},
qr{^/phabbugz/user/(\d+)$},
{
GET => {
method => 'user',
params => sub {
return {user_id => $_[0]};
},
},
},
];
}

Expand Down
54 changes: 54 additions & 0 deletions extensions/PhabBugz/web/js/phab_user.js
@@ -0,0 +1,54 @@
/* 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.
*/

var PhabUser = {};

PhabUser.getUser = async () => {
var userCell = document.getElementById("phab_user");
var revisionsCell = document.getElementById("phab_revisions");
if (!userCell || !revisionsCell) {
return;
}

var user_id = userCell.getAttribute("data-target-user-id");

userCell.textContent = "Loading...";
revisionsCell.textContent = "Loading...";

function displayLoadError(errStr) {
userCell.textContent = errStr;
revisionsCell.textContent = "";
}

try {
var { user } = await Bugzilla.API.get(`phabbugz/user/${user_id}`);
if (!user) {
userCell.textContent = "Not Found";
revisionsCell.textContent = "Not Found";
return;
}

var userLink = document.createElement("a");
userLink.setAttribute("href", user.userURL);
userLink.textContent = `${user.userName} (${user.realName})`;
userCell.textContent = "";
userCell.appendChild(userLink);

var revisionsLink = document.createElement("a");
revisionsLink.setAttribute("href", user.revisionsURL);
revisionsLink.textContent = "Open revisions";
revisionsCell.textContent = "";
revisionsCell.appendChild(revisionsLink);
} catch ({ message }) {
displayLoadError('Error: ' + message);
}
};

document.addEventListener("DOMContentLoaded", function(event) {
PhabUser.getUser();
});
Expand Up @@ -13,11 +13,18 @@
[% ELSE %]
[% filtered_identity = target.name || target.address.user FILTER html %]
[% END %]

[% javascript_urls = [ "js/field.js" ] %]

[% IF phabricator_available %]
[% javascript_urls.push('extensions/PhabBugz/web/js/phab_user.js') %]
[% END %]

[% PROCESS global/header.html.tmpl
title = "User Profile: $filtered_identity"
generate_api_token = 1
style_urls = [ "extensions/UserProfile/web/styles/user_profile.css" ]
javascript_urls = [ "js/field.js" ]
javascript_urls = javascript_urls
%]

<table id="user_profile_table">
Expand Down Expand Up @@ -174,6 +181,25 @@
</tr>
[% END %]

[% IF phabricator_available %]
<tr>
<td colspan="4" class="separator"><hr></td>
</tr>
<tr>
<td>Phabricator</td>
</tr>
<tr>
<td>&nbsp;</td>
<th>User</th>
<td colspan="2" id="phab_user" data-target-user-id="[% target.id FILTER html %]"></td>
</tr>
<tr>
<td>&nbsp;</td>
<th>Revisions</th>
<td colspan="2" id="phab_revisions"></td>
</tr>
[% END %]

<tr>
<td colspan="4" class="separator"><hr></td>
</tr>
Expand Down

0 comments on commit 8603e78

Please sign in to comment.