Search in sources :

Example 1 with Change

use of com.google.gerrit.entities.Change in project gerrit by GerritCodeReview.

the class CatServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
    String keyStr = req.getPathInfo();
    // We shouldn't have to do this extra decode pass, but somehow we
    // are now receiving our "^1" suffix as "%5E1", which confuses us
    // downstream. Other times we get our embedded "," as "%2C", which
    // is equally bad. And yet when these happen a "%2F" is left as-is,
    // rather than escaped as "%252F", which makes me feel really really
    // uncomfortable with a blind decode right here.
    // 
    keyStr = Url.decode(keyStr);
    if (!keyStr.startsWith("/")) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    keyStr = keyStr.substring(1);
    final Patch.Key patchKey;
    final int side;
    {
        final int c = keyStr.lastIndexOf('^');
        if (c == 0) {
            rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        if (c < 0) {
            side = 0;
        } else {
            try {
                side = Integer.parseInt(keyStr.substring(c + 1));
                keyStr = keyStr.substring(0, c);
            } catch (NumberFormatException e) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
        }
        try {
            patchKey = Patch.Key.parse(keyStr);
        } catch (NumberFormatException e) {
            rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
    }
    final Change.Id changeId = patchKey.patchSetId().changeId();
    String revision;
    try {
        ChangeNotes notes = changeNotesFactory.createCheckedUsingIndexLookup(changeId);
        permissionBackend.currentUser().change(notes).check(ChangePermission.READ);
        projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName())).checkStatePermitsRead();
        if (patchKey.patchSetId().get() == 0) {
            // change edit
            Optional<ChangeEdit> edit = changeEditUtil.byChange(notes);
            if (edit.isPresent()) {
                revision = ObjectId.toString(edit.get().getEditCommit());
            } else {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
        } else {
            PatchSet patchSet = psUtil.get(notes, patchKey.patchSetId());
            if (patchSet == null) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
            revision = patchSet.commitId().name();
        }
    } catch (ResourceConflictException | NoSuchChangeException | AuthException e) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    } catch (PermissionBackendException | IOException e) {
        getServletContext().log("Cannot query database", e);
        rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    String path = patchKey.fileName();
    String restUrl = String.format("%s/changes/%d/revisions/%s/files/%s/download?parent=%d", req.getContextPath(), changeId.get(), revision, Url.encode(path), side);
    rsp.sendRedirect(restUrl);
}
Also used : ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) AuthException(com.google.gerrit.extensions.restapi.AuthException) PatchSet(com.google.gerrit.entities.PatchSet) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) Change(com.google.gerrit.entities.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) Patch(com.google.gerrit.entities.Patch)

Example 2 with Change

use of com.google.gerrit.entities.Change in project gerrit by GerritCodeReview.

the class StarredChangesUtil method byAccountId.

public ImmutableSet<Change.Id> byAccountId(Account.Id accountId, String label) {
    try (Repository repo = repoManager.openRepository(allUsers)) {
        ImmutableSet.Builder<Change.Id> builder = ImmutableSet.builder();
        for (Ref ref : repo.getRefDatabase().getRefsByPrefix(RefNames.REFS_STARRED_CHANGES)) {
            Account.Id currentAccountId = Account.Id.fromRef(ref.getName());
            // Skip all refs that don't correspond with accountId.
            if (currentAccountId == null || !currentAccountId.equals(accountId)) {
                continue;
            }
            // Skip all refs that don't contain the required label.
            StarRef starRef = readLabels(repo, ref.getName());
            if (!starRef.labels().contains(label)) {
                continue;
            }
            // Skip invalid change ids.
            Change.Id changeId = Change.Id.fromAllUsersRef(ref.getName());
            if (changeId == null) {
                continue;
            }
            builder.add(changeId);
        }
        return builder.build();
    } catch (IOException e) {
        throw new StorageException(String.format("Get starred changes for account %d failed", accountId.get()), e);
    }
}
Also used : Account(com.google.gerrit.entities.Account) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ImmutableSet(com.google.common.collect.ImmutableSet) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.entities.Change) IOException(java.io.IOException) StorageException(com.google.gerrit.exceptions.StorageException)

Example 3 with Change

use of com.google.gerrit.entities.Change in project gerrit by GerritCodeReview.

the class PatchSetUtil method isPatchSetLocked.

/**
 * Is the current patch set locked against state changes?
 */
