Search in sources :

Example 11 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class DashboardsCollection method parse.

@Override
public DashboardResource parse(ProjectResource parent, IdString id) throws ResourceNotFoundException, IOException, ConfigInvalidException {
    ProjectControl myCtl = parent.getControl();
    if (id.toString().equals("default")) {
        return DashboardResource.projectDefault(myCtl);
    }
    List<String> parts = Lists.newArrayList(Splitter.on(':').limit(2).split(id.get()));
    if (parts.size() != 2) {
        throw new ResourceNotFoundException(id);
    }
    CurrentUser user = myCtl.getUser();
    String ref = parts.get(0);
    String path = parts.get(1);
    for (ProjectState ps : myCtl.getProjectState().tree()) {
        try {
            return parse(ps.controlFor(user), ref, path, myCtl);
        } catch (AmbiguousObjectException | ConfigInvalidException | IncorrectObjectTypeException e) {
            throw new ResourceNotFoundException(id);
        } catch (ResourceNotFoundException e) {
            continue;
        }
    }
    throw new ResourceNotFoundException(id);
}
Also used : AmbiguousObjectException(org.eclipse.jgit.errors.AmbiguousObjectException) CurrentUser(com.google.gerrit.server.CurrentUser) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IdString(com.google.gerrit.extensions.restapi.IdString) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 12 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class GetAccess method apply.

@Override
public ProjectAccessInfo apply(ProjectResource rsrc) throws ResourceNotFoundException, ResourceConflictException, IOException {
    // Load the current configuration from the repository, ensuring it's the most
    // recent version available. If it differs from what was in the project
    // state, force a cache flush now.
    //
    Project.NameKey projectName = rsrc.getNameKey();
    ProjectAccessInfo info = new ProjectAccessInfo();
    ProjectConfig config;
    ProjectControl pc = createProjectControl(projectName);
    RefControl metaConfigControl = pc.controlForRef(RefNames.REFS_CONFIG);
    try (MetaDataUpdate md = metaDataUpdateFactory.create(projectName)) {
        config = ProjectConfig.read(md);
        if (config.updateGroupNames(groupBackend)) {
            md.setMessage("Update group names\n");
            config.commit(md);
            projectCache.evict(config.getProject());
            pc = createProjectControl(projectName);
        } else if (config.getRevision() != null && !config.getRevision().equals(pc.getProjectState().getConfig().getRevision())) {
            projectCache.evict(config.getProject());
            pc = createProjectControl(projectName);
        }
    } catch (ConfigInvalidException e) {
        throw new ResourceConflictException(e.getMessage());
    } catch (RepositoryNotFoundException e) {
        throw new ResourceNotFoundException(rsrc.getName());
    }
    info.local = new HashMap<>();
    info.ownerOf = new HashSet<>();
    Map<AccountGroup.UUID, Boolean> visibleGroups = new HashMap<>();
    for (AccessSection section : config.getAccessSections()) {
        String name = section.getName();
        if (AccessSection.GLOBAL_CAPABILITIES.equals(name)) {
            if (pc.isOwner()) {
                info.local.put(name, createAccessSection(section));
                info.ownerOf.add(name);
            } else if (metaConfigControl.isVisible()) {
                info.local.put(section.getName(), createAccessSection(section));
            }
        } else if (RefConfigSection.isValid(name)) {
            RefControl rc = pc.controlForRef(name);
            if (rc.isOwner()) {
                info.local.put(name, createAccessSection(section));
                info.ownerOf.add(name);
            } else if (metaConfigControl.isVisible()) {
                info.local.put(name, createAccessSection(section));
            } else if (rc.isVisible()) {
                // Filter the section to only add rules describing groups that
                // are visible to the current-user. This includes any group the
                // user is a member of, as well as groups they own or that
                // are visible to all users.
                AccessSection dst = null;
                for (Permission srcPerm : section.getPermissions()) {
                    Permission dstPerm = null;
                    for (PermissionRule srcRule : srcPerm.getRules()) {
                        AccountGroup.UUID group = srcRule.getGroup().getUUID();
                        if (group == null) {
                            continue;
                        }
                        Boolean canSeeGroup = visibleGroups.get(group);
                        if (canSeeGroup == null) {
                            try {
                                canSeeGroup = groupControlFactory.controlFor(group).isVisible();
                            } catch (NoSuchGroupException e) {
                                canSeeGroup = Boolean.FALSE;
                            }
                            visibleGroups.put(group, canSeeGroup);
                        }
                        if (canSeeGroup) {
                            if (dstPerm == null) {
                                if (dst == null) {
                                    dst = new AccessSection(name);
                                    info.local.put(name, createAccessSection(dst));
                                }
                                dstPerm = dst.getPermission(srcPerm.getName(), true);
                            }
                            dstPerm.add(srcRule);
                        }
                    }
                }
            }
        }
    }
    if (info.ownerOf.isEmpty() && pc.isOwnerAnyRef()) {
        // Special case: If the section list is empty, this project has no current
        // access control information. Rely on what ProjectControl determines
        // is ownership, which probably means falling back to site administrators.
        info.ownerOf.add(AccessSection.ALL);
    }
    if (config.getRevision() != null) {
        info.revision = config.getRevision().name();
    }
    ProjectState parent = Iterables.getFirst(pc.getProjectState().parents(), null);
    if (parent != null) {
        info.inheritsFrom = projectJson.format(parent.getProject());
    }
    if (pc.getProject().getNameKey().equals(allProjectsName)) {
        if (pc.isOwner()) {
            info.ownerOf.add(AccessSection.GLOBAL_CAPABILITIES);
        }
    }
    info.isOwner = toBoolean(pc.isOwner());
    info.canUpload = toBoolean(pc.isOwner() || (metaConfigControl.isVisible() && metaConfigControl.canUpload()));
    info.canAdd = toBoolean(pc.canAddRefs());
    info.configVisible = pc.isOwner() || metaConfigControl.isVisible();
    return info;
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) HashMap(java.util.HashMap) PermissionRule(com.google.gerrit.common.data.PermissionRule) ProjectAccessInfo(com.google.gerrit.extensions.api.access.ProjectAccessInfo) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) AccessSection(com.google.gerrit.common.data.AccessSection) NoSuchGroupException(com.google.gerrit.common.errors.NoSuchGroupException) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Project(com.google.gerrit.reviewdb.client.Project) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Permission(com.google.gerrit.common.data.Permission) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 13 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class ListDashboards method scanDashboards.

private List<DashboardInfo> scanDashboards(Project definingProject, Repository git, RevWalk rw, Ref ref, String project, boolean setDefault) throws IOException {
    List<DashboardInfo> list = new ArrayList<>();
    try (TreeWalk tw = new TreeWalk(rw.getObjectReader())) {
        tw.addTree(rw.parseTree(ref.getObjectId()));
        tw.setRecursive(true);
        while (tw.next()) {
            if (tw.getFileMode(0) == FileMode.REGULAR_FILE) {
                try {
                    list.add(DashboardsCollection.parse(definingProject, ref.getName().substring(REFS_DASHBOARDS.length()), tw.getPathString(), new BlobBasedConfig(null, git, tw.getObjectId(0)), project, setDefault));
                } catch (ConfigInvalidException e) {
                    log.warn(String.format("Cannot parse dashboard %s:%s:%s: %s", definingProject.getName(), ref.getName(), tw.getPathString(), e.getMessage()));
                }
            }
        }
    }
    return list;
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ArrayList(java.util.ArrayList) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) BlobBasedConfig(org.eclipse.jgit.lib.BlobBasedConfig) DashboardInfo(com.google.gerrit.server.project.DashboardsCollection.DashboardInfo)

Example 14 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class PutDescription method apply.

@Override
public Response<String> apply(ProjectResource resource, DescriptionInput input) throws AuthException, ResourceConflictException, ResourceNotFoundException, IOException {
    if (input == null) {
        // Delete would set description to null.
        input = new DescriptionInput();
    }
    ProjectControl ctl = resource.getControl();
    IdentifiedUser user = ctl.getUser().asIdentifiedUser();
    if (!ctl.isOwner()) {
        throw new AuthException("not project owner");
    }
    try (MetaDataUpdate md = updateFactory.create(resource.getNameKey())) {
        ProjectConfig config = ProjectConfig.read(md);
        Project project = config.getProject();
        project.setDescription(Strings.emptyToNull(input.description));
        String msg = MoreObjects.firstNonNull(Strings.emptyToNull(input.commitMessage), "Updated description.\n");
        if (!msg.endsWith("\n")) {
            msg += "\n";
        }
        md.setAuthor(user);
        md.setMessage(msg);
        config.commit(md);
        cache.evict(ctl.getProject());
        md.getRepository().setGitwebDescription(project.getDescription());
        return Strings.isNullOrEmpty(project.getDescription()) ? Response.<String>none() : Response.ok(project.getDescription());
    } catch (RepositoryNotFoundException notFound) {
        throw new ResourceNotFoundException(resource.getName());
    } catch (ConfigInvalidException e) {
        throw new ResourceConflictException(String.format("invalid project.config: %s", e.getMessage()));
    }
}
Also used : ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Project(com.google.gerrit.reviewdb.client.Project) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) DescriptionInput(com.google.gerrit.extensions.api.projects.DescriptionInput) AuthException(com.google.gerrit.extensions.restapi.AuthException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 15 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class ChangeRebuilderImpl method getHashtagsEvents.

private List<HashtagsEvent> getHashtagsEvents(Change change, NoteDbUpdateManager manager) throws IOException {
    String refName = changeMetaRef(change.getId());
    Optional<ObjectId> old = manager.getChangeRepo().getObjectId(refName);
    if (!old.isPresent()) {
        return Collections.emptyList();
    }
    RevWalk rw = manager.getChangeRepo().rw;
    List<HashtagsEvent> events = new ArrayList<>();
    rw.reset();
    rw.markStart(rw.parseCommit(old.get()));
    for (RevCommit commit : rw) {
        Account.Id authorId;
        try {
            authorId = changeNoteUtil.parseIdent(commit.getAuthorIdent(), change.getId());
        } catch (ConfigInvalidException e) {
            // Corrupt data, no valid hashtags in this commit.
            continue;
        }
        PatchSet.Id psId = parsePatchSetId(change, commit);
        Set<String> hashtags = parseHashtags(commit);
        if (authorId == null || psId == null || hashtags == null) {
            continue;
        }
        Timestamp commitTime = new Timestamp(commit.getCommitterIdent().getWhen().getTime());
        events.add(new HashtagsEvent(psId, authorId, commitTime, hashtags, change.getCreatedOn()));
    }
    return events;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Timestamp(java.sql.Timestamp) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)158 IOException (java.io.IOException)95 Inject (com.google.inject.Inject)38 Repository (org.eclipse.jgit.lib.Repository)37 Provider (com.google.inject.Provider)34 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)31 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)30 ArrayList (java.util.ArrayList)30 Account (com.google.gerrit.entities.Account)27 List (java.util.List)26 Set (java.util.Set)26 ObjectId (org.eclipse.jgit.lib.ObjectId)26 AuthException (com.google.gerrit.extensions.restapi.AuthException)25 Config (org.eclipse.jgit.lib.Config)24 Singleton (com.google.inject.Singleton)23 OrmException (com.google.gwtorm.server.OrmException)22 AccountGroup (com.google.gerrit.entities.AccountGroup)21 RevWalk (org.eclipse.jgit.revwalk.RevWalk)21 StorageException (com.google.gerrit.exceptions.StorageException)20 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)20