use of com.google.gerrit.server.CurrentUser in project gerrit by GerritCodeReview.
the class Revert method revert.
private Change.Id revert(BatchUpdate.Factory updateFactory, ChangeControl ctl, String message) throws OrmException, IOException, RestApiException, UpdateException {
Change.Id changeIdToRevert = ctl.getChange().getId();
PatchSet.Id patchSetId = ctl.getChange().currentPatchSetId();
PatchSet patch = psUtil.get(db.get(), ctl.getNotes(), patchSetId);
if (patch == null) {
throw new ResourceNotFoundException(changeIdToRevert.toString());
}
Project.NameKey project = ctl.getProject().getNameKey();
CurrentUser user = ctl.getUser();
try (Repository git = repoManager.openRepository(project);
ObjectInserter oi = git.newObjectInserter();
ObjectReader reader = oi.newReader();
RevWalk revWalk = new RevWalk(reader)) {
RevCommit commitToRevert = revWalk.parseCommit(ObjectId.fromString(patch.getRevision().get()));
if (commitToRevert.getParentCount() == 0) {
throw new ResourceConflictException("Cannot revert initial commit");
}
Timestamp now = TimeUtil.nowTs();
PersonIdent committerIdent = new PersonIdent(serverIdent, now);
PersonIdent authorIdent = user.asIdentifiedUser().newCommitterIdent(now, committerIdent.getTimeZone());
RevCommit parentToCommitToRevert = commitToRevert.getParent(0);
revWalk.parseHeaders(parentToCommitToRevert);
CommitBuilder revertCommitBuilder = new CommitBuilder();
revertCommitBuilder.addParentId(commitToRevert);
revertCommitBuilder.setTreeId(parentToCommitToRevert.getTree());
revertCommitBuilder.setAuthor(authorIdent);
revertCommitBuilder.setCommitter(authorIdent);
Change changeToRevert = ctl.getChange();
if (message == null) {
message = MessageFormat.format(ChangeMessages.get().revertChangeDefaultMessage, changeToRevert.getSubject(), patch.getRevision().get());
}
ObjectId computedChangeId = ChangeIdUtil.computeChangeId(parentToCommitToRevert.getTree(), commitToRevert, authorIdent, committerIdent, message);
revertCommitBuilder.setMessage(ChangeIdUtil.insertId(message, computedChangeId, true));
Change.Id changeId = new Change.Id(seq.nextChangeId());
ObjectId id = oi.insert(revertCommitBuilder);
RevCommit revertCommit = revWalk.parseCommit(id);
ChangeInserter ins = changeInserterFactory.create(changeId, revertCommit, ctl.getChange().getDest().get()).setTopic(changeToRevert.getTopic());
ins.setMessage("Uploaded patch set 1.");
Set<Account.Id> reviewers = new HashSet<>();
reviewers.add(changeToRevert.getOwner());
reviewers.addAll(approvalsUtil.getReviewers(db.get(), ctl.getNotes()).all());
reviewers.remove(user.getAccountId());
ins.setReviewers(reviewers);
try (BatchUpdate bu = updateFactory.create(db.get(), project, user, now)) {
bu.setRepository(git, revWalk, oi);
bu.insertChange(ins);
bu.addOp(changeId, new NotifyOp(ctl.getChange(), ins));
bu.addOp(changeToRevert.getId(), new PostRevertedMessageOp(computedChangeId));
bu.execute();
}
return changeId;
} catch (RepositoryNotFoundException e) {
throw new ResourceNotFoundException(changeIdToRevert.toString(), e);
}
}
use of com.google.gerrit.server.CurrentUser in project gerrit by GerritCodeReview.
the class Submit method onBehalfOf.
private IdentifiedUser onBehalfOf(RevisionResource rsrc, SubmitInput in) throws AuthException, UnprocessableEntityException, OrmException, PermissionBackendException {
PermissionBackend.ForChange perm = rsrc.permissions().database(dbProvider);
perm.check(ChangePermission.SUBMIT);
perm.check(ChangePermission.SUBMIT_AS);
CurrentUser caller = rsrc.getUser();
IdentifiedUser submitter = accounts.parseOnBehalfOf(caller, in.onBehalfOf);
try {
perm.user(submitter).check(ChangePermission.READ);
} catch (AuthException e) {
throw new UnprocessableEntityException(String.format("on_behalf_of account %s cannot see change", submitter.getAccountId()));
}
return submitter;
}
use of com.google.gerrit.server.CurrentUser in project gerrit by GerritCodeReview.
the class PRED__check_user_label_3 method exec.
@Override
public Operation exec(Prolog engine) throws PrologException {
engine.setB0();
Term a1 = arg1.dereference();
Term a2 = arg2.dereference();
Term a3 = arg3.dereference();
if (a1 instanceof VariableTerm) {
throw new PInstantiationException(this, 1);
}
if (!(a1 instanceof SymbolTerm)) {
throw new IllegalTypeException(this, 1, "atom", a1);
}
String label = a1.name();
if (a2 instanceof VariableTerm) {
throw new PInstantiationException(this, 2);
}
if (!(a2 instanceof JavaObjectTerm) || !a2.convertible(CurrentUser.class)) {
throw new IllegalTypeException(this, 2, "CurrentUser)", a2);
}
CurrentUser user = (CurrentUser) ((JavaObjectTerm) a2).object();
if (a3 instanceof VariableTerm) {
throw new PInstantiationException(this, 3);
}
if (!(a3 instanceof IntegerTerm)) {
throw new IllegalTypeException(this, 3, "integer", a3);
}
short val = (short) ((IntegerTerm) a3).intValue();
try {
ChangeData cd = StoredValues.CHANGE_DATA.get(engine);
LabelType type = cd.getLabelTypes().byLabel(label);
if (type == null) {
return engine.fail();
}
StoredValues.PERMISSION_BACKEND.get(engine).user(user).change(cd).check(new LabelPermission.WithValue(type, val));
return cont;
} catch (OrmException err) {
throw new JavaException(this, 1, err);
} catch (AuthException err) {
return engine.fail();
} catch (PermissionBackendException err) {
SystemException se = new SystemException(err.getMessage());
se.initCause(err);
throw se;
}
}
use of com.google.gerrit.server.CurrentUser in project gerrit by GerritCodeReview.
the class PRED__user_label_range_4 method exec.
@Override
public Operation exec(Prolog engine) throws PrologException {
engine.setB0();
Term a1 = arg1.dereference();
Term a2 = arg2.dereference();
Term a3 = arg3.dereference();
Term a4 = arg4.dereference();
if (a1 instanceof VariableTerm) {
throw new PInstantiationException(this, 1);
}
if (!(a1 instanceof SymbolTerm)) {
throw new IllegalTypeException(this, 1, "atom", a1);
}
String label = a1.name();
if (a2 instanceof VariableTerm) {
throw new PInstantiationException(this, 2);
}
if (!(a2 instanceof JavaObjectTerm) || !a2.convertible(CurrentUser.class)) {
throw new IllegalTypeException(this, 2, "CurrentUser)", a2);
}
CurrentUser user = (CurrentUser) ((JavaObjectTerm) a2).object();
Set<LabelPermission.WithValue> can;
try {
ChangeData cd = StoredValues.CHANGE_DATA.get(engine);
LabelType type = cd.getLabelTypes().byLabel(label);
if (type == null) {
return engine.fail();
}
can = StoredValues.PERMISSION_BACKEND.get(engine).user(user).change(cd).test(type);
} catch (OrmException err) {
throw new JavaException(this, 1, err);
} catch (PermissionBackendException err) {
SystemException se = new SystemException(err.getMessage());
se.initCause(err);
throw se;
}
int min = 0;
int max = 0;
for (LabelPermission.WithValue v : can) {
min = Math.min(min, v.value());
max = Math.max(max, v.value());
}
if (!a3.unify(new IntegerTerm(min), engine.trail)) {
return engine.fail();
}
if (!a4.unify(new IntegerTerm(max), engine.trail)) {
return engine.fail();
}
return cont;
}
use of com.google.gerrit.server.CurrentUser in project gerrit by GerritCodeReview.
the class DashboardsCollection method parse.
@Override
public DashboardResource parse(ProjectResource parent, IdString id) throws ResourceNotFoundException, IOException, ConfigInvalidException {
ProjectControl myCtl = parent.getControl();
if (id.toString().equals("default")) {
return DashboardResource.projectDefault(myCtl);
}
List<String> parts = Lists.newArrayList(Splitter.on(':').limit(2).split(id.get()));
if (parts.size() != 2) {
throw new ResourceNotFoundException(id);
}
CurrentUser user = myCtl.getUser();
String ref = parts.get(0);
String path = parts.get(1);
for (ProjectState ps : myCtl.getProjectState().tree()) {
try {
return parse(ps.controlFor(user), ref, path, myCtl);
} catch (AmbiguousObjectException | ConfigInvalidException | IncorrectObjectTypeException e) {
throw new ResourceNotFoundException(id);
} catch (ResourceNotFoundException e) {
continue;
}
}
throw new ResourceNotFoundException(id);
}
Aggregations