Skip to content

Commit e75f490

Browse files
authored
Merge pull request #1855 from bibonix/fix-1072-event-commit-id
#1072: add Event.Smart#commitId()
2 parents 650b38d + a4ef8c7 commit e75f490

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

src/main/java/com/jcabi/github/Event.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,18 @@ public Date createdAt() throws IOException {
226226
}
227227
}
228228

229+
/**
230+
* SHA of the commit referenced by this event (if any).
231+
* @return SHA of the commit, or absent if not associated with one
232+
* @throws IOException If there is any I/O problem
233+
* @since 1.7.0
234+
*/
235+
public Optional<String> commitId() throws IOException {
236+
return Optional.fromNullable(
237+
this.event.json().getString("commit_id", null)
238+
);
239+
}
240+
229241
/**
230242
* Label that was added or removed in this event (if any).
231243
* @return Label that was added or removed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2013-2026 Yegor Bugayenko
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
package com.jcabi.github;
6+
7+
import com.google.common.base.Optional;
8+
import jakarta.json.Json;
9+
import jakarta.json.JsonObject;
10+
import jakarta.json.JsonValue;
11+
import java.io.IOException;
12+
import org.hamcrest.MatcherAssert;
13+
import org.hamcrest.Matchers;
14+
import org.junit.jupiter.api.Test;
15+
import org.mockito.Mockito;
16+
17+
/**
18+
* Test case for {@link Event.Smart}.
19+
* @since 1.7.0
20+
*/
21+
final class EventTest {
22+
23+
@Test
24+
void readsCommitIdWhenPresent() throws IOException {
25+
final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db51";
26+
MatcherAssert.assertThat(
27+
"Commit SHA is not equal",
28+
new Event.Smart(
29+
EventTest.eventWithJson(
30+
Json.createObjectBuilder()
31+
.add("commit_id", sha)
32+
.build()
33+
)
34+
).commitId(),
35+
Matchers.equalTo(Optional.of(sha))
36+
);
37+
}
38+
39+
@Test
40+
void commitIdIsAbsentWhenJsonNull() throws IOException {
41+
MatcherAssert.assertThat(
42+
"Commit ID should be absent when JSON value is null",
43+
new Event.Smart(
44+
EventTest.eventWithJson(
45+
Json.createObjectBuilder()
46+
.add("commit_id", JsonValue.NULL)
47+
.build()
48+
)
49+
).commitId(),
50+
Matchers.equalTo(Optional.<String>absent())
51+
);
52+
}
53+
54+
@Test
55+
void commitIdIsAbsentWhenMissing() throws IOException {
56+
MatcherAssert.assertThat(
57+
"Commit ID should be absent when key is missing",
58+
new Event.Smart(
59+
EventTest.eventWithJson(
60+
Json.createObjectBuilder().build()
61+
)
62+
).commitId(),
63+
Matchers.equalTo(Optional.<String>absent())
64+
);
65+
}
66+
67+
/**
68+
* Build an Event whose json() method returns the given object.
69+
* @param obj JSON object to expose via Event.json()
70+
* @return Mocked Event
71+
* @throws IOException If stubbing fails
72+
*/
73+
private static Event eventWithJson(final JsonObject obj)
74+
throws IOException {
75+
final Event event = Mockito.mock(Event.class);
76+
Mockito.doReturn(obj).when(event).json();
77+
return event;
78+
}
79+
}

0 commit comments

Comments
 (0)