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.

IssueQuery.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.server.issue.index;
  21. import java.time.ZoneId;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Date;
  25. import java.util.Map;
  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.builder.ReflectionToStringBuilder;
  31. import org.sonar.db.rule.RuleDto;
  32. import static com.google.common.base.Preconditions.checkArgument;
  33. import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE;
  34. /**
  35. * @since 3.6
  36. */
  37. public class IssueQuery {
  38. public static final String SORT_BY_CREATION_DATE = "CREATION_DATE";
  39. public static final String SORT_BY_UPDATE_DATE = "UPDATE_DATE";
  40. public static final String SORT_BY_CLOSE_DATE = "CLOSE_DATE";
  41. public static final String SORT_BY_SEVERITY = "SEVERITY";
  42. public static final String SORT_BY_STATUS = "STATUS";
  43. /**
  44. * Sort by project, file path then line id
  45. */
  46. public static final String SORT_BY_FILE_LINE = "FILE_LINE";
  47. /**
  48. * Sort hotspots by vulnerabilityProbability, sqSecurityCategory, project, file path then line id
  49. */
  50. public static final String SORT_HOTSPOTS = "HOTSPOTS";
  51. public static final Set<String> SORTS = Set.of(SORT_BY_CREATION_DATE, SORT_BY_UPDATE_DATE, SORT_BY_CLOSE_DATE, SORT_BY_SEVERITY,
  52. SORT_BY_STATUS, SORT_BY_FILE_LINE, SORT_HOTSPOTS);
  53. private final Collection<String> issueKeys;
  54. private final Collection<String> severities;
  55. private final Collection<String> impactSeverities;
  56. private final Collection<String> impactSoftwareQualities;
  57. private final Collection<String> statuses;
  58. private final Collection<String> issueStatuses;
  59. private final Collection<String> resolutions;
  60. private final Collection<String> components;
  61. private final Collection<String> projects;
  62. private final Collection<String> directories;
  63. private final Collection<String> files;
  64. private final Collection<String> views;
  65. private final Collection<RuleDto> rules;
  66. private final Collection<String> ruleUuids;
  67. private final Collection<String> assignees;
  68. private final Collection<String> authors;
  69. private final Collection<String> scopes;
  70. private final Collection<String> languages;
  71. private final Collection<String> tags;
  72. private final Collection<String> types;
  73. private final Collection<String> owaspTop10;
  74. private final Collection<String> pciDss32;
  75. private final Collection<String> pciDss40;
  76. private final Collection<String> owaspAsvs40;
  77. private final Integer owaspAsvsLevel;
  78. private final Collection<String> owaspTop10For2021;
  79. private final Collection<String> sansTop25;
  80. private final Collection<String> cwe;
  81. private final Collection<String> sonarsourceSecurity;
  82. private final Map<String, PeriodStart> createdAfterByProjectUuids;
  83. private final Boolean onComponentOnly;
  84. private final Boolean assigned;
  85. private final Boolean resolved;
  86. private final Date createdAt;
  87. private final PeriodStart createdAfter;
  88. private final Date createdBefore;
  89. private final String sort;
  90. private final Boolean asc;
  91. private final String facetMode;
  92. private final String branchUuid;
  93. private final Boolean mainBranch;
  94. private final ZoneId timeZone;
  95. private final Boolean newCodeOnReference;
  96. private final Collection<String> newCodeOnReferenceByProjectUuids;
  97. private final Collection<String> codeVariants;
  98. private final Collection<String> cleanCodeAttributesCategories;
  99. private IssueQuery(Builder builder) {
  100. this.issueKeys = defaultCollection(builder.issueKeys);
  101. this.severities = defaultCollection(builder.severities);
  102. this.impactSeverities = defaultCollection(builder.impactSeverities);
  103. this.impactSoftwareQualities = defaultCollection(builder.impactSoftwareQualities);
  104. this.statuses = defaultCollection(builder.statuses);
  105. this.resolutions = defaultCollection(builder.resolutions);
  106. this.issueStatuses = defaultCollection(builder.issueStatuses);
  107. this.components = defaultCollection(builder.components);
  108. this.projects = defaultCollection(builder.projects);
  109. this.directories = defaultCollection(builder.directories);
  110. this.files = defaultCollection(builder.files);
  111. this.views = defaultCollection(builder.views);
  112. this.rules = defaultCollection(builder.rules);
  113. this.ruleUuids = defaultCollection(builder.ruleUuids);
  114. this.assignees = defaultCollection(builder.assigneeUuids);
  115. this.authors = defaultCollection(builder.authors);
  116. this.scopes = defaultCollection(builder.scopes);
  117. this.languages = defaultCollection(builder.languages);
  118. this.tags = defaultCollection(builder.tags);
  119. this.types = defaultCollection(builder.types);
  120. this.pciDss32 = defaultCollection(builder.pciDss32);
  121. this.pciDss40 = defaultCollection(builder.pciDss40);
  122. this.owaspAsvs40 = defaultCollection(builder.owaspAsvs40);
  123. this.owaspAsvsLevel = builder.owaspAsvsLevel;
  124. this.owaspTop10 = defaultCollection(builder.owaspTop10);
  125. this.owaspTop10For2021 = defaultCollection(builder.owaspTop10For2021);
  126. this.sansTop25 = defaultCollection(builder.sansTop25);
  127. this.cwe = defaultCollection(builder.cwe);
  128. this.sonarsourceSecurity = defaultCollection(builder.sonarsourceSecurity);
  129. this.createdAfterByProjectUuids = defaultMap(builder.createdAfterByProjectUuids);
  130. this.onComponentOnly = builder.onComponentOnly;
  131. this.assigned = builder.assigned;
  132. this.resolved = builder.resolved;
  133. this.createdAt = builder.createdAt;
  134. this.createdAfter = builder.createdAfter;
  135. this.createdBefore = builder.createdBefore;
  136. this.sort = builder.sort;
  137. this.asc = builder.asc;
  138. this.facetMode = builder.facetMode;
  139. this.branchUuid = builder.branchUuid;
  140. this.mainBranch = builder.mainBranch;
  141. this.timeZone = builder.timeZone;
  142. this.newCodeOnReference = builder.newCodeOnReference;
  143. this.newCodeOnReferenceByProjectUuids = defaultCollection(builder.newCodeOnReferenceByProjectUuids);
  144. this.codeVariants = defaultCollection(builder.codeVariants);
  145. this.cleanCodeAttributesCategories = defaultCollection(builder.cleanCodeAttributesCategories);
  146. }
  147. public Collection<String> issueKeys() {
  148. return issueKeys;
  149. }
  150. public Collection<String> severities() {
  151. return severities;
  152. }
  153. public Collection<String> impactSeverities() {
  154. return impactSeverities;
  155. }
  156. public Collection<String> impactSoftwareQualities() {
  157. return impactSoftwareQualities;
  158. }
  159. public Collection<String> statuses() {
  160. return statuses;
  161. }
  162. public Collection<String> issueStatuses() {
  163. return issueStatuses;
  164. }
  165. public Collection<String> resolutions() {
  166. return resolutions;
  167. }
  168. public Collection<String> componentUuids() {
  169. return components;
  170. }
  171. public Collection<String> projectUuids() {
  172. return projects;
  173. }
  174. public Collection<String> directories() {
  175. return directories;
  176. }
  177. public Collection<String> files() {
  178. return files;
  179. }
  180. /**
  181. * Restrict issues belonging to projects that were analyzed under a view.
  182. * The view UUIDs should be portfolios, sub portfolios or application branches.
  183. */
  184. public Collection<String> viewUuids() {
  185. return views;
  186. }
  187. public Collection<RuleDto> rules() {
  188. return rules;
  189. }
  190. public Collection<String> ruleUuids() {
  191. return ruleUuids;
  192. }
  193. public Collection<String> assignees() {
  194. return assignees;
  195. }
  196. public Collection<String> authors() {
  197. return authors;
  198. }
  199. public Collection<String> scopes() {
  200. return scopes;
  201. }
  202. public Collection<String> languages() {
  203. return languages;
  204. }
  205. public Collection<String> tags() {
  206. return tags;
  207. }
  208. public Collection<String> types() {
  209. return types;
  210. }
  211. public Collection<String> pciDss32() {
  212. return pciDss32;
  213. }
  214. public Collection<String> pciDss40() {
  215. return pciDss40;
  216. }
  217. public Collection<String> owaspAsvs40() {
  218. return owaspAsvs40;
  219. }
  220. public Optional<Integer> getOwaspAsvsLevel() {
  221. return Optional.ofNullable(owaspAsvsLevel);
  222. }
  223. public Collection<String> owaspTop10() {
  224. return owaspTop10;
  225. }
  226. public Collection<String> owaspTop10For2021() {
  227. return owaspTop10For2021;
  228. }
  229. public Collection<String> sansTop25() {
  230. return sansTop25;
  231. }
  232. public Collection<String> cwe() {
  233. return cwe;
  234. }
  235. public Collection<String> sonarsourceSecurity() {
  236. return sonarsourceSecurity;
  237. }
  238. public Map<String, PeriodStart> createdAfterByProjectUuids() {
  239. return createdAfterByProjectUuids;
  240. }
  241. @CheckForNull
  242. public Boolean onComponentOnly() {
  243. return onComponentOnly;
  244. }
  245. @CheckForNull
  246. public Boolean assigned() {
  247. return assigned;
  248. }
  249. @CheckForNull
  250. public Boolean resolved() {
  251. return resolved;
  252. }
  253. @CheckForNull
  254. public PeriodStart createdAfter() {
  255. return createdAfter;
  256. }
  257. @CheckForNull
  258. public Date createdAt() {
  259. return createdAt == null ? null : new Date(createdAt.getTime());
  260. }
  261. @CheckForNull
  262. public Date createdBefore() {
  263. return createdBefore == null ? null : new Date(createdBefore.getTime());
  264. }
  265. @CheckForNull
  266. public String sort() {
  267. return sort;
  268. }
  269. @CheckForNull
  270. public Boolean asc() {
  271. return asc;
  272. }
  273. @CheckForNull
  274. public String branchUuid() {
  275. return branchUuid;
  276. }
  277. public Boolean isMainBranch() {
  278. return mainBranch;
  279. }
  280. public String facetMode() {
  281. return facetMode;
  282. }
  283. @Override
  284. public String toString() {
  285. return ReflectionToStringBuilder.toString(this);
  286. }
  287. public static Builder builder() {
  288. return new Builder();
  289. }
  290. @CheckForNull
  291. public ZoneId timeZone() {
  292. return timeZone;
  293. }
  294. @CheckForNull
  295. public Boolean newCodeOnReference() {
  296. return newCodeOnReference;
  297. }
  298. public Collection<String> newCodeOnReferenceByProjectUuids() {
  299. return newCodeOnReferenceByProjectUuids;
  300. }
  301. public Collection<String> codeVariants() {
  302. return codeVariants;
  303. }
  304. public Collection<String> cleanCodeAttributesCategories() {
  305. return cleanCodeAttributesCategories;
  306. }
  307. public static class Builder {
  308. private Collection<String> issueKeys;
  309. private Collection<String> severities;
  310. private Collection<String> impactSeverities;
  311. private Collection<String> impactSoftwareQualities;
  312. private Collection<String> statuses;
  313. private Collection<String> resolutions;
  314. private Collection<String> issueStatuses;
  315. private Collection<String> components;
  316. private Collection<String> projects;
  317. private Collection<String> directories;
  318. private Collection<String> files;
  319. private Collection<String> views;
  320. private Collection<RuleDto> rules;
  321. private Collection<String> ruleUuids;
  322. private Collection<String> assigneeUuids;
  323. private Collection<String> authors;
  324. private Collection<String> scopes;
  325. private Collection<String> languages;
  326. private Collection<String> tags;
  327. private Collection<String> types;
  328. private Collection<String> pciDss32;
  329. private Collection<String> pciDss40;
  330. private Collection<String> owaspAsvs40;
  331. private Integer owaspAsvsLevel;
  332. private Collection<String> owaspTop10;
  333. private Collection<String> owaspTop10For2021;
  334. private Collection<String> sansTop25;
  335. private Collection<String> cwe;
  336. private Collection<String> sonarsourceSecurity;
  337. private Map<String, PeriodStart> createdAfterByProjectUuids;
  338. private Boolean onComponentOnly = false;
  339. private Boolean assigned = null;
  340. private Boolean resolved = null;
  341. private Date createdAt;
  342. private PeriodStart createdAfter;
  343. private Date createdBefore;
  344. private String sort;
  345. private Boolean asc = false;
  346. private String facetMode;
  347. private String branchUuid;
  348. private Boolean mainBranch = true;
  349. private ZoneId timeZone;
  350. private Boolean newCodeOnReference = null;
  351. private Collection<String> newCodeOnReferenceByProjectUuids;
  352. private Collection<String> codeVariants;
  353. private Collection<String> cleanCodeAttributesCategories;
  354. private Builder() {
  355. }
  356. public Builder issueKeys(@Nullable Collection<String> l) {
  357. this.issueKeys = l;
  358. return this;
  359. }
  360. public Builder severities(@Nullable Collection<String> l) {
  361. this.severities = l;
  362. return this;
  363. }
  364. public Builder statuses(@Nullable Collection<String> l) {
  365. this.statuses = l;
  366. return this;
  367. }
  368. public Builder resolutions(@Nullable Collection<String> l) {
  369. this.resolutions = l;
  370. return this;
  371. }
  372. public Builder issueStatuses(@Nullable Collection<String> l) {
  373. this.issueStatuses = l;
  374. return this;
  375. }
  376. public Builder componentUuids(@Nullable Collection<String> l) {
  377. this.components = l;
  378. return this;
  379. }
  380. public Builder projectUuids(@Nullable Collection<String> l) {
  381. this.projects = l;
  382. return this;
  383. }
  384. public Builder directories(@Nullable Collection<String> l) {
  385. this.directories = l;
  386. return this;
  387. }
  388. public Builder impactSeverities(@Nullable Collection<String> l) {
  389. this.impactSeverities = l;
  390. return this;
  391. }
  392. public Builder impactSoftwareQualities(@Nullable Collection<String> l) {
  393. this.impactSoftwareQualities = l;
  394. return this;
  395. }
  396. public Builder files(@Nullable Collection<String> l) {
  397. this.files = l;
  398. return this;
  399. }
  400. /**
  401. * Restrict issues belonging to projects that were analyzed under a view.
  402. * The view UUIDs should be portfolios, sub portfolios or application branches.
  403. */
  404. public Builder viewUuids(@Nullable Collection<String> l) {
  405. this.views = l;
  406. return this;
  407. }
  408. public Builder rules(@Nullable Collection<RuleDto> rules) {
  409. this.rules = rules;
  410. return this;
  411. }
  412. public Builder ruleUuids(@Nullable Collection<String> ruleUuids) {
  413. this.ruleUuids = ruleUuids;
  414. return this;
  415. }
  416. public Builder assigneeUuids(@Nullable Collection<String> l) {
  417. this.assigneeUuids = l;
  418. return this;
  419. }
  420. public Builder authors(@Nullable Collection<String> l) {
  421. this.authors = l;
  422. return this;
  423. }
  424. public Builder scopes(@Nullable Collection<String> s) {
  425. this.scopes = s;
  426. return this;
  427. }
  428. public Builder languages(@Nullable Collection<String> l) {
  429. this.languages = l;
  430. return this;
  431. }
  432. public Builder tags(@Nullable Collection<String> t) {
  433. this.tags = t;
  434. return this;
  435. }
  436. public Builder types(@Nullable Collection<String> t) {
  437. this.types = t;
  438. return this;
  439. }
  440. public Builder pciDss32(@Nullable Collection<String> o) {
  441. this.pciDss32 = o;
  442. return this;
  443. }
  444. public Builder pciDss40(@Nullable Collection<String> o) {
  445. this.pciDss40 = o;
  446. return this;
  447. }
  448. public Builder owaspAsvs40(@Nullable Collection<String> o) {
  449. this.owaspAsvs40 = o;
  450. return this;
  451. }
  452. public Builder owaspAsvsLevel(@Nullable Integer level) {
  453. this.owaspAsvsLevel = level;
  454. return this;
  455. }
  456. public Builder owaspTop10(@Nullable Collection<String> o) {
  457. this.owaspTop10 = o;
  458. return this;
  459. }
  460. public Builder owaspTop10For2021(@Nullable Collection<String> o) {
  461. this.owaspTop10For2021 = o;
  462. return this;
  463. }
  464. public Builder sansTop25(@Nullable Collection<String> s) {
  465. this.sansTop25 = s;
  466. return this;
  467. }
  468. public Builder cwe(@Nullable Collection<String> cwe) {
  469. this.cwe = cwe;
  470. return this;
  471. }
  472. public Builder sonarsourceSecurity(@Nullable Collection<String> sonarsourceSecurity) {
  473. this.sonarsourceSecurity = sonarsourceSecurity;
  474. return this;
  475. }
  476. public Builder createdAfterByProjectUuids(@Nullable Map<String, PeriodStart> createdAfterByProjectUuids) {
  477. this.createdAfterByProjectUuids = createdAfterByProjectUuids;
  478. return this;
  479. }
  480. /**
  481. * If true, it will return only issues on the passed component(s)
  482. * If false, it will return all issues on the passed component(s) and their descendants
  483. */
  484. public Builder onComponentOnly(@Nullable Boolean b) {
  485. this.onComponentOnly = b;
  486. return this;
  487. }
  488. /**
  489. * If true, it will return all issues assigned to someone
  490. * If false, it will return all issues not assigned to someone
  491. */
  492. public Builder assigned(@Nullable Boolean b) {
  493. this.assigned = b;
  494. return this;
  495. }
  496. /**
  497. * If true, it will return all resolved issues
  498. * If false, it will return all none resolved issues
  499. */
  500. public Builder resolved(@Nullable Boolean resolved) {
  501. this.resolved = resolved;
  502. return this;
  503. }
  504. public Builder createdAt(@Nullable Date d) {
  505. this.createdAt = d == null ? null : new Date(d.getTime());
  506. return this;
  507. }
  508. public Builder createdAfter(@Nullable Date d) {
  509. this.createdAfter(d, true);
  510. return this;
  511. }
  512. public Builder createdAfter(@Nullable Date d, boolean inclusive) {
  513. this.createdAfter = d == null ? null : new PeriodStart(new Date(d.getTime()), inclusive);
  514. return this;
  515. }
  516. public Builder createdBefore(@Nullable Date d) {
  517. this.createdBefore = d == null ? null : new Date(d.getTime());
  518. return this;
  519. }
  520. public Builder sort(@Nullable String s) {
  521. if (s != null && !SORTS.contains(s)) {
  522. throw new IllegalArgumentException("Bad sort field: " + s);
  523. }
  524. this.sort = s;
  525. return this;
  526. }
  527. public Builder asc(@Nullable Boolean asc) {
  528. this.asc = asc;
  529. return this;
  530. }
  531. public IssueQuery build() {
  532. if (issueKeys != null) {
  533. checkArgument(issueKeys.size() <= MAX_PAGE_SIZE, "Number of issue keys must be less than " + MAX_PAGE_SIZE + " (got " + issueKeys.size() + ")");
  534. }
  535. return new IssueQuery(this);
  536. }
  537. public Builder facetMode(String facetMode) {
  538. this.facetMode = facetMode;
  539. return this;
  540. }
  541. public Builder branchUuid(@Nullable String s) {
  542. this.branchUuid = s;
  543. return this;
  544. }
  545. public Builder mainBranch(@Nullable Boolean mainBranch) {
  546. this.mainBranch = mainBranch;
  547. return this;
  548. }
  549. public Builder timeZone(ZoneId timeZone) {
  550. this.timeZone = timeZone;
  551. return this;
  552. }
  553. public Builder newCodeOnReference(@Nullable Boolean newCodeOnReference) {
  554. this.newCodeOnReference = newCodeOnReference;
  555. return this;
  556. }
  557. public Builder newCodeOnReferenceByProjectUuids(@Nullable Collection<String> newCodeOnReferenceByProjectUuids) {
  558. this.newCodeOnReferenceByProjectUuids = newCodeOnReferenceByProjectUuids;
  559. return this;
  560. }
  561. public Builder codeVariants(@Nullable Collection<String> codeVariants) {
  562. this.codeVariants = codeVariants;
  563. return this;
  564. }
  565. public Builder cleanCodeAttributesCategories(@Nullable Collection<String> cleanCodeAttributesCategories) {
  566. this.cleanCodeAttributesCategories = cleanCodeAttributesCategories;
  567. return this;
  568. }
  569. }
  570. private static <T> Collection<T> defaultCollection(@Nullable Collection<T> c) {
  571. return c == null ? Collections.emptyList() : Collections.unmodifiableCollection(c);
  572. }
  573. private static <K, V> Map<K, V> defaultMap(@Nullable Map<K, V> map) {
  574. return map == null ? Collections.emptyMap() : Collections.unmodifiableMap(map);
  575. }
  576. public static class PeriodStart {
  577. private final Date date;
  578. private final boolean inclusive;
  579. public PeriodStart(Date date, boolean inclusive) {
  580. this.date = date;
  581. this.inclusive = inclusive;
  582. }
  583. public Date date() {
  584. return date;
  585. }
  586. public boolean inclusive() {
  587. return inclusive;
  588. }
  589. }
  590. }