Improve Automatic Actions plugin api

This commit is contained in:
Frederic Guillot
2016-01-03 16:43:13 -05:00
parent d578b612ea
commit a296ba5b18
82 changed files with 3011 additions and 1665 deletions

View File

@@ -3,7 +3,6 @@
namespace Kanboard\Action;
use Kanboard\Event\GenericEvent;
use Pimple\Container;
/**
* Base class for automatic actions
@@ -13,6 +12,14 @@ use Pimple\Container;
*/
abstract class Base extends \Kanboard\Core\Base
{
/**
* Extended events
*
* @access private
* @var array
*/
private $compatibleEvents = array();
/**
* Flag for called listener
*
@@ -27,7 +34,7 @@ abstract class Base extends \Kanboard\Core\Base
* @access private
* @var integer
*/
private $project_id = 0;
private $projectId = 0;
/**
* User parameters
@@ -38,20 +45,25 @@ abstract class Base extends \Kanboard\Core\Base
private $params = array();
/**
* Attached event name
* Get automatic action name
*
* @access protected
* @var string
* @final
* @access public
* @return string
*/
protected $event_name = '';
final public function getName()
{
return '\\'.get_called_class();
}
/**
* Container instance
* Get automatic action description
*
* @access protected
* @var \Pimple\Container
* @abstract
* @access public
* @return string
*/
protected $container;
abstract public function getDescription();
/**
* Execute the action
@@ -99,22 +111,6 @@ abstract class Base extends \Kanboard\Core\Base
*/
abstract public function hasRequiredCondition(array $data);
/**
* Constructor
*
* @access public
* @param \Pimple\Container $container Container
* @param integer $project_id Project id
* @param string $event_name Attached event name
*/
public function __construct(Container $container, $project_id, $event_name)
{
$this->container = $container;
$this->project_id = $project_id;
$this->event_name = $event_name;
$this->called = false;
}
/**
* Return class information
*
@@ -123,7 +119,19 @@ abstract class Base extends \Kanboard\Core\Base
*/
public function __toString()
{
return get_called_class();
return $this->getName();
}
/**
* Set project id
*
* @access public
* @return Base
*/
public function setProjectId($project_id)
{
$this->projectId = $project_id;
return $this;
}
/**
@@ -134,7 +142,7 @@ abstract class Base extends \Kanboard\Core\Base
*/
public function getProjectId()
{
return $this->project_id;
return $this->projectId;
}
/**
@@ -143,10 +151,12 @@ abstract class Base extends \Kanboard\Core\Base
* @access public
* @param string $name Parameter name
* @param mixed $value Value
* @param Base
*/
public function setParam($name, $value)
{
$this->params[$name] = $value;
return $this;
}
/**
@@ -154,24 +164,25 @@ abstract class Base extends \Kanboard\Core\Base
*
* @access public
* @param string $name Parameter name
* @param mixed $default_value Default value
* @param mixed $default Default value
* @return mixed
*/
public function getParam($name, $default_value = null)
public function getParam($name, $default = null)
{
return isset($this->params[$name]) ? $this->params[$name] : $default_value;
return isset($this->params[$name]) ? $this->params[$name] : $default;
}
/**
* Check if an action is executable (right project and required parameters)
*
* @access public
* @param array $data Event data dictionary
* @return bool True if the action is executable
* @param array $data
* @param string $eventName
* @return bool
*/
public function isExecutable(array $data)
public function isExecutable(array $data, $eventName)
{
return $this->hasCompatibleEvent() &&
return $this->hasCompatibleEvent($eventName) &&
$this->hasRequiredProject($data) &&
$this->hasRequiredParameters($data) &&
$this->hasRequiredCondition($data);
@@ -181,11 +192,12 @@ abstract class Base extends \Kanboard\Core\Base
* Check if the event is compatible with the action
*
* @access public
* @param string $eventName
* @return bool
*/
public function hasCompatibleEvent()
public function hasCompatibleEvent($eventName)
{
return in_array($this->event_name, $this->getCompatibleEvents());
return in_array($eventName, $this->getEvents());
}
/**
@@ -197,7 +209,7 @@ abstract class Base extends \Kanboard\Core\Base
*/
public function hasRequiredProject(array $data)
{
return isset($data['project_id']) && $data['project_id'] == $this->project_id;
return isset($data['project_id']) && $data['project_id'] == $this->getProjectId();
}
/**
@@ -222,10 +234,11 @@ abstract class Base extends \Kanboard\Core\Base
* Execute the action
*
* @access public
* @param \Event\GenericEvent $event Event data dictionary
* @return bool True if the action was executed or false when not executed
* @param \Kanboard\Event\GenericEvent $event
* @param string $eventName
* @return bool
*/
public function execute(GenericEvent $event)
public function execute(GenericEvent $event, $eventName)
{
// Avoid infinite loop, a listener instance can be called only one time
if ($this->called) {
@@ -235,15 +248,38 @@ abstract class Base extends \Kanboard\Core\Base
$data = $event->getAll();
$result = false;
if ($this->isExecutable($data)) {
if ($this->isExecutable($data, $eventName)) {
$this->called = true;
$result = $this->doAction($data);
}
if (DEBUG) {
$this->logger->debug(get_called_class().' => '.($result ? 'true' : 'false'));
}
$this->logger->debug('AutomaticAction '.$this->getName().' => '.($result ? 'true' : 'false'));
return $result;
}
/**
* Register a new event for the automatic action
*
* @access public
* @param string $event
* @param string $description
*/
public function addEvent($event, $description)
{
$this->eventManager->register($event, $description);
$this->compatibleEvents[] = $event;
return $this;
}
/**
* Get all compatible events of an automatic action
*
* @access public
* @return array
*/
public function getEvents()
{
return array_unique(array_merge($this->getCompatibleEvents(), $this->compatibleEvents));
}
}

View File

@@ -14,6 +14,17 @@ use Kanboard\Integration\GitlabWebhook;
*/
class CommentCreation extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Create a comment from an external provider');
}
/**
* Get the list of compatible events
*
@@ -67,9 +78,9 @@ class CommentCreation extends Base
{
return (bool) $this->comment->create(array(
'reference' => isset($data['reference']) ? $data['reference'] : '',
'comment' => empty($data['comment']) ? $data['commit_comment'] : $data['comment'],
'comment' => $data['comment'],
'task_id' => $data['task_id'],
'user_id' => empty($data['user_id']) ? 0 : $data['user_id'],
'user_id' => isset($data['user_id']) && $this->projectPermission->isAssignable($this->getProjectId(), $data['user_id']) ? $data['user_id'] : 0,
));
}
@@ -82,6 +93,6 @@ class CommentCreation extends Base
*/
public function hasRequiredCondition(array $data)
{
return ! empty($data['comment']) || ! empty($data['commit_comment']);
return ! empty($data['comment']);
}
}

View File

@@ -5,13 +5,24 @@ namespace Kanboard\Action;
use Kanboard\Model\Task;
/**
* Add a log of the triggering event to the task description.
* Add a comment of the triggering event to the task description.
*
* @package action
* @author Oren Ben-Kiki
*/
class TaskLogMoveAnotherColumn extends Base
class CommentCreationMoveTaskColumn extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Add a comment log when moving the task between columns');
}
/**
* Get the list of compatible events
*

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskAssignCategoryColor extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Assign automatically a category based on a color');
}
/**
* Get the list of compatible events
*

View File

@@ -12,6 +12,17 @@ use Kanboard\Integration\GithubWebhook;
*/
class TaskAssignCategoryLabel extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Change the category based on an external label');
}
/**
* Get the list of compatible events
*
@@ -64,7 +75,7 @@ class TaskAssignCategoryLabel extends Base
{
$values = array(
'id' => $data['task_id'],
'category_id' => isset($data['category_id']) ? $data['category_id'] : $this->getParam('category_id'),
'category_id' => $this->getParam('category_id'),
);
return $this->taskModification->update($values);
@@ -79,6 +90,6 @@ class TaskAssignCategoryLabel extends Base
*/
public function hasRequiredCondition(array $data)
{
return $data['label'] == $this->getParam('label');
return $data['label'] == $this->getParam('label') && empty($data['category_id']);
}
}

