@Nullable
private final String buildString;
- public ProjectAttributes(String projectVersion, @Nullable String buildString) {
+ @Nullable
+ private final String scmRevisionId;
+
+ public ProjectAttributes(String projectVersion, @Nullable String buildString, @Nullable String scmRevisionId) {
this.projectVersion = requireNonNull(projectVersion, "project version can't be null");
this.buildString = buildString;
+ this.scmRevisionId = scmRevisionId;
}
public ProjectAttributes(ProjectAttributes projectAttributes) {
this.projectVersion = projectAttributes.projectVersion;
this.buildString = projectAttributes.buildString;
+ this.scmRevisionId = projectAttributes.scmRevisionId;
}
public String getProjectVersion() {
return Optional.ofNullable(buildString);
}
+ public Optional<String> getScmRevisionId() {
+ return Optional.ofNullable(scmRevisionId);
+ }
+
@Override
public String toString() {
return "ProjectAttributes{" +
"projectVersion='" + projectVersion + '\'' +
"buildString='" + buildString + '\'' +
+ "scmRevisionId='" + scmRevisionId + '\'' +
'}';
}
}
private static ProjectAttributes createProjectAttributes(ScannerReport.Metadata metadata, @Nullable SnapshotDto baseAnalysis) {
String projectVersion = computeProjectVersion(trimToNull(metadata.getProjectVersion()), baseAnalysis);
String buildString = trimToNull(metadata.getBuildString());
- return new ProjectAttributes(projectVersion, buildString);
+ String scmRevisionId = trimToNull(metadata.getScmRevisionId());
+ return new ProjectAttributes(projectVersion, buildString, scmRevisionId);
}
private static String computeProjectVersion(@Nullable String projectVersion, @Nullable SnapshotDto baseAnalysis) {
String componentUuid = component.getUuid();
String projectVersion = component.getType() == PROJECT ? component.getProjectAttributes().getProjectVersion() : null;
String buildString = component.getType() == PROJECT ? component.getProjectAttributes().getBuildString().orElse(null) : null;
- return new SnapshotDto()
+ SnapshotDto dto = new SnapshotDto()
.setUuid(snapshotUuid)
.setProjectVersion(projectVersion)
.setBuildString(buildString)
.setStatus(SnapshotDto.STATUS_UNPROCESSED)
.setCreatedAt(analysisDate)
.setBuildDate(system2.now());
+
+ if (component.getType() == PROJECT) {
+ component.getProjectAttributes().getScmRevisionId().ifPresent(dto::setRevision);
+ }
+
+ return dto;
}
private void persist(SnapshotDto snapshotDto, DbSession dbSession) {
.setReportAttributes(ReportAttributes.newBuilder(dbKey.hashCode()).build());
if (type == PROJECT) {
String buildString = randomAlphabetic(15);
- builder.setProjectAttributes(new ProjectAttributes("version_1", buildString));
+ builder.setProjectAttributes(new ProjectAttributes("version_1", buildString, "453def"));
}
return builder;
}
private static final String NO_SCM_BASE_PATH = "";
// both no project as "" or null should be supported
private static final ProjectAttributes SOME_PROJECT_ATTRIBUTES = new ProjectAttributes(
- randomAlphabetic(20), new Random().nextBoolean() ? null : randomAlphabetic(12));
+ randomAlphabetic(20), new Random().nextBoolean() ? null : randomAlphabetic(12), "1def5123");
@Rule
public ExpectedException expectedException = ExpectedException.none();
.setRef(42)
.setName(nameInReport)
.setDescription(descriptionInReport)
- .build(), NO_SCM_BASE_PATH, new ProjectAttributes("6.5", buildString));
+ .build(), NO_SCM_BASE_PATH, new ProjectAttributes("6.5", buildString, "4124af4"));
assertThat(root.getUuid()).isEqualTo("generated_K1_uuid");
assertThat(root.getDbKey()).isEqualTo("generated_K1");
this.description = builder.description;
this.uuid = builder.uuid;
this.projectAttributes = Optional.ofNullable(builder.projectVersion)
- .map(v -> new ProjectAttributes(v, builder.buildString))
+ .map(v -> new ProjectAttributes(v, builder.buildString, builder.scmRevisionId))
.orElse(null);
this.reportAttributes = ReportAttributes.newBuilder(builder.ref)
.build();
private String shortName;
private String projectVersion;
private String buildString;
+ private String scmRevisionId;
private String description;
private FileAttributes fileAttributes;
private final List<Component> children = new ArrayList<>();
return this;
}
+ public Builder setScmRevisionId(@Nullable String scmRevisionId) {
+ this.scmRevisionId = scmRevisionId;
+ return this;
+ }
+
public Builder setFileAttributes(FileAttributes fileAttributes) {
checkState(type == Type.FILE, "Only Component of type File can have File attributes");
this.fileAttributes = fileAttributes;
.setKey("key 1")
.setProjectVersion(PROJECT_VERSION)
.setBuildString("V1.9")
+ .setScmRevisionId("456def")
.addChildren(ReportComponent.builder(Component.Type.DIRECTORY, 2).build())
.build();
private static final String INVALID_ALERT_STATUS = "trololo";
private static final String PROJECT_KEY = "PROJECT_KEY";
private static final String ANALYSIS_UUID = "U1";
+ private static final String REVISION_ID = "5f6432a1";
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
analysisMetadataHolder.setUuid(ANALYSIS_UUID);
analysisMetadataHolder.setAnalysisDate(analysisDate);
+ analysisMetadataHolder.setScmRevisionId(REVISION_ID);
dbIdsRepository = new DbIdsRepositoryImpl();
now = DateUtils.parseDateQuietly("2015-06-02").getTime();
.setKey(PROJECT_KEY)
.setProjectVersion(projectVersion)
.setBuildString(buildString)
+ .setScmRevisionId(REVISION_ID)
.addChildren(directory)
.build();
treeRootHolder.setRoot(project);
assertThat(projectSnapshot.getStatus()).isEqualTo("U");
assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
+ assertThat(projectSnapshot.getRevision()).isEqualTo(REVISION_ID);
assertThat(dbIdsRepository.getComponentId(directory)).isEqualTo(directoryDto.getId());
assertThat(dbIdsRepository.getComponentId(file)).isEqualTo(fileDto.getId());
For example these additional parameters:
```
-sonar-scanner -Dsonar.analysis.scmRevision=628f5175ada0d685fd7164baa7c6382c1f25cab4 -Dsonar.analysis.buildNumber=12345
+sonar-scanner -Dsonar.analysis.buildNumber=12345
```
Would add this to the payload:
```
"properties": {
- "sonar.analysis.scmRevision": "628f5175ada0d685fd7164baa7c6382c1f25cab4",
"sonar.analysis.buildNumber": "12345"
}
```
import java.nio.file.Path;
import java.util.LinkedList;
import java.util.Map.Entry;
+import java.util.Optional;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
LOG.debug(e.getMessage());
}
try {
- builder.setScmRevisionId(scmProvider.revisionId(projectBasedir));
+ computeScmRevision().ifPresent(builder::setScmRevisionId);
} catch (UnsupportedOperationException e) {
LOG.debug(e.getMessage());
}
}
}
+ private Optional<String> computeScmRevision() {
+ Optional<String> scmRevision = properties.getScmRevision();
+ ScmProvider scmProvider = scmConfiguration.provider();
+ if (!scmRevision.isPresent() && scmProvider != null) {
+ scmRevision = Optional.ofNullable(scmProvider.revisionId(moduleHierarchy.root().getBaseDir()));
+ }
+
+ return scmRevision;
+ }
+
private void addBranchInformation(ScannerReport.Metadata.Builder builder) {
builder.setBranchName(branchConfiguration.branchName());
BranchType branchType = toProtobufBranchType(branchConfiguration.branchType());
public static final String SONAR_REPORT_EXPORT_PATH = "sonar.report.export.path";
public static final String PRELOAD_FILE_METADATA_KEY = "sonar.preloadFileMetadata";
public static final String FORCE_RELOAD_KEY = "sonar.scm.forceReloadAll";
+ public static final String SCM_REVISION = "sonar.scm.revision";
private final Configuration configuration;
private final DefaultInputProject project;
}
}
+ public Optional<String> getScmRevision() {
+ return configuration.get(SCM_REVISION);
+ }
+
/**
* This should be called in the beginning of the analysis to fail fast
*/
.setRulesUpdatedAt(date.getTime())
.build()));
assertThat(metadata.getPluginsByKey()).containsOnly(entry("java", org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin.newBuilder()
- .setKey("java")
- .setUpdatedAt(12345)
- .build()),
+ .setKey("java")
+ .setUpdatedAt(12345)
+ .build()),
entry("php", org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin.newBuilder()
.setKey("php")
.setUpdatedAt(45678)
assertThat(metadata.getScmRevisionId()).isEqualTo(revisionId);
}
+ public void revision_from_scanner_props_overrides_scm_provider_revision_if_specified() throws IOException {
+ String revisionId = "some-sha1";
+ when(scmProvider.revisionId(any(Path.class))).thenReturn(revisionId);
+ when(properties.getScmRevision()).thenReturn(Optional.of("123"));
+
+ File outputDir = temp.newFolder();
+ underTest.publish(new ScannerReportWriter(outputDir));
+
+ ScannerReportReader reader = new ScannerReportReader(outputDir);
+ ScannerReport.Metadata metadata = reader.readMetadata();
+ assertThat(metadata.getScmRevisionId()).isEqualTo("123");
+ }
+
@Test
public void should_not_crash_when_scm_provider_does_not_support_relativePathFromScmRoot() throws IOException {
String revisionId = "some-sha1";