Long analysisDate = getAnalysisDate();
if (analysisDate != null) {
- return of(new AnalysisImpl(analysisMetadataHolder.getUuid(), analysisDate));
+ return of(new AnalysisImpl(analysisMetadataHolder.getUuid(), analysisDate, analysisMetadataHolder.getScmRevisionId()));
}
return empty();
}
private final String analysisUuid;
private final long date;
+ private final Optional<String> revision;
- private AnalysisImpl(String analysisUuid, long date) {
+ private AnalysisImpl(String analysisUuid, long date, Optional<String> revision) {
this.analysisUuid = analysisUuid;
this.date = date;
+ this.revision = revision;
}
@Override
public Date getDate() {
return new Date(date);
}
+
+ @Override
+ public Optional<String> getRevision() {
+ return revision;
+ }
}
private static class OrganizationImpl implements Organization {
private static org.sonar.server.webhook.ProjectAnalysis convert(ProjectAnalysis projectAnalysis) {
CeTask ceTask = new CeTask(projectAnalysis.getCeTask().getId(), CeTask.Status.valueOf(projectAnalysis.getCeTask().getStatus().name()));
- Analysis analysis = projectAnalysis.getAnalysis().map(a -> new Analysis(a.getAnalysisUuid(), a.getDate().getTime())).orElse(null);
+ Analysis analysis = projectAnalysis.getAnalysis().map(a -> new Analysis(a.getAnalysisUuid(), a.getDate().getTime(), a.getRevision().orElse(null))).orElse(null);
Branch branch = projectAnalysis.getBranch().map(b -> new Branch(b.isMain(), b.getName().orElse(null), Branch.Type.valueOf(b.getType().name()))).orElse(null);
EvaluatedQualityGate qualityGate = Optional.ofNullable(projectAnalysis.getQualityGate())
.map(QGToEvaluatedQG.INSTANCE)
verify(payloadFactory).create(new ProjectAnalysis(
new org.sonar.server.webhook.Project(project.getUuid(), project.getKey(), project.getName()),
new org.sonar.server.webhook.CeTask(ceTask.getId(), org.sonar.server.webhook.CeTask.Status.valueOf(ceTask.getStatus().name())),
- analysisUUid == null ? null : new Analysis(analysisUUid, date.getTime()),
+ analysisUUid == null ? null : new Analysis(analysisUUid, date.getTime(), null),
new org.sonar.server.webhook.Branch(branch.isMain(), branch.getName().get(), org.sonar.server.webhook.Branch.Type.valueOf(branch.getType().name())),
webQualityGate,
analysisUUid == null ? null : date.getTime(),
*/
package org.sonar.server.webhook;
-import com.google.common.base.Objects;
-import java.util.Date;
+import java.util.Objects;
+import java.util.Optional;
+import javax.annotation.Nullable;
import static java.util.Objects.requireNonNull;
public final class Analysis {
private final String uuid;
private final long date;
+ @Nullable
+ private final String revision;
- public Analysis(String uuid, long date) {
- requireNonNull(uuid, "uuid must not be null");
- this.uuid = uuid;
+ public Analysis(String uuid, long date, @Nullable String revision) {
+ this.uuid = requireNonNull(uuid, "uuid must not be null");
this.date = date;
+ this.revision = revision;
}
public String getUuid() {
return uuid;
}
- public Date getDate() {
- return new Date(date);
+ public long getDate() {
+ return date;
+ }
+
+ public Optional<String> getRevision() {
+ return Optional.ofNullable(revision);
}
@Override
if (this == o) {
return true;
}
- if (!(o instanceof Analysis)) {
+ if (o == null || getClass() != o.getClass()) {
return false;
}
Analysis analysis = (Analysis) o;
- return Objects.equal(uuid, analysis.uuid) &&
- Objects.equal(date, analysis.date);
+ return date == analysis.date &&
+ uuid.equals(analysis.uuid) &&
+ Objects.equals(revision, analysis.revision);
}
@Override
public int hashCode() {
- return Objects.hashCode(uuid, date);
+ return Objects.hash(uuid, date, revision);
}
@Override
return "Analysis{" +
"uuid='" + uuid + '\'' +
", date=" + date +
+ ", revision=" + revision +
'}';
}
}
writer.beginObject();
writeServer(writer);
writeTask(writer, analysis.getCeTask());
- writeDates(writer, analysis, system2);
+ writeAnalysis(writer, analysis, system2);
writeProject(analysis, writer, analysis.getProject());
analysis.getBranch().ifPresent(b -> writeBranch(writer, analysis.getProject(), b));
analysis.getQualityGate().ifPresent(qualityGate -> writeQualityGate(writer, qualityGate));
writer.prop("serverUrl", server.getPublicRootUrl());
}
- private static void writeDates(JsonWriter writer, ProjectAnalysis analysis, System2 system2) {
- analysis.getAnalysis().ifPresent(a -> writer.propDateTime("analysedAt", a.getDate()));
+ private static void writeAnalysis(JsonWriter writer, ProjectAnalysis analysis, System2 system2) {
+ analysis.getAnalysis().ifPresent(a -> {
+ writer.propDateTime("analysedAt", new Date(a.getDate()));
+ a.getRevision().ifPresent(rev -> writer.prop("revision", rev));
+ });
writer.propDateTime("changedAt", new Date(analysis.getUpdatedAt().orElse(system2.now())));
}
*/
package org.sonar.server.webhook;
-import java.util.Date;
-import java.util.Random;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
public class AnalysisTest {
expectedException.expect(NullPointerException.class);
expectedException.expectMessage("uuid must not be null");
- new Analysis(null, 1_990L);
+ new Analysis(null, 1_990L, "abcde");
}
@Test
- public void test_equality() {
- String uuid = randomAlphanumeric(35);
- long date = new Random().nextLong();
- Analysis underTest = new Analysis(uuid, date);
-
- assertThat(underTest).isEqualTo(underTest);
- assertThat(underTest.getUuid()).isEqualTo(uuid);
- assertThat(underTest.getDate()).isEqualTo(new Date(date));
-
- assertThat(underTest).isNotEqualTo(null);
- assertThat(underTest).isNotEqualTo(new Analysis(uuid + "1", date));
- assertThat(underTest).isNotEqualTo(new Analysis(uuid, date + 1_000L));
- }
-
- @Test
- public void test_hashcode() {
- String uuid = randomAlphanumeric(35);
- long date = new Random().nextLong();
- Analysis underTest = new Analysis(uuid, date);
-
- assertThat(underTest.hashCode()).isEqualTo(underTest.hashCode());
- assertThat(underTest.hashCode()).isNotEqualTo(new Analysis(uuid + "1", date).hashCode());
- assertThat(underTest.hashCode()).isNotEqualTo(new Analysis(uuid, date + 1_000).hashCode());
+ public void test_bean() {
+ Analysis analysis = new Analysis("u1", 1_990L, "abcde");
+ assertThat(analysis.getUuid()).isEqualTo("u1");
+ assertThat(analysis.getDate()).isEqualTo(1_990L);
+ assertThat(analysis.getRevision()).hasValue("abcde");
+
+ analysis = new Analysis("u1", 1_990L, null);
+ assertThat(analysis.getRevision()).isEmpty();
}
}
private final CeTask ceTask = new CeTask("id", CeTask.Status.SUCCESS);
private final Project project = new Project("uuid", "key", "name");
- private final Analysis analysis = new Analysis("analysis_uuid", 1_500L);
+ private final Analysis analysis = new Analysis("analysis_uuid", 1_500L, "sha1");
private final Branch branch = new Branch(true, "name", Branch.Type.SHORT);
private final EvaluatedQualityGate qualityGate = EvaluatedQualityGate.newBuilder()
.setQualityGate(new QualityGate("id", "name", emptySet()))
assertThat(underTest).isNotEqualTo(new ProjectAnalysis(new Project("A", "B", "C"), ceTask, analysis, branch, qualityGate, 1L, properties));
assertThat(underTest).isNotEqualTo(new ProjectAnalysis(project, null, null, null, qualityGate, 1L, properties));
assertThat(underTest).isNotEqualTo(new ProjectAnalysis(project, ceTask, null, null, qualityGate, 1L, properties));
- assertThat(underTest).isNotEqualTo(new ProjectAnalysis(project, ceTask, new Analysis("foo", 1_500L), null, qualityGate, 1L, properties));
+ assertThat(underTest).isNotEqualTo(new ProjectAnalysis(project, ceTask, new Analysis("foo", 1_500L, "sha1"), null, qualityGate, 1L, properties));
assertThat(underTest).isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, null, qualityGate, 1L, properties));
assertThat(underTest).isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, new Branch(false, "B", Branch.Type.SHORT), qualityGate, 1L, properties));
assertThat(underTest).isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, branch, null, 1L, properties));
assertThat(underTest.hashCode()).isNotEqualTo(new ProjectAnalysis(new Project("A", "B", "C"), ceTask, analysis, branch, qualityGate, 1L, properties).hashCode());
assertThat(underTest.hashCode()).isNotEqualTo(new ProjectAnalysis(project, null, null, null, qualityGate, 1L, properties).hashCode());
assertThat(underTest.hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, null, null, qualityGate, 1L, properties).hashCode());
- assertThat(underTest.hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, new Analysis("foo", 1_500L), null, qualityGate, 1L, properties).hashCode());
+ assertThat(underTest.hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, new Analysis("foo", 1_500L, "sha1"), null, qualityGate, 1L, properties).hashCode());
assertThat(underTest.hashCode()).isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, null, qualityGate, 1L, properties).hashCode());
assertThat(underTest.hashCode())
.isNotEqualTo(new ProjectAnalysis(project, ceTask, analysis, new Branch(false, "B", Branch.Type.SHORT), qualityGate, 1L, properties).hashCode());
@Test
public void verify_toString() {
assertThat(underTest.toString()).isEqualTo(
- "ProjectAnalysis{project=Project{uuid='uuid', key='key', name='name'}, ceTask=CeTask{id='id', status=SUCCESS}, branch=Branch{main=true, name='name', type=SHORT}, qualityGate=EvaluatedQualityGate{qualityGate=QualityGate{id=id, name='name', conditions=[]}, status=ERROR, evaluatedConditions=[]}, updatedAt=1, properties={a=b}, analysis=Analysis{uuid='analysis_uuid', date=1500}}");
+ "ProjectAnalysis{project=Project{uuid='uuid', key='key', name='name'}, ceTask=CeTask{id='id', status=SUCCESS}, branch=Branch{main=true, name='name', type=SHORT}, qualityGate=EvaluatedQualityGate{qualityGate=QualityGate{id=id, name='name', conditions=[]}, status=ERROR, evaluatedConditions=[]}, updatedAt=1, properties={a=b}, analysis=Analysis{uuid='analysis_uuid', date=1500, revision=sha1}}");
}
}
" \"taskId\": \"#1\"," +
" \"status\": \"SUCCESS\"," +
" \"analysedAt\": \"2017-07-14T04:40:00+0200\"," +
+ " \"revision\": \"sha1\"," +
" \"changedAt\": \"2017-07-14T04:40:00+0200\"," +
" \"project\": {" +
" \"key\": \"P1\"," +
" \"taskId\": \"#1\"," +
" \"status\": \"SUCCESS\"," +
" \"analysedAt\": \"2017-07-14T04:40:00+0200\"," +
+ " \"revision\": \"sha1\"," +
" \"changedAt\": \"2017-07-14T04:40:00+0200\"," +
" \"project\": {" +
" \"key\": \"P1\"," +
.setStatus(Metric.Level.ERROR)
.build();
Map<String, String> scannerProperties = ImmutableMap.of(
- "sonar.analysis.revision", "ab45d24",
+ "sonar.analysis.foo", "bar",
"sonar.analysis.buildNumber", "B123",
"not.prefixed.with.sonar.analysis", "should be ignored",
"ignored", "should be ignored too");
" \"taskId\": \"#1\"," +
" \"status\": \"SUCCESS\"," +
" \"analysedAt\": \"2017-07-14T04:40:00+0200\"," +
+ " \"revision\": \"sha1\"," +
" \"changedAt\": \"2017-07-14T04:40:00+0200\"," +
" \"project\": {" +
" \"key\": \"P1\"," +
" ]" +
" }," +
" \"properties\": {" +
- " \"sonar.analysis.revision\": \"ab45d24\"," +
+ " \"sonar.analysis.foo\": \"bar\"," +
" \"sonar.analysis.buildNumber\": \"B123\"" +
" }" +
"}");
private static ProjectAnalysis newAnalysis(@Nullable CeTask task, @Nullable EvaluatedQualityGate gate,
@Nullable Branch branch, @Nullable Long analysisDate, Map<String, String> scannerProperties) {
- return new ProjectAnalysis(new Project("P1_UUID", PROJECT_KEY, "Project One"), task, analysisDate == null ? null : new Analysis("A_UUID1", analysisDate), branch,
+ return new ProjectAnalysis(new Project("P1_UUID", PROJECT_KEY, "Project One"), task, analysisDate == null ? null : new Analysis("A_UUID1", analysisDate, "sha1"), branch,
gate, analysisDate, scannerProperties);
}
}
ProjectAnalysis projectAnalysis = new ProjectAnalysis(
new Project(projectUuid, project.getKey(), project.name()),
null,
- new Analysis(analysis.getUuid(), analysis.getCreatedAt()),
+ new Analysis(analysis.getUuid(), analysis.getCreatedAt(), analysis.getRevision()),
new Branch(branch.isMain(), branch.getKey(), Type.valueOf(branch.getBranchType().name())),
evaluatedQualityGate,
null,
new ProjectAnalysis(
new Project(project.uuid(), project.getKey(), project.name()),
null,
- new Analysis(analysis.getUuid(), analysis.getCreatedAt()),
+ new Analysis(analysis.getUuid(), analysis.getCreatedAt(), analysis.getRevision()),
new Branch(false, "foo", Branch.Type.SHORT),
newQualityGate,
null,
package org.sonar.api.ce.posttask;
import java.util.Date;
+import java.util.Optional;
public interface Analysis {
/**
* Date of the analysis.
*/
Date getDate();
+
+ /**
+ * SCM revision, if declared during analysis
+ * @since 7.8
+ */
+ Optional<String> getRevision();
}
*
* <p>
* If more then one implementation of {@link PostProjectAnalysisTask} is found, they will be executed in no specific order.
- *
*
* <p>
* Class {@link PostProjectAnalysisTaskTester} is provided to write unit tests of implementations of this interface.
- *
*
* @since 5.5
* @see PostProjectAnalysisTaskTester
ScannerContext getScannerContext();
/**
- * Revision Id that has been analysed
+ * Revision Id that has been analysed. May return null.
+ * @since 7.6
+ * @deprecated in 7.8, replaced by {@code Analysis#getRevision()}
+ * @see #getAnalysis()
*/
String getScmRevisionId();
}
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
/**
public static final class AnalysisBuilder {
private String analysisUuid;
private Date date;
+ @Nullable
+ private String revision;
private AnalysisBuilder() {
// prevents instantiation outside PostProjectAnalysisTaskTester
return this;
}
+ public AnalysisBuilder setRevision(@Nullable String s) {
+ this.revision = s;
+ return this;
+ }
+
public Analysis build() {
return new Analysis() {
public Date getDate() {
return date;
}
+
+ @Override
+ public Optional<String> getRevision() {
+ return Optional.ofNullable(revision);
+ }
};
}
}