From 1891e87d035c235550b5889da585e166cf49502f Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 19 Apr 2015 14:48:12 -0400 Subject: [PATCH] Add Postmark integration (inbound emails for task creation) --- README.markdown | 1 + app/Controller/Webhook.php | 16 +++++ app/Integration/PostmarkWebhook.php | 67 ++++++++++++++++++++ app/Locale/da_DK/translations.php | 4 ++ app/Locale/de_DE/translations.php | 4 ++ app/Locale/es_ES/translations.php | 4 ++ app/Locale/fi_FI/translations.php | 4 ++ app/Locale/fr_FR/translations.php | 4 ++ app/Locale/hu_HU/translations.php | 4 ++ app/Locale/it_IT/translations.php | 4 ++ app/Locale/ja_JP/translations.php | 4 ++ app/Locale/nl_NL/translations.php | 4 ++ app/Locale/pl_PL/translations.php | 4 ++ app/Locale/pt_BR/translations.php | 4 ++ app/Locale/ru_RU/translations.php | 4 ++ app/Locale/sr_Latn_RS/translations.php | 4 ++ app/Locale/sv_SE/translations.php | 4 ++ app/Locale/th_TH/translations.php | 4 ++ app/Locale/tr_TR/translations.php | 4 ++ app/Locale/zh_CN/translations.php | 4 ++ app/Model/Project.php | 31 +++++++++ app/Model/User.php | 12 ++++ app/Schema/Mysql.php | 7 ++- app/Schema/Postgres.php | 7 ++- app/Schema/Sqlite.php | 7 ++- app/ServiceProvider/ClassProvider.php | 1 + app/Template/config/integrations.php | 8 ++- app/Template/project/edit.php | 4 ++ app/Template/project/index.php | 4 ++ assets/img/gravatar-icon.png | Bin 0 -> 517 bytes assets/img/postmark-icon.png | Bin 0 -> 474 bytes composer.json | 3 +- composer.lock | 48 +++++++++++++- docs/postmark.markdown | 59 ++++++++++++++++++ tests/units/Base.php | 5 ++ tests/units/PostmarkWebhookTest.php | 83 +++++++++++++++++++++++++ tests/units/ProjectTest.php | 53 ++++++++++++++++ 37 files changed, 478 insertions(+), 6 deletions(-) create mode 100644 app/Integration/PostmarkWebhook.php create mode 100644 assets/img/gravatar-icon.png create mode 100644 assets/img/postmark-icon.png create mode 100644 docs/postmark.markdown create mode 100644 tests/units/PostmarkWebhookTest.php diff --git a/README.markdown b/README.markdown index 5bf71088d..4c4eda674 100644 --- a/README.markdown +++ b/README.markdown @@ -108,6 +108,7 @@ Documentation - [Gitlab webhooks](docs/gitlab-webhooks.markdown) - [Hipchat](docs/hipchat.markdown) - [Slack](docs/slack.markdown) +- [Postmark](docs/postmark.markdown) #### More diff --git a/app/Controller/Webhook.php b/app/Controller/Webhook.php index ef79379f5..afa0543ad 100644 --- a/app/Controller/Webhook.php +++ b/app/Controller/Webhook.php @@ -100,4 +100,20 @@ class Webhook extends Base echo $result ? 'PARSED' : 'IGNORED'; } + + /** + * Handle Postmark webhooks + * + * @access public + */ + public function postmark() + { + if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) { + $this->response->text('Not Authorized', 401); + } + + $result = $this->postmarkWebhook->parsePayload($this->request->getJson() ?: array()); + + echo $result ? 'PARSED' : 'IGNORED'; + } } diff --git a/app/Integration/PostmarkWebhook.php b/app/Integration/PostmarkWebhook.php new file mode 100644 index 000000000..6387ba20d --- /dev/null +++ b/app/Integration/PostmarkWebhook.php @@ -0,0 +1,67 @@ +user->getByEmail($payload['From']); + + if (empty($user)) { + $this->container['logger']->debug('PostmarkWebhook: ignored => user not found'); + return false; + } + + // The project must have a short name + $project = $this->project->getByIdentifier($payload['MailboxHash']); + + if (empty($project)) { + $this->container['logger']->debug('PostmarkWebhook: ignored => project not found'); + return false; + } + + // The user must be member of the project + if (! $this->projectPermission->isMember($project['id'], $user['id'])) { + $this->container['logger']->debug('PostmarkWebhook: ignored => user is not member of the project'); + return false; + } + + // Get the Markdown contents + if (empty($payload['HtmlBody'])) { + $description = $payload['TextBody']; + } + else { + $markdown = new HTML_To_Markdown($payload['HtmlBody'], array('strip_tags' => true)); + $description = $markdown->output(); + } + + // Finally, we create the task + return (bool) $this->taskCreation->create(array( + 'project_id' => $project['id'], + 'title' => $payload['Subject'], + 'description' => $description, + 'creator_id' => $user['id'], + )); + } +} diff --git a/app/Locale/da_DK/translations.php b/app/Locale/da_DK/translations.php index cc602ef40..f021ea348 100644 --- a/app/Locale/da_DK/translations.php +++ b/app/Locale/da_DK/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/de_DE/translations.php b/app/Locale/de_DE/translations.php index 6a589cddd..f58e46307 100644 --- a/app/Locale/de_DE/translations.php +++ b/app/Locale/de_DE/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/es_ES/translations.php b/app/Locale/es_ES/translations.php index d230d8451..3160e6f65 100644 --- a/app/Locale/es_ES/translations.php +++ b/app/Locale/es_ES/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/fi_FI/translations.php b/app/Locale/fi_FI/translations.php index badb7d883..934f90f47 100644 --- a/app/Locale/fi_FI/translations.php +++ b/app/Locale/fi_FI/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/fr_FR/translations.php b/app/Locale/fr_FR/translations.php index 3bc3e7dff..cdb9045bc 100644 --- a/app/Locale/fr_FR/translations.php +++ b/app/Locale/fr_FR/translations.php @@ -860,4 +860,8 @@ return array( 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => 'Prenez une capture d\'écran et appuyez sur CTRL+V ou ⌘+V pour coller ici.', 'Screenshot uploaded successfully.' => 'Capture d\'écran téléchargée avec succès.', 'SEK - Swedish Krona' => 'SEK - Couronne suédoise', + 'The project identifier is an optional alphanumeric code used to identify your project.' => 'L\'identificateur du projet est un code alpha-numérique optionnel pour identifier votre projet.', + 'Identifier' => 'Identificateur', + 'Postmark (incoming emails)' => 'Postmark (emails entrants)', + 'Help on Postmark integration' => 'Aide sur l\'intégration avec Postmark', ); diff --git a/app/Locale/hu_HU/translations.php b/app/Locale/hu_HU/translations.php index 558bb25ea..38bed1a2b 100644 --- a/app/Locale/hu_HU/translations.php +++ b/app/Locale/hu_HU/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/it_IT/translations.php b/app/Locale/it_IT/translations.php index c8df81ab3..59f057b3c 100644 --- a/app/Locale/it_IT/translations.php +++ b/app/Locale/it_IT/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/ja_JP/translations.php b/app/Locale/ja_JP/translations.php index 11af3feb6..9fa0a7239 100644 --- a/app/Locale/ja_JP/translations.php +++ b/app/Locale/ja_JP/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/nl_NL/translations.php b/app/Locale/nl_NL/translations.php index 37c7c69db..5f57937c2 100644 --- a/app/Locale/nl_NL/translations.php +++ b/app/Locale/nl_NL/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/pl_PL/translations.php b/app/Locale/pl_PL/translations.php index 366c63717..42b2865e6 100644 --- a/app/Locale/pl_PL/translations.php +++ b/app/Locale/pl_PL/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/pt_BR/translations.php b/app/Locale/pt_BR/translations.php index 87845f779..4dd237ec8 100644 --- a/app/Locale/pt_BR/translations.php +++ b/app/Locale/pt_BR/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/ru_RU/translations.php b/app/Locale/ru_RU/translations.php index 18fd6dfef..e3039db9d 100644 --- a/app/Locale/ru_RU/translations.php +++ b/app/Locale/ru_RU/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/sr_Latn_RS/translations.php b/app/Locale/sr_Latn_RS/translations.php index cb2d72879..6e4a82288 100644 --- a/app/Locale/sr_Latn_RS/translations.php +++ b/app/Locale/sr_Latn_RS/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/sv_SE/translations.php b/app/Locale/sv_SE/translations.php index e67402b2a..abdb2b70d 100644 --- a/app/Locale/sv_SE/translations.php +++ b/app/Locale/sv_SE/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', 'SEK - Swedish Krona' => 'SEK - Svensk Krona', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/th_TH/translations.php b/app/Locale/th_TH/translations.php index 024b211aa..b6c140d67 100644 --- a/app/Locale/th_TH/translations.php +++ b/app/Locale/th_TH/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/tr_TR/translations.php b/app/Locale/tr_TR/translations.php index c5c1c79e3..7457e0b1d 100644 --- a/app/Locale/tr_TR/translations.php +++ b/app/Locale/tr_TR/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Locale/zh_CN/translations.php b/app/Locale/zh_CN/translations.php index e61663356..63570ca2d 100644 --- a/app/Locale/zh_CN/translations.php +++ b/app/Locale/zh_CN/translations.php @@ -858,4 +858,8 @@ return array( // 'Take a screenshot and press CTRL+V or ⌘+V to paste here.' => '', // 'Screenshot uploaded successfully.' => '', // 'SEK - Swedish Krona' => '', + // 'The project identifier is an optional alphanumeric code used to identify your project.' => '', + // 'Identifier' => '', + // 'Postmark (incoming emails)' => '', + // 'Help on Postmark integration' => '', ); diff --git a/app/Model/Project.php b/app/Model/Project.php index dbb9db1bb..231d57e75 100644 --- a/app/Model/Project.php +++ b/app/Model/Project.php @@ -59,6 +59,18 @@ class Project extends Base return $this->db->table(self::TABLE)->eq('name', $name)->findOne(); } + /** + * Get a project by the identifier (code) + * + * @access public + * @param string $identifier + * @return array + */ + public function getByIdentifier($identifier) + { + return $this->db->table(self::TABLE)->eq('identifier', strtoupper($identifier))->findOne(); + } + /** * Fetch project data by using the token * @@ -276,6 +288,10 @@ class Project extends Base $values['last_modified'] = time(); $values['is_private'] = empty($values['is_private']) ? 0 : 1; + if (! empty($values['identifier'])) { + $values['identifier'] = strtoupper($values['identifier']); + } + if (! $this->db->table(self::TABLE)->save($values)) { $this->db->cancelTransaction(); return false; @@ -338,6 +354,10 @@ class Project extends Base */ public function update(array $values) { + if (! empty($values['identifier'])) { + $values['identifier'] = strtoupper($values['identifier']); + } + return $this->exists($values['id']) && $this->db->table(self::TABLE)->eq('id', $values['id'])->save($values); } @@ -443,7 +463,10 @@ class Project extends Base new Validators\Integer('is_active', t('This value must be an integer')), new Validators\Required('name', t('The project name is required')), new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50), + new Validators\MaxLength('identifier', t('The maximum length is %d characters', 50), 50), + new Validators\AlphaNumeric('identifier', t('This value must be alphanumeric')) , new Validators\Unique('name', t('This project must be unique'), $this->db->getConnection(), self::TABLE), + new Validators\Unique('identifier', t('The identifier must be unique'), $this->db->getConnection(), self::TABLE), ); } @@ -456,6 +479,10 @@ class Project extends Base */ public function validateCreation(array $values) { + if (! empty($values['identifier'])) { + $values['identifier'] = strtoupper($values['identifier']); + } + $v = new Validator($values, $this->commonValidationRules()); return array( @@ -473,6 +500,10 @@ class Project extends Base */ public function validateModification(array $values) { + if (! empty($values['identifier'])) { + $values['identifier'] = strtoupper($values['identifier']); + } + $rules = array( new Validators\Required('id', t('This value is required')), ); diff --git a/app/Model/User.php b/app/Model/User.php index 6c348caa1..d9f174bd4 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -141,6 +141,18 @@ class User extends Base return $this->db->table(self::TABLE)->eq('username', $username)->findOne(); } + /** + * Get a specific user by the email address + * + * @access public + * @param string $email Email + * @return array + */ + public function getByEmail($email) + { + return $this->db->table(self::TABLE)->eq('email', $email)->findOne(); + } + /** * Get all users * diff --git a/app/Schema/Mysql.php b/app/Schema/Mysql.php index 3e67387d6..22f8c1b0f 100644 --- a/app/Schema/Mysql.php +++ b/app/Schema/Mysql.php @@ -6,7 +6,12 @@ use PDO; use Core\Security; use Model\Link; -const VERSION = 65; +const VERSION = 66; + +function version_66($pdo) +{ + $pdo->exec("ALTER TABLE projects ADD COLUMN identifier VARCHAR(50) DEFAULT ''"); +} function version_65($pdo) { diff --git a/app/Schema/Postgres.php b/app/Schema/Postgres.php index 6973b2660..db30af676 100644 --- a/app/Schema/Postgres.php +++ b/app/Schema/Postgres.php @@ -6,7 +6,12 @@ use PDO; use Core\Security; use Model\Link; -const VERSION = 46; +const VERSION = 47; + +function version_47($pdo) +{ + $pdo->exec("ALTER TABLE projects ADD COLUMN identifier VARCHAR(50) DEFAULT ''"); +} function version_46($pdo) { diff --git a/app/Schema/Sqlite.php b/app/Schema/Sqlite.php index c4ebd98a7..79c504584 100644 --- a/app/Schema/Sqlite.php +++ b/app/Schema/Sqlite.php @@ -6,7 +6,12 @@ use Core\Security; use PDO; use Model\Link; -const VERSION = 64; +const VERSION = 65; + +function version_65($pdo) +{ + $pdo->exec("ALTER TABLE projects ADD COLUMN identifier TEXT DEFAULT ''"); +} function version_64($pdo) { diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index b08d506c9..0c02058e2 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -78,6 +78,7 @@ class ClassProvider implements ServiceProviderInterface 'BitbucketWebhook', 'Hipchat', 'SlackWebhook', + 'PostmarkWebhook', ) ); diff --git a/app/Template/config/integrations.php b/app/Template/config/integrations.php index e11b62f87..b7dab2b4c 100644 --- a/app/Template/config/integrations.php +++ b/app/Template/config/integrations.php @@ -6,7 +6,13 @@ formCsrf() ?> -