public boolean isPatchSetLocked(ChangeNotes notes) {
    Change change = notes.getChange();
    if (change.isMerged()) {
        return false;
    }
    ProjectState projectState = projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName()));
    ApprovalsUtil approvalsUtil = approvalsUtilProvider.get();
    for (PatchSetApproval ap : approvalsUtil.byPatchSet(notes, change.currentPatchSetId())) {
        Optional<LabelType> type = projectState.getLabelTypes(notes).byLabel(ap.label());
        if (type.isPresent() && ap.value() == 1 && type.get().getFunction() == LabelFunction.PATCH_SET_LOCK) {
            return true;
        }
    }
    return false;
}
Also used : LabelType(com.google.gerrit.entities.LabelType) ApprovalsUtil(com.google.gerrit.server.approval.ApprovalsUtil) ProjectState(com.google.gerrit.server.project.ProjectState) Change(com.google.gerrit.entities.Change) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 4 with Change

use of com.google.gerrit.entities.Change 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 5 with Change

use of com.google.gerrit.entities.Change in project gerrit by GerritCodeReview.

the class AddReviewersOp method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws RestApiException, IOException {
    change = ctx.getChange();
    if (!accountIds.isEmpty()) {
        if (state == CC) {
            addedCCs = approvalsUtil.addCcs(ctx.getNotes(), ctx.getUpdate(change.currentPatchSetId()), accountIds, forGroup);
        } else {
            addedReviewers = approvalsUtil.addReviewers(ctx.getNotes(), ctx.getUpdate(change.currentPatchSetId()), projectCache.get(change.getProject()).orElseThrow(illegalState(change.getProject())).getLabelTypes(change.getDest()), change, accountIds);
        }
    }
    ReviewerStateInternal internalState = ReviewerStateInternal.fromReviewerState(state);
    // TODO(dborowitz): This behavior should live in ApprovalsUtil or something, like addCcs does.
    ImmutableSet<Address> existing = ctx.getNotes().getReviewersByEmail().byState(internalState);
    ImmutableList<Address> addressesToAdd = addresses.stream().filter(a -> !existing.contains(a)).collect(toImmutableList());
    if (state == CC) {
        addedCCsByEmail = addressesToAdd;
    } else {
        addedReviewersByEmail = addressesToAdd;
    }
    for (Address a : addressesToAdd) {
        ctx.getUpdate(change.currentPatchSetId()).putReviewerByEmail(a, internalState);
    }
    if (addedCCs.isEmpty() && addedReviewers.isEmpty() && addressesToAdd.isEmpty()) {
        return false;
    }
    checkAdded();
    if (patchSet == null) {
        patchSet = requireNonNull(psUtil.current(ctx.getNotes()));
    }
    return true;
}
Also used : AccountCache(com.google.gerrit.server.account.AccountCache) CC(com.google.gerrit.extensions.client.ReviewerState.CC) ProjectCache(com.google.gerrit.server.project.ProjectCache) Inject(com.google.inject.Inject) REVIEWER(com.google.gerrit.extensions.client.ReviewerState.REVIEWER) ReviewerState(com.google.gerrit.extensions.client.ReviewerState) Assisted(com.google.inject.assistedinject.Assisted) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) PostUpdateContext(com.google.gerrit.server.update.PostUpdateContext) Objects.requireNonNull(java.util.Objects.requireNonNull) Change(com.google.gerrit.entities.Change) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) Address(com.google.gerrit.entities.Address) ChangeContext(com.google.gerrit.server.update.ChangeContext) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) ImmutableSet(com.google.common.collect.ImmutableSet) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Collection(java.util.Collection) ReviewerAdded(com.google.gerrit.server.extensions.events.ReviewerAdded) Account(com.google.gerrit.entities.Account) Set(java.util.Set) IOException(java.io.IOException) Streams(com.google.common.collect.Streams) ApprovalsUtil(com.google.gerrit.server.approval.ApprovalsUtil) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) AccountState(com.google.gerrit.server.account.AccountState) PatchSetUtil(com.google.gerrit.server.PatchSetUtil) Address(com.google.gerrit.entities.Address) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal)

Aggregations

Change (com.google.gerrit.entities.Change)659 Test (org.junit.Test)510 PatchSet (com.google.gerrit.entities.PatchSet)167 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)165 ObjectId (org.eclipse.jgit.lib.ObjectId)113 Repo (com.google.gerrit.testing.InMemoryRepositoryManager.Repo)112 RevCommit (org.eclipse.jgit.revwalk.RevCommit)88 Account (com.google.gerrit.entities.Account)83 CommentInfo (com.google.gerrit.extensions.common.CommentInfo)72 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)66 List (java.util.List)64 ImmutableList (com.google.common.collect.ImmutableList)63 ChangeData (com.google.gerrit.server.query.change.ChangeData)60 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)58 PatchSetApproval (com.google.gerrit.entities.PatchSetApproval)56 Instant (java.time.Instant)54 Project (com.google.gerrit.entities.Project)52 StorageException (com.google.gerrit.exceptions.StorageException)50 Ref (org.eclipse.jgit.lib.Ref)49 HumanComment (com.google.gerrit.entities.HumanComment)46