use of com.google.gerrit.exceptions.InvalidNameException in project gerrit by GerritCodeReview.
the class RefPattern method validateRegExp.
public static void validateRegExp(String refPattern) throws InvalidNameException {
try {
refPattern = refPattern.replace("${" + USERID_SHARDED + "}", "");
refPattern = refPattern.replace("${" + USERNAME + "}", "");
Pattern.compile(refPattern);
} catch (PatternSyntaxException e) {
throw new InvalidNameException(refPattern + " " + e.getMessage());
}
}
use of com.google.gerrit.exceptions.InvalidNameException in project gerrit by GerritCodeReview.
the class SetAccess method apply.
@Override
public Response<ProjectAccessInfo> apply(ProjectResource rsrc, ProjectAccessInput input) throws Exception {
MetaDataUpdate.User metaDataUpdateUser = metaDataUpdateFactory.get();
ProjectConfig config;
List<AccessSection> removals = accessUtil.getAccessSections(input.remove);
List<AccessSection> additions = accessUtil.getAccessSections(input.add);
try (MetaDataUpdate md = metaDataUpdateUser.create(rsrc.getNameKey())) {
config = projectConfigFactory.read(md);
// Check that the user has the right permissions.
boolean checkedAdmin = false;
for (AccessSection section : Iterables.concat(additions, removals)) {
boolean isGlobalCapabilities = AccessSection.GLOBAL_CAPABILITIES.equals(section.getName());
if (isGlobalCapabilities) {
if (!checkedAdmin) {
permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER);
checkedAdmin = true;
}
} else {
permissionBackend.currentUser().project(rsrc.getNameKey()).ref(section.getName()).check(RefPermission.WRITE_CONFIG);
}
}
accessUtil.validateChanges(config, removals, additions);
accessUtil.applyChanges(config, removals, additions);
accessUtil.setParentName(identifiedUser.get(), config, rsrc.getNameKey(), input.parent == null ? null : Project.nameKey(input.parent), !checkedAdmin);
if (!Strings.isNullOrEmpty(input.message)) {
if (!input.message.endsWith("\n")) {
input.message += "\n";
}
md.setMessage(input.message);
} else {
md.setMessage("Modify access rules\n");
}
config.commit(md);
projectCache.evictAndReindex(config.getProject());
createGroupPermissionSyncer.syncIfNeeded();
} catch (InvalidNameException e) {
throw new BadRequestException(e.toString());
} catch (ConfigInvalidException e) {
throw new ResourceConflictException(rsrc.getName(), e);
}
return Response.ok(getAccess.apply(rsrc.getNameKey()));
}
use of com.google.gerrit.exceptions.InvalidNameException in project gerrit by GerritCodeReview.
the class CreateAccessChange method apply.
@Override
public Response<ChangeInfo> apply(ProjectResource rsrc, ProjectAccessInput input) throws PermissionBackendException, AuthException, IOException, ConfigInvalidException, InvalidNameException, UpdateException, RestApiException {
PermissionBackend.ForProject forProject = permissionBackend.user(rsrc.getUser()).project(rsrc.getNameKey());
if (!check(forProject, ProjectPermission.READ_CONFIG)) {
throw new AuthException(RefNames.REFS_CONFIG + " not visible");
}
if (!check(forProject, ProjectPermission.WRITE_CONFIG)) {
try {
forProject.ref(RefNames.REFS_CONFIG).check(RefPermission.CREATE_CHANGE);
} catch (AuthException denied) {
throw new AuthException("cannot create change for " + RefNames.REFS_CONFIG, denied);
}
}
projectCache.get(rsrc.getNameKey()).orElseThrow(illegalState(rsrc.getNameKey())).checkStatePermitsWrite();
MetaDataUpdate.User metaDataUpdateUser = metaDataUpdateFactory.get();
ImmutableList<AccessSection> removals = setAccess.getAccessSections(input.remove);
ImmutableList<AccessSection> additions = setAccess.getAccessSections(input.add);
Project.NameKey newParentProjectName = input.parent == null ? null : Project.nameKey(input.parent);
try (MetaDataUpdate md = metaDataUpdateUser.create(rsrc.getNameKey())) {
ProjectConfig config = projectConfigFactory.read(md);
ObjectId oldCommit = config.getRevision();
String oldCommitSha1 = oldCommit == null ? null : oldCommit.getName();
setAccess.validateChanges(config, removals, additions);
setAccess.applyChanges(config, removals, additions);
try {
setAccess.setParentName(rsrc.getUser().asIdentifiedUser(), config, rsrc.getNameKey(), newParentProjectName, false);
} catch (AuthException e) {
throw new IllegalStateException(e);
}
md.setMessage("Review access change");
md.setInsertChangeId(true);
Change.Id changeId = Change.id(seq.nextChangeId());
RevCommit commit = config.commitToNewRef(md, PatchSet.id(changeId, Change.INITIAL_PATCH_SET_ID).toRefName());
if (commit.name().equals(oldCommitSha1)) {
throw new BadRequestException("no change");
}
try (ObjectInserter objInserter = md.getRepository().newObjectInserter();
ObjectReader objReader = objInserter.newReader();
RevWalk rw = new RevWalk(objReader);
BatchUpdate bu = updateFactory.create(rsrc.getNameKey(), rsrc.getUser(), TimeUtil.now())) {
bu.setRepository(md.getRepository(), rw, objInserter);
ChangeInserter ins = newInserter(changeId, commit);
bu.insertChange(ins);
bu.execute();
return Response.created(jsonFactory.noOptions().format(ins.getChange()));
}
} catch (InvalidNameException e) {
throw new BadRequestException(e.toString());
}
}
use of com.google.gerrit.exceptions.InvalidNameException in project gerrit by GerritCodeReview.
the class LabelDefinitionInputParser method parseBranches.
public static ImmutableList<String> parseBranches(List<String> branches) throws BadRequestException {
ImmutableList.Builder<String> validBranches = ImmutableList.builder();
for (String branch : branches) {
String newBranch = branch.trim();
if (newBranch.isEmpty()) {
continue;
}
if (!RefPattern.isRE(newBranch) && !newBranch.startsWith(RefNames.REFS)) {
newBranch = RefNames.REFS_HEADS + newBranch;
}
try {
RefPattern.validate(newBranch);
} catch (InvalidNameException e) {
throw new BadRequestException("invalid branch: " + branch, e);
}
validBranches.add(newBranch);
}
return validBranches.build();
}
Aggregations