View File

@@ -13,6 +13,17 @@ use Kanboard\Model\TaskLink;
*/
class TaskAssignCategoryLink extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Assign automatically a category based on a link');
}
/**
* Get the list of compatible events
*
@@ -65,7 +76,7 @@ class TaskAssignCategoryLink extends Base
{
$values = array(
'id' => $data['task_id'],
'category_id' => isset($data['category_id']) ? $data['category_id'] : $this->getParam('category_id'),
'category_id' => $this->getParam('category_id'),
);
return $this->taskModification->update($values);

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskAssignColorCategory extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Assign automatically a color based on a category');
}
/**
* Get the list of compatible events
*

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskAssignColorColumn extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Assign a color when the task is moved to a specific column');
}
/**
* Get the list of compatible events
*

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\TaskLink;
*/
class TaskAssignColorLink extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Change task color when using a specific task link');
}
/**
* Get the list of compatible events
*

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskAssignColorUser extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Assign a color to a specific user');
}
/**
* Get the list of compatible events
*

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskAssignCurrentUser extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Assign the task to the person who does the action');
}
/**
* Get the list of compatible events
*
@@ -22,7 +33,6 @@ class TaskAssignCurrentUser extends Base
{
return array(
Task::EVENT_CREATE,
Task::EVENT_MOVE_COLUMN,
);
}
@@ -34,9 +44,7 @@ class TaskAssignCurrentUser extends Base
*/
public function getActionRequiredParameters()
{
return array(
'column_id' => t('Column'),
);
return array();
}
/**
@@ -49,7 +57,6 @@ class TaskAssignCurrentUser extends Base
{
return array(
'task_id',
'column_id',
);
}
@@ -83,6 +90,6 @@ class TaskAssignCurrentUser extends Base
*/
public function hasRequiredCondition(array $data)
{
return $data['column_id'] == $this->getParam('column_id');
return true;
}
}

View File

@@ -0,0 +1,98 @@
<?php
namespace Kanboard\Action;
use Kanboard\Model\Task;
/**
* Assign a task to the logged user on column change
*
* @package action
* @author Frederic Guillot
*/
class TaskAssignCurrentUserColumn extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Assign the task to the person who does the action when the column is changed');
}
/**
* Get the list of compatible events
*
* @access public
* @return array
*/
public function getCompatibleEvents()
{
return array(
Task::EVENT_MOVE_COLUMN,
);
}
/**
* Get the required parameter for the action (defined by the user)
*
* @access public
* @return array
*/
public function getActionRequiredParameters()
{
return array(
'column_id' => t('Column'),
);
}
/**
* Get the required parameter for the event
*
* @access public
* @return string[]
*/
public function getEventRequiredParameters()
{
return array(
'task_id',
'column_id',
);
}
/**
* Execute the action
*
* @access public
* @param array $data Event data dictionary
* @return bool True if the action was executed or false when not executed
*/
public function doAction(array $data)
{
if (! $this->userSession->isLogged()) {
return false;
}
$values = array(
'id' => $data['task_id'],
'owner_id' => $this->userSession->getId(),
);
return $this->taskModification->update($values);
}
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredCondition(array $data)
{
return $data['column_id'] == $this->getParam('column_id');
}
}

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskAssignSpecificUser extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Assign the task to a specific user');
}
/**
* Get the list of compatible events
*

View File

@@ -13,6 +13,17 @@ use Kanboard\Integration\BitbucketWebhook;
*/
class TaskAssignUser extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Change the assignee based on an external username');
}
/**
* Get the list of compatible events
*
@@ -78,6 +89,6 @@ class TaskAssignUser extends Base
*/
public function hasRequiredCondition(array $data)
{
return true;
return $this->projectPermission->isAssignable($this->getProjectId(), $data['owner_id']);
}
}

View File

