@Override public Object apply(FileResource resource, Input input) throws OrmException { ReviewDb db = dbProvider.get(); AccountPatchReview apr = getExisting(db, resource); if (apr == null) { db.accountPatchReviews() .insert( Collections.singleton( new AccountPatchReview(resource.getPatchKey(), resource.getAccountId()))); return Response.created(""); } else { return Response.ok(""); } }
@Override public Object apply(FileResource resource, Input input) throws OrmException { ReviewDb db = dbProvider.get(); AccountPatchReview apr = getExisting(db, resource); if (apr != null) { db.accountPatchReviews().delete(Collections.singleton(apr)); } return Response.none(); }
@Override public Response<String> apply(CacheResource rsrc, Input input) throws AuthException { if (WEB_SESSIONS.equals(rsrc.getName()) && !self.get().getCapabilities().canMaintainServer()) { throw new AuthException(String.format("only site maintainers can flush %s", WEB_SESSIONS)); } rsrc.getCache().invalidateAll(); return Response.ok(""); }
public Response<String> apply(IdentifiedUser user, String newPassword) throws ResourceNotFoundException, ResourceConflictException, OrmException { if (user.getUserName() == null) { throw new ResourceConflictException("username must be set"); } AccountExternalId id = dbProvider .get() .accountExternalIds() .get(new AccountExternalId.Key(SCHEME_USERNAME, user.getUserName())); if (id == null) { throw new ResourceNotFoundException(); } id.setPassword(newPassword); dbProvider.get().accountExternalIds().update(Collections.singleton(id)); accountCache.evict(user.getAccountId()); return Strings.isNullOrEmpty(newPassword) ? Response.<String>none() : Response.ok(newPassword); }
@Override public Response<List<BlameInfo>> apply(FileResource resource) throws RestApiException, OrmException, IOException, InvalidChangeOperationException { if (!allowBlame) { throw new BadRequestException("blame is disabled"); } Project.NameKey project = resource.getRevision().getChange().getProject(); try (Repository repository = repoManager.openRepository(project); ObjectInserter ins = repository.newObjectInserter(); RevWalk revWalk = new RevWalk(ins.newReader())) { String refName = resource.getRevision().getEdit().isPresent() ? resource.getRevision().getEdit().get().getRefName() : resource.getRevision().getPatchSet().getRefName(); Ref ref = repository.findRef(refName); if (ref == null) { throw new ResourceNotFoundException("unknown ref " + refName); } ObjectId objectId = ref.getObjectId(); RevCommit revCommit = revWalk.parseCommit(objectId); RevCommit[] parents = revCommit.getParents(); String path = resource.getPatchKey().getFileName(); List<BlameInfo> result; if (!base) { result = blame(revCommit, path, repository, revWalk); } else if (parents.length == 0) { throw new ResourceNotFoundException("Initial commit doesn't have base"); } else if (parents.length == 1) { result = blame(parents[0], path, repository, revWalk); } else if (parents.length == 2) { ObjectId automerge = autoMerger.merge(repository, revWalk, ins, revCommit, mergeStrategy); result = blame(automerge, path, repository, revWalk); } else { throw new ResourceNotFoundException( "Cannot generate blame for merge commit with more than 2 parents"); } Response<List<BlameInfo>> r = Response.ok(result); if (resource.isCacheable()) { r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS)); } return r; } }
@Override public ServiceUserInfo apply(ServiceUserResource rsrc) throws ResourceNotFoundException, OrmException { ProjectLevelConfig storage = projectCache.getAllProjects().getConfig(pluginName + ".db"); String username = rsrc.getUser().getUserName(); Config db = storage.get(); if (!db.getSubsections(USER).contains(username)) { throw new ResourceNotFoundException(username); } ServiceUserInfo info = new ServiceUserInfo(getAccount.get().apply(rsrc)); AccountLoader al = accountLoader.create(true); info.createdBy = al.get(new Account.Id(db.getInt(USER, username, KEY_CREATOR_ID, -1))); al.fill(); info.createdAt = db.getString(USER, username, KEY_CREATED_AT); info.inactive = !rsrc.getUser().getAccount().isActive() ? true : null; Response<GroupInfo> response = getOwner.apply(rsrc); if (response.statusCode() == SC_OK) { info.owner = response.value(); } return info; }
@Override public Response<CommentInfo> apply(RevisionResource rsrc, DraftInput in) throws RestApiException, UpdateException, OrmException { if (Strings.isNullOrEmpty(in.path)) { throw new BadRequestException("path must be non-empty"); } else if (in.message == null || in.message.trim().isEmpty()) { throw new BadRequestException("message must be non-empty"); } else if (in.line != null && in.line < 0) { throw new BadRequestException("line must be >= 0"); } else if (in.line != null && in.range != null && in.line != in.range.endLine) { throw new BadRequestException("range endLine must be on the same line as the comment"); } try (BatchUpdate bu = updateFactory.create(db.get(), rsrc.getProject(), rsrc.getUser(), TimeUtil.nowTs())) { Op op = new Op(rsrc.getPatchSet().getId(), in); bu.addOp(rsrc.getChange().getId(), op); bu.execute(); return Response.created( commentJson.get().setFillAccounts(false).newCommentFormatter().format(op.comment)); } }