Search in sources :

Example 16 with Nullable

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

the class MessageIdGenerator method fromChangeUpdateAndReason.

public MessageId fromChangeUpdateAndReason(RepoView repoView, PatchSet.Id patchsetId, @Nullable String reason) {
    String suffix = (reason != null) ? ("-" + reason) : "";
    String metaRef = patchsetId.changeId().toRefPrefix() + "meta";
    Optional<ObjectId> metaSha1;
    try {
        metaSha1 = repoView.getRef(metaRef);
    } catch (IOException ex) {
        throw new StorageException("unable to extract info for Message-Id", ex);
    }
    return metaSha1.map(optional -> new AutoValue_MessageIdGenerator_MessageId(optional.getName() + suffix)).orElseThrow(() -> new IllegalStateException(metaRef + " doesn't exist"));
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) MailMessage(com.google.gerrit.mail.MailMessage) StorageException(com.google.gerrit.exceptions.StorageException) Inject(com.google.inject.Inject) Account(com.google.gerrit.entities.Account) IOException(java.io.IOException) Instant(java.time.Instant) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ObjectId(org.eclipse.jgit.lib.ObjectId) Nullable(com.google.gerrit.common.Nullable) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) Ref(org.eclipse.jgit.lib.Ref) AutoValue(com.google.auto.value.AutoValue) Project(com.google.gerrit.entities.Project) RefNames(com.google.gerrit.entities.RefNames) Optional(java.util.Optional) PatchSet(com.google.gerrit.entities.PatchSet) RepoView(com.google.gerrit.server.update.RepoView) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) StorageException(com.google.gerrit.exceptions.StorageException)

Example 17 with Nullable

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

the class CreateChange method getParentCommit.

@Nullable
private ObjectId getParentCommit(Repository repo, RevWalk revWalk, String inputBranch, @Nullable Boolean newBranch, @Nullable PatchSet basePatchSet, @Nullable String baseCommit, @Nullable MergeInput mergeInput) throws BadRequestException, IOException, UnprocessableEntityException, ResourceConflictException {
    if (basePatchSet != null) {
        return basePatchSet.commitId();
    }
    Ref destRef = repo.getRefDatabase().exactRef(inputBranch);
    ObjectId parentCommit;
    if (baseCommit != null) {
        try {
            parentCommit = ObjectId.fromString(baseCommit);
        } catch (InvalidObjectIdException e) {
            throw new UnprocessableEntityException(String.format("Base %s doesn't represent a valid SHA-1", baseCommit), e);
        }
        RevCommit parentRevCommit;
        try {
            parentRevCommit = revWalk.parseCommit(parentCommit);
        } catch (MissingObjectException e) {
            throw new UnprocessableEntityException(String.format("Base %s doesn't exist", baseCommit), e);
        }
        if (destRef == null) {
            throw new BadRequestException("Destination branch does not exist");
        }
        RevCommit destRefRevCommit = revWalk.parseCommit(destRef.getObjectId());
        if (!revWalk.isMergedInto(parentRevCommit, destRefRevCommit)) {
            throw new BadRequestException(String.format("Commit %s doesn't exist on ref %s", baseCommit, inputBranch));
        }
    } else {
        if (destRef != null) {
            if (Boolean.TRUE.equals(newBranch)) {
                throw new ResourceConflictException(String.format("Branch %s already exists.", inputBranch));
            }
            parentCommit = destRef.getObjectId();
        } else {
            if (Boolean.TRUE.equals(newBranch)) {
                if (mergeInput != null) {
                    throw new BadRequestException("Cannot create merge: destination branch does not exist");
                }
                parentCommit = null;
            } else {
                throw new BadRequestException("Destination branch does not exist");
            }
        }
    }
    return parentCommit;
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Ref(org.eclipse.jgit.lib.Ref) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectId(org.eclipse.jgit.lib.ObjectId) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) InvalidObjectIdException(org.eclipse.jgit.errors.InvalidObjectIdException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Nullable(com.google.gerrit.common.Nullable)

Example 18 with Nullable

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

the class PutDisplayName method apply.

@Override
public Response<String> apply(AccountResource rsrc, @Nullable DisplayNameInput input) throws AuthException, ResourceNotFoundException, IOException, PermissionBackendException, ConfigInvalidException {
    IdentifiedUser user = rsrc.getUser();
    if (!self.get().hasSameAccountId(user)) {
        permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
    }
    if (input == null) {
        input = new DisplayNameInput();
    }
    String newDisplayName = input.displayName;
    AccountState accountState = accountsUpdateProvider.get().update("Set Display Name via API", user.getAccountId(), u -> u.setDisplayName(newDisplayName)).orElseThrow(() -> new ResourceNotFoundException("account not found"));
    return Strings.isNullOrEmpty(accountState.account().displayName()) ? Response.none() : Response.ok(accountState.account().displayName());
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) GlobalPermission(com.google.gerrit.server.permissions.GlobalPermission) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) CurrentUser(com.google.gerrit.server.CurrentUser) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Inject(com.google.inject.Inject) AccountResource(com.google.gerrit.server.account.AccountResource) IOException(java.io.IOException) Response(com.google.gerrit.extensions.restapi.Response) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) Strings(com.google.common.base.Strings) Provider(com.google.inject.Provider) Nullable(com.google.gerrit.common.Nullable) DisplayNameInput(com.google.gerrit.extensions.api.accounts.DisplayNameInput) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ServerInitiated(com.google.gerrit.server.ServerInitiated) AuthException(com.google.gerrit.extensions.restapi.AuthException) AccountState(com.google.gerrit.server.account.AccountState) AccountsUpdate(com.google.gerrit.server.account.AccountsUpdate) Singleton(com.google.inject.Singleton) DisplayNameInput(com.google.gerrit.extensions.api.accounts.DisplayNameInput) AccountState(com.google.gerrit.server.account.AccountState) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 19 with Nullable

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

