Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/main/java/previewcode/backend/APIModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.inject.servlet.ServletModule;
import io.atlassian.fugue.Unit;
import io.vavr.jackson.datatype.VavrModule;
import org.jboss.resteasy.plugins.guice.ext.JaxrsModule;
import previewcode.backend.api.exceptionmapper.*;
import previewcode.backend.api.v2.ApprovalsAPI;
Expand Down Expand Up @@ -61,6 +62,7 @@ public ObjectMapper getContext(Class<?> type) {
private static ObjectMapper createDefaultMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new UnitSerializerModule());
mapper.registerModule(new VavrModule());
return mapper;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/previewcode/backend/DTO/ApprovedGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import previewcode.backend.database.GroupID;

import java.util.List;
import java.util.Map;


Expand All @@ -18,11 +19,11 @@ public class ApprovedGroup {
* All the hunks in this group
*/
@JsonProperty("hunks")
public final Map<String, ApproveStatus> hunks;
public final List<HunkApprovals> hunks;

public final GroupID groupID;

public ApprovedGroup(ApproveStatus approved, Map<String, ApproveStatus> hunks, GroupID groupID) {
public ApprovedGroup(ApproveStatus approved, List<HunkApprovals> hunks, GroupID groupID) {
this.approved = approved;
this.hunks = hunks;
this.groupID = groupID;
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/previewcode/backend/DTO/GHApproveStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package previewcode.backend.DTO;

public class GHApproveStatus extends GitHubStatus {

private static final String CONTEXT = "preview-code/approve-status";
private static final String PENDING_DESCRIPTION = "Not all hunks have been reviewed";
private static final String SUCCESS_DESCRIPTION = "All hunks have been accepted";
private static final String FAILURE_DESCRIPTION = "There are rejected hunks";

public GHApproveStatus(GitHubPullRequest pullRequest) {
super("pending", PENDING_DESCRIPTION, CONTEXT, pullRequest.previewCodeUrl());
}

private GHApproveStatus(String state, String description, String url) {
super(state, description, CONTEXT, url);
}

public Boolean isComplete() {
return this.state.equals("success");
}

public Boolean isPending() {
return this.state.equals("pending");
}

public Boolean isFailure() {
return this.state.equals("failure");
}

public GHApproveStatus complete() {
return new GHApproveStatus("success", SUCCESS_DESCRIPTION, this.url);
}

public GHApproveStatus pending() {
return new GHApproveStatus("pending", PENDING_DESCRIPTION, this.url);
}

public GHApproveStatus failure() {
return new GHApproveStatus("failure", FAILURE_DESCRIPTION, this.url);
}

@Override
public String toString() {
return "GHApproveStatus{" + state + "}";
}
}
20 changes: 17 additions & 3 deletions src/main/java/previewcode/backend/DTO/GitHubPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class GitHubPullRequest {
public final String title;
public final Integer number;
public final PullRequestLinks links;
public final Base base;

private static final String PREVIEW_URL = "https://preview-code.com/";

Expand All @@ -20,15 +21,28 @@ public GitHubPullRequest(
@JsonProperty("body") String body,
@JsonProperty("url") String url,
@JsonProperty("number") Integer number,
@JsonProperty("_links") PullRequestLinks links) {
@JsonProperty("base") Base base,
@JsonProperty("_links") PullRequestLinks links
) {
this.title = title;
this.body = body;
this.url = url;
this.number = number;
this.links = links;
this.base = base;
}

public String previewCodeUrl(GitHubRepository repository) {
return PREVIEW_URL + repository.fullName + "/pulls/" + this.number;
public String previewCodeUrl() {
return PREVIEW_URL + base.repo.fullName + "/pulls/" + this.number;
}

@JsonIgnoreProperties(ignoreUnknown=true)
public static class Base {
public final GitHubRepository repo;

@JsonCreator
public Base(@JsonProperty("repo") GitHubRepository repo) {
this.repo = repo;
}
}
}
5 changes: 4 additions & 1 deletion src/main/java/previewcode/backend/DTO/HunkApprovals.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ public class HunkApprovals {
@JsonProperty("hunkID")
public String hunkChecksum;

@JsonProperty("approved")
public final ApproveStatus approved;
/**
* Per user the approvals status
*/
@JsonProperty("approvals")
public Map<String, ApproveStatus> approvals;

public HunkApprovals(HunkChecksum hunkChecksum, Map<String, ApproveStatus> approvals) {
public HunkApprovals(HunkChecksum hunkChecksum, ApproveStatus approved, Map<String, ApproveStatus> approvals) {
this.approved = approved;
this.approvals = approvals;
this.hunkChecksum = hunkChecksum.checksum;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/previewcode/backend/DTO/OrderingStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class OrderingStatus extends GitHubStatus {
private static final String PENDING_DESCRIPTION = "Waiting for author to order changes";
private static final String SUCCESS_DESCRIPTION = "Changes have been ordered by the author";

public OrderingStatus(GitHubPullRequest pullRequest, GitHubRepository repository) {
super("pending", PENDING_DESCRIPTION, CONTEXT, pullRequest.previewCodeUrl(repository));
public OrderingStatus(GitHubPullRequest pullRequest) {
super("pending", PENDING_DESCRIPTION, CONTEXT, pullRequest.previewCodeUrl());
}

private OrderingStatus(String state, String description, String url) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/previewcode/backend/MainModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import previewcode.backend.api.filter.IJWTTokenCreator;
import previewcode.backend.api.filter.JWTTokenCreator;
import previewcode.backend.api.v1.*;
import previewcode.backend.services.IGithubService;
import previewcode.backend.services.interpreters.DatabaseInterpreter;
import previewcode.backend.services.interpreters.GitHubAuthInterpreter;
import previewcode.backend.services.http.HttpRequestExecutor;
Expand Down Expand Up @@ -66,14 +67,14 @@ public void configureServlets() {
this.bind(StatusAPI.class);
this.bind(PullRequestAPI.class);
this.bind(CommentsAPI.class);
this.bind(AssigneesAPI.class);
this.bind(TrackerAPI.class);
this.bind(WebhookAPI.class);

this.bind(GitHubAccessTokenFilter.class);
this.bind(ResteasyJackson2Provider.class);

this.bind(IDatabaseService.class).to(DatabaseService.class);
this.bind(IGithubService.class).to(GithubService.class);

try {
HttpRequestExecutor http = new HttpRequestExecutor();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package previewcode.backend.api.filter;

import org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext;
import previewcode.backend.services.IGithubService;
import previewcode.backend.services.interpreters.RequestContextActionInterpreter;
import previewcode.backend.services.interpreters.GitHubAuthInterpreter;
import previewcode.backend.services.GithubService;
import previewcode.backend.services.actiondsl.Interpreter;

import javax.inject.Inject;
Expand All @@ -23,7 +23,7 @@ public class GitHubAccessTokenFilter implements ContainerRequestFilter {
private GitHubAuthInterpreter gitHubAuthInterpreter;

@Inject
private GithubService.V2 authService;
private IGithubService.V2 authService;

@Override
public void filter(ContainerRequestContext ctx) throws IOException {
Expand Down
57 changes: 0 additions & 57 deletions src/main/java/previewcode/backend/api/v1/AssigneesAPI.java

This file was deleted.

8 changes: 4 additions & 4 deletions src/main/java/previewcode/backend/api/v1/WebhookAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Response onWebhookPost(
if (action.equals("opened")) {
Pair<GitHubRepository, GitHubPullRequest> repoAndPull = readRepoAndPullFromWebhook(body);
PRComment comment = new PRComment(constructMarkdownComment(repoAndPull.first, repoAndPull.second));
OrderingStatus pendingStatus = new OrderingStatus(repoAndPull.second, repoAndPull.first);
OrderingStatus pendingStatus = new OrderingStatus(repoAndPull.second);

firebaseService.addDefaultData(new PullRequestIdentifier(repoAndPull.first, repoAndPull.second));

Expand All @@ -80,7 +80,7 @@ public Response onWebhookPost(

} else if (action.equals("synchronize")) {
Pair<GitHubRepository, GitHubPullRequest> repoAndPull = readRepoAndPullFromWebhook(body);
OrderingStatus pendingStatus = new OrderingStatus(repoAndPull.second, repoAndPull.first);
OrderingStatus pendingStatus = new OrderingStatus(repoAndPull.second);
githubService.setOrderingStatus(repoAndPull.second, pendingStatus);
}
} else if (eventType.equals("pull_request_review")) {
Expand All @@ -98,9 +98,9 @@ public Response onWebhookPost(
}

private String constructMarkdownComment(GitHubRepository repo, GitHubPullRequest pullRequest) {
return "This pull request can be reviewed with [Preview Code](" + pullRequest.previewCodeUrl(repo) + ").\n" +
return "This pull request can be reviewed with [Preview Code](" + pullRequest.previewCodeUrl() + ").\n" +
"To speed up the review process and get better feedback on your changes, " +
"please **[order your changes](" + pullRequest.previewCodeUrl(repo) + ").**\n";
"please **[order your changes](" + pullRequest.previewCodeUrl() + ").**\n";
}

private Pair<GitHubRepository, GitHubPullRequest> readRepoAndPullFromWebhook(JsonNode body) throws JsonProcessingException {
Expand Down
64 changes: 32 additions & 32 deletions src/main/java/previewcode/backend/api/v2/ApprovalsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import com.google.inject.Inject;
import io.atlassian.fugue.Unit;
import io.vavr.collection.List;
import previewcode.backend.DTO.*;
import previewcode.backend.services.IDatabaseService;
import previewcode.backend.services.IGithubService;
import previewcode.backend.services.actiondsl.Interpreter;

import javax.inject.Named;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;

import java.io.IOException;

import static previewcode.backend.services.actiondsl.ActionDSL.*;

/**
Expand All @@ -22,6 +24,9 @@ public class ApprovalsAPI {
private Interpreter interpreter;
private IDatabaseService databaseService;

@Inject
private IGithubService githubService;

@Inject
public ApprovalsAPI(@Named("database-interp") Interpreter interpreter, IDatabaseService databaseService) {
this.interpreter = interpreter;
Expand All @@ -30,9 +35,10 @@ public ApprovalsAPI(@Named("database-interp") Interpreter interpreter, IDatabase

/**
* Fetches all approvals and shows if pr/groups/hunks are (dis)approved
* @param owner The owner of the repository
* @param name The name of the repository
* @param number The pullrequest number
*
* @param owner The owner of the repository
* @param name The name of the repository
* @param number The pullrequest number
* @return if the pullrequest and the groups and hunks are disapproved or approved
*/
@Path("getApprovals")
Expand All @@ -45,44 +51,38 @@ public Response getApprovals(@PathParam("owner") String owner,
return interpreter.evaluateToResponse(action);
}

/**
* Fetches all approvals and shows per hunk whom (dis)approved
* @param owner The owner of the repository
* @param name The name of the repository
* @param number The pullrequest number
* @return per hunk the approval status of the reviewers
*/
@Path("getHunkApprovals")
@GET
public Response getHunkApprovals(@PathParam("owner") String owner,
@PathParam("name") String name,
@PathParam("number") Integer number) {
PullRequestIdentifier pull = new PullRequestIdentifier(owner, name, number);
Action<List<HunkApprovals>> action = databaseService.getHunkApprovals(pull);

return interpreter.evaluateToResponse(action);
}

/**
* Sets the approval from a user on a hunk.
* Calls github when the status of the pr changes.
*
* @param owner The owner of the repository
* @param name The name of the repository
* @param number The pullrequest number
* @param body Hunk approval information
* @param owner The owner of the repository
* @param name The name of the repository
* @param number The pullrequest number
* @param body Hunk approval information
* @return A unit response
*/
@Path("setApprove")
@POST
public Response setApprove(@PathParam("owner") String owner,
@PathParam("name") String name,
@PathParam("number") Integer number,
ApproveRequest body) {

// TODO: check if user is a reviewer on this PR
@PathParam("name") String name,
@PathParam("number") Integer number,
ApproveRequest body) throws IOException {

PullRequestIdentifier pull = new PullRequestIdentifier(owner, name, number);
Action<Unit> action = databaseService.setApproval(pull, body);
Action<Unit> action = databaseService.getApproval(pull).map(
approvedPullRequest -> approvedPullRequest.approved
).then(oldStatus -> databaseService.setApproval(pull, body).pure(oldStatus)).then(
oldStatus -> databaseService.getApproval(pull).map(newStatus -> {
if (!oldStatus.equals(newStatus.approved)) {
try {
githubService.setPRStatus(githubService.fetchPullRequest(pull), newStatus.approved);
} catch (IOException e) {
sneakyThrow(e);
}
}
return unit;
})
);

return interpreter.evaluateToResponse(action);
}
Expand Down
Loading