@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
1<?php
2
3final class HarbormasterBuildStatus extends Phobject {
4
5 const STATUS_INACTIVE = 'inactive';
6 const STATUS_PENDING = 'pending';
7 const STATUS_BUILDING = 'building';
8 const STATUS_PASSED = 'passed';
9 const STATUS_FAILED = 'failed';
10 const STATUS_ABORTED = 'aborted';
11 const STATUS_ERROR = 'error';
12 const STATUS_PAUSED = 'paused';
13 const STATUS_DEADLOCKED = 'deadlocked';
14
15 const PENDING_PAUSING = 'x-pausing';
16 const PENDING_RESUMING = 'x-resuming';
17 const PENDING_RESTARTING = 'x-restarting';
18 const PENDING_ABORTING = 'x-aborting';
19
20 private $key;
21 private $properties;
22
23 public function __construct($key, array $properties) {
24 $this->key = $key;
25 $this->properties = $properties;
26 }
27
28 public static function newBuildStatusObject($status) {
29 $spec = self::getBuildStatusSpec($status);
30 return new self($status, $spec);
31 }
32
33 private function getProperty($key) {
34 if (!array_key_exists($key, $this->properties)) {
35 throw new Exception(
36 pht(
37 'Attempting to access unknown build status property ("%s").',
38 $key));
39 }
40
41 return $this->properties[$key];
42 }
43
44 public function isBuilding() {
45 return $this->getProperty('isBuilding');
46 }
47
48 public function isPaused() {
49 return ($this->key === self::STATUS_PAUSED);
50 }
51
52 public function isComplete() {
53 return $this->getProperty('isComplete');
54 }
55
56 public function isPassed() {
57 return ($this->key === self::STATUS_PASSED);
58 }
59
60 public function isFailed() {
61 return ($this->key === self::STATUS_FAILED);
62 }
63
64 public function isAborting() {
65 return ($this->key === self::PENDING_ABORTING);
66 }
67
68 public function isRestarting() {
69 return ($this->key === self::PENDING_RESTARTING);
70 }
71
72 public function isResuming() {
73 return ($this->key === self::PENDING_RESUMING);
74 }
75
76 public function isPausing() {
77 return ($this->key === self::PENDING_PAUSING);
78 }
79
80 public function isPending() {
81 return ($this->key === self::STATUS_PENDING);
82 }
83
84 public function getIconIcon() {
85 return $this->getProperty('icon');
86 }
87
88 public function getIconColor() {
89 return $this->getProperty('color');
90 }
91
92 public function getName() {
93 return $this->getProperty('name');
94 }
95
96 /**
97 * Get a human readable name for a build status constant.
98 *
99 * @param string $status Build status constant.
100 * @return string Human-readable name.
101 */
102 public static function getBuildStatusName($status) {
103 $spec = self::getBuildStatusSpec($status);
104 return $spec['name'];
105 }
106
107 public static function getBuildStatusMap() {
108 $specs = self::getBuildStatusSpecMap();
109 return ipull($specs, 'name');
110 }
111
112 public static function getBuildStatusIcon($status) {
113 $spec = self::getBuildStatusSpec($status);
114 return $spec['icon'];
115 }
116
117 public static function getBuildStatusColor($status) {
118 $spec = self::getBuildStatusSpec($status);
119 return $spec['color'];
120 }
121
122 public static function getBuildStatusANSIColor($status) {
123 $spec = self::getBuildStatusSpec($status);
124 return $spec['color.ansi'];
125 }
126
127 public static function getWaitingStatusConstants() {
128 return array(
129 self::STATUS_INACTIVE,
130 self::STATUS_PENDING,
131 );
132 }
133
134 public static function getActiveStatusConstants() {
135 return array(
136 self::STATUS_BUILDING,
137 self::STATUS_PAUSED,
138 );
139 }
140
141 public static function getIncompleteStatusConstants() {
142 $map = self::getBuildStatusSpecMap();
143
144 $constants = array();
145 foreach ($map as $constant => $spec) {
146 if (!$spec['isComplete']) {
147 $constants[] = $constant;
148 }
149 }
150
151 return $constants;
152 }
153
154 public static function getCompletedStatusConstants() {
155 return array(
156 self::STATUS_PASSED,
157 self::STATUS_FAILED,
158 self::STATUS_ABORTED,
159 self::STATUS_ERROR,
160 self::STATUS_DEADLOCKED,
161 );
162 }
163
164 private static function getBuildStatusSpecMap() {
165 return array(
166 self::STATUS_INACTIVE => array(
167 'name' => pht('Inactive'),
168 'icon' => 'fa-circle-o',
169 'color' => 'dark',
170 'color.ansi' => 'yellow',
171 'isBuilding' => false,
172 'isComplete' => false,
173 ),
174 self::STATUS_PENDING => array(
175 'name' => pht('Pending'),
176 'icon' => 'fa-circle-o',
177 'color' => 'blue',
178 'color.ansi' => 'yellow',
179 'isBuilding' => true,
180 'isComplete' => false,
181 ),
182 self::STATUS_BUILDING => array(
183 'name' => pht('Building'),
184 'icon' => 'fa-chevron-circle-right',
185 'color' => 'blue',
186 'color.ansi' => 'yellow',
187 'isBuilding' => true,
188 'isComplete' => false,
189 ),
190 self::STATUS_PASSED => array(
191 'name' => pht('Passed'),
192 'icon' => 'fa-check-circle',
193 'color' => 'green',
194 'color.ansi' => 'green',
195 'isBuilding' => false,
196 'isComplete' => true,
197 ),
198 self::STATUS_FAILED => array(
199 'name' => pht('Failed'),
200 'icon' => 'fa-times-circle',
201 'color' => 'red',
202 'color.ansi' => 'red',
203 'isBuilding' => false,
204 'isComplete' => true,
205 ),
206 self::STATUS_ABORTED => array(
207 'name' => pht('Aborted'),
208 'icon' => 'fa-minus-circle',
209 'color' => 'red',
210 'color.ansi' => 'red',
211 'isBuilding' => false,
212 'isComplete' => true,
213 ),
214 self::STATUS_ERROR => array(
215 'name' => pht('Unexpected Error'),
216 'icon' => 'fa-minus-circle',
217 'color' => 'red',
218 'color.ansi' => 'red',
219 'isBuilding' => false,
220 'isComplete' => true,
221 ),
222 self::STATUS_PAUSED => array(
223 'name' => pht('Paused'),
224 'icon' => 'fa-pause',
225 'color' => 'yellow',
226 'color.ansi' => 'yellow',
227 'isBuilding' => false,
228 'isComplete' => false,
229 ),
230 self::STATUS_DEADLOCKED => array(
231 'name' => pht('Deadlocked'),
232 'icon' => 'fa-exclamation-circle',
233 'color' => 'red',
234 'color.ansi' => 'red',
235 'isBuilding' => false,
236 'isComplete' => true,
237 ),
238 self::PENDING_PAUSING => array(
239 'name' => pht('Pausing'),
240 'icon' => 'fa-exclamation-triangle',
241 'color' => 'red',
242 'color.ansi' => 'red',
243 'isBuilding' => false,
244 'isComplete' => false,
245 ),
246 self::PENDING_RESUMING => array(
247 'name' => pht('Resuming'),
248 'icon' => 'fa-exclamation-triangle',
249 'color' => 'red',
250 'color.ansi' => 'red',
251 'isBuilding' => false,
252 'isComplete' => false,
253 ),
254 self::PENDING_RESTARTING => array(
255 'name' => pht('Restarting'),
256 'icon' => 'fa-exclamation-triangle',
257 'color' => 'red',
258 'color.ansi' => 'red',
259 'isBuilding' => false,
260 'isComplete' => false,
261 ),
262 self::PENDING_ABORTING => array(
263 'name' => pht('Aborting'),
264 'icon' => 'fa-exclamation-triangle',
265 'color' => 'red',
266 'color.ansi' => 'red',
267 'isBuilding' => false,
268 'isComplete' => false,
269 ),
270 );
271 }
272
273 private static function getBuildStatusSpec($status) {
274 $map = self::getBuildStatusSpecMap();
275 if (isset($map[$status])) {
276 return $map[$status];
277 }
278
279 return array(
280 'name' => pht('Unknown ("%s")', $status),
281 'icon' => 'fa-question-circle',
282 'color' => 'bluegrey',
283 'color.ansi' => 'magenta',
284 'isBuilding' => false,
285 'isComplete' => false,
286 );
287 }
288
289}