Remove all "FileHasObject" edge reads and writes
[phabricator.git] / src / applications / harbormaster / controller / HarbormasterCircleCIHookController.php
blob7f6d4900fec082d5f01803ddd1071eebee9080de
1 <?php
3 final class HarbormasterCircleCIHookController
4 extends HarbormasterController {
6 public function shouldRequireLogin() {
7 return false;
10 /**
11 * @phutil-external-symbol class PhabricatorStartup
13 public function handleRequest(AphrontRequest $request) {
14 $raw_body = PhabricatorStartup::getRawInput();
15 $body = phutil_json_decode($raw_body);
17 $payload = $body['payload'];
19 $parameters = idx($payload, 'build_parameters');
20 if (!$parameters) {
21 $parameters = array();
24 $target_phid = idx($parameters, 'HARBORMASTER_BUILD_TARGET_PHID');
26 // NOTE: We'll get callbacks here for builds we triggered, but also for
27 // arbitrary builds the system executes for other reasons. So it's normal
28 // to get some notifications with no Build Target PHID. We just ignore
29 // these under the assumption that they're routine builds caused by events
30 // like branch updates.
32 if ($target_phid) {
33 $viewer = PhabricatorUser::getOmnipotentUser();
34 $target = id(new HarbormasterBuildTargetQuery())
35 ->setViewer($viewer)
36 ->withPHIDs(array($target_phid))
37 ->needBuildSteps(true)
38 ->executeOne();
39 if ($target) {
40 $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
41 $this->updateTarget($target, $payload);
45 $response = new AphrontWebpageResponse();
46 $response->setContent(pht("Request OK\n"));
47 return $response;
50 private function updateTarget(
51 HarbormasterBuildTarget $target,
52 array $payload) {
54 $step = $target->getBuildStep();
55 $impl = $step->getStepImplementation();
56 if (!($impl instanceof HarbormasterCircleCIBuildStepImplementation)) {
57 throw new Exception(
58 pht(
59 'Build target ("%s") has the wrong type of build step. Only '.
60 'CircleCI build steps may be updated via the CircleCI webhook.',
61 $target->getPHID()));
64 switch (idx($payload, 'status')) {
65 case 'success':
66 case 'fixed':
67 $message_type = HarbormasterMessageType::MESSAGE_PASS;
68 break;
69 default:
70 $message_type = HarbormasterMessageType::MESSAGE_FAIL;
71 break;
74 $viewer = PhabricatorUser::getOmnipotentUser();
76 $api_method = 'harbormaster.sendmessage';
77 $api_params = array(
78 'buildTargetPHID' => $target->getPHID(),
79 'type' => $message_type,
82 id(new ConduitCall($api_method, $api_params))
83 ->setUser($viewer)
84 ->execute();