-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathJavaRulesDefinition.java
More file actions
77 lines (65 loc) · 3.61 KB
/
Copy pathJavaRulesDefinition.java
File metadata and controls
77 lines (65 loc) · 3.61 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
/*
* Example Plugin for SonarQube
* Copyright (C) SonarSource Sàrl
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.plugins.example.rules;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.rule.RuleDescriptionSection;
import org.sonar.api.server.rule.RulesDefinition;
import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.HOW_TO_FIX_SECTION_KEY;
import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.INTRODUCTION_SECTION_KEY;
import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.ROOT_CAUSE_SECTION_KEY;
public class JavaRulesDefinition implements RulesDefinition {
public static final String REPOSITORY = "java-example";
public static final String JAVA_LANGUAGE = "java";
public static final RuleKey RULE_ON_LINE_1 = RuleKey.of(REPOSITORY, "line1");
@Override
public void define(Context context) {
NewRepository repository = context.createRepository(REPOSITORY, JAVA_LANGUAGE).setName("My Custom Java Analyzer");
var hibernate = new org.sonar.api.server.rule.Context("hibernate", "Hibernate");
var myBatis = new org.sonar.api.server.rule.Context("mybatis", "MyBatis");
NewRule x1Rule = repository.createRule(RULE_ON_LINE_1.rule())
.setName("Stupid rule")
.setHtmlDescription("Generates an issue on every line 1 of Java files")
.addDescriptionSection(descriptionSection(INTRODUCTION_SECTION_KEY, "This rule is not that stupid", null))
.addDescriptionSection(descriptionSection(ROOT_CAUSE_SECTION_KEY, "The root cause of this issue is this and that.", null))
.addDescriptionSection(descriptionSection(HOW_TO_FIX_SECTION_KEY,
"To fix an issue reported by this rule when using Hibernate do this and that.", hibernate))
.addDescriptionSection(descriptionSection(HOW_TO_FIX_SECTION_KEY,
"To fix an issue reported by this rule when using MyBatis do this and that.", myBatis))
// optional tags
.setTags("style", "stupid")
// optional status. Default value is READY.
.setStatus(RuleStatus.BETA)
// default severity when the rule is activated on a Quality profile. Default value is MAJOR.
.setSeverity(Severity.MINOR);
x1Rule.setDebtRemediationFunction(x1Rule.debtRemediationFunctions().linearWithOffset("1h", "30min"));
// don't forget to call done() to finalize the definition
repository.done();
}
private static RuleDescriptionSection descriptionSection(String sectionKey, String htmlContent, org.sonar.api.server.rule.Context context) {
return RuleDescriptionSection.builder()
.sectionKey(sectionKey)
.htmlContent(htmlContent)
//Optional context - can be any framework or component for which you want to create detailed description
.context(context)
.build();
}
}