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
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@
<plugin>
<groupId>com.qulice</groupId>
<artifactId>qulice-maven-plugin</artifactId>
<version>0.25.1</version>
<configuration>
<excludes combine.children="append">
<exclude>xml:/src/it/settings.xml</exclude>
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/jcabi/github/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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
Expand Down
79 changes: 79 additions & 0 deletions src/test/java/com/jcabi/github/EventTest.java
Original file line number Diff line number Diff line change
@@ -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.<String>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.<String>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;
}
}
Loading