3 final class ReleephRequestQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery
{
6 private $requestedCommitPHIDs;
10 private $requestorPHIDs;
12 private $requestedObjectPHIDs;
14 const STATUS_ALL
= 'status-all';
15 const STATUS_OPEN
= 'status-open';
16 const STATUS_REQUESTED
= 'status-requested';
17 const STATUS_NEEDS_PULL
= 'status-needs-pull';
18 const STATUS_REJECTED
= 'status-rejected';
19 const STATUS_ABANDONED
= 'status-abandoned';
20 const STATUS_PULLED
= 'status-pulled';
21 const STATUS_NEEDS_REVERT
= 'status-needs-revert';
22 const STATUS_REVERTED
= 'status-reverted';
24 private $status = self
::STATUS_ALL
;
26 public function withIDs(array $ids) {
31 public function withPHIDs(array $phids) {
32 $this->phids
= $phids;
36 public function withBranchIDs(array $branch_ids) {
37 $this->branchIDs
= $branch_ids;
41 public function withStatus($status) {
42 $this->status
= $status;
46 public function withRequestedCommitPHIDs(array $requested_commit_phids) {
47 $this->requestedCommitPHIDs
= $requested_commit_phids;
51 public function withRequestorPHIDs(array $phids) {
52 $this->requestorPHIDs
= $phids;
56 public function withSeverities(array $severities) {
57 $this->severities
= $severities;
61 public function withRequestedObjectPHIDs(array $phids) {
62 $this->requestedObjectPHIDs
= $phids;
66 protected function loadPage() {
67 $table = new ReleephRequest();
68 $conn_r = $table->establishConnection('r');
72 'SELECT * FROM %T %Q %Q %Q',
73 $table->getTableName(),
74 $this->buildWhereClause($conn_r),
75 $this->buildOrderClause($conn_r),
76 $this->buildLimitClause($conn_r));
78 return $table->loadAllFromArray($data);
81 protected function willFilterPage(array $requests) {
82 // Load requested objects: you must be able to see an object to see
84 $object_phids = mpull($requests, 'getRequestedObjectPHID');
85 $objects = id(new PhabricatorObjectQuery())
86 ->setViewer($this->getViewer())
87 ->setParentQuery($this)
88 ->withPHIDs($object_phids)
91 foreach ($requests as $key => $request) {
92 $object_phid = $request->getRequestedObjectPHID();
93 $object = idx($objects, $object_phid);
95 unset($requests[$key]);
98 $request->attachRequestedObject($object);
101 if ($this->severities
) {
102 $severities = array_fuse($this->severities
);
103 foreach ($requests as $key => $request) {
105 // NOTE: Facebook uses a custom field here.
106 if (ReleephDefaultFieldSelector
::isFacebook()) {
107 $severity = $request->getDetail('severity');
109 $severity = $request->getDetail('releeph:severity');
112 if (empty($severities[$severity])) {
113 unset($requests[$key]);
118 $branch_ids = array_unique(mpull($requests, 'getBranchID'));
119 $branches = id(new ReleephBranchQuery())
120 ->withIDs($branch_ids)
121 ->setViewer($this->getViewer())
123 $branches = mpull($branches, null, 'getID');
124 foreach ($requests as $key => $request) {
125 $branch = idx($branches, $request->getBranchID());
127 unset($requests[$key]);
130 $request->attachBranch($branch);
133 // TODO: These should be serviced by the query, but are not currently
134 // denormalized anywhere. For now, filter them here instead. Note that
135 // we must perform this filtering *after* querying and attaching branches,
136 // because request status depends on the product.
138 $keep_status = array_fuse($this->getKeepStatusConstants());
140 foreach ($requests as $key => $request) {
141 if (empty($keep_status[$request->getStatus()])) {
142 unset($requests[$key]);
150 protected function buildWhereClause(AphrontDatabaseConnection
$conn) {
153 if ($this->ids
!== null) {
160 if ($this->phids
!== null) {
167 if ($this->branchIDs
!== null) {
174 if ($this->requestedCommitPHIDs
!== null) {
177 'requestCommitPHID IN (%Ls)',
178 $this->requestedCommitPHIDs
);
181 if ($this->requestorPHIDs
!== null) {
184 'requestUserPHID IN (%Ls)',
185 $this->requestorPHIDs
);
188 if ($this->requestedObjectPHIDs
!== null) {
191 'requestedObjectPHID IN (%Ls)',
192 $this->requestedObjectPHIDs
);
195 $where[] = $this->buildPagingClause($conn);
197 return $this->formatWhereClause($conn, $where);
200 private function getKeepStatusConstants() {
201 switch ($this->status
) {
202 case self
::STATUS_ALL
:
204 case self
::STATUS_OPEN
:
206 ReleephRequestStatus
::STATUS_REQUESTED
,
207 ReleephRequestStatus
::STATUS_NEEDS_PICK
,
208 ReleephRequestStatus
::STATUS_NEEDS_REVERT
,
210 case self
::STATUS_REQUESTED
:
212 ReleephRequestStatus
::STATUS_REQUESTED
,
214 case self
::STATUS_NEEDS_PULL
:
216 ReleephRequestStatus
::STATUS_NEEDS_PICK
,
218 case self
::STATUS_REJECTED
:
220 ReleephRequestStatus
::STATUS_REJECTED
,
222 case self
::STATUS_ABANDONED
:
224 ReleephRequestStatus
::STATUS_ABANDONED
,
226 case self
::STATUS_PULLED
:
228 ReleephRequestStatus
::STATUS_PICKED
,
230 case self
::STATUS_NEEDS_REVERT
:
232 ReleephRequestStatus
::STATUS_NEEDS_REVERT
,
234 case self
::STATUS_REVERTED
:
236 ReleephRequestStatus
::STATUS_REVERTED
,
239 throw new Exception(pht("Unknown status '%s'!", $this->status
));
243 public function getQueryApplicationClass() {
244 return 'PhabricatorReleephApplication';