Search in sources :

Example 11 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class ChangeIdHandler method parseArguments.

@Override
public final int parseArguments(Parameters params) throws CmdLineException {
    String token = params.getParameter(0);
    List<String> tokens = Splitter.on(',').splitToList(token);
    if (tokens.size() != 3) {
        throw new CmdLineException(owner, localizable("change should be specified as <project>,<branch>,<change-id>"));
    }
    try {
        Change.Key key = Change.Key.parse(tokens.get(2));
        Project.NameKey project = Project.nameKey(tokens.get(0));
        BranchNameKey branch = BranchNameKey.create(project, tokens.get(1));
        List<ChangeData> changes = queryProvider.get().byBranchKey(branch, key);
        if (!changes.isEmpty()) {
            if (changes.size() > 1) {
                String msg = "\"%s\": resolves to multiple changes";
                logger.atSevere().log(msg, token);
                throw new CmdLineException(owner, localizable(msg), token);
            }
            setter.addValue(changes.get(0).getId());
            return 1;
        }
    } catch (IllegalArgumentException e) {
        throw new CmdLineException(owner, localizable("Change-Id is not valid: %s"), e.getMessage());
    } catch (StorageException e) {
        throw new CmdLineException(owner, localizable("Database error: %s"), e.getMessage());
    }
    throw new CmdLineException(owner, localizable("\"%s\": change not found"), token);
}
Also used : Project(com.google.gerrit.entities.Project) BranchNameKey(com.google.gerrit.entities.BranchNameKey) Change(com.google.gerrit.entities.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) StorageException(com.google.gerrit.exceptions.StorageException) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 12 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class AccountIdHandler method parseArguments.

