@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

+117 -66
+2
src/__phutil_library_map__.php
··· 985 985 'DiffusionRepositoryFunctionDatasource' => 'applications/diffusion/typeahead/DiffusionRepositoryFunctionDatasource.php', 986 986 'DiffusionRepositoryHistoryManagementPanel' => 'applications/diffusion/management/DiffusionRepositoryHistoryManagementPanel.php', 987 987 'DiffusionRepositoryIdentityEditor' => 'applications/diffusion/editor/DiffusionRepositoryIdentityEditor.php', 988 + 'DiffusionRepositoryIdentityEngine' => 'applications/diffusion/identity/DiffusionRepositoryIdentityEngine.php', 988 989 'DiffusionRepositoryIdentitySearchEngine' => 'applications/diffusion/query/DiffusionRepositoryIdentitySearchEngine.php', 989 990 'DiffusionRepositoryLimitsManagementPanel' => 'applications/diffusion/management/DiffusionRepositoryLimitsManagementPanel.php', 990 991 'DiffusionRepositoryListController' => 'applications/diffusion/controller/DiffusionRepositoryListController.php', ··· 6966 6967 'DiffusionRepositoryFunctionDatasource' => 'PhabricatorTypeaheadCompositeDatasource', 6967 6968 'DiffusionRepositoryHistoryManagementPanel' => 'DiffusionRepositoryManagementPanel', 6968 6969 'DiffusionRepositoryIdentityEditor' => 'PhabricatorApplicationTransactionEditor', 6970 + 'DiffusionRepositoryIdentityEngine' => 'Phobject', 6969 6971 'DiffusionRepositoryIdentitySearchEngine' => 'PhabricatorApplicationSearchEngine', 6970 6972 'DiffusionRepositoryLimitsManagementPanel' => 'DiffusionRepositoryManagementPanel', 6971 6973 'DiffusionRepositoryListController' => 'DiffusionController',
+80
src/applications/diffusion/identity/DiffusionRepositoryIdentityEngine.php
··· 1 + <?php 2 + 3 + final class DiffusionRepositoryIdentityEngine 4 + extends Phobject { 5 + 6 + private $viewer; 7 + private $sourcePHID; 8 + 9 + public function setViewer(PhabricatorUser $viewer) { 10 + $this->viewer = $viewer; 11 + return $this; 12 + } 13 + 14 + public function getViewer() { 15 + return $this->viewer; 16 + } 17 + 18 + public function setSourcePHID($source_phid) { 19 + $this->sourcePHID = $source_phid; 20 + return $this; 21 + } 22 + 23 + public function getSourcePHID() { 24 + if (!$this->sourcePHID) { 25 + throw new PhutilInvalidStateException('setSourcePHID'); 26 + } 27 + 28 + return $this->sourcePHID; 29 + } 30 + 31 + public function newResolvedIdentity($raw_identity) { 32 + $identity = $this->loadRawIdentity($raw_identity); 33 + 34 + if (!$identity) { 35 + $identity = $this->newIdentity($raw_identity); 36 + } 37 + 38 + return $this->updateIdentity($identity); 39 + } 40 + 41 + public function newUpdatedIdentity(PhabricatorRepositoryIdentity $identity) { 42 + return $this->updateIdentity($identity); 43 + } 44 + 45 + private function loadRawIdentity($raw_identity) { 46 + $viewer = $this->getViewer(); 47 + 48 + return id(new PhabricatorRepositoryIdentityQuery()) 49 + ->setViewer($viewer) 50 + ->withIdentityNames(array($raw_identity)) 51 + ->executeOne(); 52 + } 53 + 54 + private function newIdentity($raw_identity) { 55 + $source_phid = $this->getSourcePHID(); 56 + 57 + return id(new PhabricatorRepositoryIdentity()) 58 + ->setAuthorPHID($source_phid) 59 + ->setIdentityName($raw_identity); 60 + } 61 + 62 + private function resolveIdentity(PhabricatorRepositoryIdentity $identity) { 63 + $raw_identity = $identity->getIdentityName(); 64 + 65 + return id(new DiffusionResolveUserQuery()) 66 + ->withName($raw_identity) 67 + ->execute(); 68 + } 69 + 70 + private function updateIdentity(PhabricatorRepositoryIdentity $identity) { 71 + $resolved_phid = $this->resolveIdentity($identity); 72 + 73 + $identity 74 + ->setAutomaticGuessedUserPHID($resolved_phid) 75 + ->save(); 76 + 77 + return $identity; 78 + } 79 + 80 + }
+14 -22
src/applications/repository/management/PhabricatorRepositoryManagementRebuildIdentitiesWorkflow.php
··· 3 3 final class PhabricatorRepositoryManagementRebuildIdentitiesWorkflow 4 4 extends PhabricatorRepositoryManagementWorkflow { 5 5 6 + private $identityCache = array(); 7 + 6 8 protected function didConstruct() { 7 9 $this 8 10 ->setName('rebuild-identities') ··· 94 96 } 95 97 96 98 private function getIdentityForCommit( 97 - PhabricatorRepositoryCommit $commit, $identity_name) { 99 + PhabricatorRepositoryCommit $commit, 100 + $raw_identity) { 98 101 99 - static $seen = array(); 100 - $identity_key = PhabricatorHash::digestForIndex($identity_name); 101 - if (empty($seen[$identity_key])) { 102 - try { 103 - $user_phid = id(new DiffusionResolveUserQuery()) 104 - ->withName($identity_name) 105 - ->execute(); 102 + if (!isset($this->identityCache[$raw_identity])) { 103 + $viewer = $this->getViewer(); 106 104 107 - $identity = id(new PhabricatorRepositoryIdentity()) 108 - ->setAuthorPHID($commit->getPHID()) 109 - ->setIdentityName($identity_name) 110 - ->setAutomaticGuessedUserPHID($user_phid) 111 - ->save(); 112 - } catch (AphrontDuplicateKeyQueryException $ex) { 113 - // Somehow this identity already exists? 114 - $identity = id(new PhabricatorRepositoryIdentityQuery()) 115 - ->setViewer(PhabricatorUser::getOmnipotentUser()) 116 - ->withIdentityNames(array($identity_name)) 117 - ->executeOne(); 118 - } 119 - $seen[$identity_key] = $identity; 105 + $identity = id(new DiffusionRepositoryIdentityEngine()) 106 + ->setViewer($viewer) 107 + ->setSourcePHID($commit->getPHID()) 108 + ->newResolvedIdentity($raw_identity); 109 + 110 + $this->identityCache[$raw_identity] = $identity; 120 111 } 121 112 122 - return $seen[$identity_key]; 113 + return $this->identityCache[$raw_identity]; 123 114 } 115 + 124 116 }
+7 -5
src/applications/repository/worker/PhabricatorRepositoryIdentityChangeWorker.php
··· 1 1 <?php 2 2 3 3 final class PhabricatorRepositoryIdentityChangeWorker 4 - extends PhabricatorWorker { 4 + extends PhabricatorWorker { 5 5 6 6 protected function doWork() { 7 7 $viewer = PhabricatorUser::getOmnipotentUser(); ··· 15 15 ->executeOne(); 16 16 17 17 $emails = id(new PhabricatorUserEmail())->loadAllWhere( 18 - 'userPHID = %s ORDER BY address', 18 + 'userPHID = %s', 19 19 $user->getPHID()); 20 20 21 + $identity_engine = id(new DiffusionRepositoryIdentityEngine()) 22 + ->setViewer($viewer); 23 + 21 24 foreach ($emails as $email) { 22 25 $identities = id(new PhabricatorRepositoryIdentityQuery()) 23 26 ->setViewer($viewer) 24 - ->withEmailAddresses($email->getAddress()) 27 + ->withEmailAddresses(array($email->getAddress())) 25 28 ->execute(); 26 29 27 30 foreach ($identities as $identity) { 28 - $identity->setAutomaticGuessedUserPHID($user->getPHID()) 29 - ->save(); 31 + $identity_engine->newUpdatedIdentity($identity); 30 32 } 31 33 } 32 34 }
+14 -39
src/applications/repository/worker/commitmessageparser/PhabricatorRepositoryCommitMessageParserWorker.php
··· 65 65 $message = $ref->getMessage(); 66 66 $committer = $ref->getCommitter(); 67 67 $hashes = $ref->getHashes(); 68 + $has_committer = (bool)strlen($committer); 68 69 69 - $author_identity = id(new PhabricatorRepositoryIdentityQuery()) 70 - ->setViewer(PhabricatorUser::getOmnipotentUser()) 71 - ->withIdentityNames(array($author)) 72 - ->executeOne(); 70 + $viewer = PhabricatorUser::getOmnipotentUser(); 73 71 74 - if (!$author_identity) { 75 - $author_identity = id(new PhabricatorRepositoryIdentity()) 76 - ->setAuthorPHID($commit->getPHID()) 77 - ->setIdentityName($author) 78 - ->setAutomaticGuessedUserPHID( 79 - $this->resolveUserPHID($commit, $author)) 80 - ->save(); 81 - } 72 + $identity_engine = id(new DiffusionRepositoryIdentityEngine()) 73 + ->setViewer($viewer) 74 + ->setSourcePHID($commit->getPHID()); 82 75 83 - $committer_identity = null; 76 + $author_identity = $identity_engine->newResolvedIdentity($author); 84 77 85 - if ($committer) { 86 - $committer_identity = id(new PhabricatorRepositoryIdentityQuery()) 87 - ->setViewer(PhabricatorUser::getOmnipotentUser()) 88 - ->withIdentityNames(array($committer)) 89 - ->executeOne(); 90 - 91 - if (!$committer_identity) { 92 - $committer_identity = id(new PhabricatorRepositoryIdentity()) 93 - ->setAuthorPHID($commit->getPHID()) 94 - ->setIdentityName($committer) 95 - ->setAutomaticGuessedUserPHID( 96 - $this->resolveUserPHID($commit, $committer)) 97 - ->save(); 98 - } 78 + if ($has_committer) { 79 + $committer_identity = $identity_engine->newResolvedIdentity($committer); 80 + } else { 81 + $committer_identity = null; 99 82 } 100 83 101 84 $data = id(new PhabricatorRepositoryCommitData())->loadOneWhere( ··· 117 100 'authorIdentityPHID', $author_identity->getPHID()); 118 101 $data->setCommitDetail( 119 102 'authorPHID', 120 - $this->resolveUserPHID($commit, $author)); 103 + $author_identity->getCurrentEffectiveUserPHID()); 121 104 122 105 $data->setCommitMessage($message); 123 106 124 - if (strlen($committer)) { 107 + if ($has_committer) { 125 108 $data->setCommitDetail('committer', $committer); 126 109 127 110 $data->setCommitDetail('committerName', $ref->getCommitterName()); ··· 129 112 130 113 $data->setCommitDetail( 131 114 'committerPHID', 132 - $this->resolveUserPHID($commit, $committer)); 115 + $committer_identity->getCurrentEffectiveUserPHID()); 116 + 133 117 $data->setCommitDetail( 134 118 'committerIdentityPHID', $committer_identity->getPHID()); 135 119 ··· 175 159 176 160 $commit->writeImportStatusFlag( 177 161 PhabricatorRepositoryCommit::IMPORTED_MESSAGE); 178 - } 179 - 180 - private function resolveUserPHID( 181 - PhabricatorRepositoryCommit $commit, 182 - $user_name) { 183 - 184 - return id(new DiffusionResolveUserQuery()) 185 - ->withName($user_name) 186 - ->execute(); 187 162 } 188 163 189 164 private function closeRevisions(