Merge "show-queue accessible to users"
This commit is contained in:
@@ -58,12 +58,22 @@ public abstract class AbstractGitCommand extends BaseCommand {
|
||||
Context ctx = context.subContext(newSession(), context.getCommandLine());
|
||||
final Context old = SshScope.set(ctx);
|
||||
try {
|
||||
startThread(new CommandRunnable() {
|
||||
startThread(new ProjectCommandRunnable() {
|
||||
@Override
|
||||
public void executeParseCommand() throws Exception {
|
||||
parseCommandLine();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() throws Exception {
|
||||
parseCommandLine();
|
||||
AbstractGitCommand.this.service();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Project.NameKey getProjectName() {
|
||||
Project project = projectControl.getProjectState().getProject();
|
||||
return project.getNameKey();
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
SshScope.set(old);
|
||||
|
||||
@@ -14,9 +14,12 @@
|
||||
|
||||
package com.google.gerrit.sshd;
|
||||
|
||||
import com.google.gerrit.reviewdb.Project;
|
||||
import com.google.gerrit.reviewdb.Project.NameKey;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.RequestCleanup;
|
||||
import com.google.gerrit.server.git.ProjectRunnable;
|
||||
import com.google.gerrit.server.git.WorkQueue;
|
||||
import com.google.gerrit.server.git.WorkQueue.CancelableRunnable;
|
||||
import com.google.gerrit.server.project.NoSuchChangeException;
|
||||
@@ -335,10 +338,11 @@ public abstract class BaseCommand implements Command {
|
||||
}
|
||||
}
|
||||
|
||||
private final class TaskThunk implements CancelableRunnable {
|
||||
private final class TaskThunk implements CancelableRunnable, ProjectRunnable {
|
||||
private final CommandRunnable thunk;
|
||||
private final Context context;
|
||||
private final String taskName;
|
||||
private Project.NameKey projectName;
|
||||
|
||||
private TaskThunk(final CommandRunnable thunk) {
|
||||
this.thunk = thunk;
|
||||
@@ -372,6 +376,12 @@ public abstract class BaseCommand implements Command {
|
||||
try {
|
||||
context.started = System.currentTimeMillis();
|
||||
thisThread.setName("SSH " + taskName);
|
||||
|
||||
if (thunk instanceof ProjectCommandRunnable) {
|
||||
((ProjectCommandRunnable) thunk).executeParseCommand();
|
||||
projectName = ((ProjectCommandRunnable) thunk).getProjectName();
|
||||
}
|
||||
|
||||
try {
|
||||
thunk.run();
|
||||
} catch (NoSuchProjectException e) {
|
||||
@@ -379,6 +389,7 @@ public abstract class BaseCommand implements Command {
|
||||
} catch (NoSuchChangeException e) {
|
||||
throw new UnloggedFailure(1, e.getMessage() + " no such change");
|
||||
}
|
||||
|
||||
out.flush();
|
||||
err.flush();
|
||||
} catch (Throwable e) {
|
||||
@@ -405,6 +416,21 @@ public abstract class BaseCommand implements Command {
|
||||
public String toString() {
|
||||
return taskName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NameKey getProjectNameKey() {
|
||||
return projectName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRemoteName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCustomizedPrint() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Runnable function which can throw an exception. */
|
||||
@@ -412,6 +438,15 @@ public abstract class BaseCommand implements Command {
|
||||
public void run() throws Exception;
|
||||
}
|
||||
|
||||
/** Runnable function which can retrieve a project name related to the task */
|
||||
public static interface ProjectCommandRunnable extends CommandRunnable {
|
||||
// execute parser command before running, in order to be able to retrieve
|
||||
// project name
|
||||
public void executeParseCommand() throws Exception;
|
||||
|
||||
public Project.NameKey getProjectName();
|
||||
}
|
||||
|
||||
/** Thrown from {@link CommandRunnable#run()} with client message and code. */
|
||||
public static class Failure extends Exception {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -38,7 +38,7 @@ public class DefaultCommandModule extends CommandModule {
|
||||
command(gerrit, "ls-projects").to(ListProjects.class);
|
||||
command(gerrit, "show-caches").to(AdminShowCaches.class);
|
||||
command(gerrit, "show-connections").to(AdminShowConnections.class);
|
||||
command(gerrit, "show-queue").to(AdminShowQueue.class);
|
||||
command(gerrit, "show-queue").to(ShowQueue.class);
|
||||
command(gerrit, "stream-events").to(StreamEvents.class);
|
||||
|
||||
command(git).toProvider(new DispatchCommandProvider(git));
|
||||
|
||||
@@ -14,10 +14,14 @@
|
||||
|
||||
package com.google.gerrit.sshd.commands;
|
||||
|
||||
import com.google.gerrit.reviewdb.Project;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.git.WorkQueue;
|
||||
import com.google.gerrit.server.git.WorkQueue.ProjectTask;
|
||||
import com.google.gerrit.server.git.WorkQueue.Task;
|
||||
import com.google.gerrit.server.project.ProjectCache;
|
||||
import com.google.gerrit.server.project.ProjectState;
|
||||
import com.google.gerrit.server.util.IdGenerator;
|
||||
import com.google.gerrit.sshd.AdminCommand;
|
||||
import com.google.gerrit.sshd.BaseCommand;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@@ -33,14 +37,19 @@ import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/** Display the current work queue. */
|
||||
@AdminCommand
|
||||
final class AdminShowQueue extends BaseCommand {
|
||||
final class ShowQueue extends BaseCommand {
|
||||
@Option(name = "-w", usage = "display without line width truncation")
|
||||
private boolean wide;
|
||||
|
||||
@Inject
|
||||
private WorkQueue workQueue;
|
||||
|
||||
@Inject
|
||||
private ProjectCache projectCache;
|
||||
|
||||
@Inject
|
||||
private CurrentUser userProvider;
|
||||
|
||||
private PrintWriter p;
|
||||
private int columns = 80;
|
||||
private int taskNameWidth;
|
||||
@@ -60,7 +69,7 @@ final class AdminShowQueue extends BaseCommand {
|
||||
@Override
|
||||
public void run() throws Exception {
|
||||
parseCommandLine();
|
||||
AdminShowQueue.this.display();
|
||||
ShowQueue.this.display();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -97,7 +106,10 @@ final class AdminShowQueue extends BaseCommand {
|
||||
p.print("----------------------------------------------"
|
||||
+ "--------------------------------\n");
|
||||
|
||||
int numberOfPendingTasks = 0;
|
||||
final long now = System.currentTimeMillis();
|
||||
final boolean isAdministrator = userProvider.isAdministrator();
|
||||
|
||||
for (final Task<?> task : pending) {
|
||||
final long delay = task.getDelay(TimeUnit.MILLISECONDS);
|
||||
final Task.State state = task.getState();
|
||||
@@ -115,12 +127,56 @@ final class AdminShowQueue extends BaseCommand {
|
||||
break;
|
||||
}
|
||||
|
||||
p.print(String.format("%8s %-12s %-8s %s\n", //
|
||||
id(task.getTaskId()), start, "", format(task)));
|
||||
boolean regularUserCanSee = false;
|
||||
boolean hasCustomizedPrint = true;
|
||||
|
||||
// If the user is not administrator, check if has rights to see
|
||||
// the Task
|
||||
Project.NameKey projectName = null;
|
||||
String remoteName = null;
|
||||
|
||||
if (!isAdministrator) {
|
||||
if (task instanceof ProjectTask<?>) {
|
||||
projectName = ((ProjectTask<?>)task).getProjectNameKey();
|
||||
remoteName = ((ProjectTask<?>)task).getRemoteName();
|
||||
hasCustomizedPrint = ((ProjectTask<?>)task).hasCustomizedPrint();
|
||||
}
|
||||
|
||||
ProjectState e = null;
|
||||
if (projectName != null) {
|
||||
e = projectCache.get(projectName);
|
||||
}
|
||||
|
||||
regularUserCanSee = e != null && e.controlFor(userProvider).isVisible();
|
||||
|
||||
if (regularUserCanSee) {
|
||||
numberOfPendingTasks++;
|
||||
}
|
||||
}
|
||||
|
||||
// Shows information about tasks depending on the user rights
|
||||
if (isAdministrator || (!hasCustomizedPrint && regularUserCanSee)) {
|
||||
p.print(String.format("%8s %-12s %-8s %s\n", //
|
||||
id(task.getTaskId()), start, "", format(task)));
|
||||
} else if (regularUserCanSee) {
|
||||
if (remoteName == null) {
|
||||
remoteName = projectName.get();
|
||||
} else {
|
||||
remoteName = remoteName + "/" + projectName;
|
||||
}
|
||||
|
||||
p.print(String.format("%8s %-12s %-8s %s\n", //
|
||||
id(task.getTaskId()), start, "", remoteName));
|
||||
}
|
||||
}
|
||||
p.print("----------------------------------------------"
|
||||
+ "--------------------------------\n");
|
||||
p.print(" " + pending.size() + " tasks\n");
|
||||
|
||||
if (isAdministrator) {
|
||||
numberOfPendingTasks = pending.size();
|
||||
}
|
||||
|
||||
p.print(" " + numberOfPendingTasks + " tasks\n");
|
||||
|
||||
p.flush();
|
||||
}
|
||||
Reference in New Issue
Block a user