3 * Copyright (C) 2009-2023 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.project.ProjectExportMapper;
30 import org.sonar.db.property.PropertyDto;
32 import static java.lang.String.format;
33 import static org.apache.commons.lang.StringUtils.defaultString;
35 public class ExportSettingsStep implements ComputationStep {
38 * These properties are not exported as values depend on environment data that are not
39 * exported within the dump (Quality Gate, users).
41 private static final Set<String> IGNORED_KEYS = Set.of("sonar.issues.defaultAssigneeLogin");
43 private final DbClient dbClient;
44 private final ProjectHolder projectHolder;
45 private final DumpWriter dumpWriter;
47 public ExportSettingsStep(DbClient dbClient, ProjectHolder projectHolder,DumpWriter dumpWriter) {
48 this.dbClient = dbClient;
49 this.projectHolder = projectHolder;
50 this.dumpWriter = dumpWriter;
54 public void execute(Context context) {
57 StreamWriter<ProjectDump.Setting> output = dumpWriter.newStreamWriter(DumpElement.SETTINGS);
58 DbSession dbSession = dbClient.openSession(false)) {
60 final ProjectDump.Setting.Builder builder = ProjectDump.Setting.newBuilder();
61 final List<PropertyDto> properties = dbSession.getMapper(ProjectExportMapper.class).selectPropertiesForExport(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";