Search in sources :

Example 51 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class TestSubmitRule method apply.

@Override
public Response<TestSubmitRuleInfo> apply(RevisionResource rsrc, TestSubmitRuleInput input) throws AuthException, PermissionBackendException, BadRequestException {
    if (input == null) {
        input = new TestSubmitRuleInput();
    }
    if (input.rule == null) {
        throw new BadRequestException("rule is required");
    }
    if (!rules.isProjectRulesEnabled()) {
        throw new AuthException("project rules are disabled");
    }
    input.filters = MoreObjects.firstNonNull(input.filters, filters);
    Project.NameKey name = rsrc.getProject();
    Optional<ProjectState> project = projectCache.get(name);
    if (!project.isPresent()) {
        throw new BadRequestException("project not found " + name);
    }
    ChangeData cd = changeDataFactory.create(rsrc.getNotes());
    SubmitRecord record = prologRule.evaluate(cd, PrologOptions.dryRunOptions(input.rule, input.filters == Filters.SKIP));
    AccountLoader accounts = accountInfoFactory.create(true);
    TestSubmitRuleInfo out = newSubmitRuleInfo(record, accounts);
    accounts.fill();
    return Response.ok(out);
}
Also used : Project(com.google.gerrit.entities.Project) SubmitRecord(com.google.gerrit.entities.SubmitRecord) TestSubmitRuleInput(com.google.gerrit.extensions.common.TestSubmitRuleInput) AccountLoader(com.google.gerrit.server.account.AccountLoader) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) ProjectState(com.google.gerrit.server.project.ProjectState) ChangeData(com.google.gerrit.server.query.change.ChangeData) TestSubmitRuleInfo(com.google.gerrit.extensions.common.TestSubmitRuleInfo)

Example 52 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class RevertSubmission method checkPermissionsForAllChanges.

private void checkPermissionsForAllChanges(ChangeResource changeResource, List<ChangeData> changeDatas) throws IOException, AuthException, PermissionBackendException, ResourceConflictException {
    for (ChangeData changeData : changeDatas) {
        Change change = changeData.change();
        // Might do the permission tests multiple times, but these are necessary to ensure that the
        // user has permissions to revert all changes. If they lack any permission, no revert will be
        // done.
        contributorAgreements.check(change.getProject(), changeResource.getUser());
        permissionBackend.currentUser().ref(change.getDest()).check(CREATE_CHANGE);
        permissionBackend.currentUser().change(changeData).check(REVERT);
        permissionBackend.currentUser().change(changeData).check(ChangePermission.READ);
        projectCache.get(change.getProject()).orElseThrow(illegalState(change.getProject())).checkStatePermitsWrite();
        requireNonNull(psUtil.get(changeData.notes(), change.currentPatchSetId()), String.format("current patch set %s of change %s not found", change.currentPatchSetId(), change.currentPatchSetId()));
    }
}
Also used : Change(com.google.gerrit.entities.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 53 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class SubmittedTogether method applyInfo.

public SubmittedTogetherInfo applyInfo(ChangeResource resource) throws AuthException, IOException, PermissionBackendException {
    Change c = resource.getChange();
    try {
        List<ChangeData> cds;
        int hidden;
        if (c.isNew()) {
            ChangeSet cs = mergeSuperSet.get().completeChangeSet(c, resource.getUser(), options.contains(TOPIC_CLOSURE));
            cds = ensureRequiredDataIsLoaded(cs.changes().asList());
            hidden = cs.nonVisibleChanges().size();
        } else if (c.isMerged()) {
            cds = queryProvider.get().bySubmissionId(c.getSubmissionId());
            hidden = 0;
        } else {
            cds = Collections.emptyList();
            hidden = 0;
        }
        if (hidden != 0 && !options.contains(NON_VISIBLE_CHANGES)) {
            throw new AuthException("change would be submitted with a change that you cannot see");
        }
        cds = sort(cds, hidden);
        SubmittedTogetherInfo info = new SubmittedTogetherInfo();
        info.changes = json.create(jsonOpt).format(cds);
        info.nonVisibleChanges = hidden;
        return info;
    } catch (StorageException | IOException e) {
        logger.atSevere().withCause(e).log("Error on getting a ChangeSet");
        throw e;
    }
}
Also used : AuthException(com.google.gerrit.extensions.restapi.AuthException) Change(com.google.gerrit.entities.Change) SubmittedTogetherInfo(com.google.gerrit.extensions.api.changes.SubmittedTogetherInfo) IOException(java.io.IOException) ChangeData(com.google.gerrit.server.query.change.ChangeData) ChangeSet(com.google.gerrit.server.submit.ChangeSet) StorageException(com.google.gerrit.exceptions.StorageException)

Example 54 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class SubmittedTogether method ensureRequiredDataIsLoaded.

private static List<ChangeData> ensureRequiredDataIsLoaded(List<ChangeData> cds) {
    // lazyloading if so.
    for (ChangeData cd : cds) {
        cd.submitRecords(ChangeJson.SUBMIT_RULE_OPTIONS_LENIENT);
        cd.submitRecords(ChangeJson.SUBMIT_RULE_OPTIONS_STRICT);
        cd.currentPatchSet();
    }
    return cds;
}
Also used : ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 55 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class SubmittedTogether method sort.

private ImmutableList<ChangeData> sort(List<ChangeData> cds, int hidden) throws IOException {
    if (cds.size() <= 1 && hidden == 0) {
        // repo just to fill out the commit field in PatchSetData.
        return ImmutableList.of();
    }
    long numProjectsDistinct = cds.stream().map(ChangeData::project).distinct().count();
    long numProjects = cds.stream().map(ChangeData::project).count();
    if (numProjects == numProjectsDistinct || numProjectsDistinct > 5) {
        // which would make WalkSorter too expensive for this call.
        return cds.stream().sorted(COMPARATOR).collect(toImmutableList());
    }
    // Perform more expensive walk-sort.
    ImmutableList.Builder<ChangeData> sorted = ImmutableList.builderWithExpectedSize(cds.size());
    for (PatchSetData psd : sorter.get().sort(cds)) {
        sorted.add(psd.data());
    }
    return sorted.build();
}
Also used : PatchSetData(com.google.gerrit.server.change.WalkSorter.PatchSetData) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Aggregations

ChangeData (com.google.gerrit.server.query.change.ChangeData)208 Test (org.junit.Test)75 Change (com.google.gerrit.entities.Change)58 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)57 RevCommit (org.eclipse.jgit.revwalk.RevCommit)53 ObjectId (org.eclipse.jgit.lib.ObjectId)45 ArrayList (java.util.ArrayList)41 IOException (java.io.IOException)33 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)27 PatchSet (com.google.gerrit.entities.PatchSet)26 StorageException (com.google.gerrit.exceptions.StorageException)25 Inject (com.google.inject.Inject)25 HashMap (java.util.HashMap)25 Map (java.util.Map)24 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)23 List (java.util.List)23 Project (com.google.gerrit.entities.Project)21 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)21 OrmException (com.google.gwtorm.server.OrmException)20 Repository (org.eclipse.jgit.lib.Repository)20