Search in sources :

Example 56 with ChangeData

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

the class TestSubmitType method apply.

@Override
public Response<SubmitType> apply(RevisionResource rsrc, TestSubmitRuleInput input) throws AuthException, 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);
    ChangeData cd = changeDataFactory.create(rsrc.getNotes());
    SubmitTypeRecord rec = prologRule.getSubmitType(cd, PrologOptions.dryRunOptions(input.rule, input.filters == Filters.SKIP));
    if (rec.status != SubmitTypeRecord.Status.OK) {
        throw new BadRequestException(String.format("rule produced invalid result: %s", rec));
    }
    return Response.ok(rec.type);
}
Also used : TestSubmitRuleInput(com.google.gerrit.extensions.common.TestSubmitRuleInput) SubmitTypeRecord(com.google.gerrit.entities.SubmitTypeRecord) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 57 with ChangeData

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

the class AbstractPushForReview method testPushAFewChanges.

private void testPushAFewChanges() throws Exception {
    int n = 10;
    String r = "refs/for/master";
    ObjectId initialHead = testRepo.getRepository().resolve("HEAD");
    List<RevCommit> commits = createChanges(n, r);
    // Check that a change was created for each.
    for (RevCommit c : commits) {
        assertWithMessage("change for " + c.name()).that(byCommit(c).change().getSubject()).isEqualTo(c.getShortMessage());
    }
    List<RevCommit> commits2 = amendChanges(initialHead, commits, r);
    // Check that there are correct patch sets.
    for (int i = 0; i < n; i++) {
        RevCommit c = commits.get(i);
        RevCommit c2 = commits2.get(i);
        String name = "change for " + c2.name();
        ChangeData cd = byCommit(c);
        assertWithMessage(name).that(cd.change().getSubject()).isEqualTo(c2.getShortMessage());
        assertWithMessage(name).that(getPatchSetRevisions(cd)).containsExactlyEntriesIn(ImmutableMap.of(1, c.name(), 2, c2.name()));
    }
    // Pushing again results in "no new changes".
    assertPushRejected(pushHead(testRepo, r, false), r, "no new changes");
}
Also used : AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 58 with ChangeData

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

the class AbstractPushForReview method pushWithReviewerInFooter.

private void pushWithReviewerInFooter(String nameEmail, TestAccount expectedReviewer) throws Exception {
    int n = 5;
    String r = "refs/for/master";
    ObjectId initialHead = testRepo.getRepository().resolve("HEAD");
    List<RevCommit> commits = createChanges(n, r, ImmutableList.of("Acked-By: " + nameEmail));
    for (int i = 0; i < n; i++) {
        RevCommit c = commits.get(i);
        ChangeData cd = byCommit(c);
        String name = "reviewers for " + (i + 1);
        if (expectedReviewer != null) {
            assertWithMessage(name).that(cd.reviewers().all()).containsExactly(expectedReviewer.id());
            // Remove reviewer from PS1 so we can test adding this same reviewer on PS2 below.
            gApi.changes().id(cd.getId().get()).reviewer(expectedReviewer.id().toString()).remove();
        }
        assertWithMessage(name).that(byCommit(c).reviewers().all()).isEmpty();
    }
    List<RevCommit> commits2 = amendChanges(initialHead, commits, r);
    for (int i = 0; i < n; i++) {
        RevCommit c = commits2.get(i);
        ChangeData cd = byCommit(c);
        String name = "reviewers for " + (i + 1);
        if (expectedReviewer != null) {
            assertWithMessage(name).that(cd.reviewers().all()).containsExactly(expectedReviewer.id());
        } else {
            assertWithMessage(name).that(byCommit(c).reviewers().all()).isEmpty();
        }
    }
}
Also used : AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 59 with ChangeData

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

the class LazyDataSourceTest method orSourceIsLazy.

@Test
public void orSourceIsLazy() {
    OrSource or = new OrSource(ImmutableList.of(new LazyPredicate()));
    ResultSet<ChangeData> resultSet = or.read();
    assertThrows(AssertionError.class, () -> resultSet.toList());
}
Also used : OrSource(com.google.gerrit.server.query.change.OrSource) ChangeData(com.google.gerrit.server.query.change.ChangeData) Test(org.junit.Test)

Example 60 with ChangeData

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

the class RefVisibilityControl method isVisible.

/**
 * Returns an authoritative answer if the ref is visible to the user. Does not have support for
 * tags and will throw a {@link PermissionBackendException} if asked for tags visibility.
 */