+

 

+
+
+

+
+ +

 

formCheckbox('integration_gravatar', t('Enable Gravatar images'), 1, $values['integration_gravatar'] == 1) ?>
diff --git a/app/Template/project/edit.php b/app/Template/project/edit.php index c1f983153..ffd9be00a 100644 --- a/app/Template/project/edit.php +++ b/app/Template/project/edit.php @@ -9,6 +9,10 @@ formLabel(t('Name'), 'name') ?> formText('name', $values, $errors, array('required', 'maxlength="50"')) ?> + formLabel(t('Identifier'), 'identifier') ?> + formText('identifier', $values, $errors, array('maxlength="50"')) ?> +

+ formLabel(t('Description'), 'description') ?>
diff --git a/app/Template/project/index.php b/app/Template/project/index.php index 05a7d9552..8d2bc30b4 100644 --- a/app/Template/project/index.php +++ b/app/Template/project/index.php @@ -15,6 +15,7 @@ order(t('Id'), 'id') ?> order(t('Status'), 'is_active') ?> + order(t('Identifier'), 'identifier') ?> order(t('Project'), 'name') ?> @@ -30,6 +31,9 @@ + + e($project['identifier']) ?> + a('', 'board', 'show', array('project_id' => $project['id']), false, 'dashboard-table-link', t('Board')) ?>  diff --git a/assets/img/gravatar-icon.png b/assets/img/gravatar-icon.png new file mode 100644 index 0000000000000000000000000000000000000000..d9b3e65163ffef802ddb29b0b278dbea802314b8 GIT binary patch literal 517 zcmeAS@N?(olHy`uVBq!ia0vp^0wBx*Bp9q_EZ7UASkfJR9T^xl_H+M9WCik>lDyqr z82-2SpV<%O{r7Zn43W6Zdv>oEv!jT^!{3WuS=E=nn>({Nn{qwMopX1J174;stchm)rt9r^h0-m2H3;hpUIyN=HOq!ME#C*L8mC;$BF zJ$~<|EjAHQ*k$vNPrgdN{;~5?PKl*E`wBPC;OU=c6LsN8U)1w*^SM1YX8n3pvPddS zYVsm8xrLg6k&AAzt`;@@$M`wE*Hz46MJ+HW97|jyN|N()b5rw57@UJ#9Yg$stQ68y zbh8Rl(-q1L^$Zj=N{ch|(iJ?t+!ee6T-_DQEDSVt6befdb23XR6>JqO4Y+ot+-V0I zrV27FC9x#cD!C{XNHG{07@F!DnCKc>gcuoH0gbP0l+XkK+Qz?D literal 0 HcmV?d00001 diff --git a/assets/img/postmark-icon.png b/assets/img/postmark-icon.png new file mode 100644 index 0000000000000000000000000000000000000000..87f075948ff4847f2b296b766c240a274751d6ff GIT binary patch literal 474 zcmeAS@N?(olHy`uVBq!ia0vp^0wBx*Bp9q_EZ7UASkfJR9T^xl_H+M9WCik>lDyqr z82-2SpV<%O{qb~h43W5O+Gm@6C_tk1zWtLciqWEup04X%4Jr&3WLru&B$r%RX|bh5 zw?ymV*}n@)G*u?wTz9yZwcdd#?YjJ>*_RH@l(Tql^T74B0q3KuT6~iVWGe;ad0kzI5kIq-d; z;!C>^w-#O4&VT=6%)+&nJzh?V%ggou?RC>~uWyJwzc}PylWTO~<+In9mn{CuRCTI1 zZ++gqyT+4VG3MvrpSMk3cLQ&G;=3a)QzW+~{S;7p=JWqmiJ5Rh$|twb-LXq&-4ST! zljg55|L}LgY4+r6KN+>6C&`#p7yARlLAAs+q9i4;B-JXpC>2OC7#SFv>Kd5n8d`)H z8Cw~eTNzmZxmE@S)mjU(Q8eV{r(~v8V$om((Xfu)eI`(YB*=!~{Irtt#G+IN$CUh} fR0Yr6#Prml)Wnp^!jq{sKt&9mu6{1-oD!M<=%BNt literal 0 HcmV?d00001 diff --git a/composer.json b/composer.json index 79ec0abfc..9e08dfc79 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "symfony/console" : "@stable", "symfony/event-dispatcher" : "~2.6", "fguillot/simpleLogger" : "0.0.1", - "christian-riesen/otp": "1.4" + "christian-riesen/otp": "1.4", + "nickcernis/html-to-markdown": "2.2.1" }, "autoload" : { "psr-0" : { diff --git a/composer.lock b/composer.lock index 22602c496..a50f9480b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "01ebe465ed3a59d8350670ebd4ef8793", + "hash": "1799891b06d5a8a516a48fefd429a3ed", "packages": [ { "name": "christian-riesen/base32", @@ -356,6 +356,52 @@ ], "time": "2014-09-05 15:19:58" }, + { + "name": "nickcernis/html-to-markdown", + "version": "2.2.1", + "source": { + "type": "git", + "url": "https://github.com/nickcernis/html-to-markdown.git", + "reference": "7263d2ce65011b050fa7ecda0cbe09b23e84271d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nickcernis/html-to-markdown/zipball/7263d2ce65011b050fa7ecda0cbe09b23e84271d", + "reference": "7263d2ce65011b050fa7ecda0cbe09b23e84271d", + "shasum": "" + }, + "require": { + "php": ">=5.3" + }, + "require-dev": { + "php": ">=5.3.3", + "phpunit/phpunit": "4.*" + }, + "type": "library", + "autoload": { + "classmap": [ + "HTML_To_Markdown.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nick Cernis", + "email": "nick@cern.is", + "homepage": "http://modernnerd.net" + } + ], + "description": "An HTML-to-markdown conversion helper for PHP", + "homepage": "https://github.com/nickcernis/html-to-markdown", + "keywords": [ + "html", + "markdown" + ], + "time": "2015-02-22 12:59:02" + }, { "name": "pimple/pimple", "version": "v3.0.0", diff --git a/docs/postmark.markdown b/docs/postmark.markdown new file mode 100644 index 000000000..f0469dad8 --- /dev/null +++ b/docs/postmark.markdown @@ -0,0 +1,59 @@ +Postmark +======== + +You can use the service [Postmark](https://postmarkapp.com/) to create tasks directly by email. + +This integration works with the inbound email service of Postmark. +Kanboard use a webhook to handle incoming emails. + +Incoming emails workflow +------------------------ + +1. You send an email to a specific address, by example **something+myproject@inbound.mydomain.tld** +2. Your email is forwarded to Postmark SMTP servers +3. Postmark call the Kanboard webhook with the email in JSON format +4. Kanboard parse the received email and create the task to the right project + +Note: New tasks are automatically created in the first column. + +Email format +------------ + +- The local part of the email address must use the plus separator, by example **kanboard+project123** +- The string defined after the plus sign must match a project identifier, by example **project123** is the identifier of the project **Project 123** + +Email format +------------ + +- The email subject becomes the task subject +- The email body becomes the task description (Markdown format) + +Incoming emails can be written in text or HTML formats. +**Kanboard is able to convert simple HTML emails to Markdown**. + +Security and requirements +------------------------- + +- The Kanboard webhook is protected by a random token +- The sender email address (From header) must match a Kanboard user +- The Kanboard project must have a unique identifier, by example **MYPROJECT** +- The Kanboard user must be member of the project + +Postmark configuration +---------------------- + +- Follow the [official documentation about inbound email processing](http://developer.postmarkapp.com/developer-process-configure.html) +- The Kanboard webhook url is displayed in **Settings > Integrations > Postmark** + +Kanboard configuration +---------------------- + +1. Be sure that your users have an email address in their profiles +2. Assign a project identifier to the desired projects: **Project settings > Edit** +3. Try to send an email to your project + +Troubleshootings +---------------- + +- Test the webhook url from the Postmark console, you should have a status code `200 OK` +- Double-check requirements mentioned above diff --git a/tests/units/Base.php b/tests/units/Base.php index bce65f2d8..b63029424 100644 --- a/tests/units/Base.php +++ b/tests/units/Base.php @@ -6,6 +6,8 @@ require __DIR__.'/../../app/constants.php'; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; use Symfony\Component\Stopwatch\Stopwatch; +use SimpleLogger\Logger; +use SimpleLogger\File; date_default_timezone_set('UTC'); @@ -38,6 +40,9 @@ abstract class Base extends PHPUnit_Framework_TestCase ); $this->container['db']->log_queries = true; + + $this->container['logger'] = new Logger; + $this->container['logger']->setLogger(new File('/dev/null')); } public function tearDown() diff --git a/tests/units/PostmarkWebhookTest.php b/tests/units/PostmarkWebhookTest.php new file mode 100644 index 000000000..34be85155 --- /dev/null +++ b/tests/units/PostmarkWebhookTest.php @@ -0,0 +1,83 @@ +container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new User($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(2, $u->create(array('name' => 'me', 'email' => 'me@localhost'))); + + $this->assertEquals(1, $p->create(array('name' => 'test1'))); + $this->assertEquals(2, $p->create(array('name' => 'test2', 'identifier' => 'TEST1'))); + + // Empty payload + $this->assertFalse($w->parsePayload(array())); + + // Unknown user + $this->assertFalse($w->parsePayload(array('From' => 'a@b.c', 'Subject' => 'Email task', 'MailboxHash' => 'foobar', 'TextBody' => 'boo'))); + + // Project not found + $this->assertFalse($w->parsePayload(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test', 'TextBody' => 'boo'))); + + // User is not member + $this->assertFalse($w->parsePayload(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => 'boo'))); + $this->assertTrue($pp->addMember(2, 2)); + + // The task must be created + $this->assertTrue($w->parsePayload(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => 'boo'))); + + $task = $tf->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('boo', $task['description']); + $this->assertEquals(2, $task['creator_id']); + } + + public function testHtml2Markdown() + { + $w = new PostmarkWebhook($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new User($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(2, $u->create(array('name' => 'me', 'email' => 'me@localhost'))); + $this->assertEquals(1, $p->create(array('name' => 'test2', 'identifier' => 'TEST1'))); + $this->assertTrue($pp->addMember(1, 2)); + + $this->assertTrue($w->parsePayload(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => 'boo', 'HtmlBody' => '

