Search in sources :

Example 16 with StorageException

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

the class SubmitRuleEvaluator method evaluate.

/**
 * Evaluate the submit rules.
 *
 * @return List of {@link SubmitRecord} objects returned from the evaluated rules, including any
 *     errors.
 * @param cd ChangeData to evaluate
 */
public List<SubmitRecord> evaluate(ChangeData cd) {
    logger.atFine().log("Evaluate submit rules for change %d (caller: %s)", cd.change().getId().get(), callerFinder.findCallerLazy());
    try (Timer0.Context ignored = submitRuleEvaluationLatency.start()) {
        Change change;
        ProjectState projectState;
        try {
            change = cd.change();
            if (change == null) {
                throw new StorageException("Change not found");
            }
            Project.NameKey name = cd.project();
            Optional<ProjectState> projectStateOptional = projectCache.get(name);
            if (!projectStateOptional.isPresent()) {
                throw new NoSuchProjectException(name);
            }
            projectState = projectStateOptional.get();
        } catch (NoSuchProjectException e) {
            throw new IllegalStateException("Unable to find project while evaluating submit rule", e);
        }
        if (change.isClosed() && (!opts.recomputeOnClosedChanges() || OnlineReindexMode.isActive())) {
            return cd.notes().getSubmitRecords().stream().map(r -> {
                SubmitRecord record = r.deepCopy();
                if (record.status == SubmitRecord.Status.OK) {
                    // Submit records that were OK when they got merged are CLOSED now.
                    record.status = SubmitRecord.Status.CLOSED;
                }
                return record;
            }).collect(toImmutableList());
        }
        // and then we collect the results in one list.
        return Streams.stream(submitRules).filter(projectState.hasPrologRules() ? rule -> !(rule.get() instanceof DefaultSubmitRule) : rule -> true).map(c -> c.call(s -> {
            Optional<SubmitRecord> record = s.evaluate(cd);
            if (record.isPresent() && record.get().ruleName == null) {
                // Only back-fill the ruleName if it was not populated by the "submit
                // rule".
                record.get().ruleName = c.getPluginName() + "~" + s.getClass().getSimpleName();
            }
            return record;
        })).filter(Optional::isPresent).map(Optional::get).collect(toImmutableList());
    }
}
Also used : Inject(com.google.inject.Inject) Assisted(com.google.inject.assistedinject.Assisted) CallerFinder(com.google.gerrit.server.logging.CallerFinder) Description(com.google.gerrit.metrics.Description) PrologRule(com.google.gerrit.server.rules.PrologRule) Change(com.google.gerrit.entities.Change) OnlineReindexMode(com.google.gerrit.server.index.OnlineReindexMode) ChangeApi(com.google.gerrit.extensions.api.changes.ChangeApi) SubmitRule(com.google.gerrit.server.rules.SubmitRule) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StorageException(com.google.gerrit.exceptions.StorageException) Timer0(com.google.gerrit.metrics.Timer0) Units(com.google.gerrit.metrics.Description.Units) Streams(com.google.common.collect.Streams) SubmitRecord(com.google.gerrit.entities.SubmitRecord) DefaultSubmitRule(com.google.gerrit.server.rules.DefaultSubmitRule) SubmitTypeRecord(com.google.gerrit.entities.SubmitTypeRecord) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) Project(com.google.gerrit.entities.Project) PluginSetContext(com.google.gerrit.server.plugincontext.PluginSetContext) ChangeJson(com.google.gerrit.server.change.ChangeJson) Optional(java.util.Optional) MetricMaker(com.google.gerrit.metrics.MetricMaker) FluentLogger(com.google.common.flogger.FluentLogger) Optional(java.util.Optional) DefaultSubmitRule(com.google.gerrit.server.rules.DefaultSubmitRule) Change(com.google.gerrit.entities.Change) Project(com.google.gerrit.entities.Project) SubmitRecord(com.google.gerrit.entities.SubmitRecord) Timer0(com.google.gerrit.metrics.Timer0) StorageException(com.google.gerrit.exceptions.StorageException)

Example 17 with StorageException

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

the class ChangeData method isMergeable.

@Nullable
public Boolean isMergeable() {
    if (mergeable == null) {
        Change c = change();
        if (c == null) {
            return null;
        }
        if (c.isMerged()) {
            mergeable = true;
        } else if (c.isAbandoned()) {
            return null;
        } else if (c.isWorkInProgress()) {
            return null;
        } else {
            if (!lazyload()) {
                return null;
            }
            PatchSet ps = currentPatchSet();
            if (ps == null) {
                return null;
            }
            try (Repository repo = repoManager.openRepository(project())) {
                Ref ref = repo.getRefDatabase().exactRef(c.getDest().branch());
                SubmitTypeRecord str = submitTypeRecord();
                if (!str.isOk()) {
                    // No need to log, as SubmitRuleEvaluator already did it for us.
                    return false;
                }
                String mergeStrategy = mergeUtilFactory.create(projectCache.get(project()).orElseThrow(illegalState(project()))).mergeStrategyName();
                mergeable = mergeabilityCache.get(ps.commitId(), ref, str.type, mergeStrategy, c.getDest(), repo);
            } catch (IOException e) {
                throw new StorageException(e);
            }
        }
    }
    return mergeable;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) StarRef(com.google.gerrit.server.StarredChangesUtil.StarRef) Ref(org.eclipse.jgit.lib.Ref) SubmitTypeRecord(com.google.gerrit.entities.SubmitTypeRecord) PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) IOException(java.io.IOException) StorageException(com.google.gerrit.exceptions.StorageException) Nullable(com.google.gerrit.common.Nullable)

Example 18 with StorageException

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

the class ChangeData method reloadChange.

public Change reloadChange() {
    try {
        notes = notesFactory.createChecked(project, legacyId);
    } catch (NoSuchChangeException e) {
        throw new StorageException("Unable to load change " + legacyId, e);
    }
    change = notes.getChange();
    setPatchSets(null);
    return change;
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) StorageException(com.google.gerrit.exceptions.StorageException)

Example 19 with StorageException

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

the class ChangeNotesTest method setRevertOfOnChildCommitFails.

@Test
public void setRevertOfOnChildCommitFails() throws Exception {
    Change c = newChange();
    ChangeUpdate update = newUpdate(c, changeOwner);
    update.setRevertOf(newChange().getId().get());
    StorageException thrown = assertThrows(StorageException.class, () -> update.commit());
    assertThat(thrown).hasMessageThat().contains("Given ChangeUpdate is only allowed on initial commit");
}
Also used : Change(com.google.gerrit.entities.Change) StorageException(com.google.gerrit.exceptions.StorageException) Test(org.junit.Test)

Example 20 with StorageException

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

the class ChangeNotesTest method commitChangeNotesUnique.

@Test
public void commitChangeNotesUnique() throws Exception {
    // PatchSetId -> ObjectId must be a one to one mapping
    Change c = newChange();
    ChangeNotes notes = newNotes(c);
    PatchSet ps = notes.getCurrentPatchSet();
    assertThat(ps).isNotNull();
    // new revId for the same patch set, ps1
    ChangeUpdate update = newUpdate(c, changeOwner);
    RevCommit commit = tr.commit().message("PS1 again").create();
    update.setCommit(rw, commit);
    update.commit();
    StorageException e = assertThrows(StorageException.class, () -> newNotes(c));
    assertCause(e, ConfigInvalidException.class, "Multiple revisions parsed for patch set 1:" + " " + commit.name() + " and " + ps.commitId().name());
}
Also used : PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) StorageException(com.google.gerrit.exceptions.StorageException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

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