Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/harbormaster/controller/HarbormasterBuildkiteHookController.php
12256 views
1
<?php
2
3
final class HarbormasterBuildkiteHookController
4
extends HarbormasterController {
5
6
public function shouldRequireLogin() {
7
return false;
8
}
9
10
/**
11
* @phutil-external-symbol class PhabricatorStartup
12
*/
13
public function handleRequest(AphrontRequest $request) {
14
$raw_body = PhabricatorStartup::getRawInput();
15
$body = phutil_json_decode($raw_body);
16
17
$event = idx($body, 'event');
18
if ($event != 'build.finished') {
19
return $this->newHookResponse(pht('OK: Ignored event.'));
20
}
21
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'));
28
}
29
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'));
36
}
37
38
$target_phid = idx($meta_data, 'buildTargetPHID');
39
if (!$target_phid) {
40
return $this->newHookResponse(pht('OK: No Harbormaster target PHID.'));
41
}
42
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));
54
}
55
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));
64
}
65
66
$webhook_token = $impl->getSetting('webhook.token');
67
$request_token = $request->getHTTPHeader('X-Buildkite-Token');
68
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));
76
}
77
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;
86
}
87
88
$api_method = 'harbormaster.sendmessage';
89
$api_params = array(
90
'buildTargetPHID' => $target_phid,
91
'type' => $message_type,
92
);
93
94
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
95
96
id(new ConduitCall($api_method, $api_params))
97
->setUser($viewer)
98
->execute();
99
100
unset($unguarded);
101
102
return $this->newHookResponse(pht('OK: Processed event.'));
103
}
104
105
private function newHookResponse($message) {
106
$response = new AphrontWebpageResponse();
107
$response->setContent($message);
108
return $response;
109
}
110
111
}
112
113