4 * @task validate Configuration Validation
6 final class ManiphestTaskStatus
extends ManiphestConstants
{
8 const STATUS_OPEN
= 'open';
9 const STATUS_CLOSED_RESOLVED
= 'resolved';
10 const STATUS_CLOSED_WONTFIX
= 'wontfix';
11 const STATUS_CLOSED_INVALID
= 'invalid';
12 const STATUS_CLOSED_DUPLICATE
= 'duplicate';
13 const STATUS_CLOSED_SPITE
= 'spite';
15 const SPECIAL_DEFAULT
= 'default';
16 const SPECIAL_CLOSED
= 'closed';
17 const SPECIAL_DUPLICATE
= 'duplicate';
19 const LOCKED_COMMENTS
= 'comments';
20 const LOCKED_EDITS
= 'edits';
22 private static function getStatusConfig() {
23 return PhabricatorEnv
::getEnvConfig('maniphest.statuses');
26 private static function getEnabledStatusMap() {
27 $spec = self
::getStatusConfig();
29 $is_serious = PhabricatorEnv
::getEnvConfig('phabricator.serious-business');
30 foreach ($spec as $const => $status) {
31 if ($is_serious && !empty($status['silly'])) {
40 public static function getTaskStatusMap() {
41 return ipull(self
::getEnabledStatusMap(), 'name');
46 * Get the statuses and their command keywords.
48 * @return map Statuses to lists of command keywords.
50 public static function getTaskStatusKeywordsMap() {
51 $map = self
::getEnabledStatusMap();
52 foreach ($map as $key => $spec) {
53 $words = idx($spec, 'keywords', array());
54 if (!is_array($words)) {
55 $words = array($words);
58 // For statuses, we include the status name because it's usually
59 // at least somewhat meaningful.
62 foreach ($words as $word_key => $word) {
63 $words[$word_key] = phutil_utf8_strtolower($word);
66 $words = array_unique($words);
75 public static function getTaskStatusName($status) {
76 return self
::getStatusAttribute($status, 'name', pht('Unknown Status'));
79 public static function getTaskStatusFullName($status) {
80 $name = self
::getStatusAttribute($status, 'name.full');
85 return self
::getStatusAttribute($status, 'name', pht('Unknown Status'));
88 public static function renderFullDescription($status, $priority) {
89 if (self
::isOpenStatus($status)) {
90 $name = pht('%s, %s', self
::getTaskStatusFullName($status), $priority);
92 $icon = 'fa-square-o';
94 $name = self
::getTaskStatusFullName($status);
96 $icon = 'fa-check-square-o';
99 $tag = id(new PHUITagView())
102 ->setType(PHUITagView
::TYPE_SHADE
)
108 private static function getSpecialStatus($special) {
109 foreach (self
::getStatusConfig() as $const => $status) {
110 if (idx($status, 'special') == $special) {
117 public static function getDefaultStatus() {
118 return self
::getSpecialStatus(self
::SPECIAL_DEFAULT
);
121 public static function getDefaultClosedStatus() {
122 return self
::getSpecialStatus(self
::SPECIAL_CLOSED
);
125 public static function getDuplicateStatus() {
126 return self
::getSpecialStatus(self
::SPECIAL_DUPLICATE
);
129 public static function getOpenStatusConstants() {
131 foreach (self
::getEnabledStatusMap() as $const => $status) {
132 if (empty($status['closed'])) {
139 public static function getClosedStatusConstants() {
140 $all = array_keys(self
::getTaskStatusMap());
141 $open = self
::getOpenStatusConstants();
142 return array_diff($all, $open);
145 public static function isOpenStatus($status) {
146 foreach (self
::getOpenStatusConstants() as $constant) {
147 if ($status == $constant) {
154 public static function isClaimStatus($status) {
155 return self
::getStatusAttribute($status, 'claim', true);
158 public static function isClosedStatus($status) {
159 return !self
::isOpenStatus($status);
162 public static function areCommentsLockedInStatus($status) {
163 return (bool)self
::getStatusAttribute($status, 'locked', false);
166 public static function areEditsLockedInStatus($status) {
167 $locked = self
::getStatusAttribute($status, 'locked');
168 return ($locked === self
::LOCKED_EDITS
);
171 public static function isMFAStatus($status) {
172 return self
::getStatusAttribute($status, 'mfa', false);
175 public static function getStatusActionName($status) {
176 return self
::getStatusAttribute($status, 'name.action');
179 public static function getStatusColor($status) {
180 return self
::getStatusAttribute($status, 'transaction.color');
183 public static function isDisabledStatus($status) {
184 return self
::getStatusAttribute($status, 'disabled');
187 public static function getStatusIcon($status) {
188 $icon = self
::getStatusAttribute($status, 'transaction.icon');
193 if (self
::isOpenStatus($status)) {
194 return 'fa-exclamation-circle';
196 return 'fa-check-square-o';
200 public static function getStatusPrefixMap() {
202 foreach (self
::getEnabledStatusMap() as $const => $status) {
203 foreach (idx($status, 'prefixes', array()) as $prefix) {
204 $map[$prefix] = $const;
211 'references' => null,
218 public static function getStatusSuffixMap() {
220 foreach (self
::getEnabledStatusMap() as $const => $status) {
221 foreach (idx($status, 'suffixes', array()) as $prefix) {
222 $map[$prefix] = $const;
228 private static function getStatusAttribute($status, $key, $default = null) {
229 $config = self
::getStatusConfig();
231 $spec = idx($config, $status);
233 return idx($spec, $key, $default);
240 /* -( Configuration Validation )------------------------------------------- */
246 public static function isValidStatusConstant($constant) {
247 if (!strlen($constant) ||
strlen($constant) > 64) {
251 // Alphanumeric, but not exclusively numeric
252 if (!preg_match('/^(?![0-9]*$)[a-zA-Z0-9]+$/', $constant)) {
261 public static function validateConfiguration(array $config) {
262 foreach ($config as $key => $value) {
263 if (!self
::isValidStatusConstant($key)) {
266 'Key "%s" is not a valid status constant. Status constants '.
267 'must be 1-64 alphanumeric characters and cannot be exclusively '.
268 'digits. For example, "%s" or "%s" are reasonable choices.',
273 if (!is_array($value)) {
276 'Value for key "%s" should be a dictionary.',
280 PhutilTypeSpec
::checkMap(
284 'name.full' => 'optional string',
285 'name.action' => 'optional string',
286 'closed' => 'optional bool',
287 'special' => 'optional string',
288 'transaction.icon' => 'optional string',
289 'transaction.color' => 'optional string',
290 'silly' => 'optional bool',
291 'prefixes' => 'optional list<string>',
292 'suffixes' => 'optional list<string>',
293 'keywords' => 'optional list<string>',
294 'disabled' => 'optional bool',
295 'claim' => 'optional bool',
296 'locked' => 'optional bool|string',
297 'mfa' => 'optional bool',
301 // Supported values are "comments" or "edits". For backward compatibility,
302 // "true" is an alias of "comments".
304 foreach ($config as $key => $value) {
305 $locked = idx($value, 'locked', false);
306 if ($locked === true ||
$locked === false) {
310 if ($locked === self
::LOCKED_EDITS ||
311 $locked === self
::LOCKED_COMMENTS
) {
317 'Task status ("%s") has unrecognized value for "locked" '.
318 'configuration ("%s"). Supported values are: "%s", "%s".',
321 self
::LOCKED_COMMENTS
,
322 self
::LOCKED_EDITS
));
325 $special_map = array();
326 foreach ($config as $key => $value) {
327 $special = idx($value, 'special');
332 if (isset($special_map[$special])) {
335 'Configuration has two statuses both marked with the special '.
336 'attribute "%s" ("%s" and "%s"). There should be only one.',
338 $special_map[$special],
343 case self
::SPECIAL_DEFAULT
:
344 if (!empty($value['closed'])) {
347 'Status "%s" is marked as default, but it is a closed '.
348 'status. The default status should be an open status.',
352 case self
::SPECIAL_CLOSED
:
353 if (empty($value['closed'])) {
356 'Status "%s" is marked as the default status for closing '.
357 'tasks, but is not a closed status. It should be a closed '.
362 case self
::SPECIAL_DUPLICATE
:
363 if (empty($value['closed'])) {
366 'Status "%s" is marked as the status for closing tasks as '.
367 'duplicates, but it is not a closed status. It should '.
368 'be a closed status.',
374 $special_map[$special] = $key;
377 // NOTE: We're not explicitly validating that we have at least one open
378 // and one closed status, because the DEFAULT and CLOSED specials imply
379 // that to be true. If those change in the future, that might become a
380 // reasonable thing to validate.
383 self
::SPECIAL_DEFAULT
,
384 self
::SPECIAL_CLOSED
,
385 self
::SPECIAL_DUPLICATE
,
388 foreach ($required as $required_special) {
389 if (!isset($special_map[$required_special])) {
392 'Configuration defines no task status with special attribute '.
393 '"%s", but you must specify a status which fills this special '.