Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / repository / worker / PhabricatorRepositoryCommitParserWorker.php
bloba8cfec5977e2cc601cc634cdeab839c63f862a53
1 <?php
3 abstract class PhabricatorRepositoryCommitParserWorker
4 extends PhabricatorWorker {
6 protected $commit;
7 protected $repository;
9 private function loadCommit() {
10 if ($this->commit) {
11 return $this->commit;
14 $viewer = $this->getViewer();
15 $task_data = $this->getTaskData();
17 $commit_query = id(new DiffusionCommitQuery())
18 ->setViewer($viewer);
20 $commit_phid = idx($task_data, 'commitPHID');
22 // TODO: See T13591. This supports execution of legacy tasks and can
23 // eventually be removed. Newer tasks use "commitPHID" instead of
24 // "commitID".
25 if (!$commit_phid) {
26 $commit_id = idx($task_data, 'commitID');
27 if ($commit_id) {
28 $legacy_commit = id(clone $commit_query)
29 ->withIDs(array($commit_id))
30 ->executeOne();
31 if ($legacy_commit) {
32 $commit_phid = $legacy_commit->getPHID();
37 if (!$commit_phid) {
38 throw new PhabricatorWorkerPermanentFailureException(
39 pht('Task data has no "commitPHID".'));
42 $commit = id(clone $commit_query)
43 ->withPHIDs(array($commit_phid))
44 ->executeOne();
45 if (!$commit) {
46 throw new PhabricatorWorkerPermanentFailureException(
47 pht('Commit "%s" does not exist.', $commit_phid));
50 if ($commit->isUnreachable()) {
51 throw new PhabricatorWorkerPermanentFailureException(
52 pht(
53 'Commit "%s" (with PHID "%s") is no longer reachable from any '.
54 'branch, tag, or ref in this repository, so it will not be '.
55 'imported. This usually means that the branch the commit was on '.
56 'was deleted or overwritten.',
57 $commit->getMonogram(),
58 $commit_phid));
61 $this->commit = $commit;
63 return $commit;
66 final protected function doWork() {
67 $commit = $this->loadCommit();
68 $repository = $commit->getRepository();
70 $this->repository = $repository;
72 $this->parseCommit($repository, $this->commit);
75 private function shouldQueueFollowupTasks() {
76 return !idx($this->getTaskData(), 'only');
79 final protected function queueCommitTask($task_class) {
80 if (!$this->shouldQueueFollowupTasks()) {
81 return;
84 $commit = $this->loadCommit();
85 $repository = $commit->getRepository();
87 $data = array(
88 'commitPHID' => $commit->getPHID(),
91 $task_data = $this->getTaskData();
92 if (isset($task_data['via'])) {
93 $data['via'] = $task_data['via'];
96 $options = array(
97 // We queue followup tasks at default priority so that the queue finishes
98 // work it has started before starting more work. If followups are queued
99 // at the same priority level, we do all message parses first, then all
100 // change parses, etc. This makes progress uneven. See T11677 for
101 // discussion.
102 'priority' => parent::PRIORITY_DEFAULT,
104 'objectPHID' => $commit->getPHID(),
105 'containerPHID' => $repository->getPHID(),
108 $this->queueTask($task_class, $data, $options);
111 protected function getImportStepFlag() {
112 return null;
115 final protected function shouldSkipImportStep() {
116 // If this step has already been performed and this is a "natural" task
117 // which was queued by the normal daemons, decline to do the work again.
118 // This mitigates races if commits are rapidly deleted and revived.
119 $flag = $this->getImportStepFlag();
120 if (!$flag) {
121 // This step doesn't have an associated flag.
122 return false;
125 $commit = $this->commit;
126 if (!$commit->isPartiallyImported($flag)) {
127 // This commit doesn't have the flag set yet.
128 return false;
132 if (!$this->shouldQueueFollowupTasks()) {
133 // This task was queued by administrative tools, so do the work even
134 // if it duplicates existing work.
135 return false;
138 $this->log(
139 "%s\n",
140 pht(
141 'Skipping import step; this step was previously completed for '.
142 'this commit.'));
144 return true;
147 abstract protected function parseCommit(
148 PhabricatorRepository $repository,
149 PhabricatorRepositoryCommit $commit);
151 protected function loadCommitHint(PhabricatorRepositoryCommit $commit) {
152 $viewer = PhabricatorUser::getOmnipotentUser();
154 $repository = $commit->getRepository();
156 return id(new DiffusionCommitHintQuery())
157 ->setViewer($viewer)
158 ->withRepositoryPHIDs(array($repository->getPHID()))
159 ->withOldCommitIdentifiers(array($commit->getCommitIdentifier()))
160 ->executeOne();
163 public function renderForDisplay(PhabricatorUser $viewer) {
164 $suffix = parent::renderForDisplay($viewer);
166 $commit = id(new DiffusionCommitQuery())
167 ->setViewer($viewer)
168 ->withPHIDs(array(idx($this->getTaskData(), 'commitPHID')))
169 ->executeOne();
170 if (!$commit) {
171 return $suffix;
174 $link = DiffusionView::linkCommit(
175 $commit->getRepository(),
176 $commit->getCommitIdentifier());
178 return array($link, $suffix);
181 final protected function loadCommitData(PhabricatorRepositoryCommit $commit) {
182 if ($commit->hasCommitData()) {
183 return $commit->getCommitData();
186 $commit_id = $commit->getID();
188 $data = id(new PhabricatorRepositoryCommitData())->loadOneWhere(
189 'commitID = %d',
190 $commit_id);
191 if (!$data) {
192 $data = id(new PhabricatorRepositoryCommitData())
193 ->setCommitID($commit_id);
196 $commit->attachCommitData($data);
198 return $data;
201 final public function getViewer() {
202 return PhabricatorUser::getOmnipotentUser();