1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 05:50:55 +01:00

Refactor freeform field specification

Summary:
I want to attach the task also to revision, not only to commit by mentioning it in summary.
This is a preparation for it, useful by itself:

* One query instead of N.
* Lower CRAP score, yay!

Refs T945.

Test Plan:
My Test Plan is really //plan// this time.
I want to update Phabricator in the minute when I commit this.

Reviewers: 20after4, epriestley

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T945

Differential Revision: https://secure.phabricator.com/D3873
This commit is contained in:
vrana 2012-11-01 23:32:45 -07:00
parent 197fad554b
commit 040fac06d4

View file

@ -3,18 +3,7 @@
abstract class DifferentialFreeformFieldSpecification abstract class DifferentialFreeformFieldSpecification
extends DifferentialFieldSpecification { extends DifferentialFieldSpecification {
public function didParseCommit( private function findMentionedTasks($message) {
PhabricatorRepository $repository,
PhabricatorRepositoryCommit $commit,
PhabricatorRepositoryCommitData $data) {
$user = id(new PhabricatorUser())->loadOneWhere(
'phid = %s',
$data->getCommitDetail('authorPHID'));
if (!$user) {
return;
}
$prefixes = array( $prefixes = array(
'resolves' => ManiphestTaskStatus::STATUS_CLOSED_RESOLVED, 'resolves' => ManiphestTaskStatus::STATUS_CLOSED_RESOLVED,
'fixes' => ManiphestTaskStatus::STATUS_CLOSED_RESOLVED, 'fixes' => ManiphestTaskStatus::STATUS_CLOSED_RESOLVED,
@ -55,16 +44,13 @@ abstract class DifferentialFreeformFieldSpecification
$suffix_regex = implode('|', $suffix_regex); $suffix_regex = implode('|', $suffix_regex);
$matches = null; $matches = null;
$ok = preg_match_all( preg_match_all(
"/({$prefix_regex})\s+T(\d+)\s*({$suffix_regex})/i", "/({$prefix_regex})\s+T(\d+)\s*({$suffix_regex})/i",
$this->renderValueForCommitMessage($is_edit = false), $message,
$matches, $matches,
PREG_SET_ORDER); PREG_SET_ORDER);
if (!$ok) { $tasks_statuses = array();
return;
}
foreach ($matches as $set) { foreach ($matches as $set) {
$prefix = strtolower($set[1]); $prefix = strtolower($set[1]);
$task_id = (int)$set[2]; $task_id = (int)$set[2];
@ -75,16 +61,35 @@ abstract class DifferentialFreeformFieldSpecification
$status = idx($prefixes, $prefix); $status = idx($prefixes, $prefix);
} }
$tasks = id(new ManiphestTaskQuery()) $tasks_statuses[$task_id] = $status;
->withTaskIDs(array($task_id)) }
->execute();
$task = idx($tasks, $task_id);
if (!$task) { return $tasks_statuses;
// Task doesn't exist, or the user can't see it. }
continue;
}
public function didParseCommit(
PhabricatorRepository $repository,
PhabricatorRepositoryCommit $commit,
PhabricatorRepositoryCommitData $data) {
$user = id(new PhabricatorUser())->loadOneWhere(
'phid = %s',
$data->getCommitDetail('authorPHID'));
if (!$user) {
return;
}
$message = $this->renderValueForCommitMessage($is_edit = false);
$tasks_statuses = $this->findMentionedTasks($message);
if (!$tasks_statuses) {
return;
}
$tasks = id(new ManiphestTaskQuery())
->withTaskIDs(array_keys($tasks_statuses))
->execute();
foreach ($tasks as $task_id => $task) {
id(new PhabricatorEdgeEditor()) id(new PhabricatorEdgeEditor())
->setActor($user) ->setActor($user)
->addEdge( ->addEdge(
@ -93,6 +98,7 @@ abstract class DifferentialFreeformFieldSpecification
$commit->getPHID()) $commit->getPHID())
->save(); ->save();
$status = $tasks_statuses[$task_id];
if (!$status) { if (!$status) {
// Text like "Ref T123", don't change the task status. // Text like "Ref T123", don't change the task status.
continue; continue;