Skip to content
Permalink
Browse files
forge: support commit comments
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Jun 5, 2020
1 parent 69b126b commit dd947454407f74c5bd9aaa79327ef658304360ba
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 7 deletions.
@@ -150,4 +150,13 @@ public List<PullRequest> findPullRequestsWithComment(String author, String body)
public List<HostedBranch> branches() {
return List.of();
}

@Override
public List<CommitComment> commitComments(Hash commit) {
return List.of();
}

@Override
public void addCommitComment(Hash commit, String body) {
}
}
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.skara.forge;

import org.openjdk.skara.host.HostUser;
import org.openjdk.skara.issuetracker.Comment;
import org.openjdk.skara.vcs.Hash;

import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.*;

public class CommitComment extends Comment {
private final Hash commit;
private final Path path;
private final int line;

public CommitComment(Hash commit, Path path, int line, String id, String body, HostUser author, ZonedDateTime createdAt, ZonedDateTime updatedAt) {
super(id, body, author, createdAt, updatedAt);

this.commit = commit;
this.path = path;
this.line = line;
}

/**
* Returns the hash of the commit.
*/
public Hash commit() {
return commit;
}

/**
* Returns the relative path of the file.
*/
public Optional<Path> path() {
return Optional.ofNullable(path);
}

/**
* Returns the line number in the file.
*/
public Optional<Integer> line() {
return line == -1 ? Optional.empty() : Optional.of(line);
}

@Override
public String toString() {
return commit.hex() + ": " + body();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
var other = (CommitComment) o;
return Objects.equals(commit, other.commit) &&
Objects.equals(path, other.path) &&
line == other.line;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), commit, path, line);
}
}
@@ -67,6 +67,8 @@ PullRequest createPullRequest(HostedRepository target,
long id();
Hash branchHash(String ref);
List<HostedBranch> branches();
List<CommitComment> commitComments(Hash hash);
void addCommitComment(Hash hash, String body);

default PullRequest createPullRequest(HostedRepository target,
String targetRef,
@@ -29,6 +29,7 @@

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.regex.Pattern;
@@ -257,4 +258,35 @@ public List<HostedBranch> branches() {
new Hash(b.get("commit").get("sha").asString())))
.collect(Collectors.toList());
}

@Override
public List<CommitComment> commitComments(Hash hash) {
return request.get("commits/" + hash.hex() + "/comments")
.execute()
.stream()
.map(JSONValue::asObject)
.map(o -> {
var line = o.get("line").isNull()? -1 : o.get("line").asInt();
var path = o.get("path").isNull()? null : Path.of(o.get("path").asString());
return new CommitComment(hash,
path,
line,
o.get("id").toString(),
o.get("body").asString(),
gitHubHost.parseUserField(o),
ZonedDateTime.parse(o.get("created_at").asString()),
ZonedDateTime.parse(o.get("updated_at").asString()));


})
.collect(Collectors.toList());
}

@Override
public void addCommitComment(Hash hash, String body) {
var query = JSON.object().put("body", body);
request.post("commits/" + hash.hex() + "/comments")
.body(query)
.execute();
}
}
@@ -121,11 +121,15 @@ public Optional<HostedRepository> repository(String name) {
}
}

private HostUser parseUserDetails(JSONObject details) {
var id = details.get("id").asInt();
var username = details.get("username").asString();
var name = details.get("name").asString();
var email = details.get("email").asString();
HostUser parseAuthorField(JSONValue json) {
return parseAuthorObject(json.get("author").asObject());
}

HostUser parseAuthorObject(JSONObject o) {
var id = o.get("id").asInt();
var username = o.get("username").asString();
var name = o.get("name").asString();
var email = o.get("email").asString();
return new HostUser(id, username, name, email);
}

@@ -145,13 +149,13 @@ public Optional<HostUser> user(String username) {
return Optional.empty();
}

return Optional.of(parseUserDetails(users.get(0).asObject()));
return Optional.of(parseAuthorObject(users.get(0).asObject()));
}

@Override
public HostUser currentUser() {
var details = request.get("user").execute().asObject();
return parseUserDetails(details);
return parseAuthorObject(details);
}

@Override
@@ -29,6 +29,7 @@

import java.net.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
@@ -288,4 +289,37 @@ public List<HostedBranch> branches() {
new Hash(b.get("commit").get("id").asString())))
.collect(Collectors.toList());
}

@Override
public List<CommitComment> commitComments(Hash hash) {
return request.get("commits/" + hash.hex() + "/comments")
.execute()
.stream()
.map(JSONValue::asObject)
.map(o -> {
var line = o.get("line").isNull()? -1 : o.get("line").asInt();
var path = o.get("path").isNull()? null : Path.of(o.get("path").asString());
// GitLab does not offer updated_at for commit comments
var createdAt = ZonedDateTime.parse(o.get("created_at").asString());
// GitLab does not offer an id for commit comments
var id = "";
return new CommitComment(hash,
path,
line,
id,
o.get("note").asString(),
gitLabHost.parseAuthorField(o),
createdAt,
createdAt);
})
.collect(Collectors.toList());
}

@Override
public void addCommitComment(Hash hash, String body) {
var query = JSON.object().put("note", body);
request.post("commits/" + hash.hex() + "/comments")
.body(query)
.execute();
}
}
@@ -40,13 +40,17 @@ public class TestHostedRepository extends TestIssueProject implements HostedRepo
private final String projectName;
private final Repository localRepository;
private final Pattern pullRequestPattern;
private final Map<Hash, List<CommitComment>> commitComments;
private int nextCommitCommentId;

public TestHostedRepository(TestHost host, String projectName, Repository localRepository) {
super(host, projectName);
this.host = host;
this.projectName = projectName;
this.localRepository = localRepository;
pullRequestPattern = Pattern.compile(url().toString() + "/pr/" + "(\\d+)");
commitComments = new HashMap<Hash, List<CommitComment>>();
nextCommitCommentId = 0;
}

@Override
@@ -199,6 +203,24 @@ public List<HostedBranch> branches() {
}
}

@Override
public List<CommitComment> commitComments(Hash hash) {
if (!commitComments.containsKey(hash)) {
return List.of();
}
return commitComments.get(hash);
}

@Override
public void addCommitComment(Hash hash, String body) {
var id = nextCommitCommentId;
nextCommitCommentId += 1;
var createdAt = ZonedDateTime.now();

var comments = commitComments.putIfAbsent(hash, new ArrayList<CommitComment>());
comments.add(new CommitComment(hash, null, -1, Integer.toString(id), body, host.currentUser(), createdAt, createdAt));
}

Repository localRepository() {
return localRepository;
}

0 comments on commit dd94745

Please sign in to comment.