projectReactorBuilder());
}
- private Class<? extends ProjectReactorBuilder> projectReactorBuilder() {
- if (isRunnerPre2_4()) {
+ private Class<?> projectReactorBuilder() {
+ if (isRunnerVersionLessThan2Dot4()) {
return DeprecatedProjectReactorBuilder.class;
}
return ProjectReactorBuilder.class;
}
- private boolean isRunnerPre2_4() {
+ private boolean isRunnerVersionLessThan2Dot4() {
EnvironmentInformation env = this.getComponentByType(EnvironmentInformation.class);
// Starting from SQ Runner 2.4 the key is "SonarQubeRunner"
return env != null && "SonarRunner".equals(env.getKey());
public class ResourceKeyMigration implements BatchComponent {
+ private static final String UNABLE_TO_UPDATE_COMPONENT_NO_MATCH_WAS_FOUND = "Unable to update component {}. No match was found.";
+ private static final String COMPONENT_CHANGED_TO = "Component {} changed to {}";
private final Logger logger;
private final DatabaseSession session;
Map<String, String> deprecatedDirectoryKeyMapper,
int moduleId) {
// Find all FIL or CLA resources for this module
- StringBuilder hql = new StringBuilder().append("from ")
- .append(ResourceModel.class.getSimpleName())
- .append(" where enabled = true ")
- .append(" and rootId = :rootId ")
+ StringBuilder hql = newResourceQuery()
.append(" and scope = '").append(Scopes.FILE).append("' order by qualifier, key");
List<ResourceModel> resources = session.createQuery(hql.toString()).setParameter("rootId", moduleId).getResultList();
for (ResourceModel resourceModel : resources) {
}
resourceModel.setKey(newEffectiveKey);
resourceModel.setDeprecatedKey(oldEffectiveKey);
- logger.info("Component {} changed to {}", oldEffectiveKey, newEffectiveKey);
+ logger.info(COMPONENT_CHANGED_TO, oldEffectiveKey, newEffectiveKey);
} else {
- logger.warn("Unable to update component {}. No match was found.", oldEffectiveKey);
+ logger.warn(UNABLE_TO_UPDATE_COMPONENT_NO_MATCH_WAS_FOUND, oldEffectiveKey);
}
}
}
+ private StringBuilder newResourceQuery() {
+ return new StringBuilder().append("from ")
+ .append(ResourceModel.class.getSimpleName())
+ .append(" where enabled = true ")
+ .append(" and rootId = :rootId ");
+ }
+
private InputFile findInputFile(Map<String, InputFile> deprecatedFileKeyMapper, Map<String, InputFile> deprecatedTestKeyMapper, String oldEffectiveKey, boolean isTest) {
if (isTest) {
return deprecatedTestKeyMapper.get(oldEffectiveKey);
private void migrateDirectories(Map<String, String> deprecatedDirectoryKeyMapper, int moduleId) {
// Find all DIR resources for this module
- StringBuilder hql = new StringBuilder().append("from ")
- .append(ResourceModel.class.getSimpleName())
- .append(" where enabled = true ")
- .append(" and rootId = :rootId ")
+ StringBuilder hql = newResourceQuery()
.append(" and qualifier = '").append(Qualifiers.DIRECTORY).append("'");
List<ResourceModel> resources = session.createQuery(hql.toString()).setParameter("rootId", moduleId).getResultList();
for (ResourceModel resourceModel : resources) {
String newEffectiveKey = deprecatedDirectoryKeyMapper.get(oldEffectiveKey);
resourceModel.setKey(newEffectiveKey);
resourceModel.setDeprecatedKey(oldEffectiveKey);
- logger.info("Component {} changed to {}", oldEffectiveKey, newEffectiveKey);
+ logger.info(COMPONENT_CHANGED_TO, oldEffectiveKey, newEffectiveKey);
} else {
- logger.warn("Unable to update component {}. No match was found.", oldEffectiveKey);
+ logger.warn(UNABLE_TO_UPDATE_COMPONENT_NO_MATCH_WAS_FOUND, oldEffectiveKey);
}
}
}
import org.sonar.api.utils.MessageException;
import javax.annotation.CheckForNull;
+
+import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
String language(InputFile inputFile) {
String detectedLanguage = null;
for (String languageKey : languagesToConsider) {
- PathPattern[] patterns = patternsByLanguage.get(languageKey);
- if (patterns != null) {
- for (PathPattern pathPattern : patterns) {
- if (pathPattern.match(inputFile, false)) {
- if (detectedLanguage == null) {
- detectedLanguage = languageKey;
- break;
- } else {
- // Language was already forced by another pattern
- throw MessageException.of("Language of file '" + inputFile.relativePath() + "' can not be decided as the file matches patterns of both " + getDetails(detectedLanguage)
- + " and " + getDetails(languageKey));
- }
- }
+ if (isCandidateForLanguage(inputFile, languageKey)) {
+ if (detectedLanguage == null) {
+ detectedLanguage = languageKey;
+ } else {
+ // Language was already forced by another pattern
+ throw MessageException.of(MessageFormat.format("Language of file ''{0}'' can not be decided as the file matches patterns of both {1} and {2}",
+ inputFile.relativePath(), getDetails(detectedLanguage), getDetails(languageKey)));
}
}
}
return null;
}
+ private boolean isCandidateForLanguage(InputFile inputFile, String languageKey) {
+ PathPattern[] patterns = patternsByLanguage.get(languageKey);
+ if (patterns != null) {
+ for (PathPattern pathPattern : patterns) {
+ if (pathPattern.match(inputFile, false)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
private String getFileLangPatternPropKey(String languageKey) {
return "sonar.lang.patterns." + languageKey;
}
assertThat(ComponentKeys.createEffectiveKey(project, library)).isEqualTo("junit:junit");
}
+ @Test
+ public void isValidModuleKey() {
+ assertThat(ComponentKeys.isValidModuleKey("")).isFalse();
+ assertThat(ComponentKeys.isValidModuleKey("abc")).isTrue();
+ assertThat(ComponentKeys.isValidModuleKey("0123")).isFalse();
+ assertThat(ComponentKeys.isValidModuleKey("ab 12")).isFalse();
+ assertThat(ComponentKeys.isValidModuleKey("ab_12")).isTrue();
+ assertThat(ComponentKeys.isValidModuleKey("ab/12")).isFalse();
+ }
+
+ @Test
+ public void isValidBranchKey() {
+ assertThat(ComponentKeys.isValidBranch("")).isTrue();
+ assertThat(ComponentKeys.isValidBranch("abc")).isTrue();
+ assertThat(ComponentKeys.isValidBranch("0123")).isTrue();
+ assertThat(ComponentKeys.isValidBranch("ab 12")).isFalse();
+ assertThat(ComponentKeys.isValidBranch("ab_12")).isTrue();
+ assertThat(ComponentKeys.isValidBranch("ab/12")).isFalse();
+ }
+
}
* @throws IllegalArgumentException if the key is longer than KEY_SIZE
*/
public void setKey(String key) {
+ checkSize(key);
+ this.key = key;
+ }
+
+ private void checkSize(String key) {
if (key.length() > KEY_SIZE) {
throw new IllegalArgumentException("Resource key is too long, max is " + KEY_SIZE + " characters. Got : " + key);
}
- this.key = key;
}
/**
* @throws IllegalArgumentException if the key is longer than KEY_SIZE
*/
public void setDeprecatedKey(String deprecatedKey) {
- if (deprecatedKey.length() > KEY_SIZE) {
- throw new IllegalArgumentException("Resource deprecated key is too long, max is " + KEY_SIZE + " characters. Got : " + deprecatedKey);
- }
+ checkSize(deprecatedKey);
this.deprecatedKey = deprecatedKey;
}