1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-26 08:42:41 +01:00

Issue commands to Nuance items, at least roughly

Summary:
Ref T12738. This makes clicking "Throw In Trash" technically do something, sort of.

In Nuance, the default mode of operation for actions is asynchronous -- so you don't have to wait for a response from Twitter or GitHub after you mash the "send default reply tweet" / "close this pull request with a nice response" button and can move directly to the next item instead.

In the future, some operations will attempt to apply synchronously (e.g., local actions like "ignore this item forever"). This fakes our way through that for now.

There's also no connection to the action actually doing anything yet, but I'll probably rig that up next.

Test Plan: {F4975227}

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T12738

Differential Revision: https://secure.phabricator.com/D18010
This commit is contained in:
epriestley 2017-05-24 09:28:36 -07:00
parent e1b8532e24
commit 7e91f42b02
12 changed files with 247 additions and 51 deletions

View file

@ -0,0 +1,8 @@
ALTER TABLE {$NAMESPACE}_nuance.nuance_itemcommand
ADD dateCreated INT UNSIGNED NOT NULL;
ALTER TABLE {$NAMESPACE}_nuance.nuance_itemcommand
ADD dateModified INT UNSIGNED NOT NULL;
ALTER TABLE {$NAMESPACE}_nuance.nuance_itemcommand
ADD queuePHID VARBINARY(64);

View file

@ -0,0 +1,5 @@
ALTER TABLE {$NAMESPACE}_nuance.nuance_itemcommand
ADD status VARCHAR(64) NOT NULL;
UPDATE {$NAMESPACE}_nuance.nuance_itemcommand
SET status = 'done' WHERE status = '';

View file

@ -52,6 +52,8 @@ final class PhabricatorNuanceApplication extends PhabricatorApplication {
$this->getEditRoutePattern('edit/') => 'NuanceQueueEditController', $this->getEditRoutePattern('edit/') => 'NuanceQueueEditController',
'view/(?P<id>[1-9]\d*)/' => 'NuanceQueueViewController', 'view/(?P<id>[1-9]\d*)/' => 'NuanceQueueViewController',
'work/(?P<id>[1-9]\d*)/' => 'NuanceQueueWorkController', 'work/(?P<id>[1-9]\d*)/' => 'NuanceQueueWorkController',
'action/(?P<queueID>[1-9]\d*)/(?P<action>[^/]+)/(?P<id>[1-9]\d*)/'
=> 'NuanceItemActionController',
), ),
), ),
'/action/' => array( '/action/' => array(

View file

@ -6,6 +6,14 @@ final class NuanceItemActionController extends NuanceController {
$viewer = $this->getViewer(); $viewer = $this->getViewer();
$id = $request->getURIData('id'); $id = $request->getURIData('id');
if (!$request->validateCSRF()) {
return new Aphront400Response();
}
// NOTE: This controller can be reached from an individual item (usually
// by a user) or while working through a queue (usually by staff). When
// a command originates from a queue, the URI will have a queue ID.
$item = id(new NuanceItemQuery()) $item = id(new NuanceItemQuery())
->setViewer($viewer) ->setViewer($viewer)
->withIDs(array($id)) ->withIDs(array($id))
@ -14,13 +22,69 @@ final class NuanceItemActionController extends NuanceController {
return new Aphront404Response(); return new Aphront404Response();
} }
$cancel_uri = $item->getURI();
$queue_id = $request->getURIData('queueID');
$queue = null;
if ($queue_id) {
$queue = id(new NuanceQueueQuery())
->setViewer($viewer)
->withIDs(array($queue_id))
->executeOne();
if (!$queue) {
return new Aphront404Response();
}
$item_queue = $item->getQueue();
if (!$item_queue || ($item_queue->getPHID() != $queue->getPHID())) {
return $this->newDialog()
->setTitle(pht('Wrong Queue'))
->appendParagraph(
pht(
'You are trying to act on this item from the wrong queue: it '.
'is currently in a different queue.'))
->addCancelButton($cancel_uri);
}
}
$action = $request->getURIData('action'); $action = $request->getURIData('action');
$impl = $item->getImplementation(); $impl = $item->getImplementation();
$impl->setViewer($viewer); $impl->setViewer($viewer);
$impl->setController($this); $impl->setController($this);
return $impl->buildActionResponse($item, $action); $command = NuanceItemCommand::initializeNewCommand()
->setItemPHID($item->getPHID())
->setAuthorPHID($viewer->getPHID())
->setCommand($action);
if ($queue) {
$command->setQueuePHID($queue->getPHID());
}
$command->save();
// TODO: Here, we should check if the command should be tried immediately,
// and just defer it to the daemons if not. If we're going to try to apply
// the command directly, we should first acquire the worker lock. If we
// can not, we should defer the command even if it's an immediate command.
// For the moment, skip all this stuff by deferring unconditionally.
$should_defer = true;
if ($should_defer) {
$item->scheduleUpdate();
} else {
// ...
}
if ($queue) {
$done_uri = $queue->getWorkURI();
} else {
$done_uri = $item->getURI();
}
return id(new AphrontRedirectResponse())
->setURI($done_uri);
} }
} }

