Path: blob/master/src/applications/harbormaster/controller/HarbormasterCircleCIHookController.php
12256 views
<?php12final class HarbormasterCircleCIHookController3extends HarbormasterController {45public function shouldRequireLogin() {6return false;7}89/**10* @phutil-external-symbol class PhabricatorStartup11*/12public function handleRequest(AphrontRequest $request) {13$raw_body = PhabricatorStartup::getRawInput();14$body = phutil_json_decode($raw_body);1516$payload = $body['payload'];1718$parameters = idx($payload, 'build_parameters');19if (!$parameters) {20$parameters = array();21}2223$target_phid = idx($parameters, 'HARBORMASTER_BUILD_TARGET_PHID');2425// NOTE: We'll get callbacks here for builds we triggered, but also for26// arbitrary builds the system executes for other reasons. So it's normal27// to get some notifications with no Build Target PHID. We just ignore28// these under the assumption that they're routine builds caused by events29// like branch updates.3031if ($target_phid) {32$viewer = PhabricatorUser::getOmnipotentUser();33$target = id(new HarbormasterBuildTargetQuery())34->setViewer($viewer)35->withPHIDs(array($target_phid))36->needBuildSteps(true)37->executeOne();38if ($target) {39$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();40$this->updateTarget($target, $payload);41}42}4344$response = new AphrontWebpageResponse();45$response->setContent(pht("Request OK\n"));46return $response;47}4849private function updateTarget(50HarbormasterBuildTarget $target,51array $payload) {5253$step = $target->getBuildStep();54$impl = $step->getStepImplementation();55if (!($impl instanceof HarbormasterCircleCIBuildStepImplementation)) {56throw new Exception(57pht(58'Build target ("%s") has the wrong type of build step. Only '.59'CircleCI build steps may be updated via the CircleCI webhook.',60$target->getPHID()));61}6263switch (idx($payload, 'status')) {64case 'success':65case 'fixed':66$message_type = HarbormasterMessageType::MESSAGE_PASS;67break;68default:69$message_type = HarbormasterMessageType::MESSAGE_FAIL;70break;71}7273$viewer = PhabricatorUser::getOmnipotentUser();7475$api_method = 'harbormaster.sendmessage';76$api_params = array(77'buildTargetPHID' => $target->getPHID(),78'type' => $message_type,79);8081id(new ConduitCall($api_method, $api_params))82->setUser($viewer)83->execute();84}8586}878889