3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectexport.steps;
22 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
23 import java.util.List;
25 import org.slf4j.LoggerFactory;
26 import org.sonar.ce.task.step.ComputationStep;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.property.PropertyDto;
31 import static java.lang.String.format;
32 import static org.apache.commons.lang.StringUtils.defaultString;
34 public class ExportSettingsStep implements ComputationStep {
37 * These properties are not exported as values depend on environment data that are not
38 * exported within the dump (Quality Gate, users).
40 private static final Set<String> IGNORED_KEYS = Set.of("sonar.issues.defaultAssigneeLogin");
42 private final DbClient dbClient;
43 private final ProjectHolder projectHolder;
44 private final DumpWriter dumpWriter;
46 public ExportSettingsStep(DbClient dbClient, ProjectHolder projectHolder, DumpWriter dumpWriter) {
47 this.dbClient = dbClient;
48 this.projectHolder = projectHolder;
49 this.dumpWriter = dumpWriter;
53 public void execute(Context context) {
56 StreamWriter<ProjectDump.Setting> output = dumpWriter.newStreamWriter(DumpElement.SETTINGS);
57 DbSession dbSession = dbClient.openSession(false)) {
59 final ProjectDump.Setting.Builder builder = ProjectDump.Setting.newBuilder();
60 final List<PropertyDto> properties = dbClient.projectExportDao()
61 .selectPropertiesForExport(dbSession, projectHolder.projectDto().getUuid())
63 .filter(dto -> dto.getEntityUuid() != null)
64 .filter(dto -> !IGNORED_KEYS.contains(dto.getKey()))
66 for (PropertyDto property : properties) {
68 .setKey(property.getKey())
69 .setValue(defaultString(property.getValue()));
70 output.write(builder.build());
74 LoggerFactory.getLogger(getClass()).debug("{} settings exported", count);
75 } catch (Exception e) {
76 throw new IllegalStateException(format("Settings Export failed after processing %d settings successfully", count), e);
81 public String getDescription() {
82 return "Export settings";