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.

ProjectAction.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.batch;
  21. import java.util.Date;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import org.sonar.api.server.ws.Request;
  25. import org.sonar.api.server.ws.Response;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.scanner.protocol.input.FileData;
  28. import org.sonar.scanner.protocol.input.ProjectRepositories;
  29. import org.sonarqube.ws.Batch.WsProjectResponse;
  30. import org.sonarqube.ws.Batch.WsProjectResponse.FileData.Builder;
  31. import static org.sonar.core.util.Protobuf.setNullable;
  32. import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
  33. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  34. import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001;
  35. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  36. public class ProjectAction implements BatchWsAction {
  37. private static final String PARAM_KEY = "key";
  38. private static final String PARAM_PROFILE = "profile";
  39. private static final String PARAM_ISSUES_MODE = "issues_mode";
  40. private static final String PARAM_BRANCH = "branch";
  41. private static final String PARAM_PULL_REQUEST = "pullRequest";
  42. private final ProjectDataLoader projectDataLoader;
  43. public ProjectAction(ProjectDataLoader projectDataLoader) {
  44. this.projectDataLoader = projectDataLoader;
  45. }
  46. @Override
  47. public void define(WebService.NewController controller) {
  48. WebService.NewAction action = controller.createAction("project")
  49. .setDescription("Return project repository")
  50. .setResponseExample(getClass().getResource("project-example.json"))
  51. .setSince("4.5")
  52. .setInternal(true)
  53. .setHandler(this);
  54. action
  55. .createParam(PARAM_KEY)
  56. .setRequired(true)
  57. .setDescription("Project or module key")
  58. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  59. action
  60. .createParam(PARAM_PROFILE)
  61. .setDescription("Profile name")
  62. .setExampleValue("SonarQube Way");
  63. action
  64. .createParam(PARAM_ISSUES_MODE)
  65. .setDescription("Issues mode or not")
  66. .setDefaultValue(false)
  67. .setBooleanPossibleValues();
  68. action
  69. .createParam(PARAM_BRANCH)
  70. .setSince("6.6")
  71. .setDescription("Branch key")
  72. .setExampleValue(KEY_BRANCH_EXAMPLE_001);
  73. action
  74. .createParam(PARAM_PULL_REQUEST)
  75. .setSince("7.1")
  76. .setDescription("Pull request id")
  77. .setExampleValue(KEY_PULL_REQUEST_EXAMPLE_001);
  78. }
  79. @Override
  80. public void handle(Request wsRequest, Response wsResponse) throws Exception {
  81. ProjectRepositories data = projectDataLoader.load(ProjectDataQuery.create()
  82. .setModuleKey(wsRequest.mandatoryParam(PARAM_KEY))
  83. .setProfileName(wsRequest.param(PARAM_PROFILE))
  84. .setIssuesMode(wsRequest.mandatoryParamAsBoolean(PARAM_ISSUES_MODE))
  85. .setBranch(wsRequest.param(PARAM_BRANCH))
  86. .setPullRequest(wsRequest.param(PARAM_PULL_REQUEST)));
  87. WsProjectResponse projectResponse = buildResponse(data);
  88. writeProtobuf(projectResponse, wsRequest, wsResponse);
  89. }
  90. private static WsProjectResponse buildResponse(ProjectRepositories data) {
  91. WsProjectResponse.Builder response = WsProjectResponse.newBuilder();
  92. setNullable(data.lastAnalysisDate(), response::setLastAnalysisDate, Date::getTime);
  93. response.setTimestamp(data.timestamp());
  94. response.getMutableFileDataByModuleAndPath()
  95. .putAll(buildFileDataByModuleAndPath(data));
  96. response.getMutableSettingsByModule()
  97. .putAll(buildSettingsByModule(data));
  98. return response.build();
  99. }
  100. private static Map<String, WsProjectResponse.FileDataByPath> buildFileDataByModuleAndPath(ProjectRepositories data) {
  101. Map<String, WsProjectResponse.FileDataByPath> fileDataByModuleAndPathResponse = new HashMap<>();
  102. for (Map.Entry<String, Map<String, FileData>> moduleAndFileDataByPathEntry : data.fileDataByModuleAndPath().entrySet()) {
  103. fileDataByModuleAndPathResponse.put(
  104. moduleAndFileDataByPathEntry.getKey(),
  105. buildFileDataByPath(moduleAndFileDataByPathEntry.getValue()));
  106. }
  107. return fileDataByModuleAndPathResponse;
  108. }
  109. private static WsProjectResponse.FileDataByPath buildFileDataByPath(Map<String, FileData> fileDataByPath) {
  110. WsProjectResponse.FileDataByPath.Builder response = WsProjectResponse.FileDataByPath.newBuilder();
  111. Map<String, WsProjectResponse.FileData> fileDataByPathResponse = response.getMutableFileDataByPath();
  112. for (Map.Entry<String, FileData> pathFileDataEntry : fileDataByPath.entrySet()) {
  113. fileDataByPathResponse.put(
  114. pathFileDataEntry.getKey(),
  115. toFileDataResponse(pathFileDataEntry.getValue()));
  116. }
  117. return response.build();
  118. }
  119. private static Map<String, WsProjectResponse.Settings> buildSettingsByModule(ProjectRepositories data) {
  120. Map<String, WsProjectResponse.Settings> settingsByModuleResponse = new HashMap<>();
  121. for (Map.Entry<String, Map<String, String>> moduleSettingsEntry : data.settings().entrySet()) {
  122. settingsByModuleResponse.put(
  123. moduleSettingsEntry.getKey(),
  124. toSettingsResponse(moduleSettingsEntry.getValue()));
  125. }
  126. return settingsByModuleResponse;
  127. }
  128. private static WsProjectResponse.Settings toSettingsResponse(Map<String, String> settings) {
  129. WsProjectResponse.Settings.Builder settingsResponse = WsProjectResponse.Settings
  130. .newBuilder();
  131. settingsResponse
  132. .getMutableSettings()
  133. .putAll(settings);
  134. return settingsResponse.build();
  135. }
  136. private static WsProjectResponse.FileData toFileDataResponse(FileData fileData) {
  137. Builder fileDataBuilder = WsProjectResponse.FileData.newBuilder();
  138. setNullable(fileData.hash(), fileDataBuilder::setHash);
  139. setNullable(fileData.revision(), fileDataBuilder::setRevision);
  140. return fileDataBuilder.build();
  141. }
  142. }