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.

ChangelogAction.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.qualityprofile.ws;
  21. import com.google.common.collect.Lists;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Objects;
  27. import java.util.Set;
  28. import java.util.function.Function;
  29. import java.util.stream.Collectors;
  30. import javax.annotation.CheckForNull;
  31. import org.sonar.api.resources.Languages;
  32. import org.sonar.api.server.ws.Request;
  33. import org.sonar.api.server.ws.Response;
  34. import org.sonar.api.server.ws.WebService.NewAction;
  35. import org.sonar.api.server.ws.WebService.NewController;
  36. import org.sonar.api.server.ws.WebService.Param;
  37. import org.sonar.api.utils.DateUtils;
  38. import org.sonar.api.utils.text.JsonWriter;
  39. import org.sonar.db.DbClient;
  40. import org.sonar.db.DbSession;
  41. import org.sonar.db.qualityprofile.QProfileChangeDto;
  42. import org.sonar.db.qualityprofile.QProfileChangeQuery;
  43. import org.sonar.db.qualityprofile.QProfileDto;
  44. import org.sonar.db.rule.RuleDto;
  45. import org.sonar.db.user.UserDto;
  46. import static org.sonar.api.utils.DateUtils.parseEndingDateOrDateTime;
  47. import static org.sonar.api.utils.DateUtils.parseStartingDateOrDateTime;
  48. import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE;
  49. import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_SINCE;
  50. import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TO;
  51. public class ChangelogAction implements QProfileWsAction {
  52. private final QProfileWsSupport wsSupport;
  53. private final Languages languages;
  54. private DbClient dbClient;
  55. public ChangelogAction(QProfileWsSupport wsSupport, Languages languages, DbClient dbClient) {
  56. this.wsSupport = wsSupport;
  57. this.languages = languages;
  58. this.dbClient = dbClient;
  59. }
  60. @Override
  61. public void define(NewController context) {
  62. NewAction wsAction = context.createAction("changelog")
  63. .setSince("5.2")
  64. .setDescription("Get the history of changes on a quality profile: rule activation/deactivation, change in parameters/severity. " +
  65. "Events are ordered by date in descending order (most recent first).")
  66. .setChangelog(
  67. new org.sonar.api.server.ws.Change("10.2", "Response fields 'total', 's', 'ps' dropped"),
  68. new org.sonar.api.server.ws.Change("9.8", "response fields 'total', 's', 'ps' have been deprecated, please use 'paging' object instead"),
  69. new org.sonar.api.server.ws.Change("9.8", "The field 'paging' has been added to the response"))
  70. .setHandler(this)
  71. .setResponseExample(getClass().getResource("changelog-example.json"));
  72. QProfileReference.defineParams(wsAction, languages);
  73. wsAction.addPagingParams(50, MAX_PAGE_SIZE);
  74. wsAction.createParam(PARAM_SINCE)
  75. .setDescription("Start date for the changelog (inclusive). <br>" +
  76. "Either a date (server timezone) or datetime can be provided.")
  77. .setExampleValue("2017-10-19 or 2017-10-19T13:00:00+0200");
  78. wsAction.createParam(PARAM_TO)
  79. .setDescription("End date for the changelog (exclusive, strictly before). <br>" +
  80. "Either a date (server timezone) or datetime can be provided.")
  81. .setExampleValue("2017-10-19 or 2017-10-19T13:00:00+0200");
  82. }
  83. @Override
  84. public void handle(Request request, Response response) throws Exception {
  85. QProfileReference reference = QProfileReference.fromName(request);
  86. try (DbSession dbSession = dbClient.openSession(false)) {
  87. QProfileDto profile = wsSupport.getProfile(dbSession, reference);
  88. QProfileChangeQuery query = new QProfileChangeQuery(profile.getKee());
  89. Date since = parseStartingDateOrDateTime(request.param(PARAM_SINCE));
  90. if (since != null) {
  91. query.setFromIncluded(since.getTime());
  92. }
  93. Date to = parseEndingDateOrDateTime(request.param(PARAM_TO));
  94. if (to != null) {
  95. query.setToExcluded(to.getTime());
  96. }
  97. int page = request.mandatoryParamAsInt(Param.PAGE);
  98. int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE);
  99. query.setPage(page, pageSize);
  100. int total = dbClient.qProfileChangeDao().countByQuery(dbSession, query);
  101. List<Change> changelogs = load(dbSession, query);
  102. Map<String, UserDto> usersByUuid = getUsersByUserUuid(dbSession, changelogs);
  103. Map<String, RuleDto> rulesByRuleIds = getRulesByRuleUuids(dbSession, changelogs);
  104. writeResponse(response.newJsonWriter(), total, page, pageSize, changelogs, usersByUuid, rulesByRuleIds);
  105. }
  106. }
  107. private Map<String, UserDto> getUsersByUserUuid(DbSession dbSession, List<Change> changes) {
  108. Set<String> userUuids = changes.stream()
  109. .map(Change::getUserUuid)
  110. .filter(Objects::nonNull)
  111. .collect(Collectors.toSet());
  112. return dbClient.userDao()
  113. .selectByUuids(dbSession, userUuids)
  114. .stream()
  115. .collect(Collectors.toMap(UserDto::getUuid, Function.identity()));
  116. }
  117. private Map<String, RuleDto> getRulesByRuleUuids(DbSession dbSession, List<Change> changes) {
  118. Set<String> ruleUuids = changes.stream()
  119. .map(c -> c.ruleUuid)
  120. .filter(Objects::nonNull)
  121. .collect(Collectors.toSet());
  122. return dbClient.ruleDao()
  123. .selectByUuids(dbSession, Lists.newArrayList(ruleUuids))
  124. .stream()
  125. .collect(Collectors.toMap(RuleDto::getUuid, Function.identity()));
  126. }
  127. private static void writeResponse(JsonWriter json, int total, int page, int pageSize, List<Change> changelogs,
  128. Map<String, UserDto> usersByUuid, Map<String, RuleDto> rulesByRuleUuids) {
  129. json.beginObject();
  130. writePaging(json, total, page, pageSize);
  131. json.name("events").beginArray();
  132. changelogs.forEach(change -> {
  133. JsonWriter changeWriter = json.beginObject();
  134. changeWriter
  135. .prop("date", DateUtils.formatDateTime(change.getCreatedAt()))
  136. .prop("action", change.getType());
  137. UserDto user = usersByUuid.get(change.getUserUuid());
  138. if (user != null) {
  139. changeWriter
  140. .prop("authorLogin", user.getLogin())
  141. .prop("authorName", user.getName());
  142. }
  143. RuleDto rule = rulesByRuleUuids.get(change.getRuleUuid());
  144. if (rule != null) {
  145. changeWriter
  146. .prop("ruleKey", rule.getKey().toString())
  147. .prop("ruleName", rule.getName());
  148. }
  149. writeParameters(json, change);
  150. json.endObject();
  151. });
  152. json.endArray();
  153. json.endObject().close();
  154. }
  155. private static void writeParameters(JsonWriter json, Change change) {
  156. json.name("params").beginObject()
  157. .prop("severity", change.getSeverity());
  158. for (Map.Entry<String, String> param : change.getParams().entrySet()) {
  159. json.prop(param.getKey(), param.getValue());
  160. }
  161. json.endObject();
  162. }
  163. private static void writePaging(JsonWriter json, int total, int page, int pageSize) {
  164. json.name("paging").beginObject()
  165. .prop("pageIndex", page)
  166. .prop("pageSize", pageSize)
  167. .prop("total", total)
  168. .endObject();
  169. }
  170. /**
  171. * @return non-null list of changes, by descending order of date
  172. */
  173. public List<Change> load(DbSession dbSession, QProfileChangeQuery query) {
  174. List<QProfileChangeDto> changeDtos = dbClient.qProfileChangeDao().selectByQuery(dbSession, query);
  175. return changeDtos.stream()
  176. .map(Change::from)
  177. .toList();
  178. }
  179. static class Change {
  180. private String key;
  181. private String type;
  182. private long at;
  183. private String severity;
  184. private String userUuid;
  185. private String inheritance;
  186. private String ruleUuid;
  187. private final Map<String, String> params = new HashMap<>();
  188. private Change() {
  189. }
  190. public String getKey() {
  191. return key;
  192. }
  193. @CheckForNull
  194. public String getSeverity() {
  195. return severity;
  196. }
  197. @CheckForNull
  198. public String getUserUuid() {
  199. return userUuid;
  200. }
  201. public String getType() {
  202. return type;
  203. }
  204. @CheckForNull
  205. public String getInheritance() {
  206. return inheritance;
  207. }
  208. @CheckForNull
  209. public String getRuleUuid() {
  210. return ruleUuid;
  211. }
  212. public long getCreatedAt() {
  213. return at;
  214. }
  215. public Map<String, String> getParams() {
  216. return params;
  217. }
  218. private static Change from(QProfileChangeDto dto) {
  219. Map<String, String> data = dto.getDataAsMap();
  220. Change change = new Change();
  221. change.key = dto.getUuid();
  222. change.userUuid = dto.getUserUuid();
  223. change.type = dto.getChangeType();
  224. change.at = dto.getCreatedAt();
  225. // see content of data in class org.sonar.server.qualityprofile.ActiveRuleChange
  226. change.severity = data.get("severity");
  227. String ruleUuid = data.get("ruleUuid");
  228. if (ruleUuid != null) {
  229. change.ruleUuid = ruleUuid;
  230. }
  231. change.inheritance = data.get("inheritance");
  232. data.entrySet().stream()
  233. .filter(entry -> entry.getKey().startsWith("param_"))
  234. .forEach(entry -> change.params.put(entry.getKey().replace("param_", ""), entry.getValue()));
  235. return change;
  236. }
  237. }
  238. }