Search in sources :

Example 11 with Nullable

use of com.google.gerrit.common.Nullable in project gerrit by GerritCodeReview.

the class ReviewerModifier method addWholeGroup.

@Nullable
private ReviewerModification addWholeGroup(ReviewerInput input, ChangeNotes notes, CurrentUser user, boolean confirmed, boolean allowGroup, boolean allowByEmail) throws IOException, PermissionBackendException {
    if (!allowGroup) {
        return null;
    }
    GroupDescription.Basic group;
    try {
        // TODO(dborowitz): This currently doesn't work in the push path because InternalGroupBackend
        // depends on the Provider<CurrentUser> which returns anonymous in that path.
        group = groupResolver.parseInternal(input.reviewer);
    } catch (UnprocessableEntityException e) {
        if (!allowByEmail) {
            return fail(input, FailureType.NOT_FOUND, MessageFormat.format(ChangeMessages.get().reviewerNotFoundUserOrGroup, input.reviewer));
        }
        return null;
    }
    if (!isLegalReviewerGroup(group.getGroupUUID())) {
        return fail(input, FailureType.OTHER, MessageFormat.format(ChangeMessages.get().groupIsNotAllowed, group.getName()));
    }
    if (input.state().equals(REMOVED)) {
        return fail(input, FailureType.OTHER, MessageFormat.format(ChangeMessages.get().groupRemovalIsNotAllowed, group.getName()));
    }
    Set<Account> reviewers = new HashSet<>();
    Set<Account> members;
    try {
        members = groupMembers.listAccounts(group.getGroupUUID(), notes.getProjectName());
    } catch (NoSuchProjectException e) {
        return fail(input, FailureType.OTHER, e.getMessage());
    }
    // if maxAllowed is set to 0, it is allowed to add any number of
    // reviewers
    int maxAllowed = cfg.getInt("addreviewer", "maxAllowed", DEFAULT_MAX_REVIEWERS);
    if (maxAllowed > 0 && members.size() > maxAllowed) {
        logger.atFine().log("Adding %d group members is not allowed (maxAllowed = %d)", members.size(), maxAllowed);
        return fail(input, FailureType.OTHER, MessageFormat.format(ChangeMessages.get().groupHasTooManyMembers, group.getName()));
    }
    // if maxWithoutCheck is set to 0, we never ask for confirmation
    int maxWithoutConfirmation = cfg.getInt("addreviewer", "maxWithoutConfirmation", DEFAULT_MAX_REVIEWERS_WITHOUT_CHECK);
    if (!confirmed && maxWithoutConfirmation > 0 && members.size() > maxWithoutConfirmation) {
        logger.atFine().log("Adding %d group members as reviewer requires confirmation (maxWithoutConfirmation = %d)", members.size(), maxWithoutConfirmation);
        return fail(input, FailureType.OTHER, true, MessageFormat.format(ChangeMessages.get().groupManyMembersConfirmation, group.getName(), members.size()));
    }
    for (Account member : members) {
        if (isValidReviewer(notes.getChange().getDest(), member)) {
            reviewers.add(member);
        }
    }
    return new ReviewerModification(input, notes, user, reviewers, null, true, true);
}
Also used : GroupDescription(com.google.gerrit.entities.GroupDescription) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Account(com.google.gerrit.entities.Account) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) HashSet(java.util.HashSet) Nullable(com.google.gerrit.common.Nullable)

Example 12 with Nullable

use of com.google.gerrit.common.Nullable in project gerrit by GerritCodeReview.

the class PostReviewers method addByAccountId.

@Nullable
private Addition addByAccountId(String reviewer, ChangeResource rsrc, ReviewerState state, NotifyHandling notify, ListMultimap<RecipientType, Account.Id> accountsToNotify, boolean allowGroup, boolean allowByEmail) throws OrmException, PermissionBackendException {
    Account.Id accountId = null;
    try {
        accountId = accounts.parse(reviewer).getAccountId();
    } catch (UnprocessableEntityException | AuthException e) {
        // AuthException won't occur since the user is authenticated at this point.
        if (!allowGroup && !allowByEmail) {
            // Only return failure if we aren't going to try other interpretations.
            return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerNotFoundUser, reviewer));
        }
        return null;
    }
    ReviewerResource rrsrc = reviewerFactory.create(rsrc, accountId);
    Account member = rrsrc.getReviewerUser().getAccount();
    PermissionBackend.ForRef perm = permissionBackend.user(rrsrc.getReviewerUser()).ref(rrsrc.getChange().getDest());
    if (isValidReviewer(member, perm)) {
        return new Addition(reviewer, rsrc, ImmutableSet.of(member.getId()), null, state, notify, accountsToNotify);
    }
    if (!member.isActive()) {
        if (allowByEmail && state == CC) {
            return null;
        }
        return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerInactive, reviewer));
    }
    return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerCantSeeChange, reviewer));
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) AuthException(com.google.gerrit.extensions.restapi.AuthException) Nullable(com.google.gerrit.common.Nullable)