the class CherryPickChange method getDestChangeWithVerification.

/**
 * Returns the change from the destination branch, if it exists and is valid for the cherry-pick.
 *
 * @param destChangeId the Change-ID of the change in the destination branch.
 * @param destBranch the branch to search by the Change-ID.
 * @param verifyIsMissing if {@code true}, verifies that the change should be missing in the
 *     destination branch.
 * @return the verified change or {@code null} if the change was not found.
 * @throws InvalidChangeOperationException if the change was found but failed validation
 */
@Nullable
private ChangeData getDestChangeWithVerification(String destChangeId, BranchNameKey destBranch, boolean verifyIsMissing) throws InvalidChangeOperationException {
    List<ChangeData> destChanges = queryProvider.get().setLimit(2).byBranchKey(destBranch, Change.key(destChangeId));
    if (destChanges.size() > 1) {
        throw new InvalidChangeOperationException("Several changes with key " + destChangeId + " reside on the same branch. " + "Cannot create a new patch set.");
    }
    if (destChanges.size() == 1 && verifyIsMissing) {
        throw new InvalidChangeOperationException(String.format("Expected that cherry-pick with Change-Id %s to branch %s " + "in project %s creates a new change, but found existing change %d", destChangeId, destBranch.branch(), destBranch.project().get(), destChanges.get(0).getId().get()));
    }
    ChangeData destChange = destChanges.size() == 1 ? destChanges.get(0) : null;
    if (destChange != null && destChange.change().isClosed()) {
        throw new InvalidChangeOperationException(String.format("Cherry-pick with Change-Id %s could not update the existing change %d " + "in destination branch %s of project %s, because the change was closed (%s)", destChangeId, destChange.getId().get(), destBranch.branch(), destBranch.project(), destChange.change().getStatus().name()));
    }
    return destChange;
}
Also used : InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) ChangeData(com.google.gerrit.server.query.change.ChangeData) Nullable(com.google.gerrit.common.Nullable)

Example 20 with Nullable

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

the class GroupsOnInit method getPathToAllUsersRepository.

@Nullable
private File getPathToAllUsersRepository() {
    Path basePath = site.resolve(flags.cfg.getString("gerrit", null, "basePath"));
    checkArgument(basePath != null, "gerrit.basePath must be configured");
    return RepositoryCache.FileKey.resolve(basePath.resolve(allUsers.get()).toFile(), FS.DETECTED);
}
Also used : Path(java.nio.file.Path) 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