2014-03-08 02:05:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class DifferentialRevisionIDField
|
|
|
|
extends DifferentialCustomField {
|
|
|
|
|
2014-03-14 19:50:22 +01:00
|
|
|
private $revisionID;
|
|
|
|
|
2014-03-08 02:05:00 +01:00
|
|
|
public function getFieldKey() {
|
2014-03-08 18:13:51 +01:00
|
|
|
return 'differential:revision-id';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFieldKeyForConduit() {
|
2014-03-08 02:05:00 +01:00
|
|
|
return 'revisionID';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFieldName() {
|
|
|
|
return pht('Differential Revision');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFieldDescription() {
|
|
|
|
return pht(
|
|
|
|
'Ties commits to revisions and provides a permananent link between '.
|
|
|
|
'them.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canDisableField() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shouldAppearInCommitMessage() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parseValueFromCommitMessage($value) {
|
2015-05-07 20:10:23 +02:00
|
|
|
// If the value is just "D123" or similar, parse the ID from it directly.
|
|
|
|
$value = trim($value);
|
|
|
|
$matches = null;
|
|
|
|
if (preg_match('/^[dD]([1-9]\d*)\z/', $value, $matches)) {
|
|
|
|
return (int)$matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, try to extract a URI value.
|
2014-03-09 20:55:57 +01:00
|
|
|
return self::parseRevisionIDFromURI($value);
|
2014-03-08 02:05:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function renderCommitMessageValue(array $handles) {
|
2014-03-14 19:50:22 +01:00
|
|
|
$id = coalesce($this->revisionID, $this->getObject()->getID());
|
|
|
|
if (!$id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return PhabricatorEnv::getProductionURI('/D'.$id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function readValueFromCommitMessage($value) {
|
|
|
|
$this->revisionID = $value;
|
2014-03-08 02:05:00 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 02:08:57 +02:00
|
|
|
private static function parseRevisionIDFromURI($uri_string) {
|
|
|
|
$uri = new PhutilURI($uri_string);
|
|
|
|
$path = $uri->getPath();
|
2014-03-09 20:55:57 +01:00
|
|
|
|
|
|
|
$matches = null;
|
|
|
|
if (preg_match('#^/D(\d+)$#', $path, $matches)) {
|
|
|
|
$id = (int)$matches[1];
|
2014-10-14 02:08:57 +02:00
|
|
|
|
|
|
|
$prod_uri = new PhutilURI(PhabricatorEnv::getProductionURI('/D'.$id));
|
|
|
|
|
2014-03-09 20:55:57 +01:00
|
|
|
// Make sure the URI is the same as our URI. Basically, we want to ignore
|
|
|
|
// commits from other Phabricator installs.
|
2014-10-14 02:08:57 +02:00
|
|
|
if ($uri->getDomain() == $prod_uri->getDomain()) {
|
2014-03-09 20:55:57 +01:00
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$allowed_uris = PhabricatorEnv::getAllowedURIs('/D'.$id);
|
|
|
|
|
|
|
|
foreach ($allowed_uris as $allowed_uri) {
|
2014-10-14 02:08:57 +02:00
|
|
|
if ($uri_string == $allowed_uri) {
|
2014-03-09 20:55:57 +01:00
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-03-08 02:05:00 +01:00
|
|
|
}
|