Skip to content
Permalink
Browse files
forge: add commitMetadata method to HostedRepository
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Jun 5, 2020
1 parent dd94745 commit c11028a093e0a2b0f4180d02ffc46ce18b5b1361
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
@@ -159,4 +159,9 @@ public List<CommitComment> commitComments(Hash commit) {
@Override
public void addCommitComment(Hash commit, String body) {
}

@Override
public Optional<CommitMetadata> commitMetadata(Hash commit) {
return Optional.empty();
}
}
@@ -69,6 +69,7 @@ PullRequest createPullRequest(HostedRepository target,
List<HostedBranch> branches();
List<CommitComment> commitComments(Hash hash);
void addCommitComment(Hash hash, String body);
Optional<CommitMetadata> commitMetadata(Hash hash);

default PullRequest createPullRequest(HostedRepository target,
String targetRef,
@@ -289,4 +289,24 @@ public void addCommitComment(Hash hash, String body) {
.body(query)
.execute();
}

@Override
public Optional<CommitMetadata> commitMetadata(Hash hash) {
var c = request.get("commits/" + hash.hex())
.onError(r -> Optional.of(JSON.of()))
.execute();
if (c.isNull()) {
return Optional.empty();
}
var parents = c.get("parents").stream()
.map(p -> new Hash(p.get("sha").asString()))
.collect(Collectors.toList());
var author = new Author(c.get("author").get("name").asString(),
c.get("author").get("email").asString());
var committer = new Author(c.get("committer").get("name").asString(),
c.get("committer").get("email").asString());
var date = ZonedDateTime.parse(c.get("author").get("date").asString());
var message = Arrays.asList(c.get("message").asString().split("\n"));
return Optional.of(new CommitMetadata(hash, parents, author, committer, date, message));
}
}
@@ -322,4 +322,25 @@ public void addCommitComment(Hash hash, String body) {
.body(query)
.execute();
}

@Override
public Optional<CommitMetadata> commitMetadata(Hash hash) {
var c = request.get("commits/" + hash.hex())
.onError(r -> Optional.of(JSON.of()))
.execute();
if (c.isNull()) {
return Optional.empty();
}
var parents = c.get("parent_ids").stream()
.map(JSONValue::asString)
.map(Hash::new)
.collect(Collectors.toList());
var author = new Author(c.get("author_name").asString(),
c.get("author_email").asString());
var committer = new Author(c.get("committer_name").asString(),
c.get("committer_email").asString());
var date = ZonedDateTime.parse(c.get("authored_date").asString());
var message = Arrays.asList(c.get("message").asString().split("\n"));
return Optional.of(new CommitMetadata(hash, parents, author, committer, date, message));
}
}
@@ -221,6 +221,15 @@ public void addCommitComment(Hash hash, String body) {
comments.add(new CommitComment(hash, null, -1, Integer.toString(id), body, host.currentUser(), createdAt, createdAt));
}

@Override
public Optional<CommitMetadata> commitMetadata(Hash hash) {
try {
return localRepository.commitMetadata(hash);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

Repository localRepository() {
return localRepository;
}
@@ -49,6 +49,16 @@ public interface ReadOnlyRepository {
Optional<Commit> lookup(Branch b) throws IOException;
Optional<Commit> lookup(Tag t) throws IOException;
List<CommitMetadata> commitMetadata() throws IOException;
default Optional<CommitMetadata> commitMetadata(Hash hash) throws IOException {
var l = commitMetadata(range(hash));
if (l.size() > 1) {
throw new IllegalStateException("More than one commit for hash: " + hash.hex());
}
if (l.size() == 0) {
return Optional.empty();
}
return Optional.of(l.get(0));
}
List<CommitMetadata> commitMetadata(boolean reverse) throws IOException;
List<CommitMetadata> commitMetadata(String range) throws IOException;
List<CommitMetadata> commitMetadata(Hash from, Hash to) throws IOException;

0 comments on commit c11028a

Please sign in to comment.