Search in sources :

Example 51 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class CreateMergePatchSet method applyImpl.

@Override
protected Response<ChangeInfo> applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, MergePatchSetInput in) throws OrmException, IOException, InvalidChangeOperationException, RestApiException, UpdateException, PermissionBackendException {
    rsrc.permissions().database(db).check(ChangePermission.ADD_PATCH_SET);
    MergeInput merge = in.merge;
    if (merge == null || Strings.isNullOrEmpty(merge.source)) {
        throw new BadRequestException("merge.source must be non-empty");
    }
    ChangeControl ctl = rsrc.getControl();
    PatchSet ps = psUtil.current(db.get(), ctl.getNotes());
    ProjectControl projectControl = ctl.getProjectControl();
    Change change = ctl.getChange();
    Project.NameKey project = change.getProject();
    Branch.NameKey dest = change.getDest();
    try (Repository git = gitManager.openRepository(project);
        ObjectInserter oi = git.newObjectInserter();
        ObjectReader reader = oi.newReader();
        RevWalk rw = new RevWalk(reader)) {
        RevCommit sourceCommit = MergeUtil.resolveCommit(git, rw, merge.source);
        if (!projectControl.canReadCommit(db.get(), git, sourceCommit)) {
            throw new ResourceNotFoundException("cannot find source commit: " + merge.source + " to merge.");
        }
        RevCommit currentPsCommit = rw.parseCommit(ObjectId.fromString(ps.getRevision().get()));
        Timestamp now = TimeUtil.nowTs();
        IdentifiedUser me = user.get().asIdentifiedUser();
        PersonIdent author = me.newCommitterIdent(now, serverTimeZone);
        RevCommit newCommit = createMergeCommit(in, projectControl, dest, git, oi, rw, currentPsCommit, sourceCommit, author, ObjectId.fromString(change.getKey().get().substring(1)));
        PatchSet.Id nextPsId = ChangeUtil.nextPatchSetId(ps.getId());
        PatchSetInserter psInserter = patchSetInserterFactory.create(ctl, nextPsId, newCommit);
        try (BatchUpdate bu = updateFactory.create(db.get(), project, me, now)) {
            bu.setRepository(git, rw, oi);
            bu.addOp(ctl.getId(), psInserter.setMessage("Uploaded patch set " + nextPsId.get() + ".").setDraft(ps.isDraft()).setNotify(NotifyHandling.NONE).setCheckAddPatchSetPermission(false));
            bu.execute();
        }
        ChangeJson json = jsonFactory.create(ListChangesOption.CURRENT_REVISION);
        return Response.ok(json.format(psInserter.getChange()));
    }
}
Also used : MergeInput(com.google.gerrit.extensions.common.MergeInput) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) RevWalk(org.eclipse.jgit.revwalk.RevWalk) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ProjectControl(com.google.gerrit.server.project.ProjectControl) Timestamp(java.sql.Timestamp) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) ChangeControl(com.google.gerrit.server.project.ChangeControl) Branch(com.google.gerrit.reviewdb.client.Branch) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 52 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class Move method applyImpl.

