Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forge: add method PullRequest.diff #882

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -295,4 +295,9 @@ public Optional<ZonedDateTime> labelAddedAt(String label) {
public URI headUrl() {
return null;
}

@Override
public Diff diff() {
return null;
}
}
@@ -23,6 +23,7 @@
package org.openjdk.skara.forge;

import org.openjdk.skara.issuetracker.Issue;
import org.openjdk.skara.vcs.Diff;
import org.openjdk.skara.vcs.Hash;

import java.net.URI;
@@ -120,6 +121,11 @@ public interface PullRequest extends Issue {
*/
URI diffUrl();

/** Returns a diff of the changes between PR HEAD and target branch.
* @return
*/
Diff diff();

/**
* Creates a new check.
* @param check
@@ -27,6 +27,7 @@
import org.openjdk.skara.issuetracker.*;
import org.openjdk.skara.json.*;
import org.openjdk.skara.network.*;
import org.openjdk.skara.vcs.Diff;
import org.openjdk.skara.vcs.Hash;

import java.net.URI;
@@ -662,4 +663,10 @@ public Optional<ZonedDateTime> labelAddedAt(String label) {
public URI headUrl() {
return URI.create(webUrl() + "/commits/" + headHash().hex());
}

@Override
public Diff diff() {
var files = request.get("pulls/" + json.get("number").toString() + "/files").execute();
return host.toDiff(targetHash(), headHash(), files);
}
}
@@ -27,6 +27,7 @@
import org.openjdk.skara.issuetracker.*;
import org.openjdk.skara.json.*;
import org.openjdk.skara.network.*;
import org.openjdk.skara.vcs.Diff;
import org.openjdk.skara.vcs.Hash;

import java.net.URI;
@@ -42,12 +43,14 @@ public class GitLabMergeRequest implements PullRequest {
private final RestRequest request;
private final Logger log = Logger.getLogger("org.openjdk.skara.host");;
private final GitLabRepository repository;
private final GitLabHost host;

private Hash targetHash = null;
private List<String> labels;

GitLabMergeRequest(GitLabRepository repository, JSONValue jsonValue, RestRequest request) {
GitLabMergeRequest(GitLabRepository repository, GitLabHost host, JSONValue jsonValue, RestRequest request) {
this.repository = repository;
this.host = host;
this.json = jsonValue;
this.request = request.restrict("merge_requests/" + json.get("iid").toString() + "/");

@@ -736,4 +739,10 @@ public Optional<ZonedDateTime> labelAddedAt(String label) {
public URI headUrl() {
return URI.create(webUrl() + "/diffs?commit_id=" + headHash().hex());
}

@Override
public Diff diff() {
var changes = request.get("changes").execute();
return host.toDiff(targetHash(), headHash(), changes.get("changes"));
}
}
@@ -104,21 +104,21 @@ public PullRequest createPullRequest(HostedRepository target,
.execute();

var targetRepo = (GitLabRepository) target;
return new GitLabMergeRequest(targetRepo, pr, targetRepo.request);
return new GitLabMergeRequest(targetRepo, gitLabHost, pr, targetRepo.request);
}

@Override
public PullRequest pullRequest(String id) {
var pr = request.get("merge_requests/" + id).execute();
return new GitLabMergeRequest(this, pr, request);
return new GitLabMergeRequest(this, gitLabHost, pr, request);
}

@Override
public List<PullRequest> pullRequests() {
return request.get("merge_requests")
.param("state", "opened")
.execute().stream()
.map(value -> new GitLabMergeRequest(this, value, request))
.map(value -> new GitLabMergeRequest(this, gitLabHost, value, request))
.collect(Collectors.toList());
}

@@ -128,7 +128,7 @@ public List<PullRequest> pullRequests(ZonedDateTime updatedAfter) {
.param("order_by", "updated_at")
.param("updated_after", updatedAfter.format(DateTimeFormatter.ISO_DATE_TIME))
.execute().stream()
.map(value -> new GitLabMergeRequest(this, value, request))
.map(value -> new GitLabMergeRequest(this, gitLabHost, value, request))
.collect(Collectors.toList());
}

@@ -26,6 +26,7 @@
import org.openjdk.skara.host.*;
import org.openjdk.skara.issuetracker.IssueProject;
import org.openjdk.skara.network.URIBuilder;
import org.openjdk.skara.vcs.Diff;
import org.openjdk.skara.vcs.Hash;

import java.io.*;
@@ -232,4 +233,22 @@ public Optional<ZonedDateTime> labelAddedAt(String label) {
public URI headUrl() {
return URI.create(webUrl().toString() + "/commits/" + headHash().hex());
}

@Override
public Diff diff() {
try {
var targetLocalRepository = targetRepository.localRepository();
var sourceLocalRepository = sourceRepository.localRepository();
if (targetLocalRepository.root().equals(sourceLocalRepository.root())) {
// same target and source repo, must contain both commits
return targetLocalRepository.diff(targetHash(), headHash());
} else {
var uri = URI.create("file://" + sourceLocalRepository.root().toString());
var fetchHead = targetLocalRepository.fetch(uri, sourceRef);
return targetLocalRepository.diff(targetHash(), fetchHead);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}