Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / releeph / editor / ReleephRequestTransactionalEditor.php
blobda488d9c72207ea282336e7701e83ad4bb802d76
1 <?php
3 final class ReleephRequestTransactionalEditor
4 extends PhabricatorApplicationTransactionEditor {
6 public function getEditorApplicationClass() {
7 return 'PhabricatorReleephApplication';
10 public function getEditorObjectsDescription() {
11 return pht('Releeph Requests');
14 public function getTransactionTypes() {
15 $types = parent::getTransactionTypes();
17 $types[] = PhabricatorTransactions::TYPE_COMMENT;
18 $types[] = ReleephRequestTransaction::TYPE_COMMIT;
19 $types[] = ReleephRequestTransaction::TYPE_DISCOVERY;
20 $types[] = ReleephRequestTransaction::TYPE_EDIT_FIELD;
21 $types[] = ReleephRequestTransaction::TYPE_MANUAL_IN_BRANCH;
22 $types[] = ReleephRequestTransaction::TYPE_PICK_STATUS;
23 $types[] = ReleephRequestTransaction::TYPE_REQUEST;
24 $types[] = ReleephRequestTransaction::TYPE_USER_INTENT;
26 return $types;
29 protected function getCustomTransactionOldValue(
30 PhabricatorLiskDAO $object,
31 PhabricatorApplicationTransaction $xaction) {
33 switch ($xaction->getTransactionType()) {
34 case ReleephRequestTransaction::TYPE_REQUEST:
35 return $object->getRequestCommitPHID();
37 case ReleephRequestTransaction::TYPE_EDIT_FIELD:
38 $field = newv($xaction->getMetadataValue('fieldClass'), array());
39 $value = $field->setReleephRequest($object)->getValue();
40 return $value;
42 case ReleephRequestTransaction::TYPE_USER_INTENT:
43 $user_phid = $xaction->getAuthorPHID();
44 return idx($object->getUserIntents(), $user_phid);
46 case ReleephRequestTransaction::TYPE_PICK_STATUS:
47 return (int)$object->getPickStatus();
48 break;
50 case ReleephRequestTransaction::TYPE_COMMIT:
51 return $object->getCommitIdentifier();
53 case ReleephRequestTransaction::TYPE_DISCOVERY:
54 return $object->getCommitPHID();
56 case ReleephRequestTransaction::TYPE_MANUAL_IN_BRANCH:
57 return $object->getInBranch();
61 protected function getCustomTransactionNewValue(
62 PhabricatorLiskDAO $object,
63 PhabricatorApplicationTransaction $xaction) {
65 switch ($xaction->getTransactionType()) {
66 case ReleephRequestTransaction::TYPE_REQUEST:
67 case ReleephRequestTransaction::TYPE_USER_INTENT:
68 case ReleephRequestTransaction::TYPE_EDIT_FIELD:
69 case ReleephRequestTransaction::TYPE_PICK_STATUS:
70 case ReleephRequestTransaction::TYPE_COMMIT:
71 case ReleephRequestTransaction::TYPE_DISCOVERY:
72 case ReleephRequestTransaction::TYPE_MANUAL_IN_BRANCH:
73 return $xaction->getNewValue();
77 protected function applyCustomInternalTransaction(
78 PhabricatorLiskDAO $object,
79 PhabricatorApplicationTransaction $xaction) {
81 $new = $xaction->getNewValue();
83 switch ($xaction->getTransactionType()) {
84 case ReleephRequestTransaction::TYPE_REQUEST:
85 $object->setRequestCommitPHID($new);
86 break;
88 case ReleephRequestTransaction::TYPE_USER_INTENT:
89 $user_phid = $xaction->getAuthorPHID();
90 $intents = $object->getUserIntents();
91 $intents[$user_phid] = $new;
92 $object->setUserIntents($intents);
93 break;
95 case ReleephRequestTransaction::TYPE_EDIT_FIELD:
96 $field = newv($xaction->getMetadataValue('fieldClass'), array());
97 $field
98 ->setReleephRequest($object)
99 ->setValue($new);
100 break;
102 case ReleephRequestTransaction::TYPE_PICK_STATUS:
103 $object->setPickStatus($new);
104 break;
106 case ReleephRequestTransaction::TYPE_COMMIT:
107 $this->setInBranchFromAction($object, $xaction);
108 $object->setCommitIdentifier($new);
109 break;
111 case ReleephRequestTransaction::TYPE_DISCOVERY:
112 $this->setInBranchFromAction($object, $xaction);
113 $object->setCommitPHID($new);
114 break;
116 case ReleephRequestTransaction::TYPE_MANUAL_IN_BRANCH:
117 $object->setInBranch((int)$new);
118 break;
122 protected function applyCustomExternalTransaction(
123 PhabricatorLiskDAO $object,
124 PhabricatorApplicationTransaction $xaction) {
126 return;
129 protected function filterTransactions(
130 PhabricatorLiskDAO $object,
131 array $xactions) {
133 // Remove TYPE_DISCOVERY xactions that are the result of a reparse.
134 $previously_discovered_commits = array();
135 $discovery_xactions = idx(
136 mgroup($xactions, 'getTransactionType'),
137 ReleephRequestTransaction::TYPE_DISCOVERY);
138 if ($discovery_xactions) {
139 $previous_xactions = id(new ReleephRequestTransactionQuery())
140 ->setViewer(PhabricatorUser::getOmnipotentUser())
141 ->withObjectPHIDs(array($object->getPHID()))
142 ->execute();
144 foreach ($previous_xactions as $xaction) {
145 if ($xaction->getTransactionType() ===
146 ReleephRequestTransaction::TYPE_DISCOVERY) {
148 $commit_phid = $xaction->getNewValue();
149 $previously_discovered_commits[$commit_phid] = true;
154 foreach ($xactions as $key => $xaction) {
155 if ($xaction->getTransactionType() ===
156 ReleephRequestTransaction::TYPE_DISCOVERY &&
157 idx($previously_discovered_commits, $xaction->getNewValue())) {
159 unset($xactions[$key]);
163 return parent::filterTransactions($object, $xactions);
166 protected function shouldSendMail(
167 PhabricatorLiskDAO $object,
168 array $xactions) {
170 // Avoid sending emails that only talk about commit discovery.
171 $types = array_unique(mpull($xactions, 'getTransactionType'));
172 if ($types === array(ReleephRequestTransaction::TYPE_DISCOVERY)) {
173 return false;
176 // Don't email people when we discover that something picks or reverts OK.
177 if ($types === array(ReleephRequestTransaction::TYPE_PICK_STATUS)) {
178 if (!mfilter($xactions, 'isBoringPickStatus', true /* negate */)) {
179 // If we effectively call "isInterestingPickStatus" and get nothing...
180 return false;
184 return true;
187 protected function buildReplyHandler(PhabricatorLiskDAO $object) {
188 return id(new ReleephRequestReplyHandler())
189 ->setActor($this->getActor())
190 ->setMailReceiver($object);
193 protected function getMailSubjectPrefix() {
194 return '[Releeph]';
197 protected function buildMailTemplate(PhabricatorLiskDAO $object) {
198 $id = $object->getID();
199 $title = $object->getSummaryForDisplay();
200 return id(new PhabricatorMetaMTAMail())
201 ->setSubject("RQ{$id}: {$title}");
204 protected function getMailTo(PhabricatorLiskDAO $object) {
205 $to_phids = array();
207 $product = $object->getBranch()->getProduct();
208 foreach ($product->getPushers() as $phid) {
209 $to_phids[] = $phid;
212 foreach ($object->getUserIntents() as $phid => $intent) {
213 $to_phids[] = $phid;
216 return $to_phids;
219 protected function getMailCC(PhabricatorLiskDAO $object) {
220 return array();
223 protected function buildMailBody(
224 PhabricatorLiskDAO $object,
225 array $xactions) {
227 $body = parent::buildMailBody($object, $xactions);
229 $rq = $object;
230 $releeph_branch = $rq->getBranch();
231 $releeph_project = $releeph_branch->getProduct();
234 * If any of the events we are emailing about were about a pick failure
235 * (and/or a revert failure?), include pick failure instructions.
237 $has_pick_failure = false;
238 foreach ($xactions as $xaction) {
239 if ($xaction->getTransactionType() ===
240 ReleephRequestTransaction::TYPE_PICK_STATUS &&
241 $xaction->getNewValue() === ReleephRequest::PICK_FAILED) {
243 $has_pick_failure = true;
244 break;
247 if ($has_pick_failure) {
248 $instructions = $releeph_project->getDetail('pick_failure_instructions');
249 if ($instructions) {
250 $body->addRemarkupSection(
251 pht('PICK FAILURE INSTRUCTIONS'),
252 $instructions);
256 $name = sprintf('RQ%s: %s', $rq->getID(), $rq->getSummaryForDisplay());
257 $body->addTextSection(
258 pht('RELEEPH REQUEST'),
259 $name."\n".
260 PhabricatorEnv::getProductionURI('/RQ'.$rq->getID()));
262 $project_and_branch = sprintf(
263 '%s - %s',
264 $releeph_project->getName(),
265 $releeph_branch->getDisplayNameWithDetail());
267 $body->addTextSection(
268 pht('RELEEPH BRANCH'),
269 $project_and_branch."\n".
270 PhabricatorEnv::getProductionURI($releeph_branch->getURI()));
272 return $body;
275 private function setInBranchFromAction(
276 ReleephRequest $rq,
277 ReleephRequestTransaction $xaction) {
279 $action = $xaction->getMetadataValue('action');
280 switch ($action) {
281 case 'pick':
282 $rq->setInBranch(1);
283 break;
285 case 'revert':
286 $rq->setInBranch(0);
287 break;
289 default:
290 $id = $rq->getID();
291 $type = $xaction->getTransactionType();
292 $new = $xaction->getNewValue();
293 phlog(
294 pht(
295 "Unknown discovery action '%s' for xaction of type %s ".
296 "with new value %s mentioning %s!",
297 $action,
298 $type,
299 $new,
300 'RQ'.$id));
301 break;
304 return $this;