Skip to content

Commit 1ac6f4f

Browse files
akashRindhewwong
authored andcommitted
(feat) Add function to search code scanning alert by id
[#1133]
1 parent 9d919a2 commit 1ac6f4f

File tree

10 files changed

+752
-1
lines changed

10 files changed

+752
-1
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3547,6 +3547,22 @@ private PagedIterable<GHCodeScanningAlert> listCodeScanningAlerts(Map<String, Ob
35473547
root.createRequest().withUrlPath(getApiTailUrl("code-scanning/alerts")).with(filters));
35483548
}
35493549

3550+
/**
3551+
* Get code scanning alert by id
3552+
*
3553+
* @param id
3554+
* id of the code scanning alert
3555+
* @return the code scanning alert
3556+
* @throws IOException
3557+
* the io exception
3558+
*/
3559+
public GHCodeScanningAlert getCodeScanningAlert(long id) throws IOException {
3560+
return root.createRequest()
3561+
.withUrlPath(getApiTailUrl("code-scanning/alerts"), String.valueOf(id))
3562+
.fetch(GHCodeScanningAlert.class)
3563+
.wrap(this);
3564+
}
3565+
35503566
/**
35513567
* Streams a zip archive of the repository, optionally at a given <code>ref</code>.
35523568
*

src/test/java/org/kohsuke/github/GHCodeScanningAlertTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.kohsuke.github;
22

3+
import org.junit.Assume;
34
import org.junit.Before;
45
import org.junit.Test;
56

7+
import java.io.IOException;
68
import java.util.List;
79

810
import static org.hamcrest.Matchers.equalTo;
11+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
912
import static org.hamcrest.Matchers.is;
1013
import static org.hamcrest.Matchers.not;
1114

@@ -44,7 +47,7 @@ public void testListCodeScanningAlerts() {
4447
assertThat(tool.getName(), is("CodeQL"));
4548
assertThat(tool.getVersion(), not((Object) null));
4649

47-
// Verify the generic values of the code scanning rule
50+
// Verify that fields of the code scanning rule are non-null
4851
assertThat(alert.getRule(), not((Object) null));
4952
GHCodeScanningAlert.Rule rule = alert.getRule();
5053
assertThat(rule.getId(), not((Object) null));
@@ -63,4 +66,25 @@ public void testListCodeScanningAlerts() {
6366
assertThat(openAlert.getState(), is(GHCodeScanningAlertState.OPEN));
6467
}
6568

69+
@Test
70+
public void testGetCodeScanningAlert() throws IOException {
71+
// Arrange
72+
List<GHCodeScanningAlert> dismissedAlerts = repo.listCodeScanningAlerts(GHCodeScanningAlertState.DISMISSED)
73+
._iterator(1)
74+
.nextPage();
75+
Assume.assumeThat(dismissedAlerts.size(), greaterThanOrEqualTo(1));
76+
GHCodeScanningAlert dismissedAlert = dismissedAlerts.get(0);
77+
long idOfDismissed = dismissedAlert.getId();
78+
79+
// Act
80+
GHCodeScanningAlert result = repo.getCodeScanningAlert(idOfDismissed);
81+
82+
// Assert
83+
assertThat(result, not((Object) null));
84+
assertThat(result.getId(), equalTo(idOfDismissed));
85+
assertThat(result.getDismissedReason(), equalTo(dismissedAlert.getDismissedReason()));
86+
assertThat(result.getDismissedAt(), equalTo(dismissedAlert.getDismissedAt()));
87+
assertThat(result.getDismissedBy().login, equalTo(dismissedAlert.getDismissedBy().login));
88+
}
89+
6690
}

src/test/resources/org/kohsuke/github/GHCodeScanningAlertTest/wiremock/testGetCodeScanningAlert/__files/repos_hub4j-test-org_pixi-2.json

Lines changed: 332 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
[
2+
{
3+
"number": 1,
4+
"created_at": "2021-05-17T14:55:01Z",
5+
"url": "https://api.github.com/repos/hub4j-test-org/Pixi/code-scanning/alerts/1",
6+
"html_url": "https://github.com/hub4j-test-org/Pixi/security/code-scanning/1",
7+
"state": "dismissed",
8+
"dismissed_by": {
9+
"login": "akashRindhe",
10+
"id": 14114123,
11+
"node_id": "MDQ6VXNlcjE0MTE0MTIz",
12+
"avatar_url": "https://avatars.githubusercontent.com/u/14114123?v=4",
13+
"gravatar_id": "",
14+
"url": "https://api.github.com/users/akashRindhe",
15+
"html_url": "https://github.com/akashRindhe",
16+
"followers_url": "https://api.github.com/users/akashRindhe/followers",
17+
"following_url": "https://api.github.com/users/akashRindhe/following{/other_user}",
18+
"gists_url": "https://api.github.com/users/akashRindhe/gists{/gist_id}",
19+
"starred_url": "https://api.github.com/users/akashRindhe/starred{/owner}{/repo}",
20+
"subscriptions_url": "https://api.github.com/users/akashRindhe/subscriptions",
21+
"organizations_url": "https://api.github.com/users/akashRindhe/orgs",
22+
"repos_url": "https://api.github.com/users/akashRindhe/repos",
23+
"events_url": "https://api.github.com/users/akashRindhe/events{/privacy}",
24+
"received_events_url": "https://api.github.com/users/akashRindhe/received_events",
25+
"type": "User",
26+
"site_admin": false
27+
},
28+
"dismissed_at": "2021-05-18T01:45:16Z",
29+
"dismissed_reason": "used in tests",
30+
"rule": {
31+
"id": "js/angular/disabling-sce",
32+
"severity": "warning",
33+
"description": "Disabling SCE",
34+
"name": "js/angular/disabling-sce"
35+
},
36+
"tool": {
37+
"name": "CodeQL",
38+
"guid": null,
39+
"version": "2.5.4"
40+
},
41+
"most_recent_instance": {
42+
"ref": "refs/heads/master",
43+
"analysis_key": ".github/workflows/codeql-analysis.yml:analyze",
44+
"environment": "{\"language\":\"javascript\"}",
45+
"state": "dismissed",
46+
"commit_sha": "b3cfb0474bb3d5b5cd499a17e448281abbd256d7",
47+
"message": {
48+
"text": "Disabling SCE is strongly discouraged."
49+
},
50+
"location": {
51+
"path": "app/pixi.html",
52+
"start_line": 179,
53+
"end_line": 179,
54+
"start_column": 4,
55+
"end_column": 31
56+
},
57+
"classifications": []
58+
},
59+
"instances_url": "https://api.github.com/repos/hub4j-test-org/Pixi/code-scanning/alerts/1/instances"
60+
}
61+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"number": 1,
3+
"created_at": "2021-05-17T14:55:01Z",
4+
"url": "https://api.github.com/repos/hub4j-test-org/Pixi/code-scanning/alerts/1",
5+
"html_url": "https://github.com/hub4j-test-org/Pixi/security/code-scanning/1",
6+
"state": "dismissed",
7+
"dismissed_by": {
8+
"login": "akashRindhe",
9+
"id": 14114123,
10+
"node_id": "MDQ6VXNlcjE0MTE0MTIz",
11+
"avatar_url": "https://avatars.githubusercontent.com/u/14114123?v=4",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/akashRindhe",
14+
"html_url": "https://github.com/akashRindhe",
15+
"followers_url": "https://api.github.com/users/akashRindhe/followers",
16+
"following_url": "https://api.github.com/users/akashRindhe/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/akashRindhe/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/akashRindhe/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/akashRindhe/subscriptions",
20+
"organizations_url": "https://api.github.com/users/akashRindhe/orgs",
21+
"repos_url": "https://api.github.com/users/akashRindhe/repos",
22+
"events_url": "https://api.github.com/users/akashRindhe/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/akashRindhe/received_events",
24+
"type": "User",
25+
"site_admin": false
26+
},
27+
"dismissed_at": "2021-05-18T01:45:16Z",
28+
"dismissed_reason": "used in tests",
29+
"rule": {
30+
"id": "js/angular/disabling-sce",
31+
"severity": "warning",
32+
"description": "Disabling SCE",
33+
"name": "js/angular/disabling-sce",
34+
"full_description": "Disabling strict contextual escaping (SCE) can cause security vulnerabilities.",
35+
"tags": [
36+
"frameworks/angularjs",
37+
"maintainability",
38+
"security"
39+
],
40+
"help": "# Disabling SCE\nAngularJS is secure by default through automated sanitization and filtering of untrusted values that could cause vulnerabilities such as XSS. Strict Contextual Escaping (SCE) is an execution mode in AngularJS that provides this security mechanism.\n\nDisabling SCE in an AngularJS application is strongly discouraged. It is even more discouraged to disable SCE in a library, since it is an application-wide setting.\n\n\n## Recommendation\nDo not disable SCE.\n\n\n## Example\nThe following example shows an AngularJS application that disables SCE in order to dynamically construct an HTML fragment, which is later inserted into the DOM through `$scope.html`.\n\n\n```javascript\nangular.module('app', [])\n .config(function($sceProvider) {\n $sceProvider.enabled(false); // BAD\n }).controller('controller', function($scope) {\n // ...\n $scope.html = '<ul><li>' + item.toString() + '</li></ul>';\n });\n\n```\nThis is problematic, since it disables SCE for the entire AngularJS application.\n\nInstead, just mark the dynamically constructed HTML fragment as safe using `$sce.trustAsHtml`, before assigning it to `$scope.html`:\n\n\n```javascript\nangular.module('app', [])\n .controller('controller', function($scope, $sce) {\n // ...\n // GOOD (but should use the templating system instead)\n $scope.html = $sce.trustAsHtml('<ul><li>' + item.toString() + '</li></ul>'); \n });\n\n```\nPlease note that this example is for illustrative purposes only; use the AngularJS templating system to dynamically construct HTML when possible.\n\n\n## References\n* AngularJS Developer Guide: [Strict Contextual Escaping](https://docs.angularjs.org/api/ng/service/$sce)\n* AngularJS Developer Guide: [Can I disable SCE completely?](https://docs.angularjs.org/api/ng/service/$sce#can-i-disable-sce-completely-).\n"
41+
},
42+
"tool": {
43+
"name": "CodeQL",
44+
"guid": null,
45+
"version": "2.5.4"
46+
},
47+
"most_recent_instance": {
48+
"ref": "refs/heads/master",
49+
"analysis_key": ".github/workflows/codeql-analysis.yml:analyze",
50+
"environment": "{\"language\":\"javascript\"}",
51+
"state": "dismissed",
52+
"commit_sha": "b3cfb0474bb3d5b5cd499a17e448281abbd256d7",
53+
"message": {
54+
"text": "Disabling SCE is strongly discouraged."
55+
},
56+
"location": {
57+
"path": "app/pixi.html",
58+
"start_line": 179,
59+
"end_line": 179,
60+
"start_column": 4,
61+
"end_column": 31
62+
},
63+
"classifications": []
64+
},
65+
"instances_url": "https://api.github.com/repos/hub4j-test-org/Pixi/code-scanning/alerts/1/instances",
66+
"instances": [
67+
{
68+
"ref": "refs/heads/master",
69+
"analysis_key": ".github/workflows/codeql-analysis.yml:analyze",
70+
"environment": "{\"language\":\"javascript\"}",
71+
"state": "dismissed",
72+
"commit_sha": "b3cfb0474bb3d5b5cd499a17e448281abbd256d7",
73+
"message": {
74+
"text": "Disabling SCE is strongly discouraged."
75+
},
76+
"location": {
77+
"path": "app/pixi.html",
78+
"start_line": 179,
79+
"end_line": 179,
80+
"start_column": 4,
81+
"end_column": 31
82+
},
83+
"classifications": []
84+
}
85+
]
86+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"login": "akashRindhe",
3+
"id": 14114123,
4+
"node_id": "MDQ6VXNlcjE0MTE0MTIz",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/14114123?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/akashRindhe",
8+
"html_url": "https://github.com/akashRindhe",
9+
"followers_url": "https://api.github.com/users/akashRindhe/followers",
10+
"following_url": "https://api.github.com/users/akashRindhe/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/akashRindhe/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/akashRindhe/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/akashRindhe/subscriptions",
14+
"organizations_url": "https://api.github.com/users/akashRindhe/orgs",
15+
"repos_url": "https://api.github.com/users/akashRindhe/repos",
16+
"events_url": "https://api.github.com/users/akashRindhe/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/akashRindhe/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Akash Rindhe",
21+
"company": null,
22+
"blog": "",
23+
"location": "Singapore",
24+
"email": null,
25+
"hireable": null,
26+
"bio": null,
27+
"twitter_username": null,
28+
"public_repos": 10,
29+
"public_gists": 0,
30+
"followers": 0,
31+
"following": 6,
32+
"created_at": "2015-09-03T18:07:43Z",
33+
"updated_at": "2021-05-17T14:48:09Z",
34+
"private_gists": 0,
35+
"total_private_repos": 4,
36+
"owned_private_repos": 4,
37+
"disk_usage": 24553,
38+
"collaborators": 1,
39+
"two_factor_authentication": true,
40+
"plan": {
41+
"name": "free",
42+
"space": 976562499,
43+
"collaborators": 0,
44+
"private_repos": 10000
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"id": "eaf33f02-185f-4be9-a97b-924271e6e7c5",
3+
"name": "repos_hub4j-test-org_pixi",
4+
"request": {
5+
"url": "/repos/hub4j-test-org/Pixi",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "repos_hub4j-test-org_pixi-2.json",
16+
"headers": {
17+
"Server": "GitHub.com",
18+
"Date": "Tue, 18 May 2021 01:47:57 GMT",
19+
"Content-Type": "application/json; charset=utf-8",
20+
"Cache-Control": "private, max-age=60, s-maxage=60",
21+
"Vary": [
22+
"Accept, Authorization, Cookie, X-GitHub-OTP",
23+
"Accept-Encoding, Accept, X-Requested-With"
24+
],
25+
"ETag": "W/\"798dc78bb4c7a5f7af29dd24986abff44807dbd0601e8a2e0b5768a9e86d236f\"",
26+
"Last-Modified": "Mon, 17 May 2021 14:50:01 GMT",
27+
"X-OAuth-Scopes": "admin:enterprise, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete:packages, delete_repo, gist, notifications, repo, user, workflow, write:discussion, write:packages",
28+
"X-Accepted-OAuth-Scopes": "repo",
29+
"X-GitHub-Media-Type": "unknown, github.v3",
30+
"X-RateLimit-Limit": "5000",
31+
"X-RateLimit-Remaining": "4989",
32+
"X-RateLimit-Reset": "1621305488",
33+
"X-RateLimit-Used": "11",
34+
"X-RateLimit-Resource": "core",
35+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
36+
"X-Frame-Options": "deny",
37+
"X-Content-Type-Options": "nosniff",
38+
"X-XSS-Protection": "0",
39+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
40+
"Content-Security-Policy": "default-src 'none'",
41+
"X-GitHub-Request-Id": "C5B4:3979:DD47:15200:60A31CCC"
42+
}
43+
},
44+
"uuid": "eaf33f02-185f-4be9-a97b-924271e6e7c5",
45+
"persistent": true,
46+
"insertionIndex": 2
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"id": "60d5109b-44b1-4891-94f2-10cb944b54a8",
3+
"name": "repos_hub4j-test-org_pixi_code-scanning_alerts",
4+
"request": {
5+
"url": "/repos/hub4j-test-org/Pixi/code-scanning/alerts?state=dismissed&per_page=1",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "repos_hub4j-test-org_pixi_code-scanning_alerts-3.json",
16+
"headers": {
17+
"Server": "GitHub.com",
18+
"Date": "Tue, 18 May 2021 01:47:57 GMT",
19+
"Content-Type": "application/json; charset=utf-8",
20+
"Cache-Control": "private, max-age=60, s-maxage=60",
21+
"Vary": [
22+
"Accept, Authorization, Cookie, X-GitHub-OTP",
23+
"Accept-Encoding, Accept, X-Requested-With"
24+
],
25+
"ETag": "W/\"9a1d2adc0632ce1ed356060f568dde4d49ff9bb466dbd8dd178c2d7f2deb168a\"",
26+
"X-OAuth-Scopes": "admin:enterprise, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete:packages, delete_repo, gist, notifications, repo, user, workflow, write:discussion, write:packages",
27+
"X-Accepted-OAuth-Scopes": "admin:repo_hook, delete_repo, read:repo_hook, repo, repo:invite, repo:status, repo_deployment, security_events, write:repo_hook",
28+
"X-GitHub-Media-Type": "unknown, github.v3",
29+
"X-RateLimit-Limit": "5000",
30+
"X-RateLimit-Remaining": "4988",
31+
"X-RateLimit-Reset": "1621305488",
32+
"X-RateLimit-Used": "12",
33+
"X-RateLimit-Resource": "core",
34+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
35+
"X-Frame-Options": "deny",
36+
"X-Content-Type-Options": "nosniff",
37+
"X-XSS-Protection": "0",
38+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
39+
"Content-Security-Policy": "default-src 'none'",
40+
"X-GitHub-Request-Id": "C5B4:3979:DD51:1520E:60A31CCD"
41+
}
42+
},
43+
"uuid": "60d5109b-44b1-4891-94f2-10cb944b54a8",
44+
"persistent": true,
45+
"insertionIndex": 3
46+
}

0 commit comments

Comments
 (0)