@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<?php
2
3final class DiffusionRepositoryIdentityEngine
4 extends Phobject {
5
6 private $viewer;
7 private $sourcePHID;
8 private $dryRun;
9
10 public function setViewer(PhabricatorUser $viewer) {
11 $this->viewer = $viewer;
12 return $this;
13 }
14
15 public function getViewer() {
16 return $this->viewer;
17 }
18
19 public function setSourcePHID($source_phid) {
20 $this->sourcePHID = $source_phid;
21 return $this;
22 }
23
24 public function getSourcePHID() {
25 if (!$this->sourcePHID) {
26 throw new PhutilInvalidStateException('setSourcePHID');
27 }
28
29 return $this->sourcePHID;
30 }
31
32 public function setDryRun($dry_run) {
33 $this->dryRun = $dry_run;
34 return $this;
35 }
36
37 public function getDryRun() {
38 return $this->dryRun;
39 }
40
41 public function newResolvedIdentity($raw_identity) {
42 $identity = $this->loadRawIdentity($raw_identity);
43
44 if (!$identity) {
45 $identity = $this->newIdentity($raw_identity);
46 }
47
48 return $this->updateIdentity($identity);
49 }
50
51 public function newUpdatedIdentity(PhabricatorRepositoryIdentity $identity) {
52 return $this->updateIdentity($identity);
53 }
54
55 private function loadRawIdentity($raw_identity) {
56 $viewer = $this->getViewer();
57
58 return id(new PhabricatorRepositoryIdentityQuery())
59 ->setViewer($viewer)
60 ->withIdentityNames(array($raw_identity))
61 ->executeOne();
62 }
63
64 private function newIdentity($raw_identity) {
65 $source_phid = $this->getSourcePHID();
66
67 return id(new PhabricatorRepositoryIdentity())
68 ->setAuthorPHID($source_phid)
69 ->setIdentityName($raw_identity);
70 }
71
72 private function resolveIdentity(PhabricatorRepositoryIdentity $identity) {
73 $raw_identity = $identity->getIdentityName();
74
75 return id(new DiffusionResolveUserQuery())
76 ->withName($raw_identity)
77 ->execute();
78 }
79
80 private function updateIdentity(PhabricatorRepositoryIdentity $identity) {
81
82 // If we're updating an identity and it has a manual user PHID associated
83 // with it but the user is no longer valid, remove the value. This likely
84 // corresponds to a user that was destroyed.
85
86 $assigned_phid = $identity->getManuallySetUserPHID();
87 $unassigned = DiffusionIdentityUnassignedDatasource::FUNCTION_TOKEN;
88 if ($assigned_phid && ($assigned_phid !== $unassigned)) {
89 $viewer = $this->getViewer();
90 $user = id(new PhabricatorPeopleQuery())
91 ->setViewer($viewer)
92 ->withPHIDs(array($assigned_phid))
93 ->executeOne();
94 if (!$user) {
95 $identity->setManuallySetUserPHID(null);
96 }
97 }
98
99 $resolved_phid = $this->resolveIdentity($identity);
100
101 $identity->setAutomaticGuessedUserPHID($resolved_phid);
102
103 if ($this->getDryRun()) {
104 $identity->makeEphemeral();
105 } else {
106 $identity->save();
107 }
108
109 return $identity;
110 }
111
112 public function didUpdateEmailAddress($raw_address) {
113 PhabricatorWorker::scheduleTask(
114 'PhabricatorRepositoryIdentityChangeWorker',
115 array(
116 'emailAddresses' => array($raw_address),
117 ));
118 }
119
120}