mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-07-05 06:59:35 +02:00
Improve logging for mercurial hooks
This commit is contained in:
@@ -68,11 +68,13 @@ class DefaultHookHandler implements HookHandler {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
LOG.trace("start handling hook protocol");
|
||||
try (InputStream input = socket.getInputStream(); OutputStream output = socket.getOutputStream()) {
|
||||
handleHookRequest(input, output);
|
||||
} catch (IOException e) {
|
||||
LOG.warn("failed to read hook request", e);
|
||||
} finally {
|
||||
LOG.trace("close client socket");
|
||||
close();
|
||||
}
|
||||
}
|
||||
@@ -84,8 +86,10 @@ class DefaultHookHandler implements HookHandler {
|
||||
}
|
||||
|
||||
private Response handleHookRequest(Request request) {
|
||||
LOG.trace("process {} hook for node {}", request.getType(), request.getNode());
|
||||
try {
|
||||
if (!environment.isAcceptAble(request.getChallenge())) {
|
||||
LOG.warn("received hook with invalid challenge: {}", request.getChallenge());
|
||||
return error("invalid hook challenge");
|
||||
}
|
||||
|
||||
@@ -111,6 +115,7 @@ class DefaultHookHandler implements HookHandler {
|
||||
}
|
||||
|
||||
private void authenticate(Request request) {
|
||||
LOG.trace("authenticate hook request");
|
||||
String token = CipherUtil.getInstance().decode(request.getToken());
|
||||
BearerToken bearer = BearerToken.valueOf(token);
|
||||
Subject subject = SecurityUtils.getSubject();
|
||||
|
||||
@@ -96,6 +96,7 @@ public class HookServer implements AutoCloseable {
|
||||
acceptor.submit(() -> {
|
||||
while (!serverSocket.isClosed()) {
|
||||
try {
|
||||
LOG.trace("wait for next hook connection");
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
LOG.trace("accept incoming hook client from {}", clientSocket.getInetAddress());
|
||||
HookHandler hookHandler = handlerFactory.create(clientSocket);
|
||||
@@ -104,6 +105,7 @@ public class HookServer implements AutoCloseable {
|
||||
LOG.debug("failed to accept socket, possible closed", ex);
|
||||
}
|
||||
}
|
||||
LOG.warn("ServerSocket is closed");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ class Sockets {
|
||||
byte[] bytes = objectMapper.writeValueAsBytes(object);
|
||||
out.write(bytes);
|
||||
out.write('\0');
|
||||
out.flush();
|
||||
}
|
||||
|
||||
static <T> T read(InputStream in, Class<T> type) throws IOException {
|
||||
|
||||
@@ -31,6 +31,8 @@ import com.aragost.javahg.commands.ExecutionException;
|
||||
import com.aragost.javahg.commands.PullCommand;
|
||||
import com.aragost.javahg.commands.RemoveCommand;
|
||||
import com.aragost.javahg.commands.StatusCommand;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.NoChangesMadeException;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.work.WorkingCopy;
|
||||
@@ -40,9 +42,12 @@ import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("java:S3252") // it is ok for javahg classes to access static method of subtype
|
||||
public class HgModifyCommand implements ModifyCommand {
|
||||
|
||||
private HgCommandContext context;
|
||||
private static final Logger LOG = LoggerFactory.getLogger(HgModifyCommand.class);
|
||||
|
||||
private final HgCommandContext context;
|
||||
private final HgWorkingCopyFactory workingCopyFactory;
|
||||
|
||||
public HgModifyCommand(HgCommandContext context, HgWorkingCopyFactory workingCopyFactory) {
|
||||
@@ -52,7 +57,6 @@ public class HgModifyCommand implements ModifyCommand {
|
||||
|
||||
@Override
|
||||
public String execute(ModifyCommandRequest request) {
|
||||
|
||||
try (WorkingCopy<com.aragost.javahg.Repository, com.aragost.javahg.Repository> workingCopy = workingCopyFactory.createWorkingCopy(context, request.getBranch())) {
|
||||
Repository workingRepository = workingCopy.getWorkingRepository();
|
||||
request.getRequests().forEach(
|
||||
@@ -97,12 +101,21 @@ public class HgModifyCommand implements ModifyCommand {
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (StatusCommand.on(workingRepository).lines().isEmpty()) {
|
||||
throw new NoChangesMadeException(context.getScmRepository());
|
||||
}
|
||||
CommitCommand.on(workingRepository).user(String.format("%s <%s>", request.getAuthor().getName(), request.getAuthor().getMail())).message(request.getCommitMessage()).execute();
|
||||
|
||||
LOG.trace("commit changes in working copy");
|
||||
CommitCommand.on(workingRepository)
|
||||
.user(String.format("%s <%s>", request.getAuthor().getName(), request.getAuthor().getMail()))
|
||||
.message(request.getCommitMessage()).execute();
|
||||
|
||||
List<Changeset> execute = pullModifyChangesToCentralRepository(request, workingCopy);
|
||||
return execute.get(0).getNode();
|
||||
|
||||
String node = execute.get(0).getNode();
|
||||
LOG.debug("successfully pulled changes from working copy, new node {}", node);
|
||||
return node;
|
||||
} catch (ExecutionException e) {
|
||||
throwInternalRepositoryException("could not execute command on repository", e);
|
||||
return null;
|
||||
@@ -110,6 +123,7 @@ public class HgModifyCommand implements ModifyCommand {
|
||||
}
|
||||
|
||||
private List<Changeset> pullModifyChangesToCentralRepository(ModifyCommandRequest request, WorkingCopy<com.aragost.javahg.Repository, com.aragost.javahg.Repository> workingCopy) {
|
||||
LOG.trace("pull changes from working copy");
|
||||
try {
|
||||
com.aragost.javahg.commands.PullCommand pullCommand = PullCommand.on(workingCopy.getCentralRepository());
|
||||
workingCopyFactory.configure(pullCommand);
|
||||
|
||||
Reference in New Issue
Block a user