Changeset View
Changeset View
Standalone View
Standalone View
src/applications/diffusion/query/DiffusionCommitQuery.php
<?php | <?php | ||||
final class DiffusionCommitQuery | final class DiffusionCommitQuery | ||||
extends PhabricatorCursorPagedPolicyAwareQuery { | extends PhabricatorCursorPagedPolicyAwareQuery { | ||||
private $ids; | private $ids; | ||||
private $phids; | private $phids; | ||||
private $authorPHIDs; | private $authorPHIDs; | ||||
private $defaultRepository; | private $defaultRepository; | ||||
private $identifiers; | private $identifiers; | ||||
private $repositoryIDs; | private $repositoryIDs; | ||||
private $repositoryPHIDs; | private $repositoryPHIDs; | ||||
private $identifierMap; | private $identifierMap; | ||||
private $responsiblePHIDs; | |||||
private $statuses; | |||||
private $needAuditRequests; | private $needAuditRequests; | ||||
private $auditIDs; | private $auditIDs; | ||||
private $auditorPHIDs; | private $auditorPHIDs; | ||||
private $needsAuditByPHIDs; | |||||
private $auditStatus; | |||||
private $epochMin; | private $epochMin; | ||||
private $epochMax; | private $epochMax; | ||||
private $importing; | private $importing; | ||||
const AUDIT_STATUS_ANY = 'audit-status-any'; | |||||
const AUDIT_STATUS_OPEN = 'audit-status-open'; | |||||
const AUDIT_STATUS_CONCERN = 'audit-status-concern'; | |||||
const AUDIT_STATUS_ACCEPTED = 'audit-status-accepted'; | |||||
const AUDIT_STATUS_PARTIAL = 'audit-status-partial'; | |||||
private $needCommitData; | private $needCommitData; | ||||
public function withIDs(array $ids) { | public function withIDs(array $ids) { | ||||
$this->ids = $ids; | $this->ids = $ids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withPHIDs(array $phids) { | public function withPHIDs(array $phids) { | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | public function withAuditIDs(array $ids) { | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withAuditorPHIDs(array $auditor_phids) { | public function withAuditorPHIDs(array $auditor_phids) { | ||||
$this->auditorPHIDs = $auditor_phids; | $this->auditorPHIDs = $auditor_phids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withNeedsAuditByPHIDs(array $needs_phids) { | public function withResponsiblePHIDs(array $responsible_phids) { | ||||
$this->needsAuditByPHIDs = $needs_phids; | $this->responsiblePHIDs = $responsible_phids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withAuditStatus($status) { | public function withStatuses(array $statuses) { | ||||
$this->auditStatus = $status; | $this->statuses = $statuses; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withAuditStatus($status) { | |||||
// TODO: Replace callers with `withStatuses()`. | |||||
return $this->withStatuses( | |||||
array( | |||||
$status, | |||||
)); | |||||
} | |||||
public function withEpochRange($min, $max) { | public function withEpochRange($min, $max) { | ||||
$this->epochMin = $min; | $this->epochMin = $min; | ||||
$this->epochMax = $max; | $this->epochMax = $max; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withImporting($importing) { | public function withImporting($importing) { | ||||
$this->importing = $importing; | $this->importing = $importing; | ||||
▲ Show 20 Lines • Show All 106 Lines • ▼ Show 20 Lines | if ($this->needCommitData) { | ||||
$commit_data = idx($data, $commit->getID()); | $commit_data = idx($data, $commit->getID()); | ||||
if (!$commit_data) { | if (!$commit_data) { | ||||
$commit_data = new PhabricatorRepositoryCommitData(); | $commit_data = new PhabricatorRepositoryCommitData(); | ||||
} | } | ||||
$commit->attachCommitData($commit_data); | $commit->attachCommitData($commit_data); | ||||
} | } | ||||
} | } | ||||
// TODO: This should just be `needAuditRequests`, not `shouldJoinAudits()`, | if ($this->needAuditRequests) { | ||||
// but leave that for a future diff. | |||||
if ($this->needAuditRequests || $this->shouldJoinAudits()) { | |||||
$requests = id(new PhabricatorRepositoryAuditRequest())->loadAllWhere( | $requests = id(new PhabricatorRepositoryAuditRequest())->loadAllWhere( | ||||
'commitPHID IN (%Ls)', | 'commitPHID IN (%Ls)', | ||||
mpull($commits, 'getPHID')); | mpull($commits, 'getPHID')); | ||||
$requests = mgroup($requests, 'getCommitPHID'); | $requests = mgroup($requests, 'getCommitPHID'); | ||||
foreach ($commits as $commit) { | foreach ($commits as $commit) { | ||||
$audit_requests = idx($requests, $commit->getPHID(), array()); | $audit_requests = idx($requests, $commit->getPHID(), array()); | ||||
$commit->attachAudits($audit_requests); | $commit->attachAudits($audit_requests); | ||||
▲ Show 20 Lines • Show All 188 Lines • ▼ Show 20 Lines | if ($this->identifiers !== null) { | ||||
} | } | ||||
$where[] = '('.implode(' OR ', $sql).')'; | $where[] = '('.implode(' OR ', $sql).')'; | ||||
} | } | ||||
if ($this->auditIDs !== null) { | if ($this->auditIDs !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn, | $conn, | ||||
'audit.id IN (%Ld)', | 'auditor.id IN (%Ld)', | ||||
$this->auditIDs); | $this->auditIDs); | ||||
} | } | ||||
if ($this->auditorPHIDs !== null) { | if ($this->auditorPHIDs !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn, | $conn, | ||||
'audit.auditorPHID IN (%Ls)', | 'auditor.auditorPHID IN (%Ls)', | ||||
$this->auditorPHIDs); | $this->auditorPHIDs); | ||||
} | } | ||||
if ($this->needsAuditByPHIDs !== null) { | if ($this->responsiblePHIDs !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn, | $conn, | ||||
'needs.auditorPHID IN (%Ls)', | '(audit.auditorPHID IN (%Ls) OR commit.authorPHID IN (%Ls))', | ||||
$this->needsAuditByPHIDs); | $this->responsiblePHIDs, | ||||
$this->responsiblePHIDs); | |||||
} | } | ||||
$status = $this->auditStatus; | if ($this->statuses !== null) { | ||||
if ($status !== null) { | |||||
switch ($status) { | |||||
case self::AUDIT_STATUS_PARTIAL: | |||||
$where[] = qsprintf( | |||||
$conn, | |||||
'commit.auditStatus = %d', | |||||
PhabricatorAuditCommitStatusConstants::PARTIALLY_AUDITED); | |||||
break; | |||||
case self::AUDIT_STATUS_ACCEPTED: | |||||
$where[] = qsprintf( | |||||
$conn, | |||||
'commit.auditStatus = %d', | |||||
PhabricatorAuditCommitStatusConstants::FULLY_AUDITED); | |||||
break; | |||||
case self::AUDIT_STATUS_CONCERN: | |||||
$where[] = qsprintf( | |||||
$conn, | |||||
'status.auditStatus = %s', | |||||
PhabricatorAuditStatusConstants::CONCERNED); | |||||
break; | |||||
case self::AUDIT_STATUS_OPEN: | |||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn, | $conn, | ||||
'status.auditStatus in (%Ls)', | 'commit.auditStatus IN (%Ls)', | ||||
PhabricatorAuditStatusConstants::getOpenStatusConstants()); | $this->statuses); | ||||
break; | |||||
case self::AUDIT_STATUS_ANY: | |||||
break; | |||||
default: | |||||
$valid = array( | |||||
self::AUDIT_STATUS_ANY, | |||||
self::AUDIT_STATUS_OPEN, | |||||
self::AUDIT_STATUS_CONCERN, | |||||
self::AUDIT_STATUS_ACCEPTED, | |||||
self::AUDIT_STATUS_PARTIAL, | |||||
); | |||||
throw new Exception( | |||||
pht( | |||||
"Unknown audit status '%s'! Valid statuses are: %s.", | |||||
$status, | |||||
implode(', ', $valid))); | |||||
} | |||||
} | } | ||||
return $where; | return $where; | ||||
} | } | ||||
protected function didFilterResults(array $filtered) { | protected function didFilterResults(array $filtered) { | ||||
if ($this->identifierMap) { | if ($this->identifierMap) { | ||||
foreach ($this->identifierMap as $name => $commit) { | foreach ($this->identifierMap as $name => $commit) { | ||||
if (isset($filtered[$commit->getPHID()])) { | if (isset($filtered[$commit->getPHID()])) { | ||||
unset($this->identifierMap[$name]); | unset($this->identifierMap[$name]); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
private function shouldJoinStatus() { | private function shouldJoinAuditor() { | ||||
return $this->auditStatus; | return ($this->auditIDs || $this->auditorPHIDs); | ||||
} | } | ||||
private function shouldJoinAudits() { | private function shouldJoinAudit() { | ||||
return $this->auditIDs || $this->auditorPHIDs; | return (bool)$this->responsiblePHIDs; | ||||
} | |||||
private function shouldJoinNeeds() { | |||||
return $this->needsAuditByPHIDs; | |||||
} | } | ||||
protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) { | protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) { | ||||
$join = parent::buildJoinClauseParts($conn); | $join = parent::buildJoinClauseParts($conn); | ||||
$audit_request = new PhabricatorRepositoryAuditRequest(); | $audit_request = new PhabricatorRepositoryAuditRequest(); | ||||
if ($this->shouldJoinStatus()) { | if ($this->shouldJoinAuditor()) { | ||||
$join[] = qsprintf( | $join[] = qsprintf( | ||||
$conn, | $conn, | ||||
'LEFT JOIN %T status ON commit.phid = status.commitPHID', | 'JOIN %T auditor ON commit.phid = auditor.commitPHID', | ||||
$audit_request->getTableName()); | $audit_request->getTableName()); | ||||
} | } | ||||
if ($this->shouldJoinAudits()) { | if ($this->shouldJoinAudit()) { | ||||
$join[] = qsprintf( | $join[] = qsprintf( | ||||
$conn, | $conn, | ||||
'JOIN %T audit ON commit.phid = audit.commitPHID', | 'LEFT JOIN %T audit ON commit.phid = audit.commitPHID', | ||||
$audit_request->getTableName()); | $audit_request->getTableName()); | ||||
} | } | ||||
if ($this->shouldJoinNeeds()) { | |||||
$join[] = qsprintf( | |||||
$conn, | |||||
'JOIN %T needs ON commit.phid = needs.commitPHID | |||||
AND needs.auditStatus IN (%Ls)', | |||||
$audit_request->getTableName(), | |||||
array( | |||||
PhabricatorAuditStatusConstants::AUDIT_REQUESTED, | |||||
PhabricatorAuditStatusConstants::AUDIT_REQUIRED, | |||||
)); | |||||
} | |||||
return $join; | return $join; | ||||
} | } | ||||
protected function shouldGroupQueryResultRows() { | protected function shouldGroupQueryResultRows() { | ||||
if ($this->shouldJoinStatus()) { | if ($this->shouldJoinAuditor()) { | ||||
return true; | |||||
} | |||||
if ($this->shouldJoinAudits()) { | |||||
return true; | return true; | ||||
} | } | ||||
if ($this->shouldJoinNeeds()) { | if ($this->shouldJoinAudit()) { | ||||
return true; | return true; | ||||
} | } | ||||
return parent::shouldGroupQueryResultRows(); | return parent::shouldGroupQueryResultRows(); | ||||
} | } | ||||
public function getQueryApplicationClass() { | public function getQueryApplicationClass() { | ||||
return 'PhabricatorDiffusionApplication'; | return 'PhabricatorDiffusionApplication'; | ||||
▲ Show 20 Lines • Show All 47 Lines • Show Last 20 Lines |