Skip to content

Commit 9d919a2

Browse files
akashRindhewwong
authored andcommitted
(feat) Add methods to list code scanning alerts on a repository
3 methods added - 1 with no filter, 1 with state filter and 1 with tool name filter. [#1133]
1 parent 953e5c4 commit 9d919a2

File tree

11 files changed

+882
-0
lines changed

11 files changed

+882
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.kohsuke.github;
2+
3+
import java.net.MalformedURLException;
4+
import java.util.Iterator;
5+
6+
import javax.annotation.Nonnull;
7+
8+
class GHCodeScanningAlertsIterable extends PagedIterable<GHCodeScanningAlert> {
9+
private final GHRepository owner;
10+
private final GitHubRequest request;
11+
private GHCodeScanningAlert[] result;
12+
13+
public GHCodeScanningAlertsIterable(GHRepository 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<GHCodeScanningAlert> _iterator(int pageSize) {
25+
return new PagedIterator<>(
26+
adapt(GitHubPageIterator
27+
.create(owner.getRoot().getClient(), GHCodeScanningAlert[].class, request, pageSize)),
28+
null);
29+
}
30+
31+
protected Iterator<GHCodeScanningAlert[]> adapt(final Iterator<GHCodeScanningAlert[]> base) {
32+
return new Iterator<GHCodeScanningAlert[]>() {
33+
public boolean hasNext() {
34+
return base.hasNext();
35+
}
36+
37+
public GHCodeScanningAlert[] next() {
38+
GHCodeScanningAlert[] v = base.next();
39+
if (result == null) {
40+
result = v;
41+
}
42+
43+
for (GHCodeScanningAlert alert : result) {
44+
alert.wrap(owner);
45+
}
46+
return result;
47+
}
48+
};
49+
}
50+
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3511,6 +3511,42 @@ public GHTagObject createTag(String tag, String message, String object, String t
35113511
.wrap(this);
35123512
}
35133513

3514+
/**
3515+
* Lists the code scanning alerts of this repository.
3516+
*
3517+
* @return the paged iterable
3518+
*/
3519+
public PagedIterable<GHCodeScanningAlert> listCodeScanningAlerts() {
3520+
return listCodeScanningAlerts(Collections.emptyMap());
3521+
}
3522+
3523+
/**
3524+
* Lists the code scanning alerts of this repository filtered on the alert status
3525+
*
3526+
* @param state
3527+
* alert status to filter on
3528+
* @return the paged iterable
3529+
*/
3530+
public PagedIterable<GHCodeScanningAlert> listCodeScanningAlerts(GHCodeScanningAlertState state) {
3531+
return listCodeScanningAlerts(Collections.singletonMap("state", state.name().toLowerCase()));
3532+
}
3533+
3534+
/**
3535+
* Lists the code scanning alerts of this repository filtered on the code scanning tool name
3536+
*
3537+
* @param toolName
3538+
* name of code scanning tool that creates alerts
3539+
* @return the paged iterable
3540+
*/
3541+
public PagedIterable<GHCodeScanningAlert> listCodeScanningAlerts(String toolName) {
3542+
return listCodeScanningAlerts(Collections.singletonMap("tool_name", toolName));
3543+
}
3544+
3545+
private PagedIterable<GHCodeScanningAlert> listCodeScanningAlerts(Map<String, Object> filters) {
3546+
return new GHCodeScanningAlertsIterable(this,
3547+
root.createRequest().withUrlPath(getApiTailUrl("code-scanning/alerts")).with(filters));
3548+
}
3549+
35143550
/**
35153551
* Streams a zip archive of the repository, optionally at a given <code>ref</code>.
35163552
*
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.List;
7+
8+
import static org.hamcrest.Matchers.equalTo;
9+
import static org.hamcrest.Matchers.is;
10+
import static org.hamcrest.Matchers.not;
11+
12+
/**
13+
* <p>
14+
* Note : As the code scanning alerts cannot be tailored as part of test setup, lot of the test cases are dependent on
15+
* manual setup of the mock repo. Assertions and verifications will often simply check that the values are non-null
16+
* rather than depending on hard-coded values, to prevent making the tests flimsy
17+
* </p>
18+
*/
19+
public class GHCodeScanningAlertTest extends AbstractGitHubWireMockTest {
20+
private static final String REPO_NAME = "Pixi";
21+
private GHRepository repo;
22+
23+
@Before
24+
public void setUp() throws Exception {
25+
repo = gitHub.getRepository(GITHUB_API_TEST_ORG + "/" + REPO_NAME);
26+
}
27+
28+
@Test
29+
public void testListCodeScanningAlerts() {
30+
// Arrange
31+
32+
// Act - Search by filtering on code scanning tool
33+
List<GHCodeScanningAlert> codeQlAlerts = repo.listCodeScanningAlerts("CodeQL")._iterator(2).nextPage();
34+
35+
// Assert
36+
assertThat(codeQlAlerts.size(), equalTo(2)); // This assertion is based on manual setup done on repo to
37+
// guarantee there are atleast 2 issues
38+
39+
GHCodeScanningAlert alert = codeQlAlerts.get(0);
40+
41+
// Verify the code scanning tool details
42+
assertThat(alert.getTool(), not((Object) null));
43+
GHCodeScanningAlert.Tool tool = alert.getTool();
44+
assertThat(tool.getName(), is("CodeQL"));
45+
assertThat(tool.getVersion(), not((Object) null));
46+
47+
// Verify the generic values of the code scanning rule
48+
assertThat(alert.getRule(), not((Object) null));
49+
GHCodeScanningAlert.Rule rule = alert.getRule();
50+
assertThat(rule.getId(), not((Object) null));
51+
assertThat(rule.getName(), not((Object) null));
52+
assertThat(rule.getSeverity(), not((Object) null));
53+
54+
// Act - Search by filtering on alert status
55+
List<GHCodeScanningAlert> openAlerts = repo.listCodeScanningAlerts(GHCodeScanningAlertState.OPEN)
56+
._iterator(2)
57+
.nextPage(); // This assertion is based on manual setup done on repo to
58+
// guarantee there are atleast 2 issues
59+
60+
// Assert
61+
assertThat(openAlerts.size(), equalTo(2));
62+
GHCodeScanningAlert openAlert = openAlerts.get(0);
63+
assertThat(openAlert.getState(), is(GHCodeScanningAlertState.OPEN));
64+
}
65+
66+
}

0 commit comments

Comments
 (0)