@@ -15,6 +15,17 @@ use Kanboard\Model\Task;
*/
class TaskClose extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Close a task');
}
/**
* Get the list of compatible events
*
@@ -24,7 +35,6 @@ class TaskClose extends Base
public function getCompatibleEvents()
{
return array(
Task::EVENT_MOVE_COLUMN,
GithubWebhook::EVENT_COMMIT,
GithubWebhook::EVENT_ISSUE_CLOSED,
GitlabWebhook::EVENT_COMMIT,
@@ -42,17 +52,7 @@ class TaskClose extends Base
*/
public function getActionRequiredParameters()
{
switch ($this->event_name) {
case GithubWebhook::EVENT_COMMIT:
case GithubWebhook::EVENT_ISSUE_CLOSED:
case GitlabWebhook::EVENT_COMMIT:
case GitlabWebhook::EVENT_ISSUE_CLOSED:
case BitbucketWebhook::EVENT_COMMIT:
case BitbucketWebhook::EVENT_ISSUE_CLOSED:
return array();
default:
return array('column_id' => t('Column'));
}
return array();
}
/**
@@ -63,17 +63,7 @@ class TaskClose extends Base
*/
public function getEventRequiredParameters()
{
switch ($this->event_name) {
case GithubWebhook::EVENT_COMMIT:
case GithubWebhook::EVENT_ISSUE_CLOSED:
case GitlabWebhook::EVENT_COMMIT:
case GitlabWebhook::EVENT_ISSUE_CLOSED:
case BitbucketWebhook::EVENT_COMMIT:
case BitbucketWebhook::EVENT_ISSUE_CLOSED:
return array('task_id');
default:
return array('task_id', 'column_id');
}
return array('task_id');
}
/**
@@ -97,16 +87,6 @@ class TaskClose extends Base
*/
public function hasRequiredCondition(array $data)
{
switch ($this->event_name) {
case GithubWebhook::EVENT_COMMIT:
case GithubWebhook::EVENT_ISSUE_CLOSED:
case GitlabWebhook::EVENT_COMMIT:
case GitlabWebhook::EVENT_ISSUE_CLOSED:
case BitbucketWebhook::EVENT_COMMIT:
case BitbucketWebhook::EVENT_ISSUE_CLOSED:
return true;
default:
return $data['column_id'] == $this->getParam('column_id');
}
return true;
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Kanboard\Action;
use Kanboard\Model\Task;
/**
* Close automatically a task in a specific column
*
* @package action
* @author Frederic Guillot
*/
class TaskCloseColumn extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Close a task in a specific column');
}
/**
* Get the list of compatible events
*
* @access public
* @return array
*/
public function getCompatibleEvents()
{
return array(
Task::EVENT_MOVE_COLUMN,
);
}
/**
* Get the required parameter for the action (defined by the user)
*
* @access public
* @return array
*/
public function getActionRequiredParameters()
{
return array('column_id' => t('Column'));
}
/**
* Get the required parameter for the event
*
* @access public
* @return string[]
*/
public function getEventRequiredParameters()
{
return array('task_id', 'column_id');
}
/**
* Execute the action (close the task)
*
* @access public
* @param array $data Event data dictionary
* @return bool True if the action was executed or false when not executed
*/
public function doAction(array $data)
{
return $this->taskStatus->close($data['task_id']);
}
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredCondition(array $data)
{
return $data['column_id'] == $this->getParam('column_id');
}
}

View File

@@ -14,6 +14,17 @@ use Kanboard\Integration\BitbucketWebhook;
*/
class TaskCreation extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Create a task from an external provider');
}
/**
* Get the list of compatible events
*

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskDuplicateAnotherProject extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Duplicate the task to another project');
}
/**
* Get the list of compatible events
*
@@ -51,7 +62,6 @@ class TaskDuplicateAnotherProject extends Base
return array(
'task_id',
'column_id',
'project_id',
);
}
@@ -65,7 +75,6 @@ class TaskDuplicateAnotherProject extends Base
public function doAction(array $data)
{
$destination_column_id = $this->board->getFirstColumn($this->getParam('project_id'));
return (bool) $this->taskDuplication->duplicateToProject($data['task_id'], $this->getParam('project_id'), null, $destination_column_id);
}

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskEmail extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Send a task by email to someone');
}
/**
* Get the list of compatible events
*

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskMoveAnotherProject extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Move the task to another project');
}
/**
* Get the list of compatible events
*

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskMoveColumnAssigned extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Move the task to another column when assigned to a user');
}
/**
* Get the list of compatible events
*
@@ -51,7 +62,6 @@ class TaskMoveColumnAssigned extends Base
return array(
'task_id',
'column_id',
'project_id',
'owner_id'
);
}

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskMoveColumnCategoryChange extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Move the task to another column when the category is changed');
}
/**
* Get the list of compatible events
*
@@ -50,7 +61,6 @@ class TaskMoveColumnCategoryChange extends Base
return array(
'task_id',
'column_id',
'project_id',
'category_id',
);
}
@@ -71,7 +81,8 @@ class TaskMoveColumnCategoryChange extends Base
$data['task_id'],
$this->getParam('dest_column_id'),
$original_task['position'],
$original_task['swimlane_id']
$original_task['swimlane_id'],
false
);
}

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskMoveColumnUnAssigned extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Move the task to another column when assignee is cleared');
}
/**
* Get the list of compatible events
*
@@ -51,7 +62,6 @@ class TaskMoveColumnUnAssigned extends Base
return array(
'task_id',
'column_id',
'project_id',
'owner_id'
);
}

View File

@@ -14,6 +14,17 @@ use Kanboard\Integration\BitbucketWebhook;
*/
class TaskOpen extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Open a task');
}
/**
* Get the list of compatible events
*

View File

@@ -12,6 +12,17 @@ use Kanboard\Model\Task;
*/
class TaskUpdateStartDate extends Base
{
/**
* Get automatic action description
*
* @access public
* @return string
*/
public function getDescription()
{
return t('Automatically update the start date');
}
/**
* Get the list of compatible events
*