Search in sources :

Example 11 with RestApiException

use of com.google.gerrit.extensions.restapi.RestApiException in project gerrit by GerritCodeReview.

the class AccountsImpl method query.

private List<AccountInfo> query(QueryRequest r) throws RestApiException {
    try {
        QueryAccounts myQueryAccounts = queryAccountsProvider.get();
        myQueryAccounts.setQuery(r.getQuery());
        myQueryAccounts.setLimit(r.getLimit());
        myQueryAccounts.setStart(r.getStart());
        for (ListAccountsOption option : r.getOptions()) {
            myQueryAccounts.addOption(option);
        }
        return myQueryAccounts.apply(TopLevelResource.INSTANCE);
    } catch (Exception e) {
        throw asRestApiException("Cannot retrieve suggested accounts", e);
    }
}
Also used : QueryAccounts(com.google.gerrit.server.account.QueryAccounts) ListAccountsOption(com.google.gerrit.extensions.client.ListAccountsOption) ApiUtil.asRestApiException(com.google.gerrit.server.api.ApiUtil.asRestApiException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 12 with RestApiException

use of com.google.gerrit.extensions.restapi.RestApiException in project gerrit by GerritCodeReview.

the class AccountsImpl method create.

@Override
public AccountApi create(AccountInput in) throws RestApiException {
    if (checkNotNull(in, "AccountInput").username == null) {
        throw new BadRequestException("AccountInput must specify username");
    }
    try {
        CreateAccount impl = createAccount.create(in.username);
        permissionBackend.user(self).checkAny(GlobalPermission.fromAnnotation(impl.getClass()));
        AccountInfo info = impl.apply(TopLevelResource.INSTANCE, in).value();
        return id(info._accountId);
    } catch (Exception e) {
        throw asRestApiException("Cannot create account " + in.username, e);
    }
}
Also used : CreateAccount(com.google.gerrit.server.account.CreateAccount) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AccountInfo(com.google.gerrit.extensions.common.AccountInfo) ApiUtil.asRestApiException(com.google.gerrit.server.api.ApiUtil.asRestApiException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 13 with RestApiException

use of com.google.gerrit.extensions.restapi.RestApiException in project gerrit by GerritCodeReview.

the class DeleteExternalIds method apply.

@Override
public Response<?> apply(AccountResource resource, List<String> extIds) throws RestApiException, IOException, OrmException, ConfigInvalidException {
    if (self.get() != resource.getUser() && !self.get().getCapabilities().canAccessDatabase()) {
        throw new AuthException("not allowed to delete external IDs");
    }
    if (extIds == null || extIds.size() == 0) {
        throw new BadRequestException("external IDs are required");
    }
    Map<ExternalId.Key, ExternalId> externalIdMap = externalIds.byAccount(resource.getUser().getAccountId()).stream().collect(toMap(i -> i.key(), i -> i));
    List<ExternalId> toDelete = new ArrayList<>();
    ExternalId.Key last = resource.getUser().getLastLoginExternalIdKey();
    for (String externalIdStr : extIds) {
        ExternalId id = externalIdMap.get(ExternalId.Key.parse(externalIdStr));
        if (id == null) {
            throw new UnprocessableEntityException(String.format("External id %s does not exist", externalIdStr));
        }
        if ((!id.isScheme(SCHEME_USERNAME)) && ((last == null) || (!last.get().equals(id.key().get())))) {
            toDelete.add(id);
        } else {
            throw new ResourceConflictException(String.format("External id %s cannot be deleted", externalIdStr));
        }
    }
    try {
        for (ExternalId extId : toDelete) {
            AuthRequest authRequest = new AuthRequest(extId.key());
            authRequest.setEmailAddress(extId.email());
            accountManager.unlink(extId.accountId(), authRequest);
        }
    } catch (AccountException e) {
        throw new ResourceConflictException(e.getMessage());
    }
    return Response.none();
}
Also used : CurrentUser(com.google.gerrit.server.CurrentUser) OrmException(com.google.gwtorm.server.OrmException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Inject(com.google.inject.Inject) IOException(java.io.IOException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Response(com.google.gerrit.extensions.restapi.Response) ExternalIds(com.google.gerrit.server.account.externalids.ExternalIds) ArrayList(java.util.ArrayList) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) Provider(com.google.inject.Provider) List(java.util.List) Collectors.toMap(java.util.stream.Collectors.toMap) SCHEME_USERNAME(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Map(java.util.Map) AuthException(com.google.gerrit.extensions.restapi.AuthException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Example 14 with RestApiException

use of com.google.gerrit.extensions.restapi.RestApiException in project gerrit by GerritCodeReview.

the class SubmoduleOp method updateSuperProjects.

public void updateSuperProjects(BatchUpdate.Factory updateFactory) throws SubmoduleException {
    ImmutableSet<Project.NameKey> projects = getProjectsInOrder();
    if (projects == null) {
        return;
    }
    LinkedHashSet<Project.NameKey> superProjects = new LinkedHashSet<>();
    try {
        for (Project.NameKey project : projects) {
            // only need superprojects
            if (branchesByProject.containsKey(project)) {
                superProjects.add(project);
                // get a new BatchUpdate for the super project
                OpenRepo or = orm.getRepo(project);
                for (Branch.NameKey branch : branchesByProject.get(project)) {
                    addOp(or.getUpdate(updateFactory), branch);
                }
            }
        }
        batchUpdateFactory.execute(orm.batchUpdates(updateFactory, superProjects), BatchUpdateListener.NONE, orm.getSubmissionId(), false);
    } catch (RestApiException | UpdateException | IOException | NoSuchProjectException e) {
        throw new SubmoduleException("Cannot update gitlinks", e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) IOException(java.io.IOException) Project(com.google.gerrit.reviewdb.client.Project) Branch(com.google.gerrit.reviewdb.client.Branch) OpenRepo(com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo) UpdateException(com.google.gerrit.server.update.UpdateException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 15 with RestApiException

use of com.google.gerrit.extensions.restapi.RestApiException in project gerrit by GerritCodeReview.

the class MergeOp method abandonAllOpenChangeForDeletedProject.

private void abandonAllOpenChangeForDeletedProject(Project.NameKey destProject) {
    try {
        for (ChangeData cd : internalChangeQuery.byProjectOpen(destProject)) {
            try (BatchUpdate bu = batchUpdateFactory.create(db, destProject, internalUserFactory.create(), ts)) {
                bu.setRequestId(submissionId);
                bu.addOp(cd.getId(), new BatchUpdateOp() {

                    @Override
                    public boolean updateChange(ChangeContext ctx) throws OrmException {
                        Change change = ctx.getChange();
                        if (!change.getStatus().isOpen()) {
                            return false;
                        }
                        change.setStatus(Change.Status.ABANDONED);
                        ChangeMessage msg = ChangeMessagesUtil.newMessage(change.currentPatchSetId(), internalUserFactory.create(), change.getLastUpdatedOn(), ChangeMessagesUtil.TAG_MERGED, "Project was deleted.");
                        cmUtil.addChangeMessage(ctx.getDb(), ctx.getUpdate(change.currentPatchSetId()), msg);
                        return true;
                    }
                });
                try {
                    bu.execute();
                } catch (UpdateException | RestApiException e) {
                    logWarn("Cannot abandon changes for deleted project " + destProject, e);
                }
            }
        }
    } catch (OrmException e) {
        logWarn("Cannot abandon changes for deleted project " + destProject, e);
    }
}
Also used : ChangeContext(com.google.gerrit.server.update.ChangeContext) OrmException(com.google.gwtorm.server.OrmException) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) Change(com.google.gerrit.reviewdb.client.Change) UpdateException(com.google.gerrit.server.update.UpdateException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeData(com.google.gerrit.server.query.change.ChangeData) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Aggregations

RestApiException (com.google.gerrit.extensions.restapi.RestApiException)50 ApiUtil.asRestApiException (com.google.gerrit.server.api.ApiUtil.asRestApiException)16 OrmException (com.google.gwtorm.server.OrmException)14 IOException (java.io.IOException)14 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)12 UpdateException (com.google.gerrit.server.update.UpdateException)12 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)10 Change (com.google.gerrit.reviewdb.client.Change)8 AuthException (com.google.gerrit.extensions.restapi.AuthException)7 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)7 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)7 BatchUpdateOp (com.google.gerrit.server.update.BatchUpdateOp)6 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)5 ArrayList (java.util.ArrayList)5 RevWalk (org.eclipse.jgit.revwalk.RevWalk)5 Test (org.junit.Test)5 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)4 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)4 ChangeContext (com.google.gerrit.server.update.ChangeContext)4 Provider (com.google.inject.Provider)4