1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-19 16:58:48 +02:00

Improve error messages for bad hashtags and project names

Summary: Ref T8509. We currently give you a fairly obtuse error when trying to name a project something like "!!". The error is correct, but not as helpful as it could be. Give users a more specific, more helpful error.

Test Plan: {F1042883}

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T8509

Differential Revision: https://secure.phabricator.com/D14872
This commit is contained in:
epriestley 2015-12-24 04:43:02 -08:00
parent 33f55a85b0
commit e2edb1577c
4 changed files with 45 additions and 1 deletions

View file

@ -310,7 +310,6 @@ final class PhabricatorProjectCoreTestCase extends PhabricatorTestCase {
$slugs = mpull($slugs, 'getSlug'); $slugs = mpull($slugs, 'getSlug');
$this->assertTrue(in_array($name2, $slugs)); $this->assertTrue(in_array($name2, $slugs));
} }
public function testDuplicateSlugs() { public function testDuplicateSlugs() {

View file

@ -268,6 +268,17 @@ final class PhabricatorProjectTransactionEditor
} }
$name = last($xactions)->getNewValue(); $name = last($xactions)->getNewValue();
if (!PhabricatorSlug::isValidProjectSlug($name)) {
$errors[] = new PhabricatorApplicationTransactionValidationError(
$type,
pht('Invalid'),
pht(
'Project names must contain at least one letter or number.'),
last($xactions));
break;
}
$name_used_already = id(new PhabricatorProjectQuery()) $name_used_already = id(new PhabricatorProjectQuery())
->setViewer($this->getActor()) ->setViewer($this->getActor())
->withNames(array($name)) ->withNames(array($name))
@ -304,6 +315,27 @@ final class PhabricatorProjectTransactionEditor
$slug_xaction = last($xactions); $slug_xaction = last($xactions);
$new = $slug_xaction->getNewValue(); $new = $slug_xaction->getNewValue();
$invalid = array();
foreach ($new as $slug) {
if (!PhabricatorSlug::isValidProjectSlug($slug)) {
$invalid[] = $slug;
}
}
if ($invalid) {
$errors[] = new PhabricatorApplicationTransactionValidationError(
$type,
pht('Invalid'),
pht(
'Hashtags must contain at least one letter or number. %s '.
'project hashtag(s) are invalid: %s.',
phutil_count($invalid),
implode(', ', $invalid)),
$slug_xaction);
break;
}
$new = $this->normalizeSlugs($new); $new = $this->normalizeSlugs($new);
if ($new) { if ($new) {

View file

@ -774,6 +774,14 @@ final class PhabricatorUSEnglishTranslation
'%s changed project hashtag(s), added %d: %s; removed %d: %s.' => '%s changed project hashtag(s), added %d: %s; removed %d: %s.' =>
'%s changed project hashtags, added %3$s; removed %5$s.', '%s changed project hashtags, added %3$s; removed %5$s.',
'Hashtags must contain at least one letter or number. %s '.
'project hashtag(s) are invalid: %s.' => array(
'Hashtags must contain at least one letter or number. The '.
'hashtag "%2$s" is not valid.',
'Hashtags must contain at least one letter or number. These '.
'hashtags are invalid: %2$s.',
),
'%s added %d project hashtag(s): %s.' => array( '%s added %d project hashtag(s): %s.' => array(
array( array(
'%s added a hashtag: %3$s.', '%s added a hashtag: %3$s.',

View file

@ -8,6 +8,11 @@ final class PhabricatorSlug extends Phobject {
return rtrim($slug, '/'); return rtrim($slug, '/');
} }
public static function isValidProjectSlug($slug) {
$slug = self::normalizeProjectSlug($slug);
return ($slug != '_');
}
public static function normalize($slug, $hashtag = false) { public static function normalize($slug, $hashtag = false) {
$slug = preg_replace('@/+@', '/', $slug); $slug = preg_replace('@/+@', '/', $slug);
$slug = trim($slug, '/'); $slug = trim($slug, '/');