boo

'))); + + $task = $tf->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('**boo**', $task['description']); + $this->assertEquals(2, $task['creator_id']); + + $this->assertTrue($w->parsePayload(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => '**boo**', 'HtmlBody' => ''))); + + $task = $tf->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('**boo**', $task['description']); + $this->assertEquals(2, $task['creator_id']); + } +} diff --git a/tests/units/ProjectTest.php b/tests/units/ProjectTest.php index 4864a3aee..231d403f9 100644 --- a/tests/units/ProjectTest.php +++ b/tests/units/ProjectTest.php @@ -203,4 +203,57 @@ class ProjectTest extends Base $this->assertFalse($p->disablePublicAccess(123)); } + + public function testIdentifier() + { + $p = new Project($this->container); + + // Creation + $this->assertEquals(1, $p->create(array('name' => 'UnitTest1', 'identifier' => 'test1'))); + $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'))); + + $project = $p->getById(1); + $this->assertNotEmpty($project); + $this->assertEquals('TEST1', $project['identifier']); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('', $project['identifier']); + + // Update + $this->assertTrue($p->update(array('id' => '2', 'identifier' => 'test2'))); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('TEST2', $project['identifier']); + + $project = $p->getByIdentifier('test1'); + $this->assertNotEmpty($project); + $this->assertEquals('TEST1', $project['identifier']); + + // Validation rules + $r = $p->validateCreation(array('name' => 'test', 'identifier' => 'TEST1')); + $this->assertFalse($r[0]); + + $r = $p->validateCreation(array('name' => 'test', 'identifier' => 'test1')); + $this->assertFalse($r[0]); + + $r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'TEST1')); + $this->assertTrue($r[0]); + + $r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'test3')); + $this->assertTrue($r[0]); + + $r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => '')); + $this->assertTrue($r[0]); + + $r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'TEST2')); + $this->assertFalse($r[0]); + + $r = $p->validateCreation(array('name' => 'test', 'identifier' => 'a-b-c')); + $this->assertFalse($r[0]); + + $r = $p->validateCreation(array('name' => 'test', 'identifier' => 'test 123')); + $this->assertFalse($r[0]); + } }