use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.
the class ChangeEditUtil method byChange.
/**
* Retrieve edit for a change and the given user.
*
* <p>At most one change edit can exist per user and change.
*
* @param ctl control with user to retrieve change edits for.
* @return edit for this change for this user, if present.
* @throws AuthException if this is not a logged-in user.
* @throws IOException if an error occurs.
*/
public Optional<ChangeEdit> byChange(ChangeControl ctl) throws AuthException, IOException {
if (!ctl.getUser().isIdentifiedUser()) {
throw new AuthException("Authentication required");
}
IdentifiedUser u = ctl.getUser().asIdentifiedUser();
Change change = ctl.getChange();
try (Repository repo = gitManager.openRepository(change.getProject())) {
int n = change.currentPatchSetId().get();
String[] refNames = new String[n];
for (int i = n; i > 0; i--) {
refNames[i - 1] = RefNames.refsEdit(u.getAccountId(), change.getId(), new PatchSet.Id(change.getId(), i));
}
Ref ref = repo.getRefDatabase().firstExactRef(refNames);
if (ref == null) {
return Optional.empty();
}
try (RevWalk rw = new RevWalk(repo)) {
RevCommit commit = rw.parseCommit(ref.getObjectId());
PatchSet basePs = getBasePatchSet(ctl, ref);
return Optional.of(new ChangeEdit(change, ref.getName(), commit, basePs));
}
}
}
use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.
the class BanCommit method apply.
@Override
public BanResultInfo apply(ProjectResource rsrc, Input input) throws UnprocessableEntityException, AuthException, ResourceConflictException, IOException {
BanResultInfo r = new BanResultInfo();
if (input != null && input.commits != null && !input.commits.isEmpty()) {
List<ObjectId> commitsToBan = new ArrayList<>(input.commits.size());
for (String c : input.commits) {
try {
commitsToBan.add(ObjectId.fromString(c));
} catch (IllegalArgumentException e) {
throw new UnprocessableEntityException(e.getMessage());
}
}
try {
BanCommitResult result = banCommit.ban(rsrc.getControl(), commitsToBan, input.reason);
r.newlyBanned = transformCommits(result.getNewlyBannedCommits());
r.alreadyBanned = transformCommits(result.getAlreadyBannedCommits());
r.ignored = transformCommits(result.getIgnoredObjectIds());
} catch (PermissionDeniedException e) {
throw new AuthException(e.getMessage());
} catch (ConcurrentRefUpdateException e) {
throw new ResourceConflictException(e.getMessage(), e);
}
}
return r;
}
use of com.google.gerrit.extensions.restapi.AuthException in project gerrit by GerritCodeReview.
the class ChangeArgumentParser method addChange.
public void addChange(String id, Map<Change.Id, ChangeResource> changes, ProjectControl projectControl, boolean useIndex) throws UnloggedFailure, OrmException {
List<ChangeControl> matched = useIndex ? changeFinder.find(id, currentUser) : changeFromNotesFactory(id, currentUser);
List<ChangeControl> toAdd = new ArrayList<>(changes.size());
boolean canMaintainServer;
try {
permissionBackend.user(currentUser).check(GlobalPermission.MAINTAIN_SERVER);
canMaintainServer = true;
} catch (AuthException | PermissionBackendException e) {
canMaintainServer = false;
}
for (ChangeControl ctl : matched) {
if (!changes.containsKey(ctl.getId()) && inProject(projectControl, ctl.getProject()) && (canMaintainServer || ctl.isVisible(db))) {
toAdd.add(ctl);
}
}
if (toAdd.isEmpty()) {
throw new UnloggedFailure(1, "\"" + id + "\" no such change");
} else if (toAdd.size() > 1) {
throw new UnloggedFailure(1, "\"" + id + "\" matches multiple changes");
}
ChangeControl ctl = toAdd.get(0);
changes.put(ctl.getId(), changesCollection.parse(ctl));
}
Aggregations