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 java.util.stream.Collectors;
26 import org.sonar.api.utils.log.Loggers;
27 import org.sonar.ce.task.projectexport.component.ComponentRepository;
28 import org.sonar.ce.task.step.ComputationStep;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.project.ProjectExportMapper;
32 import org.sonar.db.property.PropertyDto;
34 import static java.lang.String.format;
35 import static org.apache.commons.lang.StringUtils.defaultString;
37 public class ExportSettingsStep implements ComputationStep {
40 * These properties are not exported as values depend on environment data that are not
41 * exported within the dump (Quality Gate, users).
43 private static final Set<String> IGNORED_KEYS = Set.of("sonar.issues.defaultAssigneeLogin");
45 private final DbClient dbClient;
46 private final ProjectHolder projectHolder;
47 private final ComponentRepository componentRepository;
48 private final DumpWriter dumpWriter;
50 public ExportSettingsStep(DbClient dbClient, ProjectHolder projectHolder, ComponentRepository componentRepository, DumpWriter dumpWriter) {
51 this.dbClient = dbClient;
52 this.projectHolder = projectHolder;
53 this.componentRepository = componentRepository;
54 this.dumpWriter = dumpWriter;
58 public void execute(Context context) {
61 StreamWriter<ProjectDump.Setting> output = dumpWriter.newStreamWriter(DumpElement.SETTINGS);
62 DbSession dbSession = dbClient.openSession(false)) {
64 final ProjectDump.Setting.Builder builder = ProjectDump.Setting.newBuilder();
65 final List<PropertyDto> properties = dbSession.getMapper(ProjectExportMapper.class).selectPropertiesForExport(projectHolder.projectDto().getUuid())
67 .filter(dto -> dto.getComponentUuid() != null)
68 .filter(dto -> !IGNORED_KEYS.contains(dto.getKey()))
69 .collect(Collectors.toList());
70 for (PropertyDto property : properties) {
72 .setKey(property.getKey())
73 .setValue(defaultString(property.getValue()));
75 if (property.getComponentUuid() != null) {
76 builder.setComponentRef(componentRepository.getRef(property.getComponentUuid()));
78 output.write(builder.build());
82 Loggers.get(getClass()).debug("{} settings exported", count);
83 } catch (Exception e) {
84 throw new IllegalStateException(format("Settings Export failed after processing %d settings successfully", count), e);
89 public String getDescription() {
90 return "Export settings";