Search in sources :

Example 1 with InvalidNameException

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());
    }
}
Also used : InvalidNameException(com.google.gerrit.exceptions.InvalidNameException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 2 with InvalidNameException

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()));
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) InvalidNameException(com.google.gerrit.exceptions.InvalidNameException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AccessSection(com.google.gerrit.entities.AccessSection) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 3 with InvalidNameException

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());
    }
}
Also used : PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) ObjectId(org.eclipse.jgit.lib.ObjectId) AuthException(com.google.gerrit.extensions.restapi.AuthException) Change(com.google.gerrit.entities.Change) RevWalk(org.eclipse.jgit.revwalk.RevWalk) AccessSection(com.google.gerrit.entities.AccessSection) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Project(com.google.gerrit.entities.Project) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) InvalidNameException(com.google.gerrit.exceptions.InvalidNameException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ChangeInserter(com.google.gerrit.server.change.ChangeInserter) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with InvalidNameException

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();
}
Also used : InvalidNameException(com.google.gerrit.exceptions.InvalidNameException) ImmutableList(com.google.common.collect.ImmutableList) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Aggregations

InvalidNameException (com.google.gerrit.exceptions.InvalidNameException)4 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 AccessSection (com.google.gerrit.entities.AccessSection)2 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)2 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)2 ImmutableList (com.google.common.collect.ImmutableList)1 Change (com.google.gerrit.entities.Change)1 Project (com.google.gerrit.entities.Project)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 ChangeInserter (com.google.gerrit.server.change.ChangeInserter)1 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)1 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)1 ObjectReader (org.eclipse.jgit.lib.ObjectReader)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1