-
Notifications
You must be signed in to change notification settings - Fork 775
Expand file tree
/
Copy pathGHRepositoryRule.java
More file actions
639 lines (563 loc) · 14.9 KB
/
GHRepositoryRule.java
File metadata and controls
639 lines (563 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
package org.kohsuke.github;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectReader;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.kohsuke.github.internal.EnumUtils;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Represents a repository rule.
*/
@SuppressFBWarnings(
value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
"CT_CONSTRUCTOR_THROW" },
justification = "JSON API")
public class GHRepositoryRule extends GitHubInteractiveObject {
/**
* Alerts threshold parameter.
*/
public static enum AlertsThreshold {
/**
* all
*/
ALL,
/**
* errors
*/
ERRORS,
/**
* errors_and_warnings
*/
ERRORS_AND_WARNINGS,
/**
* none
*/
NONE
}
/**
* Boolean parameter for a ruleset.
*/
public static class BooleanParameter extends Parameter<Boolean> {
/**
* Instantiates a new boolean parameter.
*
* @param key
* the key
*/
public BooleanParameter(String key) {
super(key, Boolean.class);
}
}
/**
* Code scanning tool parameter.
*/
public static class CodeScanningTool {
private AlertsThreshold alertsThreshold;
private SecurityAlertsThreshold securityAlertsThreshold;
private String tool;
/**
* Create default CodeScanningTool instance
*/
public CodeScanningTool() {
}
/**
* Gets the alerts threshold.
*
* @return the alerts threshold
*/
public AlertsThreshold getAlertsThreshold() {
return this.alertsThreshold;
}
/**
* Gets the security alerts threshold.
*
* @return the security alerts threshold
*/
public SecurityAlertsThreshold getSecurityAlertsThreshold() {
return this.securityAlertsThreshold;
}
/**
* Gets the tool.
*
* @return the tool
*/
public String getTool() {
return this.tool;
}
}
/**
* Integer parameter for a ruleset.
*/
public static class IntegerParameter extends Parameter<Integer> {
/**
* Instantiates a new integer parameter.
*
* @param key
* the key
*/
public IntegerParameter(String key) {
super(key, Integer.class);
}
}
/**
* List parameter for a ruleset.
*
* @param <T>
* the type of the list
*/
public abstract static class ListParameter<U> extends Parameter<List<U>> {
private final Class<U> itemClass;
/**
* Instantiates a new list parameter.
*
* @param key
* the key
*/
public ListParameter(String key) {
super(key, null);
throw new NoClassDefFoundError("This constructor should not have been public.");
}
/**
* Instantiates a new list parameter.
*
* @param key
* the key
* @param itemClass
* the class of items in the list parameter
*/
ListParameter(String key, Class<U> itemClass) {
super(key, null);
this.itemClass = itemClass;
}
@Override
List<U> apply(String value, GitHub root) throws IOException {
if (value == null) {
return null;
}
ObjectReader objectReader = GitHubClient.getMappingObjectReader(root);
JavaType javaType = objectReader.getTypeFactory().constructParametricType(List.class, itemClass);
return objectReader.forType(javaType).readValue(value);
}
}
/**
* Operator parameter.
*/
public static enum Operator {
/**
* contains
*/
CONTAINS,
/**
* ends_with
*/
ENDS_WITH,
/**
* regex
*/
REGEX,
/**
* starts_with
*/
STARTS_WITH
}
/**
* Basic parameter for a ruleset.
*
* @param <T>
* the type of the parameter
*/
public abstract static class Parameter<T> {
private final Class<T> clazz;
private final String key;
/**
* Instantiates a new parameter.
*
* @param key
* the key
*/
protected Parameter(String key) {
throw new NoClassDefFoundError("This constructor should not have been protected.");
}
/**
* Instantiates a new parameter.
*
* @param key
* the key
* @param clazz
* the class the the parameter
*/
Parameter(String key, Class<T> clazz) {
this.key = key;
this.clazz = clazz;
}
T apply(String value, GitHub root) throws IOException {
if (value == null) {
return null;
}
ObjectReader objectReader = GitHubClient.getMappingObjectReader(root);
return objectReader.forType(clazz).readValue(value);
}
/**
* Gets the key.
*
* @return the key
*/
String getKey() {
return this.key;
}
}
/**
* Available parameters for a ruleset.
*/
public interface Parameters {
/**
* code_scanning_tools parameter
*/
public static final ListParameter<CodeScanningTool> CODE_SCANNING_TOOLS = new ListParameter<CodeScanningTool>(
"code_scanning_tools",
CodeScanningTool.class) {
};
/**
* dismiss_stale_reviews_on_push parameter
*/
public static final BooleanParameter DISMISS_STALE_REVIEWS_ON_PUSH = new BooleanParameter(
"dismiss_stale_reviews_on_push");
/**
* name parameter
*/
public static final StringParameter NAME = new StringParameter("name");
/**
* negate parameter
*/
public static final BooleanParameter NEGATE = new BooleanParameter("negate");
/**
* operator parameter
*/
public static final Parameter<Operator> OPERATOR = new Parameter<Operator>("operator", Operator.class) {
};
/**
* regex parameter
*/
public static final StringParameter REGEX = new StringParameter("regex");
/**
* required_approving_review_count parameter
*/
public static final IntegerParameter REQUIRED_APPROVING_REVIEW_COUNT = new IntegerParameter(
"required_approving_review_count");
/**
* required_deployment_environments parameter
*/
public static final ListParameter<String> REQUIRED_DEPLOYMENT_ENVIRONMENTS = new ListParameter<String>(
"required_deployment_environments",
String.class) {
};
/**
* required_review_thread_resolution parameter
*/
public static final BooleanParameter REQUIRED_REVIEW_THREAD_RESOLUTION = new BooleanParameter(
"required_review_thread_resolution");
/**
* required_status_checks parameter
*/
public static final ListParameter<StatusCheckConfiguration> REQUIRED_STATUS_CHECKS = new ListParameter<StatusCheckConfiguration>(
"required_status_checks",
StatusCheckConfiguration.class) {
};
/**
* require_code_owner_review parameter
*/
public static final BooleanParameter REQUIRE_CODE_OWNER_REVIEW = new BooleanParameter(
"require_code_owner_review");
/**
* require_last_push_approval parameter
*/
public static final BooleanParameter REQUIRE_LAST_PUSH_APPROVAL = new BooleanParameter(
"require_last_push_approval");
/**
* strict_required_status_checks_policy parameter
*/
public static final BooleanParameter STRICT_REQUIRED_STATUS_CHECKS_POLICY = new BooleanParameter(
"strict_required_status_checks_policy");
/**
* update_allows_fetch_and_merge parameter
*/
public static final BooleanParameter UPDATE_ALLOWS_FETCH_AND_MERGE = new BooleanParameter(
"update_allows_fetch_and_merge");
/**
* workflows parameter
*/
public static final ListParameter<WorkflowFileReference> WORKFLOWS = new ListParameter<WorkflowFileReference>(
"workflows",
WorkflowFileReference.class) {
};
}
/**
* The source of the ruleset type.
*/
public enum RulesetSourceType {
/**
* Organization
*/
ORGANIZATION,
/**
* Repository
*/
REPOSITORY,
/**
* unknown
*/
UNKNOWN
}
/**
* Security alerts threshold parameter.
*/
public static enum SecurityAlertsThreshold {
/**
* all
*/
ALL,
/**
* critical
*/
CRITICAL,
/**
* high_or_higher
*/
HIGH_OR_HIGHER,
/**
* medium_or_higher
*/
MEDIUM_OR_HIGHER,
/**
* none
*/
NONE
}
/**
* Status check configuration parameter.
*/
public static class StatusCheckConfiguration {
private String context;
private Integer integrationId;
/**
* Create default StatusCheckConfiguration instance
*/
public StatusCheckConfiguration() {
}
/**
* Gets the context.
*
* @return the context
*/
public String getContext() {
return this.context;
}
/**
* Gets the integration id.
*
* @return the integration id
*/
public Integer getIntegrationId() {
return this.integrationId;
}
}
/**
* String parameter for a ruleset.
*/
public static class StringParameter extends Parameter<String> {
/**
* Instantiates a new string parameter.
*
* @param key
* the key
*/
public StringParameter(String key) {
super(key, String.class);
}
}
/**
* The type of the ruleset.
*/
public static enum Type {
/**
* branch_name_pattern
*/
BRANCH_NAME_PATTERN,
/**
* code_scanning
*/
CODE_SCANNING,
/**
* committer_email_pattern
*/
COMMITTER_EMAIL_PATTERN,
/**
* commit_author_email_pattern
*/
COMMIT_AUTHOR_EMAIL_PATTERN,
/**
* commit_message_pattern
*/
COMMIT_MESSAGE_PATTERN,
/**
* creation
*/
CREATION,
/**
* deletion
*/
DELETION,
/**
* non_fast_forward
*/
NON_FAST_FORWARD,
/**
* pull_request
*/
PULL_REQUEST,
/**
* required_deployments
*/
REQUIRED_DEPLOYMENTS,
/**
* required_linear_history
*/
REQUIRED_LINEAR_HISTORY,
/**
* required_signatures
*/
REQUIRED_SIGNATURES,
/**
* required_status_checks
*/
REQUIRED_STATUS_CHECKS,
/**
* tag_name_pattern
*/
TAG_NAME_PATTERN,
/**
* unknown
*/
UNKNOWN,
/**
* update
*/
UPDATE,
/**
* workflows
*/
WORKFLOWS
}
/**
* Workflow file reference parameter.
*/
public static class WorkflowFileReference {
private String path;
private String ref;
private long repositoryId;
private String sha;
/**
* Create default WorkflowFileReference instance
*/
public WorkflowFileReference() {
}
/**
* Gets the path.
*
* @return the path
*/
public String getPath() {
return this.path;
}
/**
* Gets the ref.
*
* @return the ref
*/
public String getRef() {
return this.ref;
}
/**
* Gets the repository id.
*
* @return the repository id
*/
public long getRepositoryId() {
return this.repositoryId;
}
/**
* Gets the sha.
*
* @return the sha
*/
public String getSha() {
return this.sha;
}
}
private Map<String, Object> parameters;
private long rulesetId;
private String rulesetSource;
private String rulesetSourceType;
private String type;
/**
* Create default GHRepositoryRule instance
*/
public GHRepositoryRule() {
}
/**
* Gets a parameter. ({@link GHRepositoryRule.Parameters Parameters} provides a list of available parameters.)
*
* @param parameter
* the parameter
* @param <T>
* the type of the parameter
* @return the parameters
* @throws IOException
* if an I/O error occurs
*/
public <T> Optional<T> getParameter(Parameter<T> parameter) throws IOException {
if (this.parameters == null) {
return Optional.empty();
}
Object value = this.parameters.get(parameter.getKey());
if (value == null) {
return Optional.empty();
}
return Optional
.ofNullable(parameter.apply(GitHubClient.getMappingObjectWriter().writeValueAsString(value), root()));
}
/**
* Gets the ruleset id.
*
* @return the ruleset id
*/
public long getRulesetId() {
return this.rulesetId;
}
/**
* Gets the ruleset source.
*
* @return the ruleset source
*/
public String getRulesetSource() {
return this.rulesetSource;
}
/**
* Gets the ruleset source type.
*
* @return the ruleset source type
*/
public RulesetSourceType getRulesetSourceType() {
return EnumUtils.getEnumOrDefault(RulesetSourceType.class, this.rulesetSourceType, RulesetSourceType.UNKNOWN);
}
/**
* Gets the type.
*
* @return the type
*/
public Type getType() {
return EnumUtils.getEnumOrDefault(Type.class, this.type, Type.UNKNOWN);
}
}