1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-20 01:08:50 +02: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:
epriestley 2019-11-13 20:20:33 -08:00
parent 6afbb6102d
commit 0014d0404c
5 changed files with 117 additions and 66 deletions

View file

@ -985,6 +985,7 @@ phutil_register_library_map(array(
'DiffusionRepositoryFunctionDatasource' => 'applications/diffusion/typeahead/DiffusionRepositoryFunctionDatasource.php',
'DiffusionRepositoryHistoryManagementPanel' => 'applications/diffusion/management/DiffusionRepositoryHistoryManagementPanel.php',
'DiffusionRepositoryIdentityEditor' => 'applications/diffusion/editor/DiffusionRepositoryIdentityEditor.php',
'DiffusionRepositoryIdentityEngine' => 'applications/diffusion/identity/DiffusionRepositoryIdentityEngine.php',
'DiffusionRepositoryIdentitySearchEngine' => 'applications/diffusion/query/DiffusionRepositoryIdentitySearchEngine.php',
'DiffusionRepositoryLimitsManagementPanel' => 'applications/diffusion/management/DiffusionRepositoryLimitsManagementPanel.php',
'DiffusionRepositoryListController' => 'applications/diffusion/controller/DiffusionRepositoryListController.php',
@ -6966,6 +6967,7 @@ phutil_register_library_map(array(
'DiffusionRepositoryFunctionDatasource' => 'PhabricatorTypeaheadCompositeDatasource',
'DiffusionRepositoryHistoryManagementPanel' => 'DiffusionRepositoryManagementPanel',
'DiffusionRepositoryIdentityEditor' => 'PhabricatorApplicationTransactionEditor',
'DiffusionRepositoryIdentityEngine' => 'Phobject',
'DiffusionRepositoryIdentitySearchEngine' => 'PhabricatorApplicationSearchEngine',
'DiffusionRepositoryLimitsManagementPanel' => 'DiffusionRepositoryManagementPanel',
'DiffusionRepositoryListController' => 'DiffusionController',

View file

@ -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;
}
}

View file

@ -3,6 +3,8 @@
final class PhabricatorRepositoryManagementRebuildIdentitiesWorkflow
extends PhabricatorRepositoryManagementWorkflow {
private $identityCache = array();
protected function didConstruct() {
$this
->setName('rebuild-identities')
@ -94,31 +96,21 @@ final class PhabricatorRepositoryManagementRebuildIdentitiesWorkflow
}
private function getIdentityForCommit(
PhabricatorRepositoryCommit $commit, $identity_name) {
PhabricatorRepositoryCommit $commit,
$raw_identity) {
static $seen = array();
$identity_key = PhabricatorHash::digestForIndex($identity_name);
if (empty($seen[$identity_key])) {
try {
$user_phid = id(new DiffusionResolveUserQuery())
->withName($identity_name)
->execute();
if (!isset($this->identityCache[$raw_identity])) {
$viewer = $this->getViewer();
$identity = id(new PhabricatorRepositoryIdentity())
->setAuthorPHID($commit->getPHID())
->setIdentityName($identity_name)
->setAutomaticGuessedUserPHID($user_phid)
->save();
} catch (AphrontDuplicateKeyQueryException $ex) {
// Somehow this identity already exists?
$identity = id(new PhabricatorRepositoryIdentityQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withIdentityNames(array($identity_name))
->executeOne();
}
$seen[$identity_key] = $identity;
$identity = id(new DiffusionRepositoryIdentityEngine())
->setViewer($viewer)
->setSourcePHID($commit->getPHID())
->newResolvedIdentity($raw_identity);
$this->identityCache[$raw_identity] = $identity;
}
return $seen[$identity_key];
return $this->identityCache[$raw_identity];
}
}

View file

@ -1,7 +1,7 @@
<?php
final class PhabricatorRepositoryIdentityChangeWorker
extends PhabricatorWorker {
extends PhabricatorWorker {
protected function doWork() {
$viewer = PhabricatorUser::getOmnipotentUser();
@ -15,18 +15,20 @@ extends PhabricatorWorker {
->executeOne();
$emails = id(new PhabricatorUserEmail())->loadAllWhere(
'userPHID = %s ORDER BY address',
'userPHID = %s',
$user->getPHID());
$identity_engine = id(new DiffusionRepositoryIdentityEngine())
->setViewer($viewer);
foreach ($emails as $email) {
$identities = id(new PhabricatorRepositoryIdentityQuery())
->setViewer($viewer)
->withEmailAddresses($email->getAddress())
->withEmailAddresses(array($email->getAddress()))
->execute();
foreach ($identities as $identity) {
$identity->setAutomaticGuessedUserPHID($user->getPHID())
->save();
$identity_engine->newUpdatedIdentity($identity);
}
}
}

View file

@ -65,37 +65,20 @@ abstract class PhabricatorRepositoryCommitMessageParserWorker
$message = $ref->getMessage();
$committer = $ref->getCommitter();
$hashes = $ref->getHashes();
$has_committer = (bool)strlen($committer);
$author_identity = id(new PhabricatorRepositoryIdentityQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withIdentityNames(array($author))
->executeOne();
$viewer = PhabricatorUser::getOmnipotentUser();
if (!$author_identity) {
$author_identity = id(new PhabricatorRepositoryIdentity())
->setAuthorPHID($commit->getPHID())
->setIdentityName($author)
->setAutomaticGuessedUserPHID(
$this->resolveUserPHID($commit, $author))
->save();
}
$identity_engine = id(new DiffusionRepositoryIdentityEngine())
->setViewer($viewer)
->setSourcePHID($commit->getPHID());
$committer_identity = null;
$author_identity = $identity_engine->newResolvedIdentity($author);
if ($committer) {
$committer_identity = id(new PhabricatorRepositoryIdentityQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withIdentityNames(array($committer))
->executeOne();
if (!$committer_identity) {
$committer_identity = id(new PhabricatorRepositoryIdentity())
->setAuthorPHID($commit->getPHID())
->setIdentityName($committer)
->setAutomaticGuessedUserPHID(
$this->resolveUserPHID($commit, $committer))
->save();
}
if ($has_committer) {
$committer_identity = $identity_engine->newResolvedIdentity($committer);
} else {
$committer_identity = null;
}
$data = id(new PhabricatorRepositoryCommitData())->loadOneWhere(
@ -117,11 +100,11 @@ abstract class PhabricatorRepositoryCommitMessageParserWorker
'authorIdentityPHID', $author_identity->getPHID());
$data->setCommitDetail(
'authorPHID',
$this->resolveUserPHID($commit, $author));
$author_identity->getCurrentEffectiveUserPHID());
$data->setCommitMessage($message);
if (strlen($committer)) {
if ($has_committer) {
$data->setCommitDetail('committer', $committer);
$data->setCommitDetail('committerName', $ref->getCommitterName());
@ -129,7 +112,8 @@ abstract class PhabricatorRepositoryCommitMessageParserWorker
$data->setCommitDetail(
'committerPHID',
$this->resolveUserPHID($commit, $committer));
$committer_identity->getCurrentEffectiveUserPHID());
$data->setCommitDetail(
'committerIdentityPHID', $committer_identity->getPHID());
@ -177,15 +161,6 @@ abstract class PhabricatorRepositoryCommitMessageParserWorker
PhabricatorRepositoryCommit::IMPORTED_MESSAGE);
}
private function resolveUserPHID(
PhabricatorRepositoryCommit $commit,
$user_name) {
return id(new DiffusionResolveUserQuery())
->withName($user_name)
->execute();
}
private function closeRevisions(
PhabricatorUser $actor,
DiffusionCommitRef $ref,