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.

SearchResponseFormat.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.ws;
  21. import com.google.common.base.Strings;
  22. import java.util.ArrayList;
  23. import java.util.Collection;
  24. import java.util.Collections;
  25. import java.util.Date;
  26. import java.util.LinkedHashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Set;
  30. import javax.annotation.Nullable;
  31. import org.sonar.api.resources.Language;
  32. import org.sonar.api.resources.Languages;
  33. import org.sonar.api.utils.DateUtils;
  34. import org.sonar.api.utils.Duration;
  35. import org.sonar.api.utils.Durations;
  36. import org.sonar.api.utils.Paging;
  37. import org.sonar.db.component.ComponentDto;
  38. import org.sonar.db.issue.IssueChangeDto;
  39. import org.sonar.db.issue.IssueDto;
  40. import org.sonar.db.protobuf.DbCommons;
  41. import org.sonar.db.protobuf.DbIssues;
  42. import org.sonar.db.rule.RuleDefinitionDto;
  43. import org.sonar.db.user.UserDto;
  44. import org.sonar.markdown.Markdown;
  45. import org.sonar.server.es.Facets;
  46. import org.sonar.server.issue.workflow.Transition;
  47. import org.sonar.server.ws.WsResponseCommonFormat;
  48. import org.sonarqube.ws.Common;
  49. import org.sonarqube.ws.Issues;
  50. import org.sonarqube.ws.Issues.Actions;
  51. import org.sonarqube.ws.Issues.Comment;
  52. import org.sonarqube.ws.Issues.Comments;
  53. import org.sonarqube.ws.Issues.Component;
  54. import org.sonarqube.ws.Issues.Flow;
  55. import org.sonarqube.ws.Issues.Issue;
  56. import org.sonarqube.ws.Issues.Location;
  57. import org.sonarqube.ws.Issues.Operation;
  58. import org.sonarqube.ws.Issues.SearchWsResponse;
  59. import org.sonarqube.ws.Issues.Transitions;
  60. import org.sonarqube.ws.Issues.Users;
  61. import static com.google.common.base.Strings.emptyToNull;
  62. import static com.google.common.base.Strings.nullToEmpty;
  63. import static org.sonar.core.util.Protobuf.setNullable;
  64. public class SearchResponseFormat {
  65. private final Durations durations;
  66. private final WsResponseCommonFormat commonFormat;
  67. private final Languages languages;
  68. private final AvatarResolver avatarFactory;
  69. public SearchResponseFormat(Durations durations, WsResponseCommonFormat commonFormat, Languages languages, AvatarResolver avatarFactory) {
  70. this.durations = durations;
  71. this.commonFormat = commonFormat;
  72. this.languages = languages;
  73. this.avatarFactory = avatarFactory;
  74. }
  75. public SearchWsResponse formatSearch(Set<SearchAdditionalField> fields, SearchResponseData data,
  76. Paging paging, @Nullable Facets facets) {
  77. SearchWsResponse.Builder response = SearchWsResponse.newBuilder();
  78. formatPaging(paging, response);
  79. formatEffortTotal(data, response);
  80. response.addAllIssues(formatIssues(fields, data));
  81. response.addAllComponents(formatComponents(data));
  82. if (facets != null) {
  83. formatFacets(facets, response);
  84. }
  85. if (fields.contains(SearchAdditionalField.RULE_IDS_AND_KEYS)) {
  86. response.setRules(formatRules(data));
  87. }
  88. if (fields.contains(SearchAdditionalField.USERS)) {
  89. response.setUsers(formatUsers(data));
  90. }
  91. if (fields.contains(SearchAdditionalField.LANGUAGES)) {
  92. response.setLanguages(formatLanguages());
  93. }
  94. return response.build();
  95. }
  96. public Operation formatOperation(SearchResponseData data) {
  97. Operation.Builder response = Operation.newBuilder();
  98. if (data.getIssues().size() == 1) {
  99. Issue.Builder issueBuilder = Issue.newBuilder();
  100. IssueDto dto = data.getIssues().get(0);
  101. formatIssue(issueBuilder, dto, data);
  102. formatIssueActions(data, issueBuilder, dto);
  103. formatIssueTransitions(data, issueBuilder, dto);
  104. formatIssueComments(data, issueBuilder, dto);
  105. response.setIssue(issueBuilder.build());
  106. }
  107. response.addAllComponents(formatComponents(data));
  108. response.addAllRules(formatRules(data).getRulesList());
  109. response.addAllUsers(formatUsers(data).getUsersList());
  110. return response.build();
  111. }
  112. private void formatEffortTotal(SearchResponseData data, SearchWsResponse.Builder response) {
  113. Long effort = data.getEffortTotal();
  114. if (effort != null) {
  115. response.setDebtTotal(effort);
  116. response.setEffortTotal(effort);
  117. }
  118. }
  119. private void formatPaging(Paging paging, SearchWsResponse.Builder response) {
  120. response.setP(paging.pageIndex());
  121. response.setPs(paging.pageSize());
  122. response.setTotal(paging.total());
  123. response.setPaging(commonFormat.formatPaging(paging));
  124. }
  125. private List<Issues.Issue> formatIssues(Set<SearchAdditionalField> fields, SearchResponseData data) {
  126. List<Issues.Issue> result = new ArrayList<>();
  127. Issue.Builder issueBuilder = Issue.newBuilder();
  128. data.getIssues().forEach(dto -> {
  129. issueBuilder.clear();
  130. formatIssue(issueBuilder, dto, data);
  131. if (fields.contains(SearchAdditionalField.ACTIONS)) {
  132. formatIssueActions(data, issueBuilder, dto);
  133. }
  134. if (fields.contains(SearchAdditionalField.TRANSITIONS)) {
  135. formatIssueTransitions(data, issueBuilder, dto);
  136. }
  137. if (fields.contains(SearchAdditionalField.COMMENTS)) {
  138. formatIssueComments(data, issueBuilder, dto);
  139. }
  140. result.add(issueBuilder.build());
  141. });
  142. return result;
  143. }
  144. private void formatIssue(Issue.Builder issueBuilder, IssueDto dto, SearchResponseData data) {
  145. issueBuilder.setKey(dto.getKey());
  146. setNullable(dto.getType(), issueBuilder::setType, Common.RuleType::valueOf);
  147. ComponentDto component = data.getComponentByUuid(dto.getComponentUuid());
  148. issueBuilder.setOrganization(data.getOrganizationKey(component.getOrganizationUuid()));
  149. issueBuilder.setComponent(component.getKey());
  150. setNullable(component.getBranch(), issueBuilder::setBranch);
  151. setNullable(component.getPullRequest(), issueBuilder::setPullRequest);
  152. ComponentDto project = data.getComponentByUuid(dto.getProjectUuid());
  153. if (project != null) {
  154. issueBuilder.setProject(project.getKey());
  155. ComponentDto subProject = data.getComponentByUuid(dto.getModuleUuid());
  156. if (subProject != null && !subProject.getDbKey().equals(project.getDbKey())) {
  157. issueBuilder.setSubProject(subProject.getKey());
  158. }
  159. }
  160. issueBuilder.setRule(dto.getRuleKey().toString());
  161. issueBuilder.setSeverity(Common.Severity.valueOf(dto.getSeverity()));
  162. setNullable(emptyToNull(dto.getAssignee()), issueBuilder::setAssignee);
  163. setNullable(emptyToNull(dto.getResolution()), issueBuilder::setResolution);
  164. issueBuilder.setStatus(dto.getStatus());
  165. issueBuilder.setMessage(nullToEmpty(dto.getMessage()));
  166. issueBuilder.addAllTags(dto.getTags());
  167. Long effort = dto.getEffort();
  168. if (effort != null) {
  169. String effortValue = durations.encode(Duration.create(effort));
  170. issueBuilder.setDebt(effortValue);
  171. issueBuilder.setEffort(effortValue);
  172. }
  173. setNullable(dto.getLine(), issueBuilder::setLine);
  174. setNullable(emptyToNull(dto.getChecksum()), issueBuilder::setHash);
  175. completeIssueLocations(dto, issueBuilder);
  176. issueBuilder.setAuthor(nullToEmpty(dto.getAuthorLogin()));
  177. setNullable(dto.getIssueCreationDate(), issueBuilder::setCreationDate, DateUtils::formatDateTime);
  178. setNullable(dto.getIssueUpdateDate(), issueBuilder::setUpdateDate, DateUtils::formatDateTime);
  179. setNullable(dto.getIssueCloseDate(), issueBuilder::setCloseDate, DateUtils::formatDateTime);
  180. }
  181. private static void completeIssueLocations(IssueDto dto, Issue.Builder issueBuilder) {
  182. DbIssues.Locations locations = dto.parseLocations();
  183. if (locations == null) {
  184. return;
  185. }
  186. if (locations.hasTextRange()) {
  187. DbCommons.TextRange textRange = locations.getTextRange();
  188. issueBuilder.setTextRange(convertTextRange(textRange));
  189. }
  190. for (DbIssues.Flow flow : locations.getFlowList()) {
  191. Flow.Builder targetFlow = Flow.newBuilder();
  192. for (DbIssues.Location flowLocation : flow.getLocationList()) {
  193. targetFlow.addLocations(convertLocation(flowLocation));
  194. }
  195. issueBuilder.addFlows(targetFlow);
  196. }
  197. }
  198. private static Location convertLocation(DbIssues.Location source) {
  199. Location.Builder target = Location.newBuilder();
  200. if (source.hasMsg()) {
  201. target.setMsg(source.getMsg());
  202. }
  203. if (source.hasTextRange()) {
  204. DbCommons.TextRange sourceRange = source.getTextRange();
  205. Common.TextRange.Builder targetRange = convertTextRange(sourceRange);
  206. target.setTextRange(targetRange);
  207. }
  208. return target.build();
  209. }
  210. private static Common.TextRange.Builder convertTextRange(DbCommons.TextRange sourceRange) {
  211. Common.TextRange.Builder targetRange = Common.TextRange.newBuilder();
  212. if (sourceRange.hasStartLine()) {
  213. targetRange.setStartLine(sourceRange.getStartLine());
  214. }
  215. if (sourceRange.hasStartOffset()) {
  216. targetRange.setStartOffset(sourceRange.getStartOffset());
  217. }
  218. if (sourceRange.hasEndLine()) {
  219. targetRange.setEndLine(sourceRange.getEndLine());
  220. }
  221. if (sourceRange.hasEndOffset()) {
  222. targetRange.setEndOffset(sourceRange.getEndOffset());
  223. }
  224. return targetRange;
  225. }
  226. private static void formatIssueTransitions(SearchResponseData data, Issue.Builder wsIssue, IssueDto dto) {
  227. Transitions.Builder wsTransitions = Transitions.newBuilder();
  228. List<Transition> transitions = data.getTransitionsForIssueKey(dto.getKey());
  229. if (transitions != null) {
  230. for (Transition transition : transitions) {
  231. wsTransitions.addTransitions(transition.key());
  232. }
  233. }
  234. wsIssue.setTransitions(wsTransitions);
  235. }
  236. private static void formatIssueActions(SearchResponseData data, Issue.Builder wsIssue, IssueDto dto) {
  237. Actions.Builder wsActions = Actions.newBuilder();
  238. List<String> actions = data.getActionsForIssueKey(dto.getKey());
  239. if (actions != null) {
  240. wsActions.addAllActions(actions);
  241. }
  242. wsIssue.setActions(wsActions);
  243. }
  244. private static void formatIssueComments(SearchResponseData data, Issue.Builder wsIssue, IssueDto dto) {
  245. Comments.Builder wsComments = Comments.newBuilder();
  246. List<IssueChangeDto> comments = data.getCommentsForIssueKey(dto.getKey());
  247. if (comments != null) {
  248. Comment.Builder wsComment = Comment.newBuilder();
  249. for (IssueChangeDto comment : comments) {
  250. String markdown = comment.getChangeData();
  251. wsComment
  252. .clear()
  253. .setKey(comment.getKey())
  254. .setLogin(nullToEmpty(comment.getUserLogin()))
  255. .setUpdatable(data.isUpdatableComment(comment.getKey()))
  256. .setCreatedAt(DateUtils.formatDateTime(new Date(comment.getIssueChangeCreationDate())));
  257. if (markdown != null) {
  258. wsComment
  259. .setHtmlText(Markdown.convertToHtml(markdown))
  260. .setMarkdown(markdown);
  261. }
  262. wsComments.addComments(wsComment);
  263. }
  264. }
  265. wsIssue.setComments(wsComments);
  266. }
  267. private Common.Rules.Builder formatRules(SearchResponseData data) {
  268. Common.Rules.Builder wsRules = Common.Rules.newBuilder();
  269. List<RuleDefinitionDto> rules = data.getRules();
  270. if (rules != null) {
  271. for (RuleDefinitionDto rule : rules) {
  272. wsRules.addRules(commonFormat.formatRule(rule));
  273. }
  274. }
  275. return wsRules;
  276. }
  277. private static List<Issues.Component> formatComponents(SearchResponseData data) {
  278. Collection<ComponentDto> components = data.getComponents();
  279. List<Issues.Component> result = new ArrayList<>();
  280. for (ComponentDto dto : components) {
  281. String uuid = dto.uuid();
  282. Component.Builder builder = Component.newBuilder()
  283. .setOrganization(data.getOrganizationKey(dto.getOrganizationUuid()))
  284. .setKey(dto.getKey())
  285. .setUuid(uuid)
  286. .setQualifier(dto.qualifier())
  287. .setName(nullToEmpty(dto.name()))
  288. .setLongName(nullToEmpty(dto.longName()))
  289. .setEnabled(dto.isEnabled());
  290. setNullable(dto.getBranch(), builder::setBranch);
  291. setNullable(dto.getPullRequest(), builder::setPullRequest);
  292. String path = dto.path();
  293. // path is not applicable to the components that are not files.
  294. // Value must not be "" in this case.
  295. if (!Strings.isNullOrEmpty(path)) {
  296. builder.setPath(path);
  297. }
  298. result.add(builder.build());
  299. }
  300. return result;
  301. }
  302. private Users.Builder formatUsers(SearchResponseData data) {
  303. Users.Builder wsUsers = Users.newBuilder();
  304. List<UserDto> users = data.getUsers();
  305. if (users != null) {
  306. for (UserDto user : users) {
  307. wsUsers.addUsers(formatUser(user));
  308. }
  309. }
  310. return wsUsers;
  311. }
  312. private Users.User.Builder formatUser(UserDto user) {
  313. Users.User.Builder builder = Users.User.newBuilder()
  314. .setLogin(user.getLogin())
  315. .setName(nullToEmpty(user.getName()))
  316. .setActive(user.isActive());
  317. setNullable(emptyToNull(user.getEmail()), email -> builder.setAvatar(avatarFactory.create(user)));
  318. return builder;
  319. }
  320. private Issues.Languages.Builder formatLanguages() {
  321. Issues.Languages.Builder wsLangs = Issues.Languages.newBuilder();
  322. Issues.Language.Builder wsLang = Issues.Language.newBuilder();
  323. for (Language lang : languages.all()) {
  324. wsLang
  325. .clear()
  326. .setKey(lang.getKey())
  327. .setName(lang.getName());
  328. wsLangs.addLanguages(wsLang);
  329. }
  330. return wsLangs;
  331. }
  332. private void formatFacets(Facets facets, SearchWsResponse.Builder wsSearch) {
  333. Common.Facets.Builder wsFacets = Common.Facets.newBuilder();
  334. Common.Facet.Builder wsFacet = Common.Facet.newBuilder();
  335. for (Map.Entry<String, LinkedHashMap<String, Long>> facet : facets.getAll().entrySet()) {
  336. wsFacet.clear();
  337. wsFacet.setProperty(facet.getKey());
  338. LinkedHashMap<String, Long> buckets = facet.getValue();
  339. if (buckets != null) {
  340. for (Map.Entry<String, Long> bucket : buckets.entrySet()) {
  341. Common.FacetValue.Builder valueBuilder = wsFacet.addValuesBuilder();
  342. valueBuilder.setVal(bucket.getKey());
  343. valueBuilder.setCount(bucket.getValue());
  344. valueBuilder.build();
  345. }
  346. } else {
  347. wsFacet.addAllValues(Collections.emptyList());
  348. }
  349. wsFacets.addFacets(wsFacet);
  350. }
  351. wsSearch.setFacets(wsFacets);
  352. }
  353. }