Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
package org.netbeans.modules.jackpot.prs.handler.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHPullRequestReviewComment;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;

Expand Down Expand Up @@ -58,6 +63,13 @@ public void createReviewComment(String fullRepoName, int prId, String comment, S
pr.createReviewComment(comment, sha, filename, targetPosition);
}

@Override
public List<ReviewComment> getReviewComments(String fullRepoName, int prId) throws IOException {
GHRepository commentTarget = github.getRepository(fullRepoName);
GHPullRequest pr = commentTarget.getPullRequest(prId);
return pr.listReviewComments().asList().stream().map(c -> new ReviewComment(c.getPath(), c.getPosition(), c.getBody())).collect(Collectors.toList());
}

public static class FactoryImpl implements Factory {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import javax.swing.event.ChangeListener;
import org.netbeans.api.java.platform.JavaPlatform;
import org.netbeans.api.java.queries.BinaryForSourceQuery;
Expand All @@ -50,6 +51,7 @@
import org.netbeans.api.project.ui.OpenProjects;
import org.netbeans.api.sendopts.CommandException;
import org.netbeans.modules.jackpot.prs.handler.impl.SiteWrapper.Factory;
import org.netbeans.modules.jackpot.prs.handler.impl.SiteWrapper.ReviewComment;
import org.netbeans.modules.java.hints.spiimpl.batch.BatchUtilities;
import org.netbeans.modules.java.hints.spiimpl.hints.HintsInvoker;
import org.netbeans.modules.java.hints.spiimpl.options.HintsSettings;
Expand Down Expand Up @@ -97,7 +99,7 @@ public static void processPullRequest(String inputData, String oauthToken, Strin
File tempDir = Places.getCacheSubdirectory("checkout");
Map<String, Object> inputParsed = new ObjectMapper().readValue(inputData, Map.class);
Object action = inputParsed.get("action");
if (!"opened".equals(action))
if (!"opened".equals(action) && !"synchronize".equals(action))
return ;
Map<String, Object> pullRequest = (Map<String, Object>) inputParsed.get("pull_request");
if (pullRequest == null) {
Expand Down Expand Up @@ -198,6 +200,7 @@ public static void processPullRequest(String inputData, String oauthToken, Strin
}
OpenProjects.getDefault().open(projects.toArray(new Project[0]), false);
Map<ClasspathInfo, Collection<FileObject>> sorted = BatchUtilities.sortFiles(file2Remap.keySet());
AtomicReference<List<ReviewComment>> reviewComments = new AtomicReference<>();
for (Entry<ClasspathInfo, Collection<FileObject>> e : sorted.entrySet()) {
System.err.println("Running hints for:");
System.err.println("files: " + e.getValue());
Expand Down Expand Up @@ -226,8 +229,21 @@ public static void processPullRequest(String inputData, String oauthToken, Strin
hasWarnings[0] = true;
if (commentGitHub[0] == null) {
commentGitHub[0] = factory.create(oauthAppToken);
reviewComments.set(commentGitHub[0].getReviewComments(fullRepoName, prId));
}
boolean writeComment = true;
for (ReviewComment rc : reviewComments.get()) {
if (fileData.filename.equals(rc.filename) &&
targetPosition == rc.linenumber &&
comment.equals(rc.comment)) {
//skip comment:
writeComment = false;
break;
}
}
if (writeComment) {
commentGitHub[0].createReviewComment(fullRepoName, prId, comment, sha, fileData.filename, targetPosition);
}
commentGitHub[0].createReviewComment(fullRepoName, prId, comment, sha, fileData.filename, targetPosition);
}
}, false).get();
} catch (Throwable ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.netbeans.modules.jackpot.prs.handler.impl;

import java.io.IOException;
import java.util.List;

/**
*
Expand All @@ -30,7 +31,21 @@ public interface SiteWrapper {
public void createCommitStatusSuccess(String fullRepoName, String sha, String text) throws IOException;

public void createReviewComment(String fullRepoName, int prId, String comment, String sha, String filename, int targetPosition) throws IOException;

public List<ReviewComment> getReviewComments(String fullRepoName, int prId) throws IOException;

public static final class ReviewComment {
public final String filename;
public final int linenumber;
public final String comment;

public ReviewComment(String filename, int linenumber, String comment) {
this.filename = filename;
this.linenumber = linenumber;
this.comment = comment;
}

}

public static interface Factory {
public SiteWrapper create(String authToken) throws IOException;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.prefs.Preferences;
import java.util.zip.GZIPOutputStream;
Expand All @@ -44,7 +48,7 @@ public class WebAppNotify {
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))
if (!"opened".equals(action) && !"synchronize".equals(action))
return ;
Map<String, Object> pullRequest = (Map<String, Object>) inputParsed.get("pull_request");
if (pullRequest == null) {
Expand All @@ -59,6 +63,9 @@ public static void webhook(String data) throws IOException {
if (!repositories.getBoolean(userAndRepo[1], false)) {
return ;
}
String prName = (String) pullRequest.getOrDefault("title", "");
String prUser = (String) ((Map<String, Object>) pullRequest.getOrDefault("user", Collections.emptyMap())).getOrDefault("login", "");
String prURL = (String) pullRequest.getOrDefault("url", "");
Preferences handlerPrefs = Config.getDefault().getPreferences().node("handler");
String handler = handlerPrefs.get("handler", "handler.local");
String remoteHost = handlerPrefs.get("remoteHost", null);
Expand All @@ -74,7 +81,29 @@ public static void webhook(String data) throws IOException {
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")));
java.nio.file.Path thisPRDir = targetDir.resolve(String.valueOf((Integer) pullRequest.get("number")));
java.nio.file.Path buildInfo = thisPRDir.resolve("info");
Properties infoProps = new Properties();
if (Files.isReadable(buildInfo)) {
try (InputStream in = Files.newInputStream(buildInfo)) {
infoProps.load(in);
}
}
int buildNumber = 1;
try {
buildNumber = Integer.parseInt(infoProps.getProperty("nextBuild", "1"));
} catch (IllegalArgumentException ex) {
//ignore
}
infoProps.setProperty("nextBuild", String.valueOf(buildNumber + 1));
infoProps.setProperty("name", prName);
infoProps.setProperty("user", prUser);
infoProps.setProperty("url", prURL);
Files.createDirectories(buildInfo.getParent());
try (OutputStream out = Files.newOutputStream(buildInfo)) {
infoProps.store(out, "");
}
java.nio.file.Path thisRunDir = thisPRDir.resolve(String.valueOf(buildNumber));
Files.createDirectories(thisRunDir);
Files.deleteIfExists(thisRunDir.resolve("finished"));
Files.newOutputStream(thisRunDir.resolve("preparing")).close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import java.io.OutputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.zip.GZIPInputStream;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -60,31 +64,62 @@ public static String repoPullRequests(@Context HttpServletRequest request, @Quer
page.append("Pull Requests of: " + repositoryName);
page.append("<ul>");
java.nio.file.Path targetDir = Config.getDefault().getRunDir().resolve("github").resolve(repositoryName);
List<String> prs = new ArrayList<>();
try (DirectoryStream<java.nio.file.Path> ds = Files.newDirectoryStream(targetDir)) {
for (java.nio.file.Path p : ds) {
String pr = p.getFileName().toString();
page.append("<li>");
page.append(pr);
prs.add(p.getFileName().toString());
}
} catch (IOException ex) {
WebApp.LOG.log(Level.FINE, null, ex);
}
Collections.sort(prs);
for (String pr : prs) {
java.nio.file.Path p = targetDir.resolve(pr);
java.nio.file.Path buildInfo = p.resolve("info");
Properties infoProps = new Properties();
if (Files.isReadable(buildInfo)) {
try (InputStream in = Files.newInputStream(buildInfo)) {
infoProps.load(in);
}
}
page.append("<li>Pull Request #");
page.append(pr);
page.append(": ");
page.append(infoProps.get("name"));
page.append(" by ");
page.append(infoProps.get("user"));
page.append("<ul>");
List<String> builds = new ArrayList<>();
try (DirectoryStream<java.nio.file.Path> buildStream = Files.newDirectoryStream(p)) {
for (java.nio.file.Path b : buildStream) {
if (!Files.isDirectory(b)) continue;
builds.add(b.getFileName().toString());
}
}
Collections.sort(builds);
for (String build : builds) {
java.nio.file.Path b = p.resolve(build);
page.append("<li>Build #");
page.append(build);
page.append("&nbsp;");
if (Files.exists(p.resolve("preparing"))) {
if (Files.exists(b.resolve("preparing"))) {
page.append("preparing");
}
if (Files.exists(p.resolve("running"))) {
if (Files.exists(b.resolve("running"))) {
page.append("running");
}
if (Files.exists(p.resolve("finished"))) {
if (Files.exists(b.resolve("finished"))) {
page.append("finished");
}
if (Files.exists(p.resolve("stdout")) || Files.exists(p.resolve("stdout.gz"))) {
page.append("<a href=\"/github/repopullrequests/stdout?repositoryName=" + repositoryName + "&pr=" + pr + "\">stdout</a>");
if (Files.exists(b.resolve("stdout")) || Files.exists(b.resolve("stdout.gz"))) {
page.append("<a href=\"/github/repopullrequests/stdout?repositoryName=" + repositoryName + "&pr=" + pr + "&build=" + build + "\">stdout</a>");
}
if (Files.exists(p.resolve("stderr")) || Files.exists(p.resolve("stderr.gz"))) {
page.append("<a href=\"/github/repopullrequests/stderr?repositoryName=" + repositoryName + "&pr=" + pr + "\">stderr</a>");
if (Files.exists(b.resolve("stderr")) || Files.exists(b.resolve("stderr.gz"))) {
page.append("<a href=\"/github/repopullrequests/stderr?repositoryName=" + repositoryName + "&pr=" + pr + "&build=" + build + "\">stderr</a>");
}
page.append("</li>");
}
} catch (IOException ex) {
WebApp.LOG.log(Level.FINE, null, ex);
page.append("</ul>");
page.append("</li>");
}
page.append("</ul>");
page.append("</body>");
Expand All @@ -100,17 +135,17 @@ public static String repoPullRequests(@Context HttpServletRequest request, @Quer

@GET
@Path("/stdout")
public static Response stdout(@Context HttpServletRequest request, @QueryParam("repositoryName") String repositoryName, @QueryParam("pr") String pr) throws IOException {
return log(request, repositoryName, pr, "stdout");
public static Response stdout(@Context HttpServletRequest request, @QueryParam("repositoryName") String repositoryName, @QueryParam("pr") String pr, @QueryParam("build") String build) throws IOException {
return log(request, repositoryName, pr, build, "stdout");
}

@GET
@Path("/stderr")
public static Response stderr(@Context HttpServletRequest request, @QueryParam("repositoryName") String repositoryName, @QueryParam("pr") String pr) throws IOException {
return log(request, repositoryName, pr, "stderr");
public static Response stderr(@Context HttpServletRequest request, @QueryParam("repositoryName") String repositoryName, @QueryParam("pr") String pr, @QueryParam("build") String build) throws IOException {
return log(request, repositoryName, pr, build, "stderr");
}

private static Response log(HttpServletRequest request, String repositoryName, String pr, String log) throws IOException {
private static Response log(HttpServletRequest request, String repositoryName, String pr, String build, String log) throws IOException {
String userName = (String) request.getSession().getAttribute("user_name");
if (userName != null) {
if (repositoryName.startsWith(userName + "/")) {
Expand All @@ -124,11 +159,11 @@ private static Response log(HttpServletRequest request, String repositoryName, S
class StreamingOutputImpl implements StreamingOutput {
@Override
public void write(OutputStream out) throws IOException, WebApplicationException {
java.nio.file.Path logFile = Config.getDefault().getRunDir().resolve("github").resolve(repositoryName).resolve(pr).resolve(log);
java.nio.file.Path logFile = Config.getDefault().getRunDir().resolve("github").resolve(repositoryName).resolve(pr).resolve(build).resolve(log);
try (InputStream in = Files.newInputStream(logFile)) {
in.transferTo(out);
} catch (IOException ex) {
java.nio.file.Path logFileGZ = Config.getDefault().getRunDir().resolve("github").resolve(repositoryName).resolve(pr).resolve(log + ".gz");
java.nio.file.Path logFileGZ = Config.getDefault().getRunDir().resolve("github").resolve(repositoryName).resolve(pr).resolve(build).resolve(log + ".gz");
try (InputStream in = new GZIPInputStream(Files.newInputStream(logFileGZ))) {
in.transferTo(out);
}
Expand Down