mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-09 16:32:39 +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:
parent
e1b8532e24
commit
7e91f42b02
12 changed files with 247 additions and 51 deletions
8
resources/sql/autopatches/20170524.nuance.01.command.sql
Normal file
8
resources/sql/autopatches/20170524.nuance.01.command.sql
Normal 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);
|
|
@ -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 = '';
|
|
@ -52,6 +52,8 @@ final class PhabricatorNuanceApplication extends PhabricatorApplication {
|
|||
$this->getEditRoutePattern('edit/') => 'NuanceQueueEditController',
|
||||
'view/(?P<id>[1-9]\d*)/' => 'NuanceQueueViewController',
|
||||
'work/(?P<id>[1-9]\d*)/' => 'NuanceQueueWorkController',
|
||||
'action/(?P<queueID>[1-9]\d*)/(?P<action>[^/]+)/(?P<id>[1-9]\d*)/'
|
||||
=> 'NuanceItemActionController',
|
||||
),
|
||||
),
|
||||
'/action/' => array(
|
||||
|
|
|
@ -6,6 +6,14 @@ final class NuanceItemActionController extends NuanceController {
|
|||
$viewer = $this->getViewer();
|
||||
$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())
|
||||
->setViewer($viewer)
|
||||
->withIDs(array($id))
|
||||
|
@ -14,13 +22,69 @@ final class NuanceItemActionController extends NuanceController {
|
|||
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');
|
||||
|
||||
$impl = $item->getImplementation();
|
||||
$impl->setViewer($viewer);
|
||||
$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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,14 +26,12 @@ final class NuanceItemViewController extends NuanceController {
|
|||
|
||||
$curtain = $this->buildCurtain($item);
|
||||
$content = $this->buildContent($item);
|
||||
$commands = $this->buildCommands($item);
|
||||
|
||||
$timeline = $this->buildTransactionTimeline(
|
||||
$item,
|
||||
new NuanceItemTransactionQuery());
|
||||
|
||||
$main = array(
|
||||
$commands,
|
||||
$content,
|
||||
$timeline,
|
||||
);
|
||||
|
@ -91,36 +89,4 @@ final class NuanceItemViewController extends NuanceController {
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -64,12 +64,14 @@ final class NuanceQueueWorkController
|
|||
$impl = $item->getImplementation()
|
||||
->setViewer($viewer);
|
||||
|
||||
$commands = $this->buildCommands($item);
|
||||
$work_content = $impl->buildItemWorkView($item);
|
||||
|
||||
$view = id(new PHUITwoColumnView())
|
||||
->setCurtain($curtain)
|
||||
->setMainColumn(
|
||||
array(
|
||||
$commands,
|
||||
$work_content,
|
||||
$timeline,
|
||||
));
|
||||
|
@ -94,12 +96,15 @@ final class NuanceQueueWorkController
|
|||
|
||||
$item_id = $item->getID();
|
||||
|
||||
$action_uri = "queue/action/{$id}/{$command_key}/{$item_id}/";
|
||||
$action_uri = $this->getApplicationURI($action_uri);
|
||||
|
||||
$curtain->addAction(
|
||||
id(new PhabricatorActionView())
|
||||
->setName($command->getName())
|
||||
->setIcon($command->getIcon())
|
||||
->setHref("queue/command/{$id}/{$command_key}/{$item_id}/"))
|
||||
->setWorkflow(true);
|
||||
->setHref($action_uri)
|
||||
->setWorkflow(true));
|
||||
}
|
||||
|
||||
$curtain->addAction(
|
||||
|
@ -120,4 +125,62 @@ final class NuanceQueueWorkController
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -47,4 +47,8 @@ final class NuanceFormItemType
|
|||
);
|
||||
}
|
||||
|
||||
protected function handleAction(NuanceItem $item, $action) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -95,13 +95,7 @@ abstract class NuanceItemType
|
|||
}
|
||||
|
||||
final public function buildActionResponse(NuanceItem $item, $action) {
|
||||
$response = $this->handleAction($item, $action);
|
||||
|
||||
if ($response === null) {
|
||||
return new Aphront404Response();
|
||||
}
|
||||
|
||||
return $response;
|
||||
return $this->handleAction($item, $action);
|
||||
}
|
||||
|
||||
protected function handleAction(NuanceItem $item, $action) {
|
||||
|
|
|
@ -5,6 +5,7 @@ final class NuanceItemCommandQuery
|
|||
|
||||
private $ids;
|
||||
private $itemPHIDs;
|
||||
private $statuses;
|
||||
|
||||
public function withIDs(array $ids) {
|
||||
$this->ids = $ids;
|
||||
|
@ -16,6 +17,11 @@ final class NuanceItemCommandQuery
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function withStatuses(array $statuses) {
|
||||
$this->statuses = $statuses;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function newResultObject() {
|
||||
return new NuanceItemCommand();
|
||||
}
|
||||
|
@ -41,6 +47,13 @@ final class NuanceItemCommandQuery
|
|||
$this->itemPHIDs);
|
||||
}
|
||||
|
||||
if ($this->statuses !== null) {
|
||||
$where[] = qsprintf(
|
||||
$conn,
|
||||
'status IN (%Ls)',
|
||||
$this->statuses);
|
||||
}
|
||||
|
||||
return $where;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,32 +4,86 @@ final class NuanceItemCommand
|
|||
extends NuanceDAO
|
||||
implements PhabricatorPolicyInterface {
|
||||
|
||||
const STATUS_ISSUED = 'issued';
|
||||
const STATUS_EXECUTING = 'executing';
|
||||
const STATUS_DONE = 'done';
|
||||
const STATUS_FAILED = 'failed';
|
||||
|
||||
protected $itemPHID;
|
||||
protected $authorPHID;
|
||||
protected $queuePHID;
|
||||
protected $command;
|
||||
protected $parameters;
|
||||
protected $status;
|
||||
protected $parameters = array();
|
||||
|
||||
public static function initializeNewCommand() {
|
||||
return new self();
|
||||
return id(new self())
|
||||
->setStatus(self::STATUS_ISSUED);
|
||||
}
|
||||
|
||||
protected function getConfiguration() {
|
||||
return array(
|
||||
self::CONFIG_TIMESTAMPS => false,
|
||||
self::CONFIG_SERIALIZATION => array(
|
||||
'parameters' => self::SERIALIZATION_JSON,
|
||||
),
|
||||
self::CONFIG_COLUMN_SCHEMA => array(
|
||||
'command' => 'text64',
|
||||
'status' => 'text64',
|
||||
'queuePHID' => 'phid?',
|
||||
),
|
||||
self::CONFIG_KEY_SCHEMA => array(
|
||||
'key_item' => array(
|
||||
'columns' => array('itemPHID'),
|
||||
'key_pending' => array(
|
||||
'columns' => array('itemPHID', 'status'),
|
||||
),
|
||||
),
|
||||
) + 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 )----------------------------------------- */
|
||||
|
||||
|
|
|
@ -43,6 +43,10 @@ final class NuanceQueue
|
|||
return '/nuance/queue/view/'.$this->getID().'/';
|
||||
}
|
||||
|
||||
public function getWorkURI() {
|
||||
return '/nuance/queue/work/'.$this->getID().'/';
|
||||
}
|
||||
|
||||
|
||||
/* -( PhabricatorPolicyInterface )----------------------------------------- */
|
||||
|
||||
|
|
|
@ -61,12 +61,31 @@ final class NuanceItemUpdateWorker
|
|||
$commands = id(new NuanceItemCommandQuery())
|
||||
->setViewer($viewer)
|
||||
->withItemPHIDs(array($item->getPHID()))
|
||||
->withStatuses(
|
||||
array(
|
||||
NuanceItemCommand::STATUS_ISSUED,
|
||||
))
|
||||
->execute();
|
||||
$commands = msort($commands, 'getID');
|
||||
|
||||
foreach ($commands as $command) {
|
||||
$impl->applyCommand($item, $command);
|
||||
$command->delete();
|
||||
$command
|
||||
->setStatus(NuanceItemCommand::STATUS_EXECUTING)
|
||||
->save();
|
||||
|
||||
try {
|
||||
$impl->applyCommand($item, $command);
|
||||
|
||||
$command
|
||||
->setStatus(NuanceItemCommand::STATUS_DONE)
|
||||
->save();
|
||||
} catch (Exception $ex) {
|
||||
$command
|
||||
->setStatus(NuanceItemCommand::STATUS_FAILED)
|
||||
->save();
|
||||
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue