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.

QProfileVerifier.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2016 SonarSource SA
  4. * mailto:contact 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.scanner.rule;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.sonar.api.batch.ScannerSide;
  26. import org.sonar.api.batch.fs.FileSystem;
  27. import org.sonar.api.config.Settings;
  28. import org.sonar.api.utils.MessageException;
  29. import static org.apache.commons.lang.StringUtils.isNotEmpty;
  30. @ScannerSide
  31. public class QProfileVerifier {
  32. private static final Logger LOG = LoggerFactory.getLogger(QProfileVerifier.class);
  33. private final Settings settings;
  34. private final FileSystem fs;
  35. private final ModuleQProfiles profiles;
  36. public QProfileVerifier(Settings settings, FileSystem fs, ModuleQProfiles profiles) {
  37. this.settings = settings;
  38. this.fs = fs;
  39. this.profiles = profiles;
  40. }
  41. public void execute() {
  42. execute(LOG);
  43. }
  44. @VisibleForTesting
  45. void execute(Logger logger) {
  46. String defaultName = settings.getString(ModuleQProfiles.SONAR_PROFILE_PROP);
  47. boolean defaultNameUsed = StringUtils.isBlank(defaultName);
  48. for (String lang : fs.languages()) {
  49. QProfile profile = profiles.findByLanguage(lang);
  50. if (profile == null) {
  51. logger.warn("No Quality profile found for language {}", lang);
  52. } else {
  53. logger.info("Quality profile for {}: {}", lang, profile.getName());
  54. if (isNotEmpty(defaultName) && defaultName.equals(profile.getName())) {
  55. defaultNameUsed = true;
  56. }
  57. }
  58. }
  59. if (!defaultNameUsed && !fs.languages().isEmpty()) {
  60. throw MessageException.of("sonar.profile was set to '" + defaultName + "' but didn't match any profile for any language. Please check your configuration.");
  61. }
  62. }
  63. }