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 18KB

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