1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-30 01:10:58 +01:00

Fix JIRA issue URI selection for JIRA installs which are not on the domain root

Summary: Fixes T4859. See that for details.

Test Plan:
  - Verified things still work on my local (domain root) install.
  - Added some unit tests.
  - Did not verify a non-root install since I don't have one handy, hopefully @salehe can help.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: salehe, epriestley

Maniphest Tasks: T4859

Differential Revision: https://secure.phabricator.com/D8836
This commit is contained in:
epriestley 2014-05-18 05:45:21 -07:00
parent 15c408be1d
commit 7a9d5f8f2d
4 changed files with 68 additions and 5 deletions

View file

@ -623,6 +623,7 @@ phutil_register_library_map(array(
'DoorkeeperBridge' => 'applications/doorkeeper/bridge/DoorkeeperBridge.php', 'DoorkeeperBridge' => 'applications/doorkeeper/bridge/DoorkeeperBridge.php',
'DoorkeeperBridgeAsana' => 'applications/doorkeeper/bridge/DoorkeeperBridgeAsana.php', 'DoorkeeperBridgeAsana' => 'applications/doorkeeper/bridge/DoorkeeperBridgeAsana.php',
'DoorkeeperBridgeJIRA' => 'applications/doorkeeper/bridge/DoorkeeperBridgeJIRA.php', 'DoorkeeperBridgeJIRA' => 'applications/doorkeeper/bridge/DoorkeeperBridgeJIRA.php',
'DoorkeeperBridgeJIRATestCase' => 'applications/doorkeeper/bridge/__tests__/DoorkeeperBridgeJIRATestCase.php',
'DoorkeeperDAO' => 'applications/doorkeeper/storage/DoorkeeperDAO.php', 'DoorkeeperDAO' => 'applications/doorkeeper/storage/DoorkeeperDAO.php',
'DoorkeeperExternalObject' => 'applications/doorkeeper/storage/DoorkeeperExternalObject.php', 'DoorkeeperExternalObject' => 'applications/doorkeeper/storage/DoorkeeperExternalObject.php',
'DoorkeeperExternalObjectQuery' => 'applications/doorkeeper/query/DoorkeeperExternalObjectQuery.php', 'DoorkeeperExternalObjectQuery' => 'applications/doorkeeper/query/DoorkeeperExternalObjectQuery.php',
@ -3276,6 +3277,7 @@ phutil_register_library_map(array(
'DoorkeeperBridge' => 'Phobject', 'DoorkeeperBridge' => 'Phobject',
'DoorkeeperBridgeAsana' => 'DoorkeeperBridge', 'DoorkeeperBridgeAsana' => 'DoorkeeperBridge',
'DoorkeeperBridgeJIRA' => 'DoorkeeperBridge', 'DoorkeeperBridgeJIRA' => 'DoorkeeperBridge',
'DoorkeeperBridgeJIRATestCase' => 'PhabricatorTestCase',
'DoorkeeperDAO' => 'PhabricatorLiskDAO', 'DoorkeeperDAO' => 'PhabricatorLiskDAO',
'DoorkeeperExternalObject' => 'DoorkeeperExternalObject' =>
array( array(

View file

@ -110,10 +110,34 @@ final class DoorkeeperBridgeJIRA extends DoorkeeperBridge {
// Convert the "self" URI, which points at the REST endpoint, into a // Convert the "self" URI, which points at the REST endpoint, into a
// browse URI. // browse URI.
$self = idx($result, 'self'); $self = idx($result, 'self');
$uri = new PhutilURI($self); $object_id = $obj->getObjectID();
$uri->setPath('browse/'.$obj->getObjectID());
$obj->setObjectURI((string)$uri); $uri = self::getJIRAIssueBrowseURIFromJIRARestURI($self, $object_id);
if ($uri !== null) {
$obj->setObjectURI($uri);
}
}
public static function getJIRAIssueBrowseURIFromJIRARestURI(
$uri,
$object_id) {
$uri = new PhutilURI($uri);
// The JIRA install might not be at the domain root, so we may need to
// keep an initial part of the path, like "/jira/". Find the API specific
// part of the URI, strip it off, then replace it with the web version.
$path = $uri->getPath();
$pos = strrpos($path, 'rest/api/2/issue/');
if ($pos === false) {
return null;
}
$path = substr($path, 0, $pos);
$path = $path.'browse/'.$object_id;
$uri->setPath($path);
return (string)$uri;
} }
} }

View file

@ -0,0 +1,37 @@
<?php
final class DoorkeeperBridgeJIRATestCase extends PhabricatorTestCase {
public function testJIRABridgeRestAPIURIConversion() {
$map = array(
array(
// Installed at domain root.
'http://jira.example.com/rest/api/2/issue/1',
'TP-1',
'http://jira.example.com/browse/TP-1'
),
array(
// Installed on path.
'http://jira.example.com/jira/rest/api/2/issue/1',
'TP-1',
'http://jira.example.com/jira/browse/TP-1'
),
array(
// A URI we don't understand.
'http://jira.example.com/wake/cli/3/task/1',
'TP-1',
null,
),
);
foreach ($map as $inputs) {
list($rest_uri, $object_id, $expect) = $inputs;
$this->assertEqual(
$expect,
DoorkeeperBridgeJIRA::getJIRAIssueBrowseURIFromJIRARestURI(
$rest_uri,
$object_id));
}
}
}

View file

@ -5,13 +5,12 @@ final class DoorkeeperRemarkupRuleJIRA
public function apply($text) { public function apply($text) {
return preg_replace_callback( return preg_replace_callback(
'@(https?://[^/]+)/browse/([A-Z]+-[1-9]\d*)@', '@(https?://\S+?)/browse/([A-Z]+-[1-9]\d*)@',
array($this, 'markupJIRALink'), array($this, 'markupJIRALink'),
$text); $text);
} }
public function markupJIRALink($matches) { public function markupJIRALink($matches) {
$match_domain = $matches[1]; $match_domain = $matches[1];
$match_issue = $matches[2]; $match_issue = $matches[2];
@ -21,6 +20,7 @@ final class DoorkeeperRemarkupRuleJIRA
return $matches[0]; return $matches[0];
} }
$jira_base = $provider->getJIRABaseURI(); $jira_base = $provider->getJIRABaseURI();
if ($match_domain != rtrim($jira_base, '/')) { if ($match_domain != rtrim($jira_base, '/')) {
return $matches[0]; return $matches[0];