1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-19 16:58:48 +02: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',
'DoorkeeperBridgeAsana' => 'applications/doorkeeper/bridge/DoorkeeperBridgeAsana.php',
'DoorkeeperBridgeJIRA' => 'applications/doorkeeper/bridge/DoorkeeperBridgeJIRA.php',
'DoorkeeperBridgeJIRATestCase' => 'applications/doorkeeper/bridge/__tests__/DoorkeeperBridgeJIRATestCase.php',
'DoorkeeperDAO' => 'applications/doorkeeper/storage/DoorkeeperDAO.php',
'DoorkeeperExternalObject' => 'applications/doorkeeper/storage/DoorkeeperExternalObject.php',
'DoorkeeperExternalObjectQuery' => 'applications/doorkeeper/query/DoorkeeperExternalObjectQuery.php',
@ -3276,6 +3277,7 @@ phutil_register_library_map(array(
'DoorkeeperBridge' => 'Phobject',
'DoorkeeperBridgeAsana' => 'DoorkeeperBridge',
'DoorkeeperBridgeJIRA' => 'DoorkeeperBridge',
'DoorkeeperBridgeJIRATestCase' => 'PhabricatorTestCase',
'DoorkeeperDAO' => 'PhabricatorLiskDAO',
'DoorkeeperExternalObject' =>
array(

View file

@ -110,10 +110,34 @@ final class DoorkeeperBridgeJIRA extends DoorkeeperBridge {
// Convert the "self" URI, which points at the REST endpoint, into a
// browse URI.
$self = idx($result, 'self');
$uri = new PhutilURI($self);
$uri->setPath('browse/'.$obj->getObjectID());
$object_id = $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) {
return preg_replace_callback(
'@(https?://[^/]+)/browse/([A-Z]+-[1-9]\d*)@',
'@(https?://\S+?)/browse/([A-Z]+-[1-9]\d*)@',
array($this, 'markupJIRALink'),
$text);
}
public function markupJIRALink($matches) {
$match_domain = $matches[1];
$match_issue = $matches[2];
@ -21,6 +20,7 @@ final class DoorkeeperRemarkupRuleJIRA
return $matches[0];
}
$jira_base = $provider->getJIRABaseURI();
if ($match_domain != rtrim($jira_base, '/')) {
return $matches[0];