View file

@ -26,14 +26,12 @@ final class NuanceItemViewController extends NuanceController {
$curtain = $this->buildCurtain($item); $curtain = $this->buildCurtain($item);
$content = $this->buildContent($item); $content = $this->buildContent($item);
$commands = $this->buildCommands($item);
$timeline = $this->buildTransactionTimeline( $timeline = $this->buildTransactionTimeline(
$item, $item,
new NuanceItemTransactionQuery()); new NuanceItemTransactionQuery());
$main = array( $main = array(
$commands,
$content, $content,
$timeline, $timeline,
); );
@ -91,36 +89,4 @@ final class NuanceItemViewController extends NuanceController {
return $impl->buildItemView($item); return $impl->buildItemView($item);
} }
private function buildCommands(NuanceItem $item) {
$viewer = $this->getViewer();
$commands = id(new NuanceItemCommandQuery())
->setViewer($viewer)
->withItemPHIDs(array($item->getPHID()))
->execute();
$commands = msort($commands, 'getID');
if (!$commands) {
return null;
}
$rows = array();
foreach ($commands as $command) {
$rows[] = array(
$command->getCommand(),
);
}
$table = id(new AphrontTableView($rows))
->setHeaders(
array(
pht('Command'),
));
return id(new PHUIObjectBoxView())
->setHeaderText(pht('Pending Commands'))
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
->setTable($table);
}
} }

View file

@ -64,12 +64,14 @@ final class NuanceQueueWorkController
$impl = $item->getImplementation() $impl = $item->getImplementation()
->setViewer($viewer); ->setViewer($viewer);
$commands = $this->buildCommands($item);
$work_content = $impl->buildItemWorkView($item); $work_content = $impl->buildItemWorkView($item);
$view = id(new PHUITwoColumnView()) $view = id(new PHUITwoColumnView())
->setCurtain($curtain) ->setCurtain($curtain)
->setMainColumn( ->setMainColumn(
array( array(
$commands,
$work_content, $work_content,
$timeline, $timeline,
)); ));
@ -94,12 +96,15 @@ final class NuanceQueueWorkController
$item_id = $item->getID(); $item_id = $item->getID();
$action_uri = "queue/action/{$id}/{$command_key}/{$item_id}/";
$action_uri = $this->getApplicationURI($action_uri);
$curtain->addAction( $curtain->addAction(
id(new PhabricatorActionView()) id(new PhabricatorActionView())
->setName($command->getName()) ->setName($command->getName())
->setIcon($command->getIcon()) ->setIcon($command->getIcon())
->setHref("queue/command/{$id}/{$command_key}/{$item_id}/")) ->setHref($action_uri)
->setWorkflow(true); ->setWorkflow(true));
} }
$curtain->addAction( $curtain->addAction(
@ -120,4 +125,62 @@ final class NuanceQueueWorkController
return $curtain; return $curtain;
} }
private function buildCommands(NuanceItem $item) {
$viewer = $this->getViewer();
$commands = id(new NuanceItemCommandQuery())
->setViewer($viewer)
->withItemPHIDs(array($item->getPHID()))
->withStatuses(
array(
NuanceItemCommand::STATUS_ISSUED,
NuanceItemCommand::STATUS_EXECUTING,
NuanceItemCommand::STATUS_FAILED,
))
->execute();
$commands = msort($commands, 'getID');
if (!$commands) {
return null;
}
$rows = array();
foreach ($commands as $command) {
$icon = $command->getStatusIcon();
$color = $command->getStatusColor();
$rows[] = array(
$command->getID(),
id(new PHUIIconView())
->setIcon($icon, $color),
$viewer->renderHandle($command->getAuthorPHID()),
$command->getCommand(),
phabricator_datetime($command->getDateCreated(), $viewer),
);
}
$table = id(new AphrontTableView($rows))
->setHeaders(
array(
pht('ID'),
null,
pht('Actor'),
pht('Command'),
pht('Date'),
))
->setColumnClasses(
array(
null,
'icon',
null,
'pri',
'wide right',
));
return id(new PHUIObjectBoxView())
->setHeaderText(pht('Pending Commands'))
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
->setTable($table);
}
} }

View file

@ -47,4 +47,8 @@ final class NuanceFormItemType
); );
} }
protected function handleAction(NuanceItem $item, $action) {
return null;
}
} }

View file

