Skip to content

Commit 43cee9f

Browse files
akashRindhewwong
authored andcommitted
(feat) Add method to list instances of code scanning alert
[#1133]
1 parent 1ac6f4f commit 43cee9f

12 files changed

+765
-6
lines changed

src/main/java/org/kohsuke/github/GHCodeScanningAlert.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ public GHCodeScanningAlertInstance getMostRecentInstance() {
128128
return most_recent_instance;
129129
}
130130

131+
/**
132+
* List all instances of the alert
133+
*
134+
* @return the paged iterable
135+
*/
136+
public PagedIterable<GHCodeScanningAlertInstance> listAlertInstances() {
137+
return new GHCodeScanningAlertInstancesIterable(this, root.createRequest().withUrlPath(instances_url));
138+
}
139+
131140
@Override
132141
public URL getHtmlUrl() throws IOException {
133142
return GitHubClient.parseURL(html_url);

src/main/java/org/kohsuke/github/GHCodeScanningAlertInstance.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package org.kohsuke.github;
22

3-
import com.fasterxml.jackson.annotation.JsonIgnore;
43
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
54

65
import java.util.Arrays;
76
import java.util.Collections;
87
import java.util.List;
98

109
public class GHCodeScanningAlertInstance {
11-
12-
@JsonIgnore
13-
private GHCodeScanningAlert owner;
14-
1510
private String ref;
1611
private String analysis_key;
1712
private String environment;
@@ -55,7 +50,6 @@ public Location getLocation() {
5550

5651
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
5752
static class Message {
58-
5953
private String text;
6054

6155
public String getText() {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.kohsuke.github;
2+
3+
import java.net.MalformedURLException;
4+
import java.util.Iterator;
5+
6+
import javax.annotation.Nonnull;
7+
8+
public class GHCodeScanningAlertInstancesIterable extends PagedIterable<GHCodeScanningAlertInstance> {
9+
private final GHCodeScanningAlert owner;
10+
private final GitHubRequest request;
11+
private GHCodeScanningAlertInstance[] result;
12+
13+
public GHCodeScanningAlertInstancesIterable(GHCodeScanningAlert owner, GitHubRequest.Builder<?> requestBuilder) {
14+
this.owner = owner;
15+
try {
16+
this.request = requestBuilder.build();
17+
} catch (MalformedURLException e) {
18+
throw new GHException("Malformed URL", e);
19+
}
20+
}
21+
22+
@Nonnull
23+
@Override
24+
public PagedIterator<GHCodeScanningAlertInstance> _iterator(int pageSize) {
25+
return new PagedIterator<>(
26+
adapt(GitHubPageIterator
27+
.create(owner.getRoot().getClient(), GHCodeScanningAlertInstance[].class, request, pageSize)),
28+
null);
29+
}
30+
31+
protected Iterator<GHCodeScanningAlertInstance[]> adapt(final Iterator<GHCodeScanningAlertInstance[]> base) {
32+
return new Iterator<GHCodeScanningAlertInstance[]>() {
33+
public boolean hasNext() {
34+
return base.hasNext();
35+
}
36+
37+
public GHCodeScanningAlertInstance[] next() {
38+
GHCodeScanningAlertInstance[] v = base.next();
39+
if (result == null) {
40+
result = v;
41+
}
42+
return result;
43+
}
44+
};
45+
}
46+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.Assume;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.IOException;
8+
import java.util.List;
9+
10+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
11+
import static org.hamcrest.Matchers.not;
12+
13+
/**
14+
* <p>
15+
* Note : As the code scanning alerts cannot be tailored as part of test setup, lot of the test cases are dependent on
16+
* manual setup of the mock repo. Assertions and verifications will often simply check that the values are non-null
17+
* rather than depending on hard-coded values, to prevent making the tests flimsy
18+
* </p>
19+
*/
20+
public class GHCodeScanningAlertInstanceTest extends AbstractGitHubWireMockTest {
21+
private static final String REPO_NAME = "Pixi";
22+
private GHCodeScanningAlert alert;
23+
24+
@Before
25+
public void setUp() throws Exception {
26+
GHRepository repo = gitHub.getRepository(GITHUB_API_TEST_ORG + "/" + REPO_NAME);
27+
alert = getAlertFromRepo(repo);
28+
}
29+
30+
private GHCodeScanningAlert getAlertFromRepo(GHRepository repo) {
31+
List<GHCodeScanningAlert> dismissedAlerts = repo.listCodeScanningAlerts(GHCodeScanningAlertState.DISMISSED)
32+
._iterator(1)
33+
.nextPage();
34+
Assume.assumeThat(dismissedAlerts.size(), greaterThanOrEqualTo(1));
35+
return dismissedAlerts.get(0);
36+
}
37+
38+
@Test
39+
public void testListAlertInstances() throws IOException {
40+
// Arrange
41+
42+
// Act
43+
List<GHCodeScanningAlertInstance> results = alert.listAlertInstances().toList();
44+
45+
// Assert
46+
assertThat(results.size(), greaterThanOrEqualTo(1));
47+
GHCodeScanningAlertInstance instance = results.get(0);
48+
// Can't assert on exact values with having to hardcode values from
49+
// json file, hence making the assertions generics
50+
assertThat(instance.getRef(), not((Object) null));
51+
assertThat(instance.getCommitSha(), not((Object) null));
52+
assertThat(instance.getState(), not((Object) null));
53+
assertThat(instance.getMessage(), not((Object) null));
54+
assertThat(instance.getLocation(), not((Object) null));
55+
56+
GHCodeScanningAlertInstance.Location location = instance.getLocation();
57+
// Can't assert on exact values with having to hardcode values from
58+
// json file, hence making the assertions generics
59+
assertThat(location.getPath(), not((Object) null));
60+
assertThat(location.getStartLine(), greaterThanOrEqualTo(0L));
61+
assertThat(location.getEndLine(), greaterThanOrEqualTo(0L));
62+
assertThat(location.getStartColumn(), greaterThanOrEqualTo(0L));
63+
assertThat(location.getStartColumn(), greaterThanOrEqualTo(0L));
64+
}
65+
}

0 commit comments

Comments
 (0)