boolean isVisible(ProjectControl projectControl, String refName) throws PermissionBackendException {
    if (refName.startsWith(Constants.R_TAGS)) {
        throw new PermissionBackendException("can't check tags through RefVisibilityControl. Use PermissionBackend#filter instead.");
    }
    if (!RefNames.isGerritRef(refName)) {
        // refs/heads or another ref the user created. Apply the regular permissions with inheritance.
        return projectControl.controlForRef(refName).hasReadPermissionOnRef(false);
    }
    if (refName.startsWith(REFS_CACHE_AUTOMERGE)) {
        // Internal cache state that is accessible to no one.
        return false;
    }
    boolean hasAccessDatabase = permissionBackend.user(projectControl.getUser()).testOrFalse(GlobalPermission.ACCESS_DATABASE);
    if (hasAccessDatabase) {
        return true;
    }
    // Change and change edit visibility
    Change.Id changeId;
    if ((changeId = Change.Id.fromRef(refName)) != null) {
        // Change ref is visible only if the change is visible.
        ChangeData cd;
        try {
            cd = changeDataFactory.create(projectControl.getProject().getNameKey(), changeId);
            checkState(cd.change().getId().equals(changeId));
        } catch (StorageException e) {
            if (Throwables.getCausalChain(e).stream().anyMatch(e2 -> e2 instanceof NoSuchChangeException)) {
                // The change was deleted or is otherwise not accessible anymore.
                // If the caller can see all refs and is allowed to see private changes on refs/, allow
                // access. This is an escape hatch for receivers of "ref deleted" events.
                PermissionBackend.ForProject forProject = projectControl.asForProject();
                return forProject.test(ProjectPermission.READ) && forProject.ref("refs/").test(RefPermission.READ_PRIVATE_CHANGES);
            }
            throw new PermissionBackendException(e);
        }
        if (RefNames.isRefsEdit(refName)) {
            // Edits are visible only to the owning user, if change is visible.
            return visibleEdit(refName, projectControl, cd);
        }
        return projectControl.controlFor(cd).isVisible();
    }
    // Account visibility
    CurrentUser user = projectControl.getUser();
    Account.Id currentUserAccountId = user.isIdentifiedUser() ? user.getAccountId() : null;
    Account.Id accountId;
    if ((accountId = Account.Id.fromRef(refName)) != null) {
        // Account ref is visible only to the corresponding account.
        if (accountId.equals(currentUserAccountId)) {
            // refs, check if the user has read permissions.
            if (RefNames.isRefsDraftsComments(refName) || RefNames.isRefsStarredChanges(refName) || projectControl.controlForRef(refName).hasReadPermissionOnRef(true)) {
                return true;
            }
        }
        return false;
    }
    // Group visibility
    AccountGroup.UUID accountGroupUuid;
    if ((accountGroupUuid = AccountGroup.UUID.fromRef(refName)) != null) {
        // Group ref is visible only to the corresponding owner group.
        try {
            return projectControl.controlForRef(refName).hasReadPermissionOnRef(true) && groupControlFactory.controlFor(user, accountGroupUuid).isOwner();
        } catch (NoSuchGroupException e) {
            // The group is broken, but the ref is still around. Pretend the ref is not visible.
            logger.atWarning().withCause(e).log("Found group ref %s but group isn't parsable", refName);
            return false;
        }
    }
    // We are done checking all cases where we would allow access to Gerrit-managed refs. Deny
    // access in case we got this far.
    logger.atFine().log("Denying access to %s because user doesn't have access to this Gerrit ref", refName);
    return false;
}
Also used : AccountGroup(com.google.gerrit.entities.AccountGroup) CurrentUser(com.google.gerrit.server.CurrentUser) GroupControl(com.google.gerrit.server.account.GroupControl) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) StorageException(com.google.gerrit.exceptions.StorageException) Throwables(com.google.common.base.Throwables) Account(com.google.gerrit.entities.Account) Constants(org.eclipse.jgit.lib.Constants) Singleton(javax.inject.Singleton) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Inject(javax.inject.Inject) ChangeData(com.google.gerrit.server.query.change.ChangeData) REFS_CACHE_AUTOMERGE(com.google.gerrit.entities.RefNames.REFS_CACHE_AUTOMERGE) RefNames(com.google.gerrit.entities.RefNames) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) Change(com.google.gerrit.entities.Change) FluentLogger(com.google.common.flogger.FluentLogger) Account(com.google.gerrit.entities.Account) CurrentUser(com.google.gerrit.server.CurrentUser) Change(com.google.gerrit.entities.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) AccountGroup(com.google.gerrit.entities.AccountGroup) StorageException(com.google.gerrit.exceptions.StorageException)

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