@ -95,13 +95,7 @@ abstract class NuanceItemType
} }
final public function buildActionResponse(NuanceItem $item, $action) { final public function buildActionResponse(NuanceItem $item, $action) {
$response = $this->handleAction($item, $action); return $this->handleAction($item, $action);
if ($response === null) {
return new Aphront404Response();
}
return $response;
} }
protected function handleAction(NuanceItem $item, $action) { protected function handleAction(NuanceItem $item, $action) {

View file

@ -5,6 +5,7 @@ final class NuanceItemCommandQuery
private $ids; private $ids;
private $itemPHIDs; private $itemPHIDs;
private $statuses;
public function withIDs(array $ids) { public function withIDs(array $ids) {
$this->ids = $ids; $this->ids = $ids;
@ -16,6 +17,11 @@ final class NuanceItemCommandQuery
return $this; return $this;
} }
public function withStatuses(array $statuses) {
$this->statuses = $statuses;
return $this;
}
public function newResultObject() { public function newResultObject() {
return new NuanceItemCommand(); return new NuanceItemCommand();
} }
@ -41,6 +47,13 @@ final class NuanceItemCommandQuery
$this->itemPHIDs); $this->itemPHIDs);
} }
if ($this->statuses !== null) {
$where[] = qsprintf(
$conn,
'status IN (%Ls)',
$this->statuses);
}
return $where; return $where;
} }

View file

@ -4,32 +4,86 @@ final class NuanceItemCommand
extends NuanceDAO extends NuanceDAO
implements PhabricatorPolicyInterface { implements PhabricatorPolicyInterface {
const STATUS_ISSUED = 'issued';
const STATUS_EXECUTING = 'executing';
const STATUS_DONE = 'done';
const STATUS_FAILED = 'failed';
protected $itemPHID; protected $itemPHID;
protected $authorPHID; protected $authorPHID;
protected $queuePHID;
protected $command; protected $command;
protected $parameters; protected $status;
protected $parameters = array();
public static function initializeNewCommand() { public static function initializeNewCommand() {
return new self(); return id(new self())
->setStatus(self::STATUS_ISSUED);
} }
protected function getConfiguration() { protected function getConfiguration() {
return array( return array(
self::CONFIG_TIMESTAMPS => false,
self::CONFIG_SERIALIZATION => array( self::CONFIG_SERIALIZATION => array(
'parameters' => self::SERIALIZATION_JSON, 'parameters' => self::SERIALIZATION_JSON,
), ),
self::CONFIG_COLUMN_SCHEMA => array( self::CONFIG_COLUMN_SCHEMA => array(
'command' => 'text64', 'command' => 'text64',
'status' => 'text64',
'queuePHID' => 'phid?',
), ),
self::CONFIG_KEY_SCHEMA => array( self::CONFIG_KEY_SCHEMA => array(
'key_item' => array( 'key_pending' => array(
'columns' => array('itemPHID'), 'columns' => array('itemPHID', 'status'),
), ),
), ),
) + parent::getConfiguration(); ) + parent::getConfiguration();
} }
public static function getStatusMap() {
return array(
self::STATUS_ISSUED => array(
'name' => pht('Issued'),
'icon' => 'fa-clock-o',
'color' => 'bluegrey',
),
self::STATUS_EXECUTING => array(
'name' => pht('Executing'),
'icon' => 'fa-play',
'color' => 'green',
),
self::STATUS_DONE => array(
'name' => pht('Done'),
'icon' => 'fa-check',
'color' => 'blue',
),
self::STATUS_FAILED => array(
'name' => pht('Failed'),
'icon' => 'fa-times',
'color' => 'red',
),
);
}
private function getStatusSpec() {
$map = self::getStatusMap();
return idx($map, $this->getStatus(), array());
}
public function getStatusIcon() {
$spec = $this->getStatusSpec();
return idx($spec, 'icon', 'fa-question');
}
public function getStatusColor() {
$spec = $this->getStatusSpec();
return idx($spec, 'color', 'indigo');
}
public function getStatusName() {
$spec = $this->getStatusSpec();
return idx($spec, 'name', $this->getStatus());
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */ /* -( PhabricatorPolicyInterface )----------------------------------------- */

View file

@ -43,6 +43,10 @@ final class NuanceQueue
return '/nuance/queue/view/'.$this->getID().'/'; return '/nuance/queue/view/'.$this->getID().'/';
} }
public function getWorkURI() {
return '/nuance/queue/work/'.$this->getID().'/';
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */ /* -( PhabricatorPolicyInterface )----------------------------------------- */

View file

@ -61,12 +61,31 @@ final class NuanceItemUpdateWorker
$commands = id(new NuanceItemCommandQuery()) $commands = id(new NuanceItemCommandQuery())
->setViewer($viewer) ->setViewer($viewer)
->withItemPHIDs(array($item->getPHID())) ->withItemPHIDs(array($item->getPHID()))
->withStatuses(
array(
NuanceItemCommand::STATUS_ISSUED,
))
->execute(); ->execute();
$commands = msort($commands, 'getID'); $commands = msort($commands, 'getID');
foreach ($commands as $command) { foreach ($commands as $command) {
$command
->setStatus(NuanceItemCommand::STATUS_EXECUTING)
->save();
try {
$impl->applyCommand($item, $command); $impl->applyCommand($item, $command);
$command->delete();
$command
->setStatus(NuanceItemCommand::STATUS_DONE)
->save();
} catch (Exception $ex) {
$command
->setStatus(NuanceItemCommand::STATUS_FAILED)
->save();
throw $ex;
}
} }
} }