1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-13 00:01:03 +01:00

Add a "Call webhooks" action to Herald

Summary: Depends on D19048. Fixes T11330.

Test Plan: Wrote rules to call webhooks selectively, saw them fire appropriately with correct trigger attribution.

Maniphest Tasks: T11330

Differential Revision: https://secure.phabricator.com/D19049
This commit is contained in:
epriestley 2018-02-09 12:18:15 -08:00
parent 41d28abfcc
commit 98c701ffc5
5 changed files with 129 additions and 0 deletions

View file

@ -1357,6 +1357,7 @@ phutil_register_library_map(array(
'HeraldApplyTranscript' => 'applications/herald/storage/transcript/HeraldApplyTranscript.php',
'HeraldBasicFieldGroup' => 'applications/herald/field/HeraldBasicFieldGroup.php',
'HeraldBuildableState' => 'applications/herald/state/HeraldBuildableState.php',
'HeraldCallWebhookAction' => 'applications/herald/action/HeraldCallWebhookAction.php',
'HeraldCommentAction' => 'applications/herald/action/HeraldCommentAction.php',
'HeraldCommitAdapter' => 'applications/diffusion/herald/HeraldCommitAdapter.php',
'HeraldCondition' => 'applications/herald/storage/HeraldCondition.php',
@ -1444,6 +1445,7 @@ phutil_register_library_map(array(
'HeraldWebhook' => 'applications/herald/storage/HeraldWebhook.php',
'HeraldWebhookCallManagementWorkflow' => 'applications/herald/management/HeraldWebhookCallManagementWorkflow.php',
'HeraldWebhookController' => 'applications/herald/controller/HeraldWebhookController.php',
'HeraldWebhookDatasource' => 'applications/herald/typeahead/HeraldWebhookDatasource.php',
'HeraldWebhookEditController' => 'applications/herald/controller/HeraldWebhookEditController.php',
'HeraldWebhookEditEngine' => 'applications/herald/editor/HeraldWebhookEditEngine.php',
'HeraldWebhookEditor' => 'applications/herald/editor/HeraldWebhookEditor.php',
@ -6631,6 +6633,7 @@ phutil_register_library_map(array(
'HeraldApplyTranscript' => 'Phobject',
'HeraldBasicFieldGroup' => 'HeraldFieldGroup',
'HeraldBuildableState' => 'HeraldState',
'HeraldCallWebhookAction' => 'HeraldAction',
'HeraldCommentAction' => 'HeraldAction',
'HeraldCommitAdapter' => array(
'HeraldAdapter',
@ -6741,6 +6744,7 @@ phutil_register_library_map(array(
),
'HeraldWebhookCallManagementWorkflow' => 'HeraldWebhookManagementWorkflow',
'HeraldWebhookController' => 'HeraldController',
'HeraldWebhookDatasource' => 'PhabricatorTypeaheadDatasource',
'HeraldWebhookEditController' => 'HeraldWebhookController',
'HeraldWebhookEditEngine' => 'PhabricatorEditEngine',
'HeraldWebhookEditor' => 'PhabricatorApplicationTransactionEditor',

View file

@ -0,0 +1,62 @@
<?php
final class HeraldCallWebhookAction extends HeraldAction {
const ACTIONCONST = 'webhook';
const DO_WEBHOOK = 'do.call-webhook';
public function getHeraldActionName() {
return pht('Call webhooks');
}
public function getActionGroupKey() {
return HeraldUtilityActionGroup::ACTIONGROUPKEY;
}
public function supportsObject($object) {
return true;
}
public function supportsRuleType($rule_type) {
return ($rule_type !== HeraldRuleTypeConfig::RULE_TYPE_PERSONAL);
}
public function applyEffect($object, HeraldEffect $effect) {
$adapter = $this->getAdapter();
$rule = $effect->getRule();
$target = $effect->getTarget();
foreach ($target as $webhook_phid) {
$adapter->queueWebhook($webhook_phid, $rule->getPHID());
}
$this->logEffect(self::DO_WEBHOOK, $target);
}
public function getHeraldActionStandardType() {
return self::STANDARD_PHID_LIST;
}
protected function getActionEffectMap() {
return array(
self::DO_WEBHOOK => array(
'icon' => 'fa-cloud-upload',
'color' => 'green',
'name' => pht('Called Webhooks'),
),
);
}
public function renderActionDescription($value) {
return pht('Call webhooks: %s.', $this->renderHandleList($value));
}
protected function renderActionEffectDescription($type, $data) {
return pht('Called webhooks: %s.', $this->renderHandleList($data));
}
protected function getDatasource() {
return new HeraldWebhookDatasource();
}
}

View file

@ -41,6 +41,7 @@ abstract class HeraldAdapter extends Phobject {
private $viewer;
private $mustEncryptReasons = array();
private $actingAsPHID;
private $webhookMap = array();
public function getEmailPHIDs() {
return array_values($this->emailPHIDs);
@ -1206,4 +1207,17 @@ abstract class HeraldAdapter extends Phobject {
return $this->mustEncryptReasons;
}
/* -( Webhooks )----------------------------------------------------------- */
final public function queueWebhook($webhook_phid, $rule_phid) {
$this->webhookMap[$webhook_phid][] = $rule_phid;
return $this;
}
final public function getWebhookMap() {
return $this->webhookMap;
}
}

View file

@ -0,0 +1,48 @@
<?php
final class HeraldWebhookDatasource
extends PhabricatorTypeaheadDatasource {
public function getPlaceholderText() {
return pht('Type a webhook name...');
}
public function getBrowseTitle() {
return pht('Browse Webhooks');
}
public function getDatasourceApplicationClass() {
return 'PhabricatorHeraldApplication';
}
public function loadResults() {
$viewer = $this->getViewer();
$raw_query = $this->getRawQuery();
$hooks = id(new HeraldWebhookQuery())
->setViewer($viewer)
->execute();
$handles = id(new PhabricatorHandleQuery())
->setViewer($viewer)
->withPHIDs(mpull($hooks, 'getPHID'))
->execute();
$results = array();
foreach ($hooks as $hook) {
$handle = $handles[$hook->getPHID()];
$result = id(new PhabricatorTypeaheadResult())
->setName($handle->getFullName())
->setPHID($handle->getPHID());
if ($hook->isDisabled()) {
$result->setClosed(pht('Disabled'));
}
$results[] = $result;
}
return $results;
}
}

View file

@ -1156,6 +1156,7 @@ abstract class PhabricatorApplicationTransactionEditor
$adapter = $this->getHeraldAdapter();
$this->heraldEmailPHIDs = $adapter->getEmailPHIDs();
$this->heraldForcedEmailPHIDs = $adapter->getForcedEmailPHIDs();
$this->webhookMap = $adapter->getWebhookMap();
}
$xactions = $this->didApplyTransactions($object, $xactions);