Path: blob/master/src/applications/herald/controller/HeraldWebhookViewController.php
12262 views
<?php12final class HeraldWebhookViewController3extends HeraldWebhookController {45public function shouldAllowPublic() {6return true;7}89public function handleRequest(AphrontRequest $request) {10$viewer = $this->getViewer();1112$hook = id(new HeraldWebhookQuery())13->setViewer($viewer)14->withIDs(array($request->getURIData('id')))15->executeOne();16if (!$hook) {17return new Aphront404Response();18}1920$header = $this->buildHeaderView($hook);2122$warnings = null;23if ($hook->isInErrorBackoff($viewer)) {24$message = pht(25'Many requests to this webhook have failed recently (at least %s '.26'errors in the last %s seconds). New requests are temporarily paused.',27$hook->getErrorBackoffThreshold(),28$hook->getErrorBackoffWindow());2930$warnings = id(new PHUIInfoView())31->setSeverity(PHUIInfoView::SEVERITY_WARNING)32->setErrors(33array(34$message,35));36}3738$curtain = $this->buildCurtain($hook);39$properties_view = $this->buildPropertiesView($hook);4041$timeline = $this->buildTransactionTimeline(42$hook,43new HeraldWebhookTransactionQuery());44$timeline->setShouldTerminate(true);4546$requests = id(new HeraldWebhookRequestQuery())47->setViewer($viewer)48->withWebhookPHIDs(array($hook->getPHID()))49->setLimit(20)50->execute();5152$warnings = array();53if (PhabricatorEnv::getEnvConfig('phabricator.silent')) {54$message = pht(55'This server is running in silent mode, so it will not '.56'publish webhooks. To adjust this setting, see '.57'@{config:phabricator.silent} in Config.');5859$warnings[] = id(new PHUIInfoView())60->setTitle(pht('Silent Mode'))61->setSeverity(PHUIInfoView::SEVERITY_WARNING)62->appendChild(new PHUIRemarkupView($viewer, $message));63}6465$requests_table = id(new HeraldWebhookRequestListView())66->setViewer($viewer)67->setRequests($requests)68->setHighlightID($request->getURIData('requestID'));6970$requests_view = id(new PHUIObjectBoxView())71->setHeaderText(pht('Recent Requests'))72->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)73->setTable($requests_table);7475$rules_view = $this->newRulesView($hook);7677$hook_view = id(new PHUITwoColumnView())78->setHeader($header)79->setMainColumn(80array(81$warnings,82$properties_view,83$rules_view,84$requests_view,85$timeline,86))87->setCurtain($curtain);8889$crumbs = $this->buildApplicationCrumbs()90->addTextCrumb(pht('Webhook %d', $hook->getID()))91->setBorder(true);9293return $this->newPage()94->setTitle(95array(96pht('Webhook %d', $hook->getID()),97$hook->getName(),98))99->setCrumbs($crumbs)100->setPageObjectPHIDs(101array(102$hook->getPHID(),103))104->appendChild($hook_view);105}106107private function buildHeaderView(HeraldWebhook $hook) {108$viewer = $this->getViewer();109110$title = $hook->getName();111112$status_icon = $hook->getStatusIcon();113$status_color = $hook->getStatusColor();114$status_name = $hook->getStatusDisplayName();115116$header = id(new PHUIHeaderView())117->setHeader($title)118->setViewer($viewer)119->setPolicyObject($hook)120->setStatus($status_icon, $status_color, $status_name)121->setHeaderIcon('fa-cloud-upload');122123return $header;124}125126127private function buildCurtain(HeraldWebhook $hook) {128$viewer = $this->getViewer();129$curtain = $this->newCurtainView($hook);130131$can_edit = PhabricatorPolicyFilter::hasCapability(132$viewer,133$hook,134PhabricatorPolicyCapability::CAN_EDIT);135136$id = $hook->getID();137$edit_uri = $this->getApplicationURI("webhook/edit/{$id}/");138$test_uri = $this->getApplicationURI("webhook/test/{$id}/");139140$key_view_uri = $this->getApplicationURI("webhook/key/view/{$id}/");141$key_cycle_uri = $this->getApplicationURI("webhook/key/cycle/{$id}/");142143$curtain->addAction(144id(new PhabricatorActionView())145->setName(pht('Edit Webhook'))146->setIcon('fa-pencil')147->setDisabled(!$can_edit)148->setWorkflow(!$can_edit)149->setHref($edit_uri));150151$curtain->addAction(152id(new PhabricatorActionView())153->setName(pht('New Test Request'))154->setIcon('fa-cloud-upload')155->setDisabled(!$can_edit)156->setWorkflow(true)157->setHref($test_uri));158159$curtain->addAction(160id(new PhabricatorActionView())161->setName(pht('View HMAC Key'))162->setIcon('fa-key')163->setDisabled(!$can_edit)164->setWorkflow(true)165->setHref($key_view_uri));166167$curtain->addAction(168id(new PhabricatorActionView())169->setName(pht('Regenerate HMAC Key'))170->setIcon('fa-refresh')171->setDisabled(!$can_edit)172->setWorkflow(true)173->setHref($key_cycle_uri));174175return $curtain;176}177178179private function buildPropertiesView(HeraldWebhook $hook) {180$viewer = $this->getViewer();181182$properties = id(new PHUIPropertyListView())183->setViewer($viewer);184185$properties->addProperty(186pht('URI'),187$hook->getWebhookURI());188189$properties->addProperty(190pht('Status'),191$hook->getStatusDisplayName());192193return id(new PHUIObjectBoxView())194->setHeaderText(pht('Details'))195->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)196->appendChild($properties);197}198199private function newRulesView(HeraldWebhook $hook) {200$viewer = $this->getViewer();201202$rules = id(new HeraldRuleQuery())203->setViewer($viewer)204->withDisabled(false)205->withAffectedObjectPHIDs(array($hook->getPHID()))206->needValidateAuthors(true)207->setLimit(10)208->execute();209210$list = id(new HeraldRuleListView())211->setViewer($viewer)212->setRules($rules)213->newObjectList();214215$list->setNoDataString(pht('No active Herald rules call this webhook.'));216217$more_href = new PhutilURI(218'/herald/',219array('affectedPHID' => $hook->getPHID()));220221$more_link = id(new PHUIButtonView())222->setTag('a')223->setIcon('fa-list-ul')224->setText(pht('View All Rules'))225->setHref($more_href);226227$header = id(new PHUIHeaderView())228->setHeader(pht('Called By Herald Rules'))229->addActionLink($more_link);230231return id(new PHUIObjectBoxView())232->setHeader($header)233->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)234->appendChild($list);235}236237}238239240