11package org .kohsuke .github ;
22
3- import com .fasterxml .jackson .core . type . TypeReference ;
4- import com .fasterxml .jackson .databind .JsonNode ;
3+ import com .fasterxml .jackson .databind . JavaType ;
4+ import com .fasterxml .jackson .databind .ObjectReader ;
55import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
66import org .kohsuke .github .internal .EnumUtils ;
77
1313/**
1414 * Represents a repository rule.
1515 */
16- @ SuppressFBWarnings (value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD" , "UWF_UNWRITTEN_FIELD" , "NP_UNWRITTEN_FIELD" },
16+ @ SuppressFBWarnings (
17+ value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD" , "UWF_UNWRITTEN_FIELD" , "NP_UNWRITTEN_FIELD" ,
18+ "CT_CONSTRUCTOR_THROW" },
1719 justification = "JSON API" )
1820public class GHRepositoryRule extends GitHubInteractiveObject {
1921
@@ -53,13 +55,7 @@ public static class BooleanParameter extends Parameter<Boolean> {
5355 * the key
5456 */
5557 public BooleanParameter (String key ) {
56- super (key );
57- }
58-
59- @ Override
60- TypeReference <Boolean > getType () {
61- return new TypeReference <Boolean >() {
62- };
58+ super (key , Boolean .class );
6359 }
6460 }
6561 /**
@@ -115,30 +111,51 @@ public static class IntegerParameter extends Parameter<Integer> {
115111 * the key
116112 */
117113 public IntegerParameter (String key ) {
118- super (key );
119- }
120-
121- @ Override
122- TypeReference <Integer > getType () {
123- return new TypeReference <Integer >() {
124- };
114+ super (key , Integer .class );
125115 }
126116 }
127117 /**
128118 * List parameter for a ruleset.
129119 *
130- * @param <T >
131- * the type of the list
120+ * @param <U >
121+ * the type of the items in the list
132122 */
133- public abstract static class ListParameter <T > extends Parameter <List <T >> {
123+ public abstract static class ListParameter <U > extends Parameter <List <U >> {
124+
125+ private final Class <U > itemClass ;
126+
134127 /**
135128 * Instantiates a new list parameter.
136129 *
137130 * @param key
138131 * the key
139132 */
140133 public ListParameter (String key ) {
141- super (key );
134+ super (key , null );
135+ throw new GHException ("This constructor should not have been public." );
136+ }
137+
138+ /**
139+ * Instantiates a new list parameter.
140+ *
141+ * @param key
142+ * the key
143+ * @param itemClass
144+ * the class of items in the list parameter
145+ */
146+ ListParameter (String key , Class <U > itemClass ) {
147+ super (key , null );
148+ this .itemClass = itemClass ;
149+ }
150+
151+ @ Override
152+ List <U > apply (String value , GitHub root ) throws IOException {
153+ if (value == null ) {
154+ return null ;
155+ }
156+ ObjectReader objectReader = GitHubClient .getMappingObjectReader (root );
157+ JavaType javaType = objectReader .getTypeFactory ().constructParametricType (List .class , itemClass );
158+ return objectReader .forType (javaType ).readValue (value );
142159 }
143160 }
144161 /**
@@ -174,6 +191,7 @@ public static enum Operator {
174191 */
175192 public abstract static class Parameter <T > {
176193
194+ private final Class <T > clazz ;
177195 private final String key ;
178196
179197 /**
@@ -183,14 +201,28 @@ public abstract static class Parameter<T> {
183201 * the key
184202 */
185203 protected Parameter (String key ) {
204+ throw new GHException ("This constructor should not have been protected." );
205+ }
206+
207+ /**
208+ * Instantiates a new parameter.
209+ *
210+ * @param key
211+ * the key
212+ * @param clazz
213+ * the class the the parameter
214+ */
215+ Parameter (String key , Class <T > clazz ) {
186216 this .key = key ;
217+ this .clazz = clazz ;
187218 }
188219
189- T apply (JsonNode jsonNode , GitHub root ) throws IOException {
190- if (jsonNode == null ) {
220+ T apply (String value , GitHub root ) throws IOException {
221+ if (value == null ) {
191222 return null ;
192223 }
193- return GitHubClient .getMappingObjectReader (root ).forType (this .getType ()).readValue (jsonNode );
224+ ObjectReader objectReader = GitHubClient .getMappingObjectReader (root );
225+ return objectReader .forType (clazz ).readValue (value );
194226 }
195227
196228 /**
@@ -201,11 +233,6 @@ T apply(JsonNode jsonNode, GitHub root) throws IOException {
201233 String getKey () {
202234 return this .key ;
203235 }
204-
205- /**
206- * Get the parameter type reference for type mapping.
207- */
208- abstract TypeReference <T > getType ();
209236 }
210237
211238 /**
@@ -216,12 +243,8 @@ public interface Parameters {
216243 * code_scanning_tools parameter
217244 */
218245 public static final ListParameter <CodeScanningTool > CODE_SCANNING_TOOLS = new ListParameter <CodeScanningTool >(
219- "code_scanning_tools" ) {
220- @ Override
221- TypeReference <List <CodeScanningTool >> getType () {
222- return new TypeReference <List <CodeScanningTool >>() {
223- };
224- }
246+ "code_scanning_tools" ,
247+ CodeScanningTool .class ) {
225248 };
226249 /**
227250 * dismiss_stale_reviews_on_push parameter
@@ -239,12 +262,7 @@ TypeReference<List<CodeScanningTool>> getType() {
239262 /**
240263 * operator parameter
241264 */
242- public static final Parameter <Operator > OPERATOR = new Parameter <Operator >("operator" ) {
243- @ Override
244- TypeReference <Operator > getType () {
245- return new TypeReference <Operator >() {
246- };
247- }
265+ public static final Parameter <Operator > OPERATOR = new Parameter <Operator >("operator" , Operator .class ) {
248266 };
249267 /**
250268 * regex parameter
@@ -259,12 +277,8 @@ TypeReference<Operator> getType() {
259277 * required_deployment_environments parameter
260278 */
261279 public static final ListParameter <String > REQUIRED_DEPLOYMENT_ENVIRONMENTS = new ListParameter <String >(
262- "required_deployment_environments" ) {
263- @ Override
264- TypeReference <List <String >> getType () {
265- return new TypeReference <List <String >>() {
266- };
267- }
280+ "required_deployment_environments" ,
281+ String .class ) {
268282 };
269283 /**
270284 * required_review_thread_resolution parameter
@@ -275,12 +289,8 @@ TypeReference<List<String>> getType() {
275289 * required_status_checks parameter
276290 */
277291 public static final ListParameter <StatusCheckConfiguration > REQUIRED_STATUS_CHECKS = new ListParameter <StatusCheckConfiguration >(
278- "required_status_checks" ) {
279- @ Override
280- TypeReference <List <StatusCheckConfiguration >> getType () {
281- return new TypeReference <List <StatusCheckConfiguration >>() {
282- };
283- }
292+ "required_status_checks" ,
293+ StatusCheckConfiguration .class ) {
284294 };
285295 /**
286296 * require_code_owner_review parameter
@@ -306,12 +316,8 @@ TypeReference<List<StatusCheckConfiguration>> getType() {
306316 * workflows parameter
307317 */
308318 public static final ListParameter <WorkflowFileReference > WORKFLOWS = new ListParameter <WorkflowFileReference >(
309- "workflows" ) {
310- @ Override
311- TypeReference <List <WorkflowFileReference >> getType () {
312- return new TypeReference <List <WorkflowFileReference >>() {
313- };
314- }
319+ "workflows" ,
320+ WorkflowFileReference .class ) {
315321 };
316322 }
317323
@@ -409,13 +415,7 @@ public static class StringParameter extends Parameter<String> {
409415 * the key
410416 */
411417 public StringParameter (String key ) {
412- super (key );
413- }
414-
415- @ Override
416- TypeReference <String > getType () {
417- return new TypeReference <String >() {
418- };
418+ super (key , String .class );
419419 }
420420 }
421421
@@ -562,7 +562,7 @@ public String getSha() {
562562 }
563563 }
564564
565- private Map <String , JsonNode > parameters ;
565+ private Map <String , Object > parameters ;
566566
567567 private long rulesetId ;
568568
@@ -593,11 +593,12 @@ public <T> Optional<T> getParameter(Parameter<T> parameter) throws IOException {
593593 if (this .parameters == null ) {
594594 return Optional .empty ();
595595 }
596- JsonNode jsonNode = this .parameters .get (parameter .getKey ());
597- if (jsonNode == null ) {
596+ Object value = this .parameters .get (parameter .getKey ());
597+ if (value == null ) {
598598 return Optional .empty ();
599599 }
600- return Optional .ofNullable (parameter .apply (jsonNode , root ()));
600+ return Optional
601+ .ofNullable (parameter .apply (GitHubClient .getMappingObjectWriter ().writeValueAsString (value ), root ()));
601602 }
602603
603604 /**
0 commit comments