Skip to content

Commit c20ace1

Browse files
committed
Improve test coverage for code scanning APIs
1 parent 0a28687 commit c20ace1

17 files changed

+247
-41
lines changed

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

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import java.net.URL;
88
import java.util.Date;
99

10+
import javax.annotation.Nonnull;
11+
import javax.annotation.Nullable;
12+
1013
/**
1114
* Code scanning alert for a repository
1215
*
@@ -142,74 +145,106 @@ public URL getHtmlUrl() throws IOException {
142145
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
143146
static class Rule {
144147
private String id;
145-
private String severity;
146-
private String description;
147148
private String name;
148-
private String full_description;
149+
private String description;
150+
private String severity;
151+
private String security_severity_level;
149152
private String[] tags;
153+
private String full_description;
150154
private String help;
151155

156+
private String help_uri;
157+
152158
/**
153-
* Id of rule
159+
* A unique identifier for the rule used to detect the alert.
154160
*
155161
* @return the id
156162
*/
163+
@Nullable
157164
public String getId() {
158165
return id;
159166
}
160167

161168
/**
162-
* Severity of rule
169+
* The name of the rule used to detect the alert.
170+
*
171+
* @return the name
172+
*/
173+
public String getName() {
174+
return name;
175+
}
176+
177+
/**
178+
* The severity of the alert.
163179
*
164180
* @return the severity
165181
*/
182+
@Nullable
166183
public String getSeverity() {
167184
return severity;
168185
}
169186

170187
/**
171-
* Description of rule
188+
* The security severity of the alert.
189+
*
190+
* @return the security severity
191+
*/
192+
@Nullable
193+
public String getSecuritySeverityLevel() {
194+
return security_severity_level;
195+
}
196+
197+
/**
198+
* A short description of the rule used to detect the alert.
172199
*
173200
* @return the description
174201
*/
202+
@Nonnull
175203
public String getDescription() {
176204
return description;
177205
}
178206

