@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at recaptime-dev/main 198 lines 4.5 kB view raw
1<?php 2 3/** 4 * @extends PhabricatorCursorPagedPolicyAwareQuery<PhabricatorRepositoryPushLog> 5 */ 6final class PhabricatorRepositoryPushLogQuery 7 extends PhabricatorCursorPagedPolicyAwareQuery { 8 9 private $ids; 10 private $phids; 11 private $repositoryPHIDs; 12 private $pusherPHIDs; 13 private $refTypes; 14 private $newRefs; 15 private $pushEventPHIDs; 16 private $epochMin; 17 private $epochMax; 18 private $blockingHeraldRulePHIDs; 19 20 public function withIDs(array $ids) { 21 $this->ids = $ids; 22 return $this; 23 } 24 25 public function withPHIDs(array $phids) { 26 $this->phids = $phids; 27 return $this; 28 } 29 30 public function withRepositoryPHIDs(array $repository_phids) { 31 $this->repositoryPHIDs = $repository_phids; 32 return $this; 33 } 34 35 public function withPusherPHIDs(array $pusher_phids) { 36 $this->pusherPHIDs = $pusher_phids; 37 return $this; 38 } 39 40 public function withRefTypes(array $ref_types) { 41 $this->refTypes = $ref_types; 42 return $this; 43 } 44 45 public function withNewRefs(array $new_refs) { 46 $this->newRefs = $new_refs; 47 return $this; 48 } 49 50 public function withPushEventPHIDs(array $phids) { 51 $this->pushEventPHIDs = $phids; 52 return $this; 53 } 54 55 public function withEpochBetween($min, $max) { 56 $this->epochMin = $min; 57 $this->epochMax = $max; 58 return $this; 59 } 60 61 public function withBlockingHeraldRulePHIDs(array $phids) { 62 $this->blockingHeraldRulePHIDs = $phids; 63 return $this; 64 } 65 66 public function newResultObject() { 67 return new PhabricatorRepositoryPushLog(); 68 } 69 70 protected function willFilterPage(array $logs) { 71 $event_phids = mpull($logs, 'getPushEventPHID'); 72 $events = id(new PhabricatorObjectQuery()) 73 ->setParentQuery($this) 74 ->setViewer($this->getViewer()) 75 ->withPHIDs($event_phids) 76 ->execute(); 77 $events = mpull($events, null, 'getPHID'); 78 79 foreach ($logs as $key => $log) { 80 $event = idx($events, $log->getPushEventPHID()); 81 if (!$event) { 82 unset($logs[$key]); 83 continue; 84 } 85 $log->attachPushEvent($event); 86 } 87 88 return $logs; 89 } 90 91 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { 92 $where = parent::buildWhereClauseParts($conn); 93 94 if ($this->ids !== null) { 95 $where[] = qsprintf( 96 $conn, 97 'log.id IN (%Ld)', 98 $this->ids); 99 } 100 101 if ($this->phids !== null) { 102 $where[] = qsprintf( 103 $conn, 104 'log.phid IN (%Ls)', 105 $this->phids); 106 } 107 108 if ($this->repositoryPHIDs !== null) { 109 $where[] = qsprintf( 110 $conn, 111 'log.repositoryPHID IN (%Ls)', 112 $this->repositoryPHIDs); 113 } 114 115 if ($this->pusherPHIDs !== null) { 116 $where[] = qsprintf( 117 $conn, 118 'log.pusherPHID in (%Ls)', 119 $this->pusherPHIDs); 120 } 121 122 if ($this->pushEventPHIDs !== null) { 123 $where[] = qsprintf( 124 $conn, 125 'log.pushEventPHID in (%Ls)', 126 $this->pushEventPHIDs); 127 } 128 129 if ($this->refTypes !== null) { 130 $where[] = qsprintf( 131 $conn, 132 'log.refType IN (%Ls)', 133 $this->refTypes); 134 } 135 136 if ($this->newRefs !== null) { 137 $where[] = qsprintf( 138 $conn, 139 'log.refNew IN (%Ls)', 140 $this->newRefs); 141 } 142 143 if ($this->epochMin !== null) { 144 $where[] = qsprintf( 145 $conn, 146 'log.epoch >= %d', 147 $this->epochMin); 148 } 149 150 if ($this->epochMax !== null) { 151 $where[] = qsprintf( 152 $conn, 153 'log.epoch <= %d', 154 $this->epochMax); 155 } 156 157 if ($this->blockingHeraldRulePHIDs !== null) { 158 $where[] = qsprintf( 159 $conn, 160 '(event.rejectCode = %d AND event.rejectDetails IN (%Ls))', 161 PhabricatorRepositoryPushLog::REJECT_HERALD, 162 $this->blockingHeraldRulePHIDs); 163 } 164 165 return $where; 166 } 167 168 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) { 169 $joins = parent::buildJoinClauseParts($conn); 170 171 if ($this->shouldJoinPushEventTable()) { 172 $joins[] = qsprintf( 173 $conn, 174 'JOIN %T event ON event.phid = log.pushEventPHID', 175 id(new PhabricatorRepositoryPushEvent())->getTableName()); 176 } 177 178 return $joins; 179 } 180 181 private function shouldJoinPushEventTable() { 182 if ($this->blockingHeraldRulePHIDs !== null) { 183 return true; 184 } 185 186 return false; 187 } 188 189 public function getQueryApplicationClass() { 190 return PhabricatorDiffusionApplication::class; 191 } 192 193 protected function getPrimaryTableAlias() { 194 return 'log'; 195 } 196 197 198}