diff --git a/pom.xml b/pom.xml index 23036f68f..ede892cbe 100644 --- a/pom.xml +++ b/pom.xml @@ -304,7 +304,6 @@ com.qulice qulice-maven-plugin - 0.25.1 xml:/src/it/settings.xml diff --git a/src/main/java/com/jcabi/github/Event.java b/src/main/java/com/jcabi/github/Event.java index 8983dc757..5488eae07 100644 --- a/src/main/java/com/jcabi/github/Event.java +++ b/src/main/java/com/jcabi/github/Event.java @@ -226,6 +226,18 @@ public Date createdAt() throws IOException { } } + /** + * SHA of the commit referenced by this event (if any). + * @return SHA of the commit, or absent if not associated with one + * @throws IOException If there is any I/O problem + * @since 1.7.0 + */ + public Optional commitId() throws IOException { + return Optional.fromNullable( + this.event.json().getString("commit_id", null) + ); + } + /** * Label that was added or removed in this event (if any). * @return Label that was added or removed diff --git a/src/test/java/com/jcabi/github/EventTest.java b/src/test/java/com/jcabi/github/EventTest.java new file mode 100644 index 000000000..e388ebf57 --- /dev/null +++ b/src/test/java/com/jcabi/github/EventTest.java @@ -0,0 +1,79 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2013-2026 Yegor Bugayenko + * SPDX-License-Identifier: MIT + */ +package com.jcabi.github; + +import com.google.common.base.Optional; +import jakarta.json.Json; +import jakarta.json.JsonObject; +import jakarta.json.JsonValue; +import java.io.IOException; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +/** + * Test case for {@link Event.Smart}. + * @since 1.7.0 + */ +final class EventTest { + + @Test + void readsCommitIdWhenPresent() throws IOException { + final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db51"; + MatcherAssert.assertThat( + "Commit SHA is not equal", + new Event.Smart( + EventTest.eventWithJson( + Json.createObjectBuilder() + .add("commit_id", sha) + .build() + ) + ).commitId(), + Matchers.equalTo(Optional.of(sha)) + ); + } + + @Test + void commitIdIsAbsentWhenJsonNull() throws IOException { + MatcherAssert.assertThat( + "Commit ID should be absent when JSON value is null", + new Event.Smart( + EventTest.eventWithJson( + Json.createObjectBuilder() + .add("commit_id", JsonValue.NULL) + .build() + ) + ).commitId(), + Matchers.equalTo(Optional.absent()) + ); + } + + @Test + void commitIdIsAbsentWhenMissing() throws IOException { + MatcherAssert.assertThat( + "Commit ID should be absent when key is missing", + new Event.Smart( + EventTest.eventWithJson( + Json.createObjectBuilder().build() + ) + ).commitId(), + Matchers.equalTo(Optional.absent()) + ); + } + + /** + * Build an Event whose json() method returns the given object. + * @param obj JSON object to expose via Event.json() + * @return Mocked Event + * @throws IOException If stubbing fails + */ + private static Event eventWithJson(final JsonObject obj) + throws IOException { + final Event event = Mockito.mock(Event.class); + Mockito.doReturn(obj).when(event).json(); + return event; + } +}