Example 13 with Nullable

use of com.google.gerrit.common.Nullable in project gerrit by GerritCodeReview.

the class RangeUtil method getRange.

/**
   * Determine the range of values being requested in the given query.
   *
   * @param rangeQuery the raw query, e.g. "{@code added:>12345}"
   * @param minValue the minimum possible value for the field, inclusive
   * @param maxValue the maximum possible value for the field, inclusive
   * @return the calculated {@link Range}, or null if the query is invalid
   */
@Nullable
public static Range getRange(String rangeQuery, int minValue, int maxValue) {
    Matcher m = RANGE_PATTERN.matcher(rangeQuery);
    String prefix;
    String test;
    Integer queryInt;
    if (m.find()) {
        prefix = rangeQuery.substring(0, m.start());
        test = m.group(1);
        queryInt = value(m.group(2));
        if (queryInt == null) {
            return null;
        }
    } else {
        return null;
    }
    return getRange(prefix, test, queryInt, minValue, maxValue);
}
Also used : Matcher(java.util.regex.Matcher) Nullable(com.google.gerrit.common.Nullable)

Example 14 with Nullable

use of com.google.gerrit.common.Nullable in project gerrit by GerritCodeReview.

the class AccountLoader method fillOne.

@Nullable
public AccountInfo fillOne(@Nullable Account.Id id) throws PermissionBackendException {
    AccountInfo info = get(id);
    fill();
    return info;
}
Also used : AccountInfo(com.google.gerrit.extensions.common.AccountInfo) Nullable(com.google.gerrit.common.Nullable)

Example 15 with Nullable

use of com.google.gerrit.common.Nullable in project gerrit by GerritCodeReview.

the class ReviewerModifier method byAccountId.

@Nullable
private ReviewerModification byAccountId(ReviewerInput input, ChangeNotes notes, CurrentUser user) throws PermissionBackendException, IOException, ConfigInvalidException {
    IdentifiedUser reviewerUser;
    boolean exactMatchFound = false;
    try {
        reviewerUser = accountResolver.resolveIncludeInactive(input.reviewer).asUniqueUser();
        if (input.reviewer.equalsIgnoreCase(reviewerUser.getName()) || input.reviewer.equals(String.valueOf(reviewerUser.getAccountId()))) {
            exactMatchFound = true;
        }
    } catch (UnprocessableEntityException e) {
        // group, but if not, the error message will be useful.
        return fail(input, FailureType.NOT_FOUND, e.getMessage());
    }
    if (isValidReviewer(notes.getChange().getDest(), reviewerUser.getAccount())) {
        return new ReviewerModification(input, notes, user, ImmutableSet.of(reviewerUser.getAccount()), null, exactMatchFound, false);
    }
    return fail(input, FailureType.OTHER, MessageFormat.format(ChangeMessages.get().reviewerCantSeeChange, input.reviewer));
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) Nullable(com.google.gerrit.common.Nullable)

Aggregations

Nullable (com.google.gerrit.common.Nullable)20 IOException (java.io.IOException)6 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)5 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)5 HashSet (java.util.HashSet)4 Ref (org.eclipse.jgit.lib.Ref)4 Account (com.google.gerrit.entities.Account)3 PatchSet (com.google.gerrit.entities.PatchSet)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Change (com.google.gerrit.entities.Change)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)2 Timer0 (com.google.gerrit.metrics.Timer0)2 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)2 NoSuchProjectException (com.google.gerrit.server.project.NoSuchProjectException)2 Inject (com.google.inject.Inject)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 Repository (org.eclipse.jgit.lib.Repository)2 AutoValue (com.google.auto.value.AutoValue)1