diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 0d6d91a67f..ad187d74f0 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1108,6 +1108,7 @@ phutil_register_library_map(array( 'HarbormasterBuiltinBuildStepGroup' => 'applications/harbormaster/stepgroup/HarbormasterBuiltinBuildStepGroup.php', 'HarbormasterCircleCIBuildStepImplementation' => 'applications/harbormaster/step/HarbormasterCircleCIBuildStepImplementation.php', 'HarbormasterCircleCIBuildableInterface' => 'applications/harbormaster/interface/HarbormasterCircleCIBuildableInterface.php', + 'HarbormasterCircleCIHookController' => 'applications/harbormaster/controller/HarbormasterCircleCIHookController.php', 'HarbormasterConduitAPIMethod' => 'applications/harbormaster/conduit/HarbormasterConduitAPIMethod.php', 'HarbormasterController' => 'applications/harbormaster/controller/HarbormasterController.php', 'HarbormasterCreateArtifactConduitAPIMethod' => 'applications/harbormaster/conduit/HarbormasterCreateArtifactConduitAPIMethod.php', @@ -5338,6 +5339,7 @@ phutil_register_library_map(array( 'HarbormasterBuildableViewController' => 'HarbormasterController', 'HarbormasterBuiltinBuildStepGroup' => 'HarbormasterBuildStepGroup', 'HarbormasterCircleCIBuildStepImplementation' => 'HarbormasterBuildStepImplementation', + 'HarbormasterCircleCIHookController' => 'HarbormasterController', 'HarbormasterConduitAPIMethod' => 'ConduitAPIMethod', 'HarbormasterController' => 'PhabricatorController', 'HarbormasterCreateArtifactConduitAPIMethod' => 'HarbormasterConduitAPIMethod', diff --git a/src/applications/harbormaster/application/PhabricatorHarbormasterApplication.php b/src/applications/harbormaster/application/PhabricatorHarbormasterApplication.php index d111f266a6..00fb570e03 100644 --- a/src/applications/harbormaster/application/PhabricatorHarbormasterApplication.php +++ b/src/applications/harbormaster/application/PhabricatorHarbormasterApplication.php @@ -91,6 +91,9 @@ final class PhabricatorHarbormasterApplication extends PhabricatorApplication { 'lint/' => array( '(?P\d+)/' => 'HarbormasterLintMessagesController', ), + 'hook/' => array( + 'circleci/' => 'HarbormasterCircleCIHookController', + ), ), ); } diff --git a/src/applications/harbormaster/controller/HarbormasterCircleCIHookController.php b/src/applications/harbormaster/controller/HarbormasterCircleCIHookController.php new file mode 100644 index 0000000000..7f6d4900fe --- /dev/null +++ b/src/applications/harbormaster/controller/HarbormasterCircleCIHookController.php @@ -0,0 +1,87 @@ +setViewer($viewer) + ->withPHIDs(array($target_phid)) + ->needBuildSteps(true) + ->executeOne(); + if ($target) { + $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); + $this->updateTarget($target, $payload); + } + } + + $response = new AphrontWebpageResponse(); + $response->setContent(pht("Request OK\n")); + return $response; + } + + private function updateTarget( + HarbormasterBuildTarget $target, + array $payload) { + + $step = $target->getBuildStep(); + $impl = $step->getStepImplementation(); + if (!($impl instanceof HarbormasterCircleCIBuildStepImplementation)) { + throw new Exception( + pht( + 'Build target ("%s") has the wrong type of build step. Only '. + 'CircleCI build steps may be updated via the CircleCI webhook.', + $target->getPHID())); + } + + switch (idx($payload, 'status')) { + case 'success': + case 'fixed': + $message_type = HarbormasterMessageType::MESSAGE_PASS; + break; + default: + $message_type = HarbormasterMessageType::MESSAGE_FAIL; + break; + } + + $viewer = PhabricatorUser::getOmnipotentUser(); + + $api_method = 'harbormaster.sendmessage'; + $api_params = array( + 'buildTargetPHID' => $target->getPHID(), + 'type' => $message_type, + ); + + id(new ConduitCall($api_method, $api_params)) + ->setUser($viewer) + ->execute(); + } + +} diff --git a/src/applications/harbormaster/step/HarbormasterCircleCIBuildStepImplementation.php b/src/applications/harbormaster/step/HarbormasterCircleCIBuildStepImplementation.php index d4124cee20..365cf657de 100644 --- a/src/applications/harbormaster/step/HarbormasterCircleCIBuildStepImplementation.php +++ b/src/applications/harbormaster/step/HarbormasterCircleCIBuildStepImplementation.php @@ -20,6 +20,9 @@ final class HarbormasterCircleCIBuildStepImplementation } public function getEditInstructions() { + $hook_uri = '/harbormaster/hook/circleci/'; + $hook_uri = PhabricatorEnv::getProductionURI($hook_uri); + return pht(<<getPHID(); + if (!$actor_phid) { + $actor_phid = id(new PhabricatorHarbormasterApplication())->getPHID(); + } + return id(new HarbormasterBuildMessage()) - ->setAuthorPHID($actor->getPHID()) + ->setAuthorPHID($actor_phid) ->setIsConsumed(0); }