Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / harbormaster / artifact / HarbormasterURIArtifact.php
blob93f756403358c4dc31e15e13c439906e828e6ed4
1 <?php
3 final class HarbormasterURIArtifact extends HarbormasterArtifact {
5 const ARTIFACTCONST = 'uri';
7 public function getArtifactTypeName() {
8 return pht('URI');
11 public function getArtifactTypeSummary() {
12 return pht('Stores a URI.');
15 public function getArtifactTypeDescription() {
16 return pht(
17 "Stores a URI.\n\n".
18 "With `ui.external`, you can use this artifact type to add links to ".
19 "build results in an external build system.");
22 public function getArtifactParameterSpecification() {
23 return array(
24 'uri' => 'string',
25 'name' => 'optional string',
26 'ui.external' => 'optional bool',
30 public function readArtifactHTTPParameter($key, $value) {
31 // TODO: This is hacky and artifact parameters should be replaced more
32 // broadly, likely with EditFields. See T11887.
33 switch ($key) {
34 case 'ui.external':
35 return (bool)$value;
37 return $value;
40 public function getArtifactParameterDescriptions() {
41 return array(
42 'uri' => pht('The URI to store.'),
43 'name' => pht('Optional label for this URI.'),
44 'ui.external' => pht(
45 'If true, display this URI in the UI as an link to '.
46 'additional build details in an external build system.'),
50 public function getArtifactDataExample() {
51 return array(
52 'uri' => 'https://buildserver.mycompany.com/build/123/',
53 'name' => pht('View External Build Results'),
54 'ui.external' => true,
58 public function renderArtifactSummary(PhabricatorUser $viewer) {
59 return $this->renderLink();
62 public function isExternalLink() {
63 $artifact = $this->getBuildArtifact();
64 return (bool)$artifact->getProperty('ui.external', false);
67 public function renderLink() {
68 $artifact = $this->getBuildArtifact();
69 $uri = $artifact->getProperty('uri');
71 try {
72 $this->validateURI($uri);
73 } catch (Exception $ex) {
74 return pht('<Invalid URI>');
77 $name = $artifact->getProperty('name', $uri);
79 return phutil_tag(
80 'a',
81 array(
82 'href' => $uri,
83 'target' => '_blank',
84 'rel' => 'noreferrer',
86 $name);
89 public function willCreateArtifact(PhabricatorUser $actor) {
90 $artifact = $this->getBuildArtifact();
91 $uri = $artifact->getProperty('uri');
92 $this->validateURI($uri);
95 private function validateURI($raw_uri) {
96 $uri = new PhutilURI($raw_uri);
98 $protocol = $uri->getProtocol();
99 if (!strlen($protocol)) {
100 throw new Exception(
101 pht(
102 'Unable to identify the protocol for URI "%s". URIs must be '.
103 'fully qualified and have an identifiable protocol.',
104 $raw_uri));
107 $protocol_key = 'uri.allowed-protocols';
108 $protocols = PhabricatorEnv::getEnvConfig($protocol_key);
109 if (empty($protocols[$protocol])) {
110 throw new Exception(
111 pht(
112 'URI "%s" does not have an allowable protocol. Configure '.
113 'protocols in `%s`. Allowed protocols are: %s.',
114 $raw_uri,
115 $protocol_key,
116 implode(', ', array_keys($protocols))));