use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.
the class GitwebServlet method service.
@Override
protected void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
if (req.getQueryString() == null || req.getQueryString().isEmpty()) {
// No query string? They want the project list, which we don't
// currently support. Return to Gerrit's own web UI.
//
rsp.sendRedirect(req.getContextPath() + "/");
return;
}
final Map<String, String> params = getParameters(req);
String a = params.get("a");
if (a != null) {
if (deniedActions.contains(a)) {
rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
if (a.equals(PROJECT_LIST_ACTION)) {
rsp.sendRedirect(req.getContextPath() + "/#" + PageLinks.ADMIN_PROJECTS + "?filter=" + Url.encode(params.get("pf") + "/"));
return;
}
}
String name = params.get("p");
if (name == null) {
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (name.endsWith(".git")) {
name = name.substring(0, name.length() - 4);
}
Project.NameKey nameKey = Project.nameKey(name);
Optional<ProjectState> projectState;
try {
projectState = projectCache.get(nameKey);
if (!projectState.isPresent()) {
sendErrorOrRedirect(req, rsp, HttpServletResponse.SC_NOT_FOUND);
return;
}
projectState.get().checkStatePermitsRead();
permissionBackend.user(userProvider.get()).project(nameKey).check(ProjectPermission.READ);
} catch (AuthException e) {
sendErrorOrRedirect(req, rsp, HttpServletResponse.SC_NOT_FOUND);
return;
} catch (IOException | PermissionBackendException err) {
logger.atSevere().withCause(err).log("cannot load %s", name);
rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
} catch (ResourceConflictException e) {
sendErrorOrRedirect(req, rsp, HttpServletResponse.SC_CONFLICT);
return;
}
try (Repository repo = repoManager.openRepository(nameKey)) {
CacheHeaders.setNotCacheable(rsp);
exec(req, rsp, projectState.get());
} catch (RepositoryNotFoundException e) {
getServletContext().log("Cannot open repository", e);
rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.
the class ConsistencyChecker method openRepo.
private boolean openRepo() {
Project.NameKey project = change().getDest().project();
try {
repo = repoManager.openRepository(project);
oi = repo.newObjectInserter();
rw = new RevWalk(oi.newReader());
return true;
} catch (RepositoryNotFoundException e) {
ProblemInfo problem = problem("Destination repository not found: " + project);
logger.atWarning().withCause(e).log("Error in consistency check of change %s: %s", notes.getChangeId(), problem);
return false;
} catch (IOException e) {
ProblemInfo problem = problem("Failed to open repository: " + project);
logger.atWarning().withCause(e).log("Error in consistency check of change %s: %s", notes.getChangeId(), problem);
return false;
}
}
use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.
the class CommentContextLoader method getContext.
/**
* Load the comment context for multiple contextInputs at once. This method will open the
* repository and read the source files for all necessary contextInputs' file paths.
*
* @param contextInputs a list of contextInputs.
* @return a Map where all entries consist of the input contextInputs and the values are their
* corresponding {@link CommentContext}.
*/
public Map<ContextInput, CommentContext> getContext(Collection<ContextInput> contextInputs) throws IOException {
ImmutableMap.Builder<ContextInput, CommentContext> result = ImmutableMap.builderWithExpectedSize(Iterables.size(contextInputs));
// Group contextInputs by commit ID so that each commit is parsed only once
Map<ObjectId, List<ContextInput>> commentsByCommitId = contextInputs.stream().collect(groupingBy(ContextInput::commitId));
try (Repository repo = repoManager.openRepository(project);
RevWalk rw = new RevWalk(repo)) {
for (ObjectId commitId : commentsByCommitId.keySet()) {
RevCommit commit;
try {
commit = rw.parseCommit(commitId);
} catch (IncorrectObjectTypeException | MissingObjectException e) {
logger.atWarning().log("Commit %s is missing or has an incorrect object type", commitId);
commentsByCommitId.get(commitId).forEach(contextInput -> result.put(contextInput, CommentContext.empty()));
continue;
}
for (ContextInput contextInput : commentsByCommitId.get(commitId)) {
Optional<Range> range = getStartAndEndLines(contextInput);
if (!range.isPresent()) {
result.put(contextInput, CommentContext.empty());
continue;
}
String filePath = contextInput.filePath();
switch(filePath) {
case COMMIT_MSG:
result.put(contextInput, getContextForCommitMessage(rw.getObjectReader(), commit, range.get(), contextInput.contextPadding()));
break;
case MERGE_LIST:
result.put(contextInput, getContextForMergeList(rw.getObjectReader(), commit, range.get(), contextInput.contextPadding()));
break;
default:
result.put(contextInput, getContextForFilePath(repo, rw, commit, filePath, range.get(), contextInput.contextPadding()));
}
}
}
return result.build();
}
}
use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.
the class RevisionJson method toRevisionInfo.
private RevisionInfo toRevisionInfo(AccountLoader accountLoader, ChangeData cd, PatchSet in, @Nullable Repository repo, @Nullable RevWalk rw, boolean fillCommit, @Nullable ChangeInfo changeInfo) throws PatchListNotAvailableException, GpgException, IOException, PermissionBackendException {
Change c = cd.change();
RevisionInfo out = new RevisionInfo();
out.isCurrent = in.id().equals(c.currentPatchSetId());
out._number = in.id().get();
out.ref = in.refName();
out.setCreated(in.createdOn());
out.uploader = accountLoader.get(in.uploader());
out.fetch = makeFetchMap(cd, in);
out.kind = changeKindCache.getChangeKind(rw, repo != null ? repo.getConfig() : null, cd, in);
out.description = in.description().orElse(null);
boolean setCommit = has(ALL_COMMITS) || (out.isCurrent && has(CURRENT_COMMIT));
boolean addFooters = out.isCurrent && has(COMMIT_FOOTERS);
if (setCommit || addFooters) {
checkState(rw != null);
checkState(repo != null);
Project.NameKey project = c.getProject();
String rev = in.commitId().name();
RevCommit commit = rw.parseCommit(ObjectId.fromString(rev));
rw.parseBody(commit);
String branchName = cd.change().getDest().branch();
if (setCommit) {
out.commit = getCommitInfo(project, rw, commit, has(WEB_LINKS), fillCommit, branchName);
}
if (addFooters) {
Ref ref = repo.exactRef(branchName);
RevCommit mergeTip = null;
if (ref != null) {
mergeTip = rw.parseCommit(ref.getObjectId());
rw.parseBody(mergeTip);
}
out.commitWithFooters = mergeUtilFactory.create(projectCache.get(project).orElseThrow(illegalState(project))).createCommitMessageOnSubmit(commit, mergeTip, cd.notes(), in.id());
}
}
if (has(ALL_FILES) || (out.isCurrent && has(CURRENT_FILES))) {
try {
out.files = fileInfoJson.getFileInfoMap(c, in);
out.files.remove(Patch.COMMIT_MSG);
out.files.remove(Patch.MERGE_LIST);
} catch (ResourceConflictException e) {
logger.atWarning().withCause(e).log("creating file list failed");
}
}
if (out.isCurrent && has(CURRENT_ACTIONS) && userProvider.get().isIdentifiedUser()) {
actionJson.addRevisionActions(changeInfo, out, new RevisionResource(changeResourceFactory.create(cd, userProvider.get()), in));
}
if (gpgApi.isEnabled() && has(PUSH_CERTIFICATES)) {
if (in.pushCertificate().isPresent()) {
out.pushCertificate = gpgApi.checkPushCertificate(in.pushCertificate().get(), userFactory.create(in.uploader()));
} else {
out.pushCertificate = new PushCertificateInfo();
}
}
return out;
}
use of com.google.gerrit.entities.Project in project gerrit by GerritCodeReview.
the class AllProjectsIndexer method reindexProjects.
private SiteIndexer.Result reindexProjects(ProjectIndex index, List<Project.NameKey> names, ProgressMonitor progress) {
progress.beginTask("Reindexing projects", names.size());
List<ListenableFuture<?>> futures = new ArrayList<>(names.size());
AtomicBoolean ok = new AtomicBoolean(true);
AtomicInteger done = new AtomicInteger();
AtomicInteger failed = new AtomicInteger();
Stopwatch sw = Stopwatch.createStarted();
for (Project.NameKey name : names) {
String desc = "project " + name;
ListenableFuture<?> future = executor.submit(() -> {
try {
projectCache.evict(name);
ProjectData projectData = projectCache.get(name).orElseThrow(illegalState(name)).toProjectData();
if (isFirstInsertForEntry.equals(IsFirstInsertForEntry.YES)) {
index.insert(projectData);
} else {
index.replace(projectData);
}
verboseWriter.println("Reindexed " + desc);
done.incrementAndGet();
} catch (Exception e) {
failed.incrementAndGet();
throw e;
}
return null;
});
addErrorListener(future, desc, progress, ok);
futures.add(future);
}
try {
Futures.successfulAsList(futures).get();
} catch (ExecutionException | InterruptedException e) {
logger.atSevere().withCause(e).log("Error waiting on project futures");
return SiteIndexer.Result.create(sw, false, 0, 0);
}
progress.endTask();
return SiteIndexer.Result.create(sw, ok.get(), done.get(), failed.get());
}
Aggregations