Fix refPattern when using username/shardeduserid pattern with regexp
The special regexp caracters ('{', '}') from gerrit pattern are causing
an exception. As ${username} and ${shardeduserid} are only meaningful to
gerrit, we substitute them for placeholders.
Unlike what is done in RefPatternMatcher.ExpandParameters, the
placeholder does not contain ':' but '_' to be correctly evaluated by
Repository.isValidRefName later on.
Bug: Issue 3340
Change-Id: Iedf3022f6ce3068f7e9534f224cf05bb99b0f970
This commit is contained in:
committed by
David Pursehouse
parent
9a5141f0ad
commit
1b4b264adf
@@ -18,10 +18,13 @@ import com.google.common.base.Throwables;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.gerrit.common.data.AccessSection;
|
||||
import com.google.gerrit.common.data.ParameterizedString;
|
||||
import com.google.gerrit.common.data.RefConfigSection;
|
||||
import com.google.gerrit.common.errors.InvalidNameException;
|
||||
import dk.brics.automaton.RegExp;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
@@ -74,7 +77,13 @@ public class RefPattern {
|
||||
if (isRE(refPattern)) {
|
||||
refPattern = refPattern.substring(1);
|
||||
}
|
||||
return new RegExp(refPattern, RegExp.NONE);
|
||||
ParameterizedString template = new ParameterizedString(refPattern);
|
||||
String replacement = "_PLACEHOLDER_";
|
||||
Map<String, String> params =
|
||||
ImmutableMap.of(
|
||||
RefPattern.USERID_SHARDED, replacement,
|
||||
RefPattern.USERNAME, replacement);
|
||||
return new RegExp(template.replace(params), RegExp.NONE);
|
||||
}
|
||||
|
||||
public static void validate(String refPattern) throws InvalidNameException {
|
||||
|
||||
@@ -525,6 +525,13 @@ public class RefControlTest {
|
||||
assertCanUpload(u);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usernamePatternRegExpCanUploadToAnyRef() throws Exception {
|
||||
allow(local, PUSH, REGISTERED_USERS, "^refs/heads/users/${username}/(public|private)/.+");
|
||||
ProjectControl u = user(local, "a-registered-user");
|
||||
assertCanUpdate("refs/heads/users/a-registered-user/private/a", u);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usernamePatternNonRegex() throws Exception {
|
||||
allow(local, READ, DEVS, "refs/sb/${username}/heads/*");
|
||||
@@ -943,6 +950,7 @@ public class RefControlTest {
|
||||
RefPattern.validate("^refs/heads/*");
|
||||
RefPattern.validate("^refs/tags/[0-9a-zA-Z-_.]+");
|
||||
RefPattern.validate("refs/heads/review/${username}/*");
|
||||
RefPattern.validate("^refs/heads/review/${username}/.+");
|
||||
}
|
||||
|
||||
@Test(expected = InvalidNameException.class)
|
||||
|
||||
Reference in New Issue
Block a user