You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RuleDto.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.db.rule;
  21. import com.google.common.base.Splitter;
  22. import com.google.common.collect.ImmutableSet;
  23. import java.util.Collection;
  24. import java.util.HashSet;
  25. import java.util.Objects;
  26. import java.util.Optional;
  27. import java.util.Set;
  28. import javax.annotation.CheckForNull;
  29. import javax.annotation.Nullable;
  30. import org.apache.commons.lang.StringUtils;
  31. import org.apache.commons.lang.builder.HashCodeBuilder;
  32. import org.sonar.api.rule.RuleKey;
  33. import org.sonar.api.rule.RuleStatus;
  34. import org.sonar.api.rules.RuleType;
  35. import static com.google.common.base.Preconditions.checkArgument;
  36. import static java.util.Collections.emptySet;
  37. import static org.sonar.db.rule.RuleDescriptionSectionDto.DEFAULT_KEY;
  38. public class RuleDto {
  39. public enum Format {
  40. HTML, MARKDOWN
  41. }
  42. public enum Scope {
  43. MAIN, TEST, ALL
  44. }
  45. private static final Splitter SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();
  46. private String uuid;
  47. private String repositoryKey;
  48. private String ruleKey;
  49. private Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = new HashSet<>();
  50. /**
  51. * Description format can be null on external rule, otherwise it should never be null
  52. */
  53. private RuleDto.Format descriptionFormat;
  54. private RuleStatus status;
  55. private String name;
  56. private String configKey;
  57. /**
  58. * Severity can be null on external rule, otherwise it should never be null
  59. */
  60. private Integer severity;
  61. private boolean isTemplate;
  62. /**
  63. * This flag specify that this is an external rule, meaning that generated issues from this rule will be provided by the analyzer without being activated on a quality profile.
  64. */
  65. private boolean isExternal;
  66. /**
  67. * When an external rule is defined as ad hoc, it means that it's not defined using {@link org.sonar.api.server.rule.RulesDefinition.Context#createExternalRepository(String, String)}.
  68. * As the opposite, an external rule not being defined as ad hoc is declared by using {@link org.sonar.api.server.rule.RulesDefinition.Context#createExternalRepository(String, String)}.
  69. * This flag is only used for external rules (it can only be set to true for when {@link #isExternal()} is true)
  70. */
  71. private boolean isAdHoc;
  72. private String language;
  73. private String templateUuid;
  74. private String defRemediationFunction;
  75. private String defRemediationGapMultiplier;
  76. private String defRemediationBaseEffort;
  77. private String gapDescription;
  78. private String systemTagsField;
  79. private String securityStandardsField;
  80. private int type;
  81. private Scope scope;
  82. private RuleKey key;
  83. private String pluginKey;
  84. private long createdAt;
  85. private long updatedAt;
  86. private final RuleMetadataDto metadata;
  87. public RuleDto() {
  88. this(new RuleMetadataDto());
  89. }
  90. public RuleDto(RuleMetadataDto metadata) {
  91. this.metadata = metadata;
  92. }
  93. public RuleMetadataDto getMetadata() {
  94. return metadata;
  95. }
  96. public RuleKey getKey() {
  97. if (key == null) {
  98. key = RuleKey.of(getRepositoryKey(), getRuleKey());
  99. }
  100. return key;
  101. }
  102. RuleDto setKey(RuleKey key) {
  103. this.key = key;
  104. setRepositoryKey(key.repository());
  105. setRuleKey(key.rule());
  106. return this;
  107. }
  108. public String getUuid() {
  109. return uuid;
  110. }
  111. public RuleDto setUuid(String uuid) {
  112. this.uuid = uuid;
  113. metadata.setRuleUuid(uuid);
  114. return this;
  115. }
  116. public String getRepositoryKey() {
  117. return repositoryKey;
  118. }
  119. public RuleDto setRepositoryKey(String repositoryKey) {
  120. checkArgument(repositoryKey.length() <= 255, "Rule repository is too long: %s", repositoryKey);
  121. this.repositoryKey = repositoryKey;
  122. return this;
  123. }
  124. public String getRuleKey() {
  125. return ruleKey;
  126. }
  127. public RuleDto setRuleKey(String ruleKey) {
  128. checkArgument(ruleKey.length() <= 200, "Rule key is too long: %s", ruleKey);
  129. this.ruleKey = ruleKey;
  130. return this;
  131. }
  132. public RuleDto setRuleKey(RuleKey ruleKey) {
  133. this.repositoryKey = ruleKey.repository();
  134. this.ruleKey = ruleKey.rule();
  135. this.key = ruleKey;
  136. return this;
  137. }
  138. @CheckForNull
  139. public String getPluginKey() {
  140. return pluginKey;
  141. }
  142. public RuleDto setPluginKey(@Nullable String pluginKey) {
  143. this.pluginKey = pluginKey;
  144. return this;
  145. }
  146. public Set<RuleDescriptionSectionDto> getRuleDescriptionSectionDtos() {
  147. return ruleDescriptionSectionDtos;
  148. }
  149. @CheckForNull
  150. public RuleDescriptionSectionDto getDefaultRuleDescriptionSection() {
  151. return findExistingSectionWithSameKey(DEFAULT_KEY).orElse(null);
  152. }
  153. public RuleDto replaceRuleDescriptionSectionDtos(Collection<RuleDescriptionSectionDto> ruleDescriptionSectionDtos) {
  154. this.ruleDescriptionSectionDtos.clear();
  155. ruleDescriptionSectionDtos.forEach(this::addRuleDescriptionSectionDto);
  156. return this;
  157. }
  158. public RuleDto addRuleDescriptionSectionDto(RuleDescriptionSectionDto ruleDescriptionSectionDto) {
  159. checkArgument(sectionWithSameKeyShouldNotExist(ruleDescriptionSectionDto),
  160. "A section with key %s already exists", ruleDescriptionSectionDto.getKey());
  161. ruleDescriptionSectionDtos.add(ruleDescriptionSectionDto);
  162. return this;
  163. }
  164. private boolean sectionWithSameKeyShouldNotExist(RuleDescriptionSectionDto ruleDescriptionSectionDto) {
  165. return findExistingSectionWithSameKey(ruleDescriptionSectionDto.getKey()).isEmpty();
  166. }
  167. public RuleDto addOrReplaceRuleDescriptionSectionDto(RuleDescriptionSectionDto ruleDescriptionSectionDto) {
  168. Optional<RuleDescriptionSectionDto> existingSectionWithSameKey = findExistingSectionWithSameKey(ruleDescriptionSectionDto.getKey());
  169. existingSectionWithSameKey.ifPresent(ruleDescriptionSectionDtos::remove);
  170. ruleDescriptionSectionDtos.add(ruleDescriptionSectionDto);
  171. return this;
  172. }
  173. private Optional<RuleDescriptionSectionDto> findExistingSectionWithSameKey(String ruleDescriptionSectionKey) {
  174. return ruleDescriptionSectionDtos.stream().filter(section -> section.getKey().equals(ruleDescriptionSectionKey)).findAny();
  175. }
  176. @CheckForNull
  177. public Format getDescriptionFormat() {
  178. return descriptionFormat;
  179. }
  180. public RuleDto setDescriptionFormat(Format descriptionFormat) {
  181. this.descriptionFormat = descriptionFormat;
  182. return this;
  183. }
  184. public RuleStatus getStatus() {
  185. return status;
  186. }
  187. public RuleDto setStatus(@Nullable RuleStatus status) {
  188. this.status = status;
  189. return this;
  190. }
  191. public String getName() {
  192. return name;
  193. }
  194. public RuleDto setName(@Nullable String name) {
  195. checkArgument(name == null || name.length() <= 255, "Rule name is too long: %s", name);
  196. this.name = name;
  197. return this;
  198. }
  199. public String getConfigKey() {
  200. return configKey;
  201. }
  202. public RuleDto setConfigKey(@Nullable String configKey) {
  203. this.configKey = configKey;
  204. return this;
  205. }
  206. public Scope getScope() {
  207. return scope;
  208. }
  209. public RuleDto setScope(Scope scope) {
  210. this.scope = scope;
  211. return this;
  212. }
  213. @CheckForNull
  214. public Integer getSeverity() {
  215. return severity;
  216. }
  217. @CheckForNull
  218. public String getSeverityString() {
  219. return severity != null ? SeverityUtil.getSeverityFromOrdinal(severity) : null;
  220. }
  221. public RuleDto setSeverity(@Nullable String severity) {
  222. return this.setSeverity(severity != null ? SeverityUtil.getOrdinalFromSeverity(severity) : null);
  223. }
  224. public RuleDto setSeverity(@Nullable Integer severity) {
  225. this.severity = severity;
  226. return this;
  227. }
  228. public boolean isExternal() {
  229. return isExternal;
  230. }
  231. public RuleDto setIsExternal(boolean isExternal) {
  232. this.isExternal = isExternal;
  233. return this;
  234. }
  235. public boolean isAdHoc() {
  236. return isAdHoc;
  237. }
  238. public RuleDto setIsAdHoc(boolean isAdHoc) {
  239. this.isAdHoc = isAdHoc;
  240. return this;
  241. }
  242. @CheckForNull
  243. public String getAdHocName() {
  244. return metadata.getAdHocName();
  245. }
  246. public RuleDto setAdHocName(@Nullable String adHocName) {
  247. metadata.setAdHocName(adHocName);
  248. return this;
  249. }
  250. @CheckForNull
  251. public String getAdHocDescription() {
  252. return metadata.getAdHocDescription();
  253. }
  254. public RuleDto setAdHocDescription(@Nullable String adHocDescription) {
  255. metadata.setAdHocDescription(adHocDescription);
  256. return this;
  257. }
  258. @CheckForNull
  259. public String getAdHocSeverity() {
  260. return metadata.getAdHocSeverity();
  261. }
  262. public RuleDto setAdHocSeverity(@Nullable String adHocSeverity) {
  263. metadata.setAdHocSeverity(adHocSeverity);
  264. return this;
  265. }
  266. @CheckForNull
  267. public Integer getAdHocType() {
  268. return metadata.getAdHocType();
  269. }
  270. public RuleDto setAdHocType(@Nullable Integer type) {
  271. metadata.setAdHocType(type);
  272. return this;
  273. }
  274. public RuleDto setAdHocType(@Nullable RuleType adHocType) {
  275. metadata.setAdHocType(adHocType);
  276. return this;
  277. }
  278. public boolean isTemplate() {
  279. return isTemplate;
  280. }
  281. public RuleDto setIsTemplate(boolean isTemplate) {
  282. this.isTemplate = isTemplate;
  283. return this;
  284. }
  285. @CheckForNull
  286. public String getLanguage() {
  287. return language;
  288. }
  289. public RuleDto setLanguage(String language) {
  290. this.language = language;
  291. return this;
  292. }
  293. @CheckForNull
  294. public String getTemplateUuid() {
  295. return templateUuid;
  296. }
  297. public RuleDto setTemplateUuid(@Nullable String templateUuid) {
  298. this.templateUuid = templateUuid;
  299. return this;
  300. }
  301. public boolean isCustomRule() {
  302. return getTemplateUuid() != null;
  303. }
  304. public RuleDto setSystemTags(Set<String> tags) {
  305. this.systemTagsField = serializeStringSet(tags);
  306. return this;
  307. }
  308. public RuleDto setSecurityStandards(Set<String> standards) {
  309. this.securityStandardsField = serializeStringSet(standards);
  310. return this;
  311. }
  312. public Set<String> getSystemTags() {
  313. return deserializeTagsString(systemTagsField);
  314. }
  315. public Set<String> getSecurityStandards() {
  316. return deserializeSecurityStandardsString(securityStandardsField);
  317. }
  318. public int getType() {
  319. return type;
  320. }
  321. public RuleDto setType(int type) {
  322. this.type = type;
  323. return this;
  324. }
  325. public RuleDto setType(RuleType type) {
  326. this.type = type.getDbConstant();
  327. return this;
  328. }
  329. public long getCreatedAt() {
  330. return createdAt;
  331. }
  332. public RuleDto setCreatedAt(long createdAt) {
  333. this.createdAt = createdAt;
  334. return this;
  335. }
  336. public long getUpdatedAt() {
  337. return updatedAt;
  338. }
  339. public RuleDto setUpdatedAt(long updatedAt) {
  340. this.updatedAt = updatedAt;
  341. return this;
  342. }
  343. @CheckForNull
  344. public String getNoteData() {
  345. return metadata.getNoteData();
  346. }
  347. public RuleDto setNoteData(@Nullable String s) {
  348. metadata.setNoteData(s);
  349. return this;
  350. }
  351. @CheckForNull
  352. public String getNoteUserUuid() {
  353. return metadata.getNoteUserUuid();
  354. }
  355. public RuleDto setNoteUserUuid(@Nullable String noteUserUuid) {
  356. metadata.setNoteUserUuid(noteUserUuid);
  357. return this;
  358. }
  359. @CheckForNull
  360. public Long getNoteCreatedAt() {
  361. return metadata.getNoteCreatedAt();
  362. }
  363. public RuleDto setNoteCreatedAt(@Nullable Long noteCreatedAt) {
  364. metadata.setNoteCreatedAt(noteCreatedAt);
  365. return this;
  366. }
  367. @CheckForNull
  368. public Long getNoteUpdatedAt() {
  369. return metadata.getNoteUpdatedAt();
  370. }
  371. public RuleDto setNoteUpdatedAt(@Nullable Long noteUpdatedAt) {
  372. metadata.setNoteUpdatedAt(noteUpdatedAt);
  373. return this;
  374. }
  375. @CheckForNull
  376. public String getDefRemediationFunction() {
  377. return defRemediationFunction;
  378. }
  379. public RuleDto setDefRemediationFunction(@Nullable String defaultRemediationFunction) {
  380. this.defRemediationFunction = defaultRemediationFunction;
  381. return this;
  382. }
  383. @CheckForNull
  384. public String getDefRemediationGapMultiplier() {
  385. return defRemediationGapMultiplier;
  386. }
  387. public RuleDto setDefRemediationGapMultiplier(@Nullable String defaultRemediationGapMultiplier) {
  388. this.defRemediationGapMultiplier = defaultRemediationGapMultiplier;
  389. return this;
  390. }
  391. @CheckForNull
  392. public String getDefRemediationBaseEffort() {
  393. return defRemediationBaseEffort;
  394. }
  395. public RuleDto setDefRemediationBaseEffort(@Nullable String defaultRemediationBaseEffort) {
  396. this.defRemediationBaseEffort = defaultRemediationBaseEffort;
  397. return this;
  398. }
  399. @CheckForNull
  400. public String getGapDescription() {
  401. return gapDescription;
  402. }
  403. public RuleDto setGapDescription(@Nullable String s) {
  404. this.gapDescription = s;
  405. return this;
  406. }
  407. @CheckForNull
  408. public String getRemediationFunction() {
  409. return metadata.getRemediationFunction();
  410. }
  411. public RuleDto setRemediationFunction(@Nullable String remediationFunction) {
  412. metadata.setRemediationFunction(remediationFunction);
  413. return this;
  414. }
  415. @CheckForNull
  416. public String getRemediationGapMultiplier() {
  417. return metadata.getRemediationGapMultiplier();
  418. }
  419. public RuleDto setRemediationGapMultiplier(@Nullable String remediationGapMultiplier) {
  420. metadata.setRemediationGapMultiplier(remediationGapMultiplier);
  421. return this;
  422. }
  423. @CheckForNull
  424. public String getRemediationBaseEffort() {
  425. return metadata.getRemediationBaseEffort();
  426. }
  427. public RuleDto setRemediationBaseEffort(@Nullable String remediationBaseEffort) {
  428. metadata.setRemediationBaseEffort(remediationBaseEffort);
  429. return this;
  430. }
  431. public Set<String> getTags() {
  432. return metadata.getTags();
  433. }
  434. /**
  435. * Used in MyBatis mapping.
  436. */
  437. private void setTagsField(String s) {
  438. metadata.setTagsField(s);
  439. }
  440. public static Set<String> deserializeTagsString(@Nullable String tags) {
  441. return deserializeStringSet(tags);
  442. }
  443. public static Set<String> deserializeSecurityStandardsString(@Nullable String securityStandards) {
  444. return deserializeStringSet(securityStandards);
  445. }
  446. private static Set<String> deserializeStringSet(@Nullable String str) {
  447. if (str == null || str.isEmpty()) {
  448. return emptySet();
  449. }
  450. return ImmutableSet.copyOf(SPLITTER.split(str));
  451. }
  452. public RuleDto setTags(Set<String> tags) {
  453. this.metadata.setTags(tags);
  454. return this;
  455. }
  456. private static String serializeStringSet(@Nullable Set<String> strings) {
  457. return strings == null || strings.isEmpty() ? null : StringUtils.join(strings, ',');
  458. }
  459. @Override
  460. public boolean equals(Object obj) {
  461. if (!(obj instanceof RuleDto)) {
  462. return false;
  463. }
  464. if (this == obj) {
  465. return true;
  466. }
  467. RuleDto other = (RuleDto) obj;
  468. return Objects.equals(this.uuid, other.uuid);
  469. }
  470. @Override
  471. public int hashCode() {
  472. return new HashCodeBuilder(17, 37)
  473. .append(this.uuid)
  474. .toHashCode();
  475. }
  476. }