179207
/**
180-
* Name of rule
208+
* A set of tags applicable for the rule.
181209
*
182-
* @return the name
210+
* @return the tags
183211
*/
184-
public String getName() {
185-
return name;
212+
@Nullable
213+
public String[] getTags() {
214+
return tags;
186215
}
187216

217+
// The following fields only appear on some endpoints.
218+
// These might be empty on endpoints like listSecurityAlerts
219+
188220
/**
189221
* Full description of rule
190222
*
191223
* @return the full description
192224
*/
225+
@Nonnull
193226
public String getFullDescription() {
194227
return full_description;
195228
}
196229

197230
/**
198-
* Tags associated with the rule
231+
* Help text for the rule
199232
*
200-
* @return the tags
233+
* @return the help text
201234
*/
202-
public String[] getTags() {
203-
return tags;
235+
@Nullable
236+
public String getHelp() {
237+
return help;
204238
}
205239

206240
/**
207-
* Help text for the rule
241+
* A link to documentation for the rule used to detect the alert. Can be null.
208242
*
209-
* @return the help text
243+
* @return alert documentation url
210244
*/
211-
public String getHelp() {
212-
return help;
245+
@Nullable
246+
public String getHelpUri() {
247+
return help_uri;
213248
}
214249
}
215250

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

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Arrays;
66
import java.util.Collections;
77
import java.util.List;
8+
import java.util.Objects;
89

910
/**
1011
* Code scanning alert instance for a repository
@@ -93,41 +94,129 @@ public Location getLocation() {
9394
return location;
9495
}
9596

97+
@Override
98+
public boolean equals(Object o) {
99+
if (this == o)
100+
return true;
101+
if (o == null || getClass() != o.getClass())
102+
return false;
103+
GHCodeScanningAlertInstance that = (GHCodeScanningAlertInstance) o;
104+
return Objects.equals(ref, that.ref) && Objects.equals(analysis_key, that.analysis_key)
105+
&& Objects.equals(environment, that.environment) && state == that.state
106+
&& Objects.equals(commit_sha, that.commit_sha) && Arrays.equals(classifications, that.classifications)
107+
&& Objects.equals(message, that.message) && Objects.equals(location, that.location);
108+
}
109+
110+
@Override
111+
public int hashCode() {
112+
int result = Objects.hash(ref, analysis_key, environment, state, commit_sha, message, location);
113+
result = 31 * result + Arrays.hashCode(classifications);
114+
return result;
115+
}
116+
117+
/**
118+
* Alert message
119+
*/
96120
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
97-
static class Message {
121+
public static class Message {
98122
private String text;
99123

124+
/**
125+
* Alert message
126+
*
127+
* @return contents of the message
128+
*/
100129
public String getText() {
101130
return text;
102131
}
132+
133+
@Override
134+
public boolean equals(Object o) {
135+
if (this == o)
136+
return true;
137+
if (o == null || getClass() != o.getClass())
138+
return false;
139+
Message message = (Message) o;
140+
return Objects.equals(text, message.text);
141+
}
142+
143+
@Override
144+
public int hashCode() {
145+
return Objects.hash(text);
146+
}
103147
}
104148

149+
/**
150+
* Describe a region within a file for an alert.
151+
*/
105152
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
106-
static class Location {
153+
public static class Location {
107154
private String path;
108155
private long start_line;
109156
private long end_line;
110157
private long start_column;
111158
private long end_column;
112159

160+
/**
161+
* Path to the file containing the described code region
162+
*
163+
* @return path
164+
*/
113165
public String getPath() {
114166
return path;
115167
}
116168

169+
/**
170+
* Line number at the start of the code region.
171+
*
172+
* @return line number at the start of the code region
173+
*/
117174
public long getStartLine() {
118175
return start_line;
119176
}
120177

178+
/**
179+
* Line number at the end of the code region.
180+
*
181+
* @return line number at the end of the code region
182+
*/
121183
public long getEndLine() {
122184
return end_line;
123185
}
124186

187+
/**
188+
* Column number at the start of the code region.
189+
*
190+
* @return column number at the start of the code region
191+
*/
125192
public long getStartColumn() {
126193
return start_column;
127194
}
128195

196+
/**
197+
* Column number at the end of the code region.
198+
*
199+
* @return column number at the end of the code region
200+
*/
129201
public long getEndColumn() {
130202
return end_column;
131203
}
204+
205+
@Override
206+
public boolean equals(Object o) {
207+
if (this == o)
208+
return true;
209+
if (o == null || getClass() != o.getClass())
210+
return false;
211+
Location location = (Location) o;
212+
return start_line == location.start_line && end_line == location.end_line
213+
&& start_column == location.start_column && end_column == location.end_column
214+
&& path.equals(location.path);
215+
}
216+
217+
@Override
218+
public int hashCode() {
219+
return Objects.hash(path, start_line, end_line, start_column, end_column);
220+
}
132221
}
133222
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3513,6 +3513,11 @@ public GHTagObject createTag(String tag, String message, String object, String t
35133513

35143514
/**
35153515
* Lists the code scanning alerts of this repository.
3516+
* <p>
3517+
* See: <a href=
3518+
* "https://docs.github.com/en/rest/code-scanning/code-scanning?apiVersion=2022-11-28#list-code-scanning-alerts-for-a-repository">List
3519+
* code scanning alerts for a repository</a>
3520+
* </p>
35163521
*
35173522
* @return the paged iterable
35183523
*/
@@ -3522,6 +3527,11 @@ public PagedIterable<GHCodeScanningAlert> listCodeScanningAlerts() {
35223527

35233528
/**
35243529
* Lists the code scanning alerts of this repository filtered on the alert status
3530+
* <p>
3531+
* See: <a href=
3532+
* "https://docs.github.com/en/rest/code-scanning/code-scanning?apiVersion=2022-11-28#list-code-scanning-alerts-for-a-repository">List
3533+
* code scanning alerts for a repository</a>
3534+
* </p>
35253535
*
35263536
* @param state
35273537
* alert status to filter on
@@ -3533,6 +3543,11 @@ public PagedIterable<GHCodeScanningAlert> listCodeScanningAlerts(GHCodeScanningA
35333543

35343544
/**
35353545
* Lists the code scanning alerts of this repository filtered on the code scanning tool name
3546+
* <p>
3547+
* See: <a href=
3548+
* "https://docs.github.com/en/rest/code-scanning/code-scanning?apiVersion=2022-11-28#list-code-scanning-alerts-for-a-repository">List
3549+
* code scanning alerts for a repository</a>
3550+
* </p>
35363551
*
35373552
* @param toolName
35383553
* name of code scanning tool that creates alerts
@@ -3550,6 +3565,12 @@ private PagedIterable<GHCodeScanningAlert> listCodeScanningAlerts(Map<String, Ob
35503565
/**
35513566
* Get code scanning alert by id
35523567
*
3568+
* <p>
3569+
* See: <a href=
3570+
* "https://docs.github.com/en/rest/code-scanning/code-scanning?apiVersion=2022-11-28#get-a-code-scanning-alert">
3571+
* Get a code scanning alert</a>
3572+
* </p>
3573+
*
35533574
* @param id
35543575
* id of the code scanning alert
35553576
* @return the code scanning alert

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import java.io.IOException;
88
import java.util.List;
99

10-
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
11-
import static org.hamcrest.Matchers.not;
10+
import static org.hamcrest.Matchers.*;
1211

1312
/**
1413
* <p>
@@ -21,6 +20,12 @@ public class GHCodeScanningAlertInstanceTest extends AbstractGitHubWireMockTest
2120
private static final String REPO_NAME = "Pixi";
2221
private GHCodeScanningAlert alert;
2322

23+
/**
24+
* Load a dismissed alert from the code scanning api web response
25+
*
26+
* @throws Exception
27+
* the exception
28+
*/
2429
@Before
2530
public void setUp() throws Exception {
2631
GHRepository repo = gitHub.getRepository(GITHUB_API_TEST_ORG + "/" + REPO_NAME);
@@ -35,6 +40,12 @@ private GHCodeScanningAlert getAlertFromRepo(GHRepository repo) {
3540
return dismissedAlerts.get(0);
3641
}
3742

43+
/**
44+
* Test that an alert returns a list of its own instances
45+
*
46+
* @throws IOException
47+
* could not get a compatible response
48+
*/
3849
@Test
3950
public void testListAlertInstances() throws IOException {
4051
// Arrange
@@ -53,13 +64,19 @@ public void testListAlertInstances() throws IOException {
5364
assertThat(instance.getMessage(), not((Object) null));
5465
assertThat(instance.getLocation(), not((Object) null));
5566

67+
assertThat(instance.getMessage().getText(), not(emptyOrNullString()));
68+
69+
assertThat(instance.getAnalysisKey(), not((Object) null));
70+
assertThat(instance.getClassifications(), not((Object) null));
71+
assertThat(instance.getEnvironment(), notNullValue());
72+
5673
GHCodeScanningAlertInstance.Location location = instance.getLocation();
5774
// Can't assert on exact values with having to hardcode values from
5875
// json file, hence making the assertions generics
5976
assertThat(location.getPath(), not((Object) null));
6077
assertThat(location.getStartLine(), greaterThanOrEqualTo(0L));
6178
assertThat(location.getEndLine(), greaterThanOrEqualTo(0L));
6279
assertThat(location.getStartColumn(), greaterThanOrEqualTo(0L));
63-
assertThat(location.getStartColumn(), greaterThanOrEqualTo(0L));
80+
assertThat(location.getEndColumn(), greaterThanOrEqualTo(0L));
6481
}
6582
}

0 commit comments

Comments
 (0)