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

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