@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 upstream/main 127 lines 3.3 kB view raw
1<?php 2 3abstract class DiffusionCommitActionTransaction 4 extends DiffusionCommitTransactionType { 5 6 final public function getCommitActionKey() { 7 return $this->getPhobjectClassConstant('ACTIONKEY', 32); 8 } 9 10 public function isActionAvailable($object, PhabricatorUser $viewer) { 11 if (!id(new PhabricatorAuditApplication())->isInstalled()) { 12 return false; 13 } 14 try { 15 $this->validateAction($object, $viewer); 16 return true; 17 } catch (Exception $ex) { 18 return false; 19 } 20 } 21 22 abstract protected function validateAction($object, PhabricatorUser $viewer); 23 abstract protected function getCommitActionLabel(); 24 25 public function getCommandKeyword() { 26 return null; 27 } 28 29 public function getCommandAliases() { 30 return array(); 31 } 32 33 public function getCommandSummary() { 34 return null; 35 } 36 37 protected function getCommitActionOrder() { 38 return 1000; 39 } 40 41 public function getCommitActionOrderVector() { 42 return id(new PhutilSortVector()) 43 ->addInt($this->getCommitActionOrder()); 44 } 45 46 protected function getCommitActionGroupKey() { 47 return DiffusionCommitEditEngine::ACTIONGROUP_COMMIT; 48 } 49 50 protected function getCommitActionDescription() { 51 return null; 52 } 53 54 public static function loadAllActions() { 55 return id(new PhutilClassMapQuery()) 56 ->setAncestorClass(self::class) 57 ->setUniqueMethod('getCommitActionKey') 58 ->execute(); 59 } 60 61 protected function isViewerCommitAuthor( 62 PhabricatorRepositoryCommit $commit, 63 PhabricatorUser $viewer) { 64 65 if (!$viewer->getPHID()) { 66 return false; 67 } 68 69 return ($viewer->getPHID() === $commit->getEffectiveAuthorPHID()); 70 } 71 72 public function newEditField( 73 PhabricatorRepositoryCommit $commit, 74 PhabricatorUser $viewer) { 75 76 // Actions in the "audit" group, like "Accept Commit", do not require 77 // that the actor be able to edit the commit. 78 $group_audit = DiffusionCommitEditEngine::ACTIONGROUP_AUDIT; 79 $is_audit = ($this->getCommitActionGroupKey() == $group_audit); 80 81 $field = id(new PhabricatorApplyEditField()) 82 ->setKey($this->getCommitActionKey()) 83 ->setTransactionType($this->getTransactionTypeConstant()) 84 ->setCanApplyWithoutEditCapability($is_audit) 85 ->setValue(true); 86 87 if ($this->isActionAvailable($commit, $viewer)) { 88 $label = $this->getCommitActionLabel(); 89 if ($label !== null) { 90 $field->setCommentActionLabel($label); 91 92 $description = $this->getCommitActionDescription(); 93 $field->setActionDescription($description); 94 95 $group_key = $this->getCommitActionGroupKey(); 96 $field->setCommentActionGroupKey($group_key); 97 98 $field->setActionConflictKey('commit.action'); 99 } 100 } 101 102 return $field; 103 } 104 105 public function validateTransactions($object, array $xactions) { 106 $errors = array(); 107 $actor = $this->getActor(); 108 109 $action_exception = null; 110 try { 111 $this->validateAction($object, $actor); 112 } catch (Exception $ex) { 113 $action_exception = $ex; 114 } 115 116 foreach ($xactions as $xaction) { 117 if ($action_exception) { 118 $errors[] = $this->newInvalidError( 119 $action_exception->getMessage(), 120 $xaction); 121 } 122 } 123 124 return $errors; 125 } 126 127}