Differential D21450 Diff 51086 src/applications/repository/worker/PhabricatorRepositoryCommitPublishWorker.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/repository/worker/PhabricatorRepositoryCommitPublishWorker.php
| Show All 24 Lines | protected function parseCommit( | ||||
| // followup workers. | // followup workers. | ||||
| } | } | ||||
| private function publishCommit( | private function publishCommit( | ||||
| PhabricatorRepository $repository, | PhabricatorRepository $repository, | ||||
| PhabricatorRepositoryCommit $commit) { | PhabricatorRepositoryCommit $commit) { | ||||
| $viewer = PhabricatorUser::getOmnipotentUser(); | $viewer = PhabricatorUser::getOmnipotentUser(); | ||||
| $publisher = $repository->newPublisher(); | |||||
| if (!$publisher->shouldPublishCommit($commit)) { | |||||
| return; | |||||
| } | |||||
| $commit_phid = $commit->getPHID(); | $commit_phid = $commit->getPHID(); | ||||
| // Reload the commit to get the commit data, identities, and any | // Reload the commit to get the commit data, identities, and any | ||||
| // outstanding audit requests. | // outstanding audit requests. | ||||
| $commit = id(new DiffusionCommitQuery()) | $commit = id(new DiffusionCommitQuery()) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->withPHIDs(array($commit_phid)) | ->withPHIDs(array($commit_phid)) | ||||
| ->needCommitData(true) | ->needCommitData(true) | ||||
| ->needIdentities(true) | ->needIdentities(true) | ||||
| ->needAuditRequests(true) | ->needAuditRequests(true) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| if (!$commit) { | if (!$commit) { | ||||
| throw new PhabricatorWorkerPermanentFailureException( | throw new PhabricatorWorkerPermanentFailureException( | ||||
| pht( | pht( | ||||
| 'Failed to reload commit "%s".', | 'Failed to reload commit "%s".', | ||||
| $commit_phid)); | $commit_phid)); | ||||
| } | } | ||||
| $publisher = $repository->newPublisher(); | |||||
| $should_publish = $publisher->shouldPublishCommit($commit); | |||||
| if (!$should_publish) { | |||||
| $hold_reasons = $publisher->getCommitHoldReasons($commit); | |||||
| } else { | |||||
| $hold_reasons = array(); | |||||
| } | |||||
| $data = $commit->getCommitData(); | |||||
| if ($data->getCommitDetail('holdReasons') !== $hold_reasons) { | |||||
| $data->setCommitDetail('holdReasons', $hold_reasons); | |||||
| $data->save(); | |||||
| } | |||||
| if (!$should_publish) { | |||||
| return; | |||||
| } | |||||
| $this->applyTransactions($viewer, $repository, $commit); | |||||
| $this->closeRevisions($viewer, $commit); | |||||
| $this->closeTasks($viewer, $commit); | |||||
| } | |||||
| private function applyTransactions( | |||||
| PhabricatorUser $actor, | |||||
| PhabricatorRepository $repository, | |||||
| PhabricatorRepositoryCommit $commit) { | |||||
| $xactions = array( | $xactions = array( | ||||
| $this->newAuditTransactions($commit), | $this->newAuditTransactions($commit), | ||||
| $this->newPublishTransactions($commit), | $this->newPublishTransactions($commit), | ||||
| ); | ); | ||||
| $xactions = array_mergev($xactions); | $xactions = array_mergev($xactions); | ||||
| $acting_phid = $this->getPublishAsPHID($commit); | $acting_phid = $this->getPublishAsPHID($commit); | ||||
| $content_source = $this->newContentSource(); | $content_source = $this->newContentSource(); | ||||
| $revision = DiffusionCommitRevisionQuery::loadRevisionForCommit( | $revision = DiffusionCommitRevisionQuery::loadRevisionForCommit( | ||||
| $viewer, | $actor, | ||||
| $commit); | $commit); | ||||
| // Prevent the commit from generating a mention of the associated | // Prevent the commit from generating a mention of the associated | ||||
| // revision, if one exists, so we don't double up because of the URI | // revision, if one exists, so we don't double up because of the URI | ||||
| // in the commit message. | // in the commit message. | ||||
| $unmentionable_phids = array(); | $unmentionable_phids = array(); | ||||
| if ($revision) { | if ($revision) { | ||||
| $unmentionable_phids[] = $revision->getPHID(); | $unmentionable_phids[] = $revision->getPHID(); | ||||
| } | } | ||||
| $editor = $commit->getApplicationTransactionEditor() | $editor = $commit->getApplicationTransactionEditor() | ||||
| ->setActor($viewer) | ->setActor($actor) | ||||
| ->setActingAsPHID($acting_phid) | ->setActingAsPHID($acting_phid) | ||||
| ->setContinueOnNoEffect(true) | ->setContinueOnNoEffect(true) | ||||
| ->setContinueOnMissingFields(true) | ->setContinueOnMissingFields(true) | ||||
| ->setContentSource($content_source) | ->setContentSource($content_source) | ||||
| ->addUnmentionablePHIDs($unmentionable_phids); | ->addUnmentionablePHIDs($unmentionable_phids); | ||||
| try { | try { | ||||
| $raw_patch = $this->loadRawPatchText($repository, $commit); | $raw_patch = $this->loadRawPatchText($repository, $commit); | ||||
| ▲ Show 20 Lines • Show All 299 Lines • ▼ Show 20 Lines | if (!$file) { | ||||
| pht( | pht( | ||||
| 'Failed to load file ("%s") returned by "%s".', | 'Failed to load file ("%s") returned by "%s".', | ||||
| $file_phid, | $file_phid, | ||||
| 'diffusion.rawdiffquery')); | 'diffusion.rawdiffquery')); | ||||
| } | } | ||||
| return $file->loadFileData(); | return $file->loadFileData(); | ||||
| } | } | ||||
| private function closeRevisions( | |||||
| PhabricatorUser $actor, | |||||
| PhabricatorRepositoryCommit $commit) { | |||||
| $differential = 'PhabricatorDifferentialApplication'; | |||||
| if (!PhabricatorApplication::isClassInstalled($differential)) { | |||||
| return; | |||||
| } | |||||
| $repository = $commit->getRepository(); | |||||
| $data = $commit->getCommitData(); | |||||
| $ref = $data->getCommitRef(); | |||||
| $field_query = id(new DiffusionLowLevelCommitFieldsQuery()) | |||||
| ->setRepository($repository) | |||||
| ->withCommitRef($ref); | |||||
| $field_values = $field_query->execute(); | |||||
| $revision_id = idx($field_values, 'revisionID'); | |||||
| if (!$revision_id) { | |||||
| return; | |||||
| } | |||||
| $revision = id(new DifferentialRevisionQuery()) | |||||
| ->setViewer($actor) | |||||
| ->withIDs(array($revision_id)) | |||||
| ->executeOne(); | |||||
| if (!$revision) { | |||||
| return; | |||||
| } | |||||
| // NOTE: This is very old code from when revisions had a single reviewer. | |||||
| // It still powers the "Reviewer (Deprecated)" field in Herald, but should | |||||
| // be removed. | |||||
| if (!empty($field_values['reviewedByPHIDs'])) { | |||||
| $data->setCommitDetail( | |||||
| 'reviewerPHID', | |||||
| head($field_values['reviewedByPHIDs'])); | |||||
| } | |||||
| $match_data = $field_query->getRevisionMatchData(); | |||||
| $data->setCommitDetail('differential.revisionID', $revision_id); | |||||
| $data->setCommitDetail('revisionMatchData', $match_data); | |||||
| $data->save(); | |||||
| $properties = array( | |||||
| 'revisionMatchData' => $match_data, | |||||
| ); | |||||
| $this->queueObjectUpdate($commit, $revision, $properties); | |||||
| } | |||||
| private function closeTasks( | |||||
| PhabricatorUser $actor, | |||||
| PhabricatorRepositoryCommit $commit) { | |||||
| $maniphest = 'PhabricatorManiphestApplication'; | |||||
| if (!PhabricatorApplication::isClassInstalled($maniphest)) { | |||||
| return; | |||||
| } | |||||
| $data = $commit->getCommitData(); | |||||
| $prefixes = ManiphestTaskStatus::getStatusPrefixMap(); | |||||
| $suffixes = ManiphestTaskStatus::getStatusSuffixMap(); | |||||
| $message = $data->getCommitMessage(); | |||||
| $matches = id(new ManiphestCustomFieldStatusParser()) | |||||
| ->parseCorpus($message); | |||||
| $task_map = array(); | |||||
| foreach ($matches as $match) { | |||||
| $prefix = phutil_utf8_strtolower($match['prefix']); | |||||
| $suffix = phutil_utf8_strtolower($match['suffix']); | |||||
| $status = idx($suffixes, $suffix); | |||||
| if (!$status) { | |||||
| $status = idx($prefixes, $prefix); | |||||
| } | |||||
| foreach ($match['monograms'] as $task_monogram) { | |||||
| $task_id = (int)trim($task_monogram, 'tT'); | |||||
| $task_map[$task_id] = $status; | |||||
| } | |||||
| } | |||||
| if (!$task_map) { | |||||
| return; | |||||
| } | |||||
| $tasks = id(new ManiphestTaskQuery()) | |||||
| ->setViewer($actor) | |||||
| ->withIDs(array_keys($task_map)) | |||||
| ->execute(); | |||||
| foreach ($tasks as $task_id => $task) { | |||||
| $status = $task_map[$task_id]; | |||||
| $properties = array( | |||||
| 'status' => $status, | |||||
| ); | |||||
| $this->queueObjectUpdate($commit, $task, $properties); | |||||
| } | |||||
| } | |||||
| private function queueObjectUpdate( | |||||
| PhabricatorRepositoryCommit $commit, | |||||
| $object, | |||||
| array $properties) { | |||||
| $this->queueTask( | |||||
| 'DiffusionUpdateObjectAfterCommitWorker', | |||||
| array( | |||||
| 'commitPHID' => $commit->getPHID(), | |||||
| 'objectPHID' => $object->getPHID(), | |||||
| 'properties' => $properties, | |||||
| ), | |||||
| array( | |||||
| 'priority' => PhabricatorWorker::PRIORITY_DEFAULT, | |||||
| )); | |||||
| } | |||||
| } | } | ||||