Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / repository / storage / PhabricatorRepositoryPullEvent.php
blob85ea27062b76b57ed69b814967a98c362dfc6b00
1 <?php
3 final class PhabricatorRepositoryPullEvent
4 extends PhabricatorRepositoryDAO
5 implements PhabricatorPolicyInterface {
7 protected $repositoryPHID;
8 protected $epoch;
9 protected $pullerPHID;
10 protected $remoteAddress;
11 protected $remoteProtocol;
12 protected $resultType;
13 protected $resultCode;
14 protected $properties;
16 private $repository = self::ATTACHABLE;
18 const RESULT_PULL = 'pull';
19 const RESULT_ERROR = 'error';
20 const RESULT_EXCEPTION = 'exception';
22 const PROTOCOL_HTTP = 'http';
23 const PROTOCOL_HTTPS = 'https';
24 const PROTOCOL_SSH = 'ssh';
26 public static function initializeNewEvent(PhabricatorUser $viewer) {
27 return id(new PhabricatorRepositoryPushEvent())
28 ->setPusherPHID($viewer->getPHID());
31 protected function getConfiguration() {
32 return array(
33 self::CONFIG_AUX_PHID => true,
34 self::CONFIG_TIMESTAMPS => false,
35 self::CONFIG_SERIALIZATION => array(
36 'properties' => self::SERIALIZATION_JSON,
38 self::CONFIG_COLUMN_SCHEMA => array(
39 'repositoryPHID' => 'phid?',
40 'pullerPHID' => 'phid?',
41 'remoteAddress' => 'ipaddress?',
42 'remoteProtocol' => 'text32?',
43 'resultType' => 'text32',
44 'resultCode' => 'uint32',
46 self::CONFIG_KEY_SCHEMA => array(
47 'key_repository' => array(
48 'columns' => array('repositoryPHID'),
50 'key_epoch' => array(
51 'columns' => array('epoch'),
54 ) + parent::getConfiguration();
57 public function generatePHID() {
58 return PhabricatorPHID::generateNewPHID(
59 PhabricatorRepositoryPullEventPHIDType::TYPECONST);
62 public function attachRepository(PhabricatorRepository $repository = null) {
63 $this->repository = $repository;
64 return $this;
67 public function getRepository() {
68 return $this->assertAttached($this->repository);
71 public function getRemoteProtocolDisplayName() {
72 $map = array(
73 self::PROTOCOL_SSH => pht('SSH'),
74 self::PROTOCOL_HTTP => pht('HTTP'),
75 self::PROTOCOL_HTTPS => pht('HTTPS'),
78 $protocol = $this->getRemoteProtocol();
80 return idx($map, $protocol, $protocol);
83 public function newResultIcon() {
84 $icon = new PHUIIconView();
85 $type = $this->getResultType();
86 $code = $this->getResultCode();
88 $protocol = $this->getRemoteProtocol();
90 $is_any_http =
91 ($protocol === self::PROTOCOL_HTTP) ||
92 ($protocol === self::PROTOCOL_HTTPS);
94 // If this was an HTTP request and we responded with a 401, that means
95 // the user didn't provide credentials. This is technically an error, but
96 // it's routine and just causes the client to prompt them. Show a more
97 // comforting icon and description in the UI.
98 if ($is_any_http) {
99 if ($code == 401) {
100 return $icon
101 ->setIcon('fa-key blue')
102 ->setTooltip(pht('Authentication Required'));
106 switch ($type) {
107 case self::RESULT_ERROR:
108 $icon
109 ->setIcon('fa-times red')
110 ->setTooltip(pht('Error'));
111 break;
112 case self::RESULT_EXCEPTION:
113 $icon
114 ->setIcon('fa-exclamation-triangle red')
115 ->setTooltip(pht('Exception'));
116 break;
117 case self::RESULT_PULL:
118 $icon
119 ->setIcon('fa-download green')
120 ->setTooltip(pht('Pull'));
121 break;
122 default:
123 $icon
124 ->setIcon('fa-question indigo')
125 ->setTooltip(pht('Unknown ("%s")', $type));
126 break;
129 return $icon;
133 /* -( PhabricatorPolicyInterface )----------------------------------------- */
136 public function getCapabilities() {
137 return array(
138 PhabricatorPolicyCapability::CAN_VIEW,
142 public function getPolicy($capability) {
143 if ($this->getRepository()) {
144 return $this->getRepository()->getPolicy($capability);
147 return PhabricatorPolicies::POLICY_ADMIN;
150 public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
151 if (!$this->getRepository()) {
152 return false;
155 return $this->getRepository()->hasAutomaticCapability($capability, $viewer);
158 public function describeAutomaticCapability($capability) {
159 return pht(
160 "A repository's pull events are visible to users who can see the ".
161 "repository.");