1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-21 14:22:41 +01:00

In the repository URI index, store Phabricator's own URIs as tokens

Summary:
Fixes T13435. If you move Phabricator or copy data from one environment to another, the repository URI index currently still references the old URI, since it writes the URI as a plain string. This may make "arc which" and similar workflows have difficulty identifying repositories.

Instead, store the "phabricator.base-uri" domain and the "diffusion.ssh-host" domain as tokens, so lookups continue to work correctly even after these values change.

Test Plan:
  - Added unit tests to cover the normalization.
  - Ran migration, ran daemons, inspected `repository_uriindex` table, saw a mixture of sensible tokens (for local domains) and static domains (like "github.com").
  - Ran this thing:

```
$ echo '{"remoteURIs": ["ssh://git@local.phacility.com/diffusion/P"]}' | ./bin/conduit call --method repository.query --trace --input -
Reading input from stdin...
>>> [2] (+0) <conduit> repository.query()
>>> [3] (+3) <connect> local_repository
<<< [3] (+3) <connect> 555 us
>>> [4] (+5) <query> SELECT `r`.* FROM `repository` `r` LEFT JOIN `local_repository`.`repository_uriindex` uri ON r.phid = uri.repositoryPHID WHERE (uri.repositoryURI IN ('<base-uri>/diffusion/P')) GROUP BY `r`.phid ORDER BY `r`.`id` DESC LIMIT 101
<<< [4] (+5) <query> 596 us
<<< [2] (+6) <conduit> 6,108 us
{
  "result": [
    {
      "id": "1",
      "name": "Phabricator",
      "phid": "PHID-REPO-2psrynlauicce7d3q7g2",
      "callsign": "P",
      "monogram": "rP",
      "vcs": "git",
      "uri": "http://local.phacility.com/source/phabricator/",
      "remoteURI": "https://github.com/phacility/phabricator.git",
      "description": "asdf",
      "isActive": true,
      "isHosted": false,
      "isImporting": false,
      "encoding": "UTF-8",
      "staging": {
        "supported": true,
        "prefix": "phabricator",
        "uri": null
      }
    }
  ]
}
```

Note the `WHERE` clause in the query normalizes the URI into "<base-uri>", and the lookup succeeds.

Subscribers: PHID-OPKG-gm6ozazyms6q6i22gyam

Maniphest Tasks: T13435

Differential Revision: https://secure.phabricator.com/D20872
This commit is contained in:
epriestley 2019-10-28 14:22:07 -07:00
parent 292f8fc612
commit 5d8457a07e
3 changed files with 61 additions and 2 deletions

View file

@ -0,0 +1,4 @@
<?php
PhabricatorRebuildIndexesWorker::rebuildObjectsWithQuery(
'PhabricatorRepositoryQuery');

View file

@ -130,10 +130,35 @@ final class PhabricatorRepositoryURINormalizer extends Phobject {
$domain = $uri->getDomain();
if (!strlen($domain)) {
$domain = '<void>';
return '<void>';
}
return phutil_utf8_strtolower($domain);
$domain = phutil_utf8_strtolower($domain);
// See T13435. If the domain for a repository URI is same as the install
// base URI, store it as a "<base-uri>" token instead of the actual domain
// so that the index does not fall out of date if the install moves.
$base_uri = PhabricatorEnv::getURI('/');
$base_uri = new PhutilURI($base_uri);
$base_domain = $base_uri->getDomain();
$base_domain = phutil_utf8_strtolower($base_domain);
if ($domain === $base_domain) {
return '<base-uri>';
}
// Likewise, store a token for the "SSH Host" domain so it can be changed
// without requiring an index rebuild.
$ssh_host = PhabricatorEnv::getEnvConfig('diffusion.ssh-host');
if (strlen($ssh_host)) {
$ssh_host = phutil_utf8_strtolower($ssh_host);
if ($domain === $ssh_host) {
return '<ssh-host>';
}
}
return $domain;
}

View file

@ -31,6 +31,36 @@ final class PhabricatorRepositoryURINormalizerTestCase
}
}
public function testDomainURINormalizer() {
$base_domain = 'base.phabricator.example.com';
$ssh_domain = 'ssh.phabricator.example.com';
$env = PhabricatorEnv::beginScopedEnv();
$env->overrideEnvConfig('phabricator.base-uri', 'http://'.$base_domain);
$env->overrideEnvConfig('diffusion.ssh-host', $ssh_domain);
$cases = array(
'/' => '<void>',
'/path/to/local/repo.git' => '<void>',
'ssh://user@domain.com/path.git' => 'domain.com',
'ssh://user@DOMAIN.COM/path.git' => 'domain.com',
'http://'.$base_domain.'/diffusion/X/' => '<base-uri>',
'ssh://'.$ssh_domain.'/diffusion/X/' => '<ssh-host>',
'git@'.$ssh_domain.':bananas.git' => '<ssh-host>',
);
$type_git = PhabricatorRepositoryURINormalizer::TYPE_GIT;
foreach ($cases as $input => $expect) {
$normal = new PhabricatorRepositoryURINormalizer($type_git, $input);
$this->assertEqual(
$expect,
$normal->getNormalizedDomain(),
pht('Normalized domain for "%s".', $input));
}
}
public function testSVNURINormalizer() {
$cases = array(
'file:///path/to/repo' => 'path/to/repo',