in prs/webapp/src/main/java/org/netbeans/jackpot/prs/webapp/WebAppNotify.java [44:110]
public static void webhook(String data) throws IOException {
Map<String, Object> inputParsed = new ObjectMapper().readValue(data, Map.class);
Object action = inputParsed.get("action");
if (!"opened".equals(action))
return ;
Map<String, Object> pullRequest = (Map<String, Object>) inputParsed.get("pull_request");
if (pullRequest == null) {
return ;
}
Map<String, Object> repository = (Map<String, Object>) inputParsed.get("repository");
if (repository == null) {
return ;
}
String[] userAndRepo = ((String) repository.get("full_name")).split("/");
Preferences repositories = Config.getDefault().getPreferences().node("users").node(userAndRepo[0]).node("repositories");
if (!repositories.getBoolean(userAndRepo[1], false)) {
return ;
}
Preferences handlerPrefs = Config.getDefault().getPreferences().node("handler");
String handler = handlerPrefs.get("handler", "handler.local");
String remoteHost = handlerPrefs.get("remoteHost", null);
String remotePath = handlerPrefs.get("remotePath", null);
ProcessBuilder builder;
if (remoteHost != null && remotePath != null) {
builder = new ProcessBuilder(System.getProperty("install.dir") + "/handler/bin/handler.remote", remoteHost, remotePath, handler);
} else {
builder = new ProcessBuilder(System.getProperty("install.dir") + "/handler/bin/" + handler);
}
builder.environment().put("PR_CONTENT", data);
//XXX: how to handle the access tokens?
builder.environment().put("OAUTH_TOKEN", Config.getDefault().getPreferences().node("users").node(userAndRepo[0]).get("access_token", ""));
builder.environment().put("OAUTH_APP_TOKEN", Config.getDefault().getPreferences().node("app").get("access_token", ""));
java.nio.file.Path targetDir = Config.getDefault().getRunDir().resolve("github").resolve((String) repository.get("full_name"));
java.nio.file.Path thisRunDir = targetDir.resolve(String.valueOf((Integer) pullRequest.get("number")));
Files.createDirectories(thisRunDir);
Files.deleteIfExists(thisRunDir.resolve("finished"));
Files.newOutputStream(thisRunDir.resolve("preparing")).close();
java.nio.file.Path stdout = thisRunDir.resolve("stdout");
builder.redirectOutput(stdout.toFile());
java.nio.file.Path stderr = thisRunDir.resolve("stderr");
builder.redirectError(stderr.toFile());
Process process = builder.start();
Files.newOutputStream(thisRunDir.resolve("running")).close();
Files.delete(thisRunDir.resolve("preparing"));
new Thread(() -> {
while (true) {
try {
process.waitFor();
break;
} catch (InterruptedException ex) {
//ignore...
}
}
try {
Files.newOutputStream(thisRunDir.resolve("finished")).close();
} catch (IOException ex) {
WebApp.LOG.log(Level.SEVERE, null, ex);
}
try {
Files.delete(thisRunDir.resolve("running"));
} catch (IOException ex) {
WebApp.LOG.log(Level.SEVERE, null, ex);
}
pack(stdout);
pack(stderr);
}).start();
}