Changeset View
Changeset View
Standalone View
Standalone View
src/applications/herald/storage/HeraldWebhook.php
<?php | <?php | ||||
final class HeraldWebhook | final class HeraldWebhook | ||||
extends HeraldDAO | extends HeraldDAO | ||||
implements | implements | ||||
PhabricatorPolicyInterface, | PhabricatorPolicyInterface, | ||||
PhabricatorApplicationTransactionInterface, | PhabricatorApplicationTransactionInterface, | ||||
PhabricatorDestructibleInterface { | PhabricatorDestructibleInterface, | ||||
PhabricatorProjectInterface { | |||||
protected $name; | protected $name; | ||||
protected $webhookURI; | protected $webhookURI; | ||||
protected $viewPolicy; | protected $viewPolicy; | ||||
protected $editPolicy; | protected $editPolicy; | ||||
protected $status; | protected $status; | ||||
protected $hmacKey; | protected $hmacKey; | ||||
Show All 22 Lines | public function getPHIDType() { | ||||
return HeraldWebhookPHIDType::TYPECONST; | return HeraldWebhookPHIDType::TYPECONST; | ||||
} | } | ||||
public static function initializeNewWebhook(PhabricatorUser $viewer) { | public static function initializeNewWebhook(PhabricatorUser $viewer) { | ||||
return id(new self()) | return id(new self()) | ||||
->setStatus(self::HOOKSTATUS_ENABLED) | ->setStatus(self::HOOKSTATUS_ENABLED) | ||||
->setViewPolicy(PhabricatorPolicies::getMostOpenPolicy()) | ->setViewPolicy(PhabricatorPolicies::getMostOpenPolicy()) | ||||
->setEditPolicy($viewer->getPHID()) | ->setEditPolicy($viewer->getPHID()) | ||||
->setHmacKey(Filesystem::readRandomCharacters(32)); | ->regenerateHMACKey(); | ||||
} | } | ||||
public function getURI() { | public function getURI() { | ||||
return '/herald/webhook/view/'.$this->getID().'/'; | return '/herald/webhook/view/'.$this->getID().'/'; | ||||
} | } | ||||
public function isDisabled() { | public function isDisabled() { | ||||
return ($this->getStatus() === self::HOOKSTATUS_DISABLED); | return ($this->getStatus() === self::HOOKSTATUS_DISABLED); | ||||
} | } | ||||
public static function getStatusDisplayNameMap() { | public static function getStatusDisplayNameMap() { | ||||
$specs = self::getStatusSpecifications(); | |||||
return ipull($specs, 'name', 'key'); | |||||
} | |||||
private static function getStatusSpecifications() { | |||||
$specs = array( | |||||
array( | |||||
'key' => self::HOOKSTATUS_FIREHOSE, | |||||
'name' => pht('Firehose'), | |||||
'color' => 'orange', | |||||
'icon' => 'fa-star-o', | |||||
), | |||||
array( | |||||
'key' => self::HOOKSTATUS_ENABLED, | |||||
'name' => pht('Enabled'), | |||||
'color' => 'bluegrey', | |||||
'icon' => 'fa-check', | |||||
), | |||||
array( | |||||
'key' => self::HOOKSTATUS_DISABLED, | |||||
'name' => pht('Disabled'), | |||||
'color' => 'dark', | |||||
'icon' => 'fa-ban', | |||||
), | |||||
); | |||||
return ipull($specs, null, 'key'); | |||||
} | |||||
private static function getSpecificationForStatus($status) { | |||||
$specs = self::getStatusSpecifications(); | |||||
if (isset($specs[$status])) { | |||||
return $specs[$status]; | |||||
} | |||||
return array( | return array( | ||||
self::HOOKSTATUS_FIREHOSE => pht('Firehose'), | 'key' => $status, | ||||
self::HOOKSTATUS_ENABLED => pht('Enabled'), | 'name' => pht('Unknown ("%s")', $status), | ||||
self::HOOKSTATUS_DISABLED => pht('Disabled'), | 'icon' => 'fa-question', | ||||
'color' => 'indigo', | |||||
); | ); | ||||
} | } | ||||
public static function getDisplayNameForStatus($status) { | |||||
$spec = self::getSpecificationForStatus($status); | |||||
return $spec['name']; | |||||
} | |||||
public static function getIconForStatus($status) { | |||||
$spec = self::getSpecificationForStatus($status); | |||||
return $spec['icon']; | |||||
} | |||||
public static function getColorForStatus($status) { | |||||
$spec = self::getSpecificationForStatus($status); | |||||
return $spec['color']; | |||||
} | |||||
public function getStatusDisplayName() { | public function getStatusDisplayName() { | ||||
$status = $this->getStatus(); | $status = $this->getStatus(); | ||||
return idx($this->getStatusDisplayNameMap(), $status); | return self::getDisplayNameForStatus($status); | ||||
} | |||||
public function getStatusIcon() { | |||||
$status = $this->getStatus(); | |||||
return self::getIconForStatus($status); | |||||
} | |||||
public function getStatusColor() { | |||||
$status = $this->getStatus(); | |||||
return self::getColorForStatus($status); | |||||
} | } | ||||
public function getErrorBackoffWindow() { | public function getErrorBackoffWindow() { | ||||
return phutil_units('5 minutes in seconds'); | return phutil_units('5 minutes in seconds'); | ||||
} | } | ||||
public function getErrorBackoffThreshold() { | public function getErrorBackoffThreshold() { | ||||
return 10; | return 10; | ||||
Show All 19 Lines | public function isInErrorBackoff(PhabricatorUser $viewer) { | ||||
if (count($requests) >= $backoff_threshold) { | if (count($requests) >= $backoff_threshold) { | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
public function regenerateHMACKey() { | |||||
return $this->setHMACKey(Filesystem::readRandomCharacters(32)); | |||||
} | |||||
/* -( PhabricatorPolicyInterface )----------------------------------------- */ | /* -( PhabricatorPolicyInterface )----------------------------------------- */ | ||||
public function getCapabilities() { | public function getCapabilities() { | ||||
return array( | return array( | ||||
PhabricatorPolicyCapability::CAN_VIEW, | PhabricatorPolicyCapability::CAN_VIEW, | ||||
PhabricatorPolicyCapability::CAN_EDIT, | PhabricatorPolicyCapability::CAN_EDIT, | ||||
▲ Show 20 Lines • Show All 66 Lines • Show Last 20 Lines |