Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/harbormaster/conduit/HarbormasterQueryAutotargetsConduitAPIMethod.php
12256 views
1
<?php
2
3
final class HarbormasterQueryAutotargetsConduitAPIMethod
4
extends HarbormasterConduitAPIMethod {
5
6
public function getAPIMethodName() {
7
return 'harbormaster.queryautotargets';
8
}
9
10
public function getMethodDescription() {
11
return pht('Load or create build autotargets.');
12
}
13
14
protected function defineParamTypes() {
15
return array(
16
'objectPHID' => 'phid',
17
'targetKeys' => 'list<string>',
18
);
19
}
20
21
protected function defineReturnType() {
22
return 'map<string, phid>';
23
}
24
25
protected function execute(ConduitAPIRequest $request) {
26
$viewer = $request->getUser();
27
28
$phid = $request->getValue('objectPHID');
29
30
// NOTE: We use withNames() to let monograms like "D123" work, which makes
31
// this a little easier to test. Real PHIDs will still work as expected.
32
33
$object = id(new PhabricatorObjectQuery())
34
->setViewer($viewer)
35
->withNames(array($phid))
36
->executeOne();
37
if (!$object) {
38
throw new Exception(
39
pht(
40
'No such object "%s" exists.',
41
$phid));
42
}
43
44
if (!($object instanceof HarbormasterBuildableInterface)) {
45
throw new Exception(
46
pht(
47
'Object "%s" does not implement interface "%s". Autotargets may '.
48
'only be queried for buildable objects.',
49
$phid,
50
'HarbormasterBuildableInterface'));
51
}
52
53
$autotargets = $request->getValue('targetKeys', array());
54
55
if ($autotargets) {
56
$targets = id(new HarbormasterTargetEngine())
57
->setViewer($viewer)
58
->setObject($object)
59
->setAutoTargetKeys($autotargets)
60
->buildTargets();
61
} else {
62
$targets = array();
63
}
64
65
// Reorder the results according to the request order so we can make test
66
// assertions that subsequent calls return the same results.
67
68
$map = mpull($targets, 'getPHID');
69
$map = array_select_keys($map, $autotargets);
70
71
return array(
72
'targetMap' => $map,
73
);
74
}
75
76
}
77
78