|
19 | 19 |
|
20 | 20 | public class ViolationsAnalyzer { |
21 | 21 |
|
22 | | - public static class FoundViolation { |
23 | | - public final Set<IParsedNode> violatingNodes = new LinkedHashSet<IParsedNode>(); |
24 | | - public boolean failuresFound = false; |
25 | | - public Rule rule; |
26 | | - public String repoKey; |
27 | | - public boolean isAdhoc = false; |
28 | | - public boolean isExternal = false; |
29 | | - |
30 | | - public SqlIssue toSqlIssue() { |
31 | | - SqlIssue issue = new SqlIssue(); |
32 | | - issue.repo = repoKey; |
33 | | - issue.isAdhoc = isAdhoc; |
34 | | - issue.isExternal = isExternal; |
35 | | - issue.description = rule.getDescription(); |
36 | | - issue.key = rule.getKey(); |
37 | | - issue.ruleType = rule.getRuleType(); |
38 | | - issue.severity = rule.getSeverity(); |
39 | | - issue.message = rule.getRuleImplementation().getRuleViolationMessage(); |
40 | | - issue.name = rule.getName(); |
41 | | - if (!violatingNodes.isEmpty()) { |
42 | | - IParsedNode node = violatingNodes.stream().findFirst().get(); |
43 | | - issue.line = node.getLine(); |
44 | | - } |
45 | | - try { |
46 | | - if (StringUtils.equalsIgnoreCase("LINEAR", rule.getRemediationFunction()) |
47 | | - && !StringUtils.isBlank(rule.getDebtRemediationFunctionCoefficient())) { |
48 | | - issue.debtRemediationEffort = org.sonar.api.utils.Duration |
49 | | - .decode(rule.getDebtRemediationFunctionCoefficient(), 8).toMinutes(); |
50 | | - } |
51 | | - } catch (Exception p) { |
52 | | - |
53 | | - } |
54 | | - |
55 | | - return issue; |
56 | | - } |
57 | | - } |
58 | | - |
59 | | - public FoundViolation isMatch(RuleToCheck item, Map<RuleImplementation, List<IParsedNode>> items) { |
60 | | - FoundViolation vio = isMatch(items); |
61 | | - vio.rule = item.rule; |
62 | | - vio.isAdhoc = item.isAdhoc; |
63 | | - vio.repoKey = item.repoKey; |
64 | | - vio.isExternal = item.isExternal; |
65 | | - return vio; |
66 | | - } |
67 | | - |
68 | | - public FoundViolation isMatched(RuleToCheck.RuleCheckResult2 result) { |
69 | | - FoundViolation vio = isMatch(result.map.items); |
70 | | - vio.rule = result.ruleToCheck.rule; |
71 | | - vio.isAdhoc = result.ruleToCheck.isAdhoc; |
72 | | - vio.repoKey = result.ruleToCheck.repoKey; |
73 | | - vio.isExternal = result.ruleToCheck.isExternal; |
74 | | - |
75 | | - if (vio.violatingNodes.isEmpty()) { |
76 | | - List<IParsedNode> nodes = result.map.getNodes(); |
77 | | - if (!nodes.isEmpty()) { |
78 | | - vio.violatingNodes.add(nodes.get(0)); |
79 | | - } |
80 | | - } |
81 | | - return vio; |
82 | | - } |
83 | | - |
84 | | - public FoundViolation isMatch(Map<RuleImplementation, List<IParsedNode>> items) { |
85 | | - |
86 | | - boolean skipSatisfied = false; |
87 | | - boolean failuresFound = false; |
88 | | - final FoundViolation violation = new FoundViolation(); |
89 | | - for (final Entry<RuleImplementation, List<IParsedNode>> item : items.entrySet()) { |
90 | | - |
91 | | - RuleImplementation impl = item.getKey(); |
92 | | - List<IParsedNode> values = item.getValue(); |
93 | | - if (RuleResultType.DEFAULT == impl.getRuleResultType()) { |
94 | | - continue; |
95 | | - } |
96 | | - |
97 | | - if (RuleResultType.SKIP_IF_FOUND == impl.getRuleResultType() && !values.isEmpty()) { |
98 | | - skipSatisfied = true; |
99 | | - } |
100 | | - if (RuleResultType.SKIP_IF_NOT_FOUND == impl.getRuleResultType() && values.isEmpty()) { |
101 | | - skipSatisfied = true; |
102 | | - } |
103 | | - if (RuleResultType.FAIL_IF_FOUND == impl.getRuleResultType() && !values.isEmpty()) { |
104 | | - failuresFound = true; |
105 | | - violation.violatingNodes.addAll(values); |
106 | | - |
107 | | - } |
108 | | - |
109 | | - if (RuleResultType.FAIL_IF_NOT_FOUND == impl.getRuleResultType() && values.isEmpty()) { |
110 | | - failuresFound = true; |
111 | | - violation.violatingNodes.addAll(values); |
112 | | - } |
113 | | - |
114 | | - if (RuleResultType.FAIL_IF_MORE_FOUND == impl.getRuleResultType() && values.size() > impl.getTimes()) { |
115 | | - failuresFound = true; |
116 | | - violation.violatingNodes.addAll(values); |
117 | | - } |
118 | | - |
119 | | - if (RuleResultType.FAIL_IF_LESS_FOUND == impl.getRuleResultType() && values.size() < impl.getTimes()) { |
120 | | - failuresFound = true; |
121 | | - violation.violatingNodes.addAll(values); |
122 | | - } |
123 | | - |
124 | | - } |
125 | | - |
126 | | - if (skipSatisfied) { |
127 | | - violation.failuresFound = false; |
128 | | - return violation; |
129 | | - } |
130 | | - |
131 | | - boolean orderViolated = false; |
132 | | - |
133 | | - // check order |
134 | | - List<RuleImplementation> rules = items.keySet().stream() |
135 | | - .sorted(Comparator.comparing(RuleImplementation::getIndex)).collect(Collectors.toList()); |
136 | | - int prev = 0; |
137 | | - for (RuleImplementation rule : rules) { |
138 | | - |
139 | | - if (rule.getIndex() == 0 || rule.getDistanceCheckType() != RuleDistanceIndexMatchType.BEFOREORAFTER) { |
140 | | - continue; |
141 | | - } |
142 | | - |
143 | | - for (IParsedNode node : items.get(rule)) { |
144 | | - if (node.getGlobalIndex() < prev) { |
145 | | - orderViolated = true; |
146 | | - violation.violatingNodes.add(node); |
147 | | - break; |
148 | | - } |
149 | | - prev = node.getGlobalIndex(); |
150 | | - } |
151 | | - } |
152 | | - |
153 | | - violation.failuresFound = failuresFound || orderViolated; |
154 | | - return violation; |
155 | | - } |
| 22 | + public static class FoundViolation { |
| 23 | + public final Set<IParsedNode> violatingNodes = new LinkedHashSet<IParsedNode>(); |
| 24 | + public boolean failuresFound = false; |
| 25 | + public Rule rule; |
| 26 | + public String repoKey; |
| 27 | + public boolean isAdhoc = false; |
| 28 | + public boolean isExternal = false; |
| 29 | + |
| 30 | + public SqlIssue toSqlIssue() { |
| 31 | + SqlIssue issue = new SqlIssue(); |
| 32 | + issue.repo = repoKey; |
| 33 | + issue.isAdhoc = isAdhoc; |
| 34 | + issue.isExternal = isExternal; |
| 35 | + issue.description = rule.getDescription(); |
| 36 | + issue.key = rule.getKey(); |
| 37 | + issue.ruleType = rule.getRuleType(); |
| 38 | + issue.severity = rule.getSeverity(); |
| 39 | + issue.message = rule.getRuleImplementation().getRuleViolationMessage(); |
| 40 | + issue.name = rule.getName(); |
| 41 | + if (!violatingNodes.isEmpty()) { |
| 42 | + IParsedNode node = violatingNodes.stream().findFirst().get(); |
| 43 | + issue.line = node.getLine(); |
| 44 | + } |
| 45 | + try { |
| 46 | + if (StringUtils.equalsIgnoreCase("LINEAR", rule.getRemediationFunction()) |
| 47 | + && !StringUtils.isBlank(rule.getDebtRemediationFunctionCoefficient())) { |
| 48 | + issue.debtRemediationEffort = org.sonar.api.utils.Duration |
| 49 | + .decode(rule.getDebtRemediationFunctionCoefficient(), 8).toMinutes(); |
| 50 | + } |
| 51 | + } catch (Exception p) { |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + return issue; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public FoundViolation isMatch(RuleToCheck item, Map<RuleImplementation, List<IParsedNode>> items) { |
| 60 | + FoundViolation vio = isMatch(items); |
| 61 | + vio.rule = item.rule; |
| 62 | + vio.isAdhoc = item.isAdhoc; |
| 63 | + vio.repoKey = item.repoKey; |
| 64 | + vio.isExternal = item.isExternal; |
| 65 | + return vio; |
| 66 | + } |
| 67 | + |
| 68 | + public FoundViolation isMatched(RuleToCheck.RuleCheckResult2 result) { |
| 69 | + FoundViolation vio = isMatch(result.map.items); |
| 70 | + vio.rule = result.ruleToCheck.rule; |
| 71 | + vio.isAdhoc = result.ruleToCheck.isAdhoc; |
| 72 | + vio.repoKey = result.ruleToCheck.repoKey; |
| 73 | + vio.isExternal = result.ruleToCheck.isExternal; |
| 74 | + |
| 75 | + if (vio.violatingNodes.isEmpty()) { |
| 76 | + List<IParsedNode> nodes = result.map.getNodes(); |
| 77 | + if (!nodes.isEmpty()) { |
| 78 | + vio.violatingNodes.add(nodes.get(0)); |
| 79 | + } |
| 80 | + } |
| 81 | + return vio; |
| 82 | + } |
| 83 | + |
| 84 | + public FoundViolation isMatch(Map<RuleImplementation, List<IParsedNode>> items) { |
| 85 | + |
| 86 | + boolean skipSatisfied = false; |
| 87 | + boolean failuresFound = false; |
| 88 | + final FoundViolation violation = new FoundViolation(); |
| 89 | + for (final Entry<RuleImplementation, List<IParsedNode>> item : items.entrySet()) { |
| 90 | + |
| 91 | + RuleImplementation impl = item.getKey(); |
| 92 | + List<IParsedNode> values = item.getValue(); |
| 93 | + if (RuleResultType.DEFAULT == impl.getRuleResultType()) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + if (RuleResultType.SKIP_IF_FOUND == impl.getRuleResultType() && !values.isEmpty()) { |
| 98 | + skipSatisfied = true; |
| 99 | + } |
| 100 | + if (RuleResultType.SKIP_IF_NOT_FOUND == impl.getRuleResultType() && values.isEmpty()) { |
| 101 | + skipSatisfied = true; |
| 102 | + } |
| 103 | + if (RuleResultType.FAIL_IF_FOUND == impl.getRuleResultType() && !values.isEmpty()) { |
| 104 | + failuresFound = true; |
| 105 | + violation.violatingNodes.addAll(values); |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + if (RuleResultType.FAIL_IF_NOT_FOUND == impl.getRuleResultType() && values.isEmpty()) { |
| 110 | + failuresFound = true; |
| 111 | + violation.violatingNodes.addAll(values); |
| 112 | + } |
| 113 | + |
| 114 | + if (RuleResultType.FAIL_IF_MORE_FOUND == impl.getRuleResultType() && values.size() > impl.getTimes()) { |
| 115 | + failuresFound = true; |
| 116 | + violation.violatingNodes.addAll(values); |
| 117 | + } |
| 118 | + |
| 119 | + if (RuleResultType.FAIL_IF_LESS_FOUND == impl.getRuleResultType() && values.size() < impl.getTimes()) { |
| 120 | + failuresFound = true; |
| 121 | + violation.violatingNodes.addAll(values); |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + if (skipSatisfied) { |
| 127 | + violation.failuresFound = false; |
| 128 | + return violation; |
| 129 | + } |
| 130 | + |
| 131 | + boolean orderViolated = false; |
| 132 | + |
| 133 | + // check nodes order against distance check rules, index is used for rules |
| 134 | + // ordering |
| 135 | + List<RuleImplementation> rules = items.keySet().stream() |
| 136 | + .filter(rule -> rule.getDistanceCheckType() == RuleDistanceIndexMatchType.BEFOREORAFTER) |
| 137 | + .sorted(Comparator.comparing(RuleImplementation::getIndex)).collect(Collectors.toList()); |
| 138 | + int prev = 0; |
| 139 | + for (RuleImplementation rule : rules) { |
| 140 | + |
| 141 | + for (IParsedNode node : items.get(rule)) { |
| 142 | + if (node.getGlobalIndex() < prev) { |
| 143 | + orderViolated = true; |
| 144 | + violation.violatingNodes.add(node); |
| 145 | + break; |
| 146 | + } |
| 147 | + prev = node.getGlobalIndex(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + violation.failuresFound = failuresFound || orderViolated; |
| 152 | + return violation; |
| 153 | + } |
156 | 154 | } |
0 commit comments