@Override
protected ChangeInfo applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, MoveInput input) throws RestApiException, OrmException, UpdateException, PermissionBackendException {
    Change change = rsrc.getChange();
    Project.NameKey project = rsrc.getProject();
    IdentifiedUser caller = rsrc.getUser();
    input.destinationBranch = RefNames.fullName(input.destinationBranch);
    if (change.getStatus().isClosed()) {
        throw new ResourceConflictException("Change is " + ChangeUtil.status(change));
    }
    Branch.NameKey newDest = new Branch.NameKey(project, input.destinationBranch);
    if (change.getDest().equals(newDest)) {
        throw new ResourceConflictException("Change is already destined for the specified branch");
    }
    // Move requires abandoning this change, and creating a new change.
    try {
        rsrc.permissions().database(dbProvider).check(ChangePermission.ABANDON);
        permissionBackend.user(caller).database(dbProvider).ref(newDest).check(RefPermission.CREATE_CHANGE);
    } catch (AuthException denied) {
        throw new AuthException("move not permitted", denied);
    }
    try (BatchUpdate u = updateFactory.create(dbProvider.get(), project, caller, TimeUtil.nowTs())) {
        u.addOp(change.getId(), new Op(input));
        u.execute();
    }
    return json.noOptions().format(project, rsrc.getId());
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Branch(com.google.gerrit.reviewdb.client.Branch) AuthException(com.google.gerrit.extensions.restapi.AuthException) Change(com.google.gerrit.reviewdb.client.Change) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 53 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class ChangeEmail method setFrom.

@Override
public void setFrom(final Account.Id id) {
    super.setFrom(id);
    /** Is the from user in an email squelching group? */
    final IdentifiedUser user = args.identifiedUserFactory.create(id);
    emailOnlyAuthors = !user.getCapabilities().canEmailReviewers();
}
Also used : IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 54 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class PRED_current_user_2 method createUser.

public Term createUser(Prolog engine, Term key) {
    if (!(key instanceof StructureTerm) || key.arity() != 1 || !((StructureTerm) key).functor().equals(user)) {
        throw new IllegalTypeException(this, 1, "user(int)", key);
    }
    Term idTerm = key.arg(0);
    CurrentUser user;
    if (idTerm instanceof IntegerTerm) {
        Map<Account.Id, IdentifiedUser> cache = StoredValues.USERS.get(engine);
        Account.Id accountId = new Account.Id(((IntegerTerm) idTerm).intValue());
        user = cache.get(accountId);
        if (user == null) {
            IdentifiedUser.GenericFactory userFactory = userFactory(engine);
            IdentifiedUser who = userFactory.create(accountId);
            cache.put(accountId, who);
            user = who;
        }
    } else if (idTerm.equals(anonymous)) {
        user = StoredValues.ANONYMOUS_USER.get(engine);
    } else {
        throw new IllegalTypeException(this, 1, "user(int)", key);
    }
    return new JavaObjectTerm(user);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) IllegalTypeException(com.googlecode.prolog_cafe.exceptions.IllegalTypeException) CurrentUser(com.google.gerrit.server.CurrentUser) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 55 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class InternalAccountDirectory method fill.

private void fill(AccountInfo info, Account account, @Nullable Collection<ExternalId> externalIds, Set<FillOptions> options) {
    if (options.contains(FillOptions.ID)) {
        info._accountId = account.getId().get();
    } else {
        // Was previously set to look up account for filling.
        info._accountId = null;
    }
    if (options.contains(FillOptions.NAME)) {
        info.name = Strings.emptyToNull(account.getFullName());
        if (info.name == null) {
            info.name = account.getUserName();
        }
    }
    if (options.contains(FillOptions.EMAIL)) {
        info.email = account.getPreferredEmail();
    }
    if (options.contains(FillOptions.SECONDARY_EMAILS)) {
        info.secondaryEmails = externalIds != null ? getSecondaryEmails(account, externalIds) : null;
    }
    if (options.contains(FillOptions.USERNAME)) {
        info.username = externalIds != null ? AccountState.getUserName(externalIds) : null;
    }
    if (options.contains(FillOptions.STATUS)) {
        info.status = account.getStatus();
    }
    if (options.contains(FillOptions.AVATARS)) {
        AvatarProvider ap = avatar.get();
        if (ap != null) {
            info.avatars = new ArrayList<>(3);
            IdentifiedUser user = userFactory.create(account.getId());
            // GWT UI uses DEFAULT_SIZE (26px).
            addAvatar(ap, info, user, AvatarInfo.DEFAULT_SIZE);
            // PolyGerrit UI prefers 32px and 100px.
            if (!info.avatars.isEmpty()) {
                if (32 != AvatarInfo.DEFAULT_SIZE) {
                    addAvatar(ap, info, user, 32);
                }
                if (100 != AvatarInfo.DEFAULT_SIZE) {
                    addAvatar(ap, info, user, 100);
                }
            }
        }
    }
}
Also used : AvatarProvider(com.google.gerrit.server.avatar.AvatarProvider) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Aggregations

IdentifiedUser (com.google.gerrit.server.IdentifiedUser)89 AuthException (com.google.gerrit.extensions.restapi.AuthException)27 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)19 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)15 CurrentUser (com.google.gerrit.server.CurrentUser)13 IOException (java.io.IOException)13 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)13 Project (com.google.gerrit.entities.Project)12 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)12 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)12 Inject (com.google.inject.Inject)12 Singleton (com.google.inject.Singleton)12 ArrayList (java.util.ArrayList)12 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)11 Provider (com.google.inject.Provider)11 Change (com.google.gerrit.entities.Change)10 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)10 Repository (org.eclipse.jgit.lib.Repository)10 Account (com.google.gerrit.entities.Account)9 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)9