Remove product literal strings in "pht()", part 6
[phabricator.git] / src / applications / harbormaster / controller / HarbormasterBuildkiteHookController.php
blobdf50ea91263794c84f949e0d94e35bab58c63a3c
1 <?php
3 final class HarbormasterBuildkiteHookController
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 $event = idx($body, 'event');
18 if ($event != 'build.finished') {
19 return $this->newHookResponse(pht('OK: Ignored event.'));
22 $build = idx($body, 'build');
23 if (!is_array($build)) {
24 throw new Exception(
25 pht(
26 'Expected "%s" property to contain a dictionary.',
27 'build'));
30 $meta_data = idx($build, 'meta_data');
31 if (!is_array($meta_data)) {
32 throw new Exception(
33 pht(
34 'Expected "%s" property to contain a dictionary.',
35 'build.meta_data'));
38 $target_phid = idx($meta_data, 'buildTargetPHID');
39 if (!$target_phid) {
40 return $this->newHookResponse(pht('OK: No Harbormaster target PHID.'));
43 $viewer = PhabricatorUser::getOmnipotentUser();
44 $target = id(new HarbormasterBuildTargetQuery())
45 ->setViewer($viewer)
46 ->withPHIDs(array($target_phid))
47 ->needBuildSteps(true)
48 ->executeOne();
49 if (!$target) {
50 throw new Exception(
51 pht(
52 'Harbormaster build target "%s" does not exist.',
53 $target_phid));
56 $step = $target->getBuildStep();
57 $impl = $step->getStepImplementation();
58 if (!($impl instanceof HarbormasterBuildkiteBuildStepImplementation)) {
59 throw new Exception(
60 pht(
61 'Harbormaster build target "%s" is not a Buildkite build step. '.
62 'Only Buildkite steps may be updated via the Buildkite hook.',
63 $target_phid));
66 $webhook_token = $impl->getSetting('webhook.token');
67 $request_token = $request->getHTTPHeader('X-Buildkite-Token');
69 if (!phutil_hashes_are_identical($webhook_token, $request_token)) {
70 throw new Exception(
71 pht(
72 'Buildkite request to target "%s" had the wrong authentication '.
73 'token. The Buildkite pipeline and Harbormaster build step must '.
74 'be configured with the same token.',
75 $target_phid));
78 $state = idx($build, 'state');
79 switch ($state) {
80 case 'passed':
81 $message_type = HarbormasterMessageType::MESSAGE_PASS;
82 break;
83 default:
84 $message_type = HarbormasterMessageType::MESSAGE_FAIL;
85 break;
88 $api_method = 'harbormaster.sendmessage';
89 $api_params = array(
90 'buildTargetPHID' => $target_phid,
91 'type' => $message_type,
94 $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
96 id(new ConduitCall($api_method, $api_params))
97 ->setUser($viewer)
98 ->execute();
100 unset($unguarded);
102 return $this->newHookResponse(pht('OK: Processed event.'));
105 private function newHookResponse($message) {
106 $response = new AphrontWebpageResponse();
107 $response->setContent($message);
108 return $response;