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.

ExportSettingsStep.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.ce.task.projectexport.steps;
  21. import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
  22. import java.util.List;
  23. import java.util.Set;
  24. import org.slf4j.LoggerFactory;
  25. import org.sonar.ce.task.step.ComputationStep;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.property.PropertyDto;
  29. import static java.lang.String.format;
  30. import static org.apache.commons.lang3.StringUtils.defaultString;
  31. public class ExportSettingsStep implements ComputationStep {
  32. /**
  33. * These properties are not exported as values depend on environment data that are not
  34. * exported within the dump (Quality Gate, users).
  35. */
  36. private static final Set<String> IGNORED_KEYS = Set.of("sonar.issues.defaultAssigneeLogin");
  37. private final DbClient dbClient;
  38. private final ProjectHolder projectHolder;
  39. private final DumpWriter dumpWriter;
  40. public ExportSettingsStep(DbClient dbClient, ProjectHolder projectHolder, DumpWriter dumpWriter) {
  41. this.dbClient = dbClient;
  42. this.projectHolder = projectHolder;
  43. this.dumpWriter = dumpWriter;
  44. }
  45. @Override
  46. public void execute(Context context) {
  47. long count = 0L;
  48. try (
  49. StreamWriter<ProjectDump.Setting> output = dumpWriter.newStreamWriter(DumpElement.SETTINGS);
  50. DbSession dbSession = dbClient.openSession(false)) {
  51. final ProjectDump.Setting.Builder builder = ProjectDump.Setting.newBuilder();
  52. final List<PropertyDto> properties = dbClient.projectExportDao()
  53. .selectPropertiesForExport(dbSession, projectHolder.projectDto().getUuid())
  54. .stream()
  55. .filter(dto -> dto.getEntityUuid() != null)
  56. .filter(dto -> !IGNORED_KEYS.contains(dto.getKey()))
  57. .toList();
  58. for (PropertyDto property : properties) {
  59. builder.clear()
  60. .setKey(property.getKey())
  61. .setValue(defaultString(property.getValue()));
  62. output.write(builder.build());
  63. ++count;
  64. }
  65. LoggerFactory.getLogger(getClass()).debug("{} settings exported", count);
  66. } catch (Exception e) {
  67. throw new IllegalStateException(format("Settings Export failed after processing %d settings successfully", count), e);
  68. }
  69. }
  70. @Override
  71. public String getDescription() {
  72. return "Export settings";
  73. }
  74. }