Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/harbormaster/query/HarbormasterBuildQuery.php
12256 views
1
<?php
2
3
final class HarbormasterBuildQuery
4
extends PhabricatorCursorPagedPolicyAwareQuery {
5
6
private $ids;
7
private $phids;
8
private $buildStatuses;
9
private $buildablePHIDs;
10
private $buildPlanPHIDs;
11
private $initiatorPHIDs;
12
private $needBuildTargets;
13
private $autobuilds;
14
15
public function withIDs(array $ids) {
16
$this->ids = $ids;
17
return $this;
18
}
19
20
public function withPHIDs(array $phids) {
21
$this->phids = $phids;
22
return $this;
23
}
24
25
public function withBuildStatuses(array $build_statuses) {
26
$this->buildStatuses = $build_statuses;
27
return $this;
28
}
29
30
public function withBuildablePHIDs(array $buildable_phids) {
31
$this->buildablePHIDs = $buildable_phids;
32
return $this;
33
}
34
35
public function withBuildPlanPHIDs(array $build_plan_phids) {
36
$this->buildPlanPHIDs = $build_plan_phids;
37
return $this;
38
}
39
40
public function withInitiatorPHIDs(array $initiator_phids) {
41
$this->initiatorPHIDs = $initiator_phids;
42
return $this;
43
}
44
45
public function withAutobuilds($with_autobuilds) {
46
$this->autobuilds = $with_autobuilds;
47
return $this;
48
}
49
50
public function needBuildTargets($need_targets) {
51
$this->needBuildTargets = $need_targets;
52
return $this;
53
}
54
55
public function newResultObject() {
56
return new HarbormasterBuild();
57
}
58
59
protected function willFilterPage(array $page) {
60
$buildables = array();
61
62
$buildable_phids = array_filter(mpull($page, 'getBuildablePHID'));
63
if ($buildable_phids) {
64
$buildables = id(new PhabricatorObjectQuery())
65
->setViewer($this->getViewer())
66
->withPHIDs($buildable_phids)
67
->setParentQuery($this)
68
->execute();
69
$buildables = mpull($buildables, null, 'getPHID');
70
}
71
72
foreach ($page as $key => $build) {
73
$buildable_phid = $build->getBuildablePHID();
74
if (empty($buildables[$buildable_phid])) {
75
unset($page[$key]);
76
continue;
77
}
78
$build->attachBuildable($buildables[$buildable_phid]);
79
}
80
81
return $page;
82
}
83
84
protected function didFilterPage(array $page) {
85
$plans = array();
86
87
$plan_phids = array_filter(mpull($page, 'getBuildPlanPHID'));
88
if ($plan_phids) {
89
$plans = id(new PhabricatorObjectQuery())
90
->setViewer($this->getViewer())
91
->withPHIDs($plan_phids)
92
->setParentQuery($this)
93
->execute();
94
$plans = mpull($plans, null, 'getPHID');
95
}
96
97
foreach ($page as $key => $build) {
98
$plan_phid = $build->getBuildPlanPHID();
99
$build->attachBuildPlan(idx($plans, $plan_phid));
100
}
101
102
$build_phids = mpull($page, 'getPHID');
103
$messages = id(new HarbormasterBuildMessage())->loadAllWhere(
104
'receiverPHID IN (%Ls) AND isConsumed = 0 ORDER BY id ASC',
105
$build_phids);
106
$messages = mgroup($messages, 'getReceiverPHID');
107
foreach ($page as $build) {
108
$unprocessed_messages = idx($messages, $build->getPHID(), array());
109
$build->attachUnprocessedMessages($unprocessed_messages);
110
}
111
112
if ($this->needBuildTargets) {
113
$targets = id(new HarbormasterBuildTargetQuery())
114
->setViewer($this->getViewer())
115
->setParentQuery($this)
116
->withBuildPHIDs($build_phids)
117
->execute();
118
119
// TODO: Some day, when targets have dependencies, we should toposort
120
// these. For now, just put them into chronological order.
121
$targets = array_reverse($targets);
122
123
$targets = mgroup($targets, 'getBuildPHID');
124
foreach ($page as $build) {
125
$build_targets = idx($targets, $build->getPHID(), array());
126
127
foreach ($build_targets as $phid => $target) {
128
if ($target->getBuildGeneration() !== $build->getBuildGeneration()) {
129
unset($build_targets[$phid]);
130
}
131
}
132
133
$build->attachBuildTargets($build_targets);
134
}
135
}
136
137
return $page;
138
}
139
140
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
141
$where = parent::buildWhereClauseParts($conn);
142
143
if ($this->ids !== null) {
144
$where[] = qsprintf(
145
$conn,
146
'b.id IN (%Ld)',
147
$this->ids);
148
}
149
150
if ($this->phids !== null) {
151
$where[] = qsprintf(
152
$conn,
153
'b.phid in (%Ls)',
154
$this->phids);
155
}
156
157
if ($this->buildStatuses !== null) {
158
$where[] = qsprintf(
159
$conn,
160
'b.buildStatus in (%Ls)',
161
$this->buildStatuses);
162
}
163
164
if ($this->buildablePHIDs !== null) {
165
$where[] = qsprintf(
166
$conn,
167
'b.buildablePHID IN (%Ls)',
168
$this->buildablePHIDs);
169
}
170
171
if ($this->buildPlanPHIDs !== null) {
172
$where[] = qsprintf(
173
$conn,
174
'b.buildPlanPHID IN (%Ls)',
175
$this->buildPlanPHIDs);
176
}
177
178
if ($this->initiatorPHIDs !== null) {
179
$where[] = qsprintf(
180
$conn,
181
'b.initiatorPHID IN (%Ls)',
182
$this->initiatorPHIDs);
183
}
184
185
if ($this->autobuilds !== null) {
186
if ($this->autobuilds) {
187
$where[] = qsprintf(
188
$conn,
189
'p.planAutoKey IS NOT NULL');
190
} else {
191
$where[] = qsprintf(
192
$conn,
193
'p.planAutoKey IS NULL');
194
}
195
}
196
197
return $where;
198
}
199
200
protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
201
$joins = parent::buildJoinClauseParts($conn);
202
203
if ($this->shouldJoinPlanTable()) {
204
$joins[] = qsprintf(
205
$conn,
206
'JOIN %T p ON b.buildPlanPHID = p.phid',
207
id(new HarbormasterBuildPlan())->getTableName());
208
}
209
210
return $joins;
211
}
212
213
private function shouldJoinPlanTable() {
214
if ($this->autobuilds !== null) {
215
return true;
216
}
217
218
return false;
219
}
220
221
public function getQueryApplicationClass() {
222
return 'PhabricatorHarbormasterApplication';
223
}
224
225
protected function getPrimaryTableAlias() {
226
return 'b';
227
}
228
229
}
230
231