mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-10 00:42:41 +01:00
Consolidate repository identity resolution and detection code
Summary: Ref T13444. Send all repository identity/detection through a new "DiffusionRepositoryIdentityEngine" which handles resolution and detection updates in one place. Test Plan: - Ran `bin/repository reparse --message ...`, saw author/committer identity updates. - Added "goose@example.com" to my email addresses, ran daemons, saw the identity relationship get picked up. - Ran `bin/repository rebuild-identities ...`, saw sensible rebuilds. Maniphest Tasks: T13444 Differential Revision: https://secure.phabricator.com/D20910
This commit is contained in:
parent
6afbb6102d
commit
0014d0404c
5 changed files with 117 additions and 66 deletions
|
@ -985,6 +985,7 @@ phutil_register_library_map(array(
|
||||||
'DiffusionRepositoryFunctionDatasource' => 'applications/diffusion/typeahead/DiffusionRepositoryFunctionDatasource.php',
|
'DiffusionRepositoryFunctionDatasource' => 'applications/diffusion/typeahead/DiffusionRepositoryFunctionDatasource.php',
|
||||||
'DiffusionRepositoryHistoryManagementPanel' => 'applications/diffusion/management/DiffusionRepositoryHistoryManagementPanel.php',
|
'DiffusionRepositoryHistoryManagementPanel' => 'applications/diffusion/management/DiffusionRepositoryHistoryManagementPanel.php',
|
||||||
'DiffusionRepositoryIdentityEditor' => 'applications/diffusion/editor/DiffusionRepositoryIdentityEditor.php',
|
'DiffusionRepositoryIdentityEditor' => 'applications/diffusion/editor/DiffusionRepositoryIdentityEditor.php',
|
||||||
|
'DiffusionRepositoryIdentityEngine' => 'applications/diffusion/identity/DiffusionRepositoryIdentityEngine.php',
|
||||||
'DiffusionRepositoryIdentitySearchEngine' => 'applications/diffusion/query/DiffusionRepositoryIdentitySearchEngine.php',
|
'DiffusionRepositoryIdentitySearchEngine' => 'applications/diffusion/query/DiffusionRepositoryIdentitySearchEngine.php',
|
||||||
'DiffusionRepositoryLimitsManagementPanel' => 'applications/diffusion/management/DiffusionRepositoryLimitsManagementPanel.php',
|
'DiffusionRepositoryLimitsManagementPanel' => 'applications/diffusion/management/DiffusionRepositoryLimitsManagementPanel.php',
|
||||||
'DiffusionRepositoryListController' => 'applications/diffusion/controller/DiffusionRepositoryListController.php',
|
'DiffusionRepositoryListController' => 'applications/diffusion/controller/DiffusionRepositoryListController.php',
|
||||||
|
@ -6966,6 +6967,7 @@ phutil_register_library_map(array(
|
||||||
'DiffusionRepositoryFunctionDatasource' => 'PhabricatorTypeaheadCompositeDatasource',
|
'DiffusionRepositoryFunctionDatasource' => 'PhabricatorTypeaheadCompositeDatasource',
|
||||||
'DiffusionRepositoryHistoryManagementPanel' => 'DiffusionRepositoryManagementPanel',
|
'DiffusionRepositoryHistoryManagementPanel' => 'DiffusionRepositoryManagementPanel',
|
||||||
'DiffusionRepositoryIdentityEditor' => 'PhabricatorApplicationTransactionEditor',
|
'DiffusionRepositoryIdentityEditor' => 'PhabricatorApplicationTransactionEditor',
|
||||||
|
'DiffusionRepositoryIdentityEngine' => 'Phobject',
|
||||||
'DiffusionRepositoryIdentitySearchEngine' => 'PhabricatorApplicationSearchEngine',
|
'DiffusionRepositoryIdentitySearchEngine' => 'PhabricatorApplicationSearchEngine',
|
||||||
'DiffusionRepositoryLimitsManagementPanel' => 'DiffusionRepositoryManagementPanel',
|
'DiffusionRepositoryLimitsManagementPanel' => 'DiffusionRepositoryManagementPanel',
|
||||||
'DiffusionRepositoryListController' => 'DiffusionController',
|
'DiffusionRepositoryListController' => 'DiffusionController',
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
final class DiffusionRepositoryIdentityEngine
|
||||||
|
extends Phobject {
|
||||||
|
|
||||||
|
private $viewer;
|
||||||
|
private $sourcePHID;
|
||||||
|
|
||||||
|
public function setViewer(PhabricatorUser $viewer) {
|
||||||
|
$this->viewer = $viewer;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getViewer() {
|
||||||
|
return $this->viewer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setSourcePHID($source_phid) {
|
||||||
|
$this->sourcePHID = $source_phid;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSourcePHID() {
|
||||||
|
if (!$this->sourcePHID) {
|
||||||
|
throw new PhutilInvalidStateException('setSourcePHID');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->sourcePHID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function newResolvedIdentity($raw_identity) {
|
||||||
|
$identity = $this->loadRawIdentity($raw_identity);
|
||||||
|
|
||||||
|
if (!$identity) {
|
||||||
|
$identity = $this->newIdentity($raw_identity);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->updateIdentity($identity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function newUpdatedIdentity(PhabricatorRepositoryIdentity $identity) {
|
||||||
|
return $this->updateIdentity($identity);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function loadRawIdentity($raw_identity) {
|
||||||
|
$viewer = $this->getViewer();
|
||||||
|
|
||||||
|
return id(new PhabricatorRepositoryIdentityQuery())
|
||||||
|
->setViewer($viewer)
|
||||||
|
->withIdentityNames(array($raw_identity))
|
||||||
|
->executeOne();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function newIdentity($raw_identity) {
|
||||||
|
$source_phid = $this->getSourcePHID();
|
||||||
|
|
||||||
|
return id(new PhabricatorRepositoryIdentity())
|
||||||
|
->setAuthorPHID($source_phid)
|
||||||
|
->setIdentityName($raw_identity);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function resolveIdentity(PhabricatorRepositoryIdentity $identity) {
|
||||||
|
$raw_identity = $identity->getIdentityName();
|
||||||
|
|
||||||
|
return id(new DiffusionResolveUserQuery())
|
||||||
|
->withName($raw_identity)
|
||||||
|
->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateIdentity(PhabricatorRepositoryIdentity $identity) {
|
||||||
|
$resolved_phid = $this->resolveIdentity($identity);
|
||||||
|
|
||||||
|
$identity
|
||||||
|
->setAutomaticGuessedUserPHID($resolved_phid)
|
||||||
|
->save();
|
||||||
|
|
||||||
|
return $identity;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -3,6 +3,8 @@
|
||||||
final class PhabricatorRepositoryManagementRebuildIdentitiesWorkflow
|
final class PhabricatorRepositoryManagementRebuildIdentitiesWorkflow
|
||||||
extends PhabricatorRepositoryManagementWorkflow {
|
extends PhabricatorRepositoryManagementWorkflow {
|
||||||
|
|
||||||
|
private $identityCache = array();
|
||||||
|
|
||||||
protected function didConstruct() {
|
protected function didConstruct() {
|
||||||
$this
|
$this
|
||||||
->setName('rebuild-identities')
|
->setName('rebuild-identities')
|
||||||
|
@ -94,31 +96,21 @@ final class PhabricatorRepositoryManagementRebuildIdentitiesWorkflow
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getIdentityForCommit(
|
private function getIdentityForCommit(
|
||||||
PhabricatorRepositoryCommit $commit, $identity_name) {
|
PhabricatorRepositoryCommit $commit,
|
||||||
|
$raw_identity) {
|
||||||
|
|
||||||
static $seen = array();
|
if (!isset($this->identityCache[$raw_identity])) {
|
||||||
$identity_key = PhabricatorHash::digestForIndex($identity_name);
|
$viewer = $this->getViewer();
|
||||||
if (empty($seen[$identity_key])) {
|
|
||||||
try {
|
|
||||||
$user_phid = id(new DiffusionResolveUserQuery())
|
|
||||||
->withName($identity_name)
|
|
||||||
->execute();
|
|
||||||
|
|
||||||
$identity = id(new PhabricatorRepositoryIdentity())
|
$identity = id(new DiffusionRepositoryIdentityEngine())
|
||||||
->setAuthorPHID($commit->getPHID())
|
->setViewer($viewer)
|
||||||
->setIdentityName($identity_name)
|
->setSourcePHID($commit->getPHID())
|
||||||
->setAutomaticGuessedUserPHID($user_phid)
|
->newResolvedIdentity($raw_identity);
|
||||||
->save();
|
|
||||||
} catch (AphrontDuplicateKeyQueryException $ex) {
|
$this->identityCache[$raw_identity] = $identity;
|
||||||
// Somehow this identity already exists?
|
|
||||||
$identity = id(new PhabricatorRepositoryIdentityQuery())
|
|
||||||
->setViewer(PhabricatorUser::getOmnipotentUser())
|
|
||||||
->withIdentityNames(array($identity_name))
|
|
||||||
->executeOne();
|
|
||||||
}
|
|
||||||
$seen[$identity_key] = $identity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $seen[$identity_key];
|
return $this->identityCache[$raw_identity];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
final class PhabricatorRepositoryIdentityChangeWorker
|
final class PhabricatorRepositoryIdentityChangeWorker
|
||||||
extends PhabricatorWorker {
|
extends PhabricatorWorker {
|
||||||
|
|
||||||
protected function doWork() {
|
protected function doWork() {
|
||||||
$viewer = PhabricatorUser::getOmnipotentUser();
|
$viewer = PhabricatorUser::getOmnipotentUser();
|
||||||
|
@ -15,18 +15,20 @@ extends PhabricatorWorker {
|
||||||
->executeOne();
|
->executeOne();
|
||||||
|
|
||||||
$emails = id(new PhabricatorUserEmail())->loadAllWhere(
|
$emails = id(new PhabricatorUserEmail())->loadAllWhere(
|
||||||
'userPHID = %s ORDER BY address',
|
'userPHID = %s',
|
||||||
$user->getPHID());
|
$user->getPHID());
|
||||||
|
|
||||||
|
$identity_engine = id(new DiffusionRepositoryIdentityEngine())
|
||||||
|
->setViewer($viewer);
|
||||||
|
|
||||||
foreach ($emails as $email) {
|
foreach ($emails as $email) {
|
||||||
$identities = id(new PhabricatorRepositoryIdentityQuery())
|
$identities = id(new PhabricatorRepositoryIdentityQuery())
|
||||||
->setViewer($viewer)
|
->setViewer($viewer)
|
||||||
->withEmailAddresses($email->getAddress())
|
->withEmailAddresses(array($email->getAddress()))
|
||||||
->execute();
|
->execute();
|
||||||
|
|
||||||
foreach ($identities as $identity) {
|
foreach ($identities as $identity) {
|
||||||
$identity->setAutomaticGuessedUserPHID($user->getPHID())
|
$identity_engine->newUpdatedIdentity($identity);
|
||||||
->save();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,37 +65,20 @@ abstract class PhabricatorRepositoryCommitMessageParserWorker
|
||||||
$message = $ref->getMessage();
|
$message = $ref->getMessage();
|
||||||
$committer = $ref->getCommitter();
|
$committer = $ref->getCommitter();
|
||||||
$hashes = $ref->getHashes();
|
$hashes = $ref->getHashes();
|
||||||
|
$has_committer = (bool)strlen($committer);
|
||||||
|
|
||||||
$author_identity = id(new PhabricatorRepositoryIdentityQuery())
|
$viewer = PhabricatorUser::getOmnipotentUser();
|
||||||
->setViewer(PhabricatorUser::getOmnipotentUser())
|
|
||||||
->withIdentityNames(array($author))
|
|
||||||
->executeOne();
|
|
||||||
|
|
||||||
if (!$author_identity) {
|
$identity_engine = id(new DiffusionRepositoryIdentityEngine())
|
||||||
$author_identity = id(new PhabricatorRepositoryIdentity())
|
->setViewer($viewer)
|
||||||
->setAuthorPHID($commit->getPHID())
|
->setSourcePHID($commit->getPHID());
|
||||||
->setIdentityName($author)
|
|
||||||
->setAutomaticGuessedUserPHID(
|
|
||||||
$this->resolveUserPHID($commit, $author))
|
|
||||||
->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
$committer_identity = null;
|
$author_identity = $identity_engine->newResolvedIdentity($author);
|
||||||
|
|
||||||
if ($committer) {
|
if ($has_committer) {
|
||||||
$committer_identity = id(new PhabricatorRepositoryIdentityQuery())
|
$committer_identity = $identity_engine->newResolvedIdentity($committer);
|
||||||
->setViewer(PhabricatorUser::getOmnipotentUser())
|
} else {
|
||||||
->withIdentityNames(array($committer))
|
$committer_identity = null;
|
||||||
->executeOne();
|
|
||||||
|
|
||||||
if (!$committer_identity) {
|
|
||||||
$committer_identity = id(new PhabricatorRepositoryIdentity())
|
|
||||||
->setAuthorPHID($commit->getPHID())
|
|
||||||
->setIdentityName($committer)
|
|
||||||
->setAutomaticGuessedUserPHID(
|
|
||||||
$this->resolveUserPHID($commit, $committer))
|
|
||||||
->save();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = id(new PhabricatorRepositoryCommitData())->loadOneWhere(
|
$data = id(new PhabricatorRepositoryCommitData())->loadOneWhere(
|
||||||
|
@ -117,11 +100,11 @@ abstract class PhabricatorRepositoryCommitMessageParserWorker
|
||||||
'authorIdentityPHID', $author_identity->getPHID());
|
'authorIdentityPHID', $author_identity->getPHID());
|
||||||
$data->setCommitDetail(
|
$data->setCommitDetail(
|
||||||
'authorPHID',
|
'authorPHID',
|
||||||
$this->resolveUserPHID($commit, $author));
|
$author_identity->getCurrentEffectiveUserPHID());
|
||||||
|
|
||||||
$data->setCommitMessage($message);
|
$data->setCommitMessage($message);
|
||||||
|
|
||||||
if (strlen($committer)) {
|
if ($has_committer) {
|
||||||
$data->setCommitDetail('committer', $committer);
|
$data->setCommitDetail('committer', $committer);
|
||||||
|
|
||||||
$data->setCommitDetail('committerName', $ref->getCommitterName());
|
$data->setCommitDetail('committerName', $ref->getCommitterName());
|
||||||
|
@ -129,7 +112,8 @@ abstract class PhabricatorRepositoryCommitMessageParserWorker
|
||||||
|
|
||||||
$data->setCommitDetail(
|
$data->setCommitDetail(
|
||||||
'committerPHID',
|
'committerPHID',
|
||||||
$this->resolveUserPHID($commit, $committer));
|
$committer_identity->getCurrentEffectiveUserPHID());
|
||||||
|
|
||||||
$data->setCommitDetail(
|
$data->setCommitDetail(
|
||||||
'committerIdentityPHID', $committer_identity->getPHID());
|
'committerIdentityPHID', $committer_identity->getPHID());
|
||||||
|
|
||||||
|
@ -177,15 +161,6 @@ abstract class PhabricatorRepositoryCommitMessageParserWorker
|
||||||
PhabricatorRepositoryCommit::IMPORTED_MESSAGE);
|
PhabricatorRepositoryCommit::IMPORTED_MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function resolveUserPHID(
|
|
||||||
PhabricatorRepositoryCommit $commit,
|
|
||||||
$user_name) {
|
|
||||||
|
|
||||||
return id(new DiffusionResolveUserQuery())
|
|
||||||
->withName($user_name)
|
|
||||||
->execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function closeRevisions(
|
private function closeRevisions(
|
||||||
PhabricatorUser $actor,
|
PhabricatorUser $actor,
|
||||||
DiffusionCommitRef $ref,
|
DiffusionCommitRef $ref,
|
||||||
|
|
Loading…
Reference in a new issue