@Override
public int parseArguments(Parameters params) throws CmdLineException {
    String token = params.getParameter(0);
    Account.Id accountId;
    try {
        try {
            accountId = accountResolver.resolve(token).asUnique().account().id();
        } catch (UnprocessableEntityException e) {
            switch(authType) {
                case HTTP_LDAP:
                case CLIENT_SSL_CERT_LDAP:
                case LDAP:
                    accountId = createAccountByLdap(token);
                    break;
                case CUSTOM_EXTENSION:
                case DEVELOPMENT_BECOME_ANY_ACCOUNT:
                case HTTP:
                case LDAP_BIND:
                case OAUTH:
                case OPENID:
                case OPENID_SSO:
                default:
                    String msg = "user \"%s\" not found";
                    logger.atSevere().withCause(e).log(msg, token);
                    throw new CmdLineException(owner, localizable(msg), token);
            }
        }
    } catch (StorageException e) {
        CmdLineException newException = new CmdLineException(owner, localizable("database is down"));
        newException.initCause(e);
        throw newException;
    } catch (IOException e) {
        throw new CmdLineException(owner, "Failed to load account", e);
    } catch (ConfigInvalidException e) {
        throw new CmdLineException(owner, "Invalid account config", e);
    }
    setter.addValue(accountId);
    return 1;
}
Also used : Account(com.google.gerrit.entities.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IOException(java.io.IOException) StorageException(com.google.gerrit.exceptions.StorageException) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 13 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class ChangeJson method format.

private ChangeInfo format(ChangeData cd, Optional<PatchSet.Id> limitToPsId, boolean fillAccountLoader, List<PluginDefinedInfo> pluginInfosForChange) {
    try {
        if (fillAccountLoader) {
            accountLoader = accountLoaderFactory.create(has(DETAILED_ACCOUNTS));
            ChangeInfo res = toChangeInfo(cd, limitToPsId, pluginInfosForChange);
            accountLoader.fill();
            return res;
        }
        return toChangeInfo(cd, limitToPsId, pluginInfosForChange);
    } catch (PatchListNotAvailableException | GpgException | IOException | PermissionBackendException | RuntimeException e) {
        if (!has(CHECK)) {
            Throwables.throwIfInstanceOf(e, StorageException.class);
            throw new StorageException(e);
        }
        return checkOnly(cd);
    }
}
Also used : GpgException(com.google.gerrit.server.GpgException) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) PatchListNotAvailableException(com.google.gerrit.server.patch.PatchListNotAvailableException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IOException(java.io.IOException) StorageException(com.google.gerrit.exceptions.StorageException)

Example 14 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class ChangeJson method loadPatchSets.

private Map<PatchSet.Id, PatchSet> loadPatchSets(ChangeData cd, Optional<PatchSet.Id> limitToPsId) {
    Collection<PatchSet> src;
    if (has(ALL_REVISIONS) || has(MESSAGES)) {
        src = cd.patchSets();
    } else {
        PatchSet ps;
        if (limitToPsId.isPresent()) {
            ps = cd.patchSet(limitToPsId.get());
            if (ps == null) {
                throw new StorageException("missing patch set " + limitToPsId.get());
            }
        } else {
            ps = cd.currentPatchSet();
            if (ps == null) {
                throw new StorageException("missing current patch set for change " + cd.getId());
            }
        }
        src = Collections.singletonList(ps);
    }
    Map<PatchSet.Id, PatchSet> map = Maps.newHashMapWithExpectedSize(src.size());
    for (PatchSet patchSet : src) {
        map.put(patchSet.id(), patchSet);
    }
    return map;
}
Also used : PatchSet(com.google.gerrit.entities.PatchSet) ObjectId(org.eclipse.jgit.lib.ObjectId) StorageException(com.google.gerrit.exceptions.StorageException)

Example 15 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class AbandonUtil method abandonInactiveOpenChanges.

public void abandonInactiveOpenChanges(BatchUpdate.Factory updateFactory) {
    if (cfg.getAbandonAfter() <= 0) {
        return;
    }
    try {
        String query = "status:new age:" + TimeUnit.MILLISECONDS.toMinutes(cfg.getAbandonAfter()) + "m";
        if (!cfg.getAbandonIfMergeable()) {
            query += " -is:mergeable";
        }
        List<ChangeData> changesToAbandon = queryProvider.get().enforceVisibility(false).query(queryBuilderProvider.get().parse(query)).entities();
        ImmutableListMultimap.Builder<Project.NameKey, ChangeData> builder = ImmutableListMultimap.builder();
        for (ChangeData cd : changesToAbandon) {
            builder.put(cd.project(), cd);
        }
        int count = 0;
        ListMultimap<Project.NameKey, ChangeData> abandons = builder.build();
        String message = cfg.getAbandonMessage();
        for (Project.NameKey project : abandons.keySet()) {
            Collection<ChangeData> changes = getValidChanges(abandons.get(project), query);
            try {
                batchAbandon.batchAbandon(updateFactory, project, internalUser, changes, message);
                count += changes.size();
            } catch (Exception e) {
                StringBuilder msg = new StringBuilder("Failed to auto-abandon inactive change(s):");
                for (ChangeData change : changes) {
                    msg.append(" ").append(change.getId().get());
                }
                msg.append(".");
                logger.atSevere().withCause(e).log("%s", msg);
            }
        }
        logger.atInfo().log("Auto-Abandoned %d of %d changes.", count, changesToAbandon.size());
    } catch (QueryParseException | StorageException e) {
        logger.atSevere().withCause(e).log("Failed to query inactive open changes for auto-abandoning.");
    }
}
Also used : ChangeData(com.google.gerrit.server.query.change.ChangeData) StorageException(com.google.gerrit.exceptions.StorageException) QueryParseException(com.google.gerrit.index.query.QueryParseException) QueryParseException(com.google.gerrit.index.query.QueryParseException) Project(com.google.gerrit.entities.Project) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) StorageException(com.google.gerrit.exceptions.StorageException)

Aggregations

StorageException (com.google.gerrit.exceptions.StorageException)153 IOException (java.io.IOException)68 Change (com.google.gerrit.entities.Change)47 ObjectId (org.eclipse.jgit.lib.ObjectId)37 Repository (org.eclipse.jgit.lib.Repository)33 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)30 PatchSet (com.google.gerrit.entities.PatchSet)29 RevCommit (org.eclipse.jgit.revwalk.RevCommit)28 ArrayList (java.util.ArrayList)25 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)24 Project (com.google.gerrit.entities.Project)22 Ref (org.eclipse.jgit.lib.Ref)22 ChangeData (com.google.gerrit.server.query.change.ChangeData)21 RevWalk (org.eclipse.jgit.revwalk.RevWalk)21 Account (com.google.gerrit.entities.Account)20 Inject (com.google.inject.Inject)19 Map (java.util.Map)19 Test (org.junit.Test)19 List (java.util.List)18 BranchNameKey (com.google.gerrit.entities.BranchNameKey)17