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.

ProjectConfigurationFactory.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.projectanalysis.analysis;
  21. import org.sonar.api.ce.ComputeEngineSide;
  22. import org.sonar.api.config.Configuration;
  23. import org.sonar.api.config.Settings;
  24. import org.sonar.api.config.internal.ConfigurationBridge;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.component.BranchType;
  27. import org.sonar.server.setting.ChildSettings;
  28. import static org.sonar.db.component.ComponentDto.generateBranchKey;
  29. import static org.sonar.db.component.ComponentDto.generatePullRequestKey;
  30. @ComputeEngineSide
  31. public class ProjectConfigurationFactory {
  32. private final Settings globalSettings;
  33. private final DbClient dbClient;
  34. public ProjectConfigurationFactory(Settings globalSettings, DbClient dbClient) {
  35. this.globalSettings = globalSettings;
  36. this.dbClient = dbClient;
  37. }
  38. public Configuration newProjectConfiguration(String projectKey, Branch branch) {
  39. Settings projectSettings = new ChildSettings(globalSettings);
  40. addSettings(projectSettings, projectKey);
  41. if (branch.getType() == BranchType.PULL_REQUEST) {
  42. addSettings(projectSettings, generatePullRequestKey(projectKey, branch.getPullRequestKey()));
  43. } else {
  44. addSettings(projectSettings, generateBranchKey(projectKey, branch.getName()));
  45. }
  46. return new ConfigurationBridge(projectSettings);
  47. }
  48. private void addSettings(Settings settings, String componentDbKey) {
  49. dbClient.propertiesDao()
  50. .selectProjectProperties(componentDbKey)
  51. .forEach(property -> settings.setProperty(property.getKey(), property.getValue()));
  52. }
  53. }