Search in sources :

Example 1 with Analysis

use of org.sonar.server.webhook.Analysis in project sonarqube by SonarSource.

the class WebhookPostTask method convert.

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(), 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).orElse(null);
    Long date = projectAnalysis.getAnalysis().map(a -> a.getDate().getTime()).orElse(null);
    Map<String, String> properties = projectAnalysis.getScannerContext().getProperties();
    Project project = new Project(projectAnalysis.getProject().getUuid(), projectAnalysis.getProject().getKey(), projectAnalysis.getProject().getName());
    return new org.sonar.server.webhook.ProjectAnalysis(project, ceTask, analysis, branch, qualityGate, date, properties);
}
Also used : CeTask(org.sonar.server.webhook.CeTask) QGToEvaluatedQG(org.sonar.ce.task.projectanalysis.api.posttask.QGToEvaluatedQG) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) Analysis(org.sonar.server.webhook.Analysis) Supplier(java.util.function.Supplier) Branch(org.sonar.server.webhook.Branch) Project(org.sonar.server.webhook.Project) WebHooks(org.sonar.server.webhook.WebHooks) Map(java.util.Map) Optional(java.util.Optional) WebhookPayloadFactory(org.sonar.server.webhook.WebhookPayloadFactory) WebhookPayload(org.sonar.server.webhook.WebhookPayload) PostProjectAnalysisTask(org.sonar.api.ce.posttask.PostProjectAnalysisTask) Project(org.sonar.server.webhook.Project) Analysis(org.sonar.server.webhook.Analysis) Branch(org.sonar.server.webhook.Branch) CeTask(org.sonar.server.webhook.CeTask) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate)

Example 2 with Analysis

use of org.sonar.server.webhook.Analysis in project sonarqube by SonarSource.

the class WebhookPostTaskTest method callWebHooks.

private void callWebHooks(@Nullable String analysisUUid, @Nullable QualityGate qualityGate) {
    Project project = newProjectBuilder().setUuid(randomAlphanumeric(3)).setKey(randomAlphanumeric(4)).setName(randomAlphanumeric(5)).build();
    CeTask ceTask = newCeTaskBuilder().setStatus(CeTask.Status.values()[random.nextInt(CeTask.Status.values().length)]).setId(randomAlphanumeric(6)).build();
    Date date = new Date();
    Map<String, String> properties = ImmutableMap.of(randomAlphanumeric(17), randomAlphanumeric(18));
    Branch branch = newBranchBuilder().setIsMain(random.nextBoolean()).setType(Branch.Type.values()[random.nextInt(Branch.Type.values().length)]).setName(randomAlphanumeric(29)).build();
    PostProjectAnalysisTaskTester.of(underTest).at(date).withCeTask(ceTask).withProject(project).withBranch(branch).withQualityGate(qualityGate).withScannerContext(newScannerContextBuilder().addProperties(properties).build()).withAnalysisUuid(analysisUUid).withQualityGate(qualityGate).execute();
    ArgumentCaptor<Supplier> supplierCaptor = ArgumentCaptor.forClass(Supplier.class);
    verify(webHooks).sendProjectAnalysisUpdate(eq(new WebHooks.Analysis(project.getUuid(), analysisUUid, ceTask.getId())), supplierCaptor.capture(), any(LogStatistics.class));
    assertThat(supplierCaptor.getValue().get()).isSameAs(webhookPayload);
    EvaluatedQualityGate webQualityGate = null;
    if (qualityGate != null) {
        QualityGate.Condition condition = qualityGate.getConditions().iterator().next();
        Condition qgCondition = new Condition(condition.getMetricKey(), Condition.Operator.valueOf(condition.getOperator().name()), condition.getErrorThreshold());
        webQualityGate = EvaluatedQualityGate.newBuilder().setQualityGate(new org.sonar.server.qualitygate.QualityGate(qualityGate.getId(), qualityGate.getName(), Collections.singleton(qgCondition))).setStatus(Metric.Level.valueOf(qualityGate.getStatus().name())).addEvaluatedCondition(qgCondition, EvaluatedCondition.EvaluationStatus.valueOf(condition.getStatus().name()), condition.getValue()).build();
    }
    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(), 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(), properties));
}
Also used : EvaluatedCondition(org.sonar.server.qualitygate.EvaluatedCondition) Condition(org.sonar.server.qualitygate.Condition) CeTask(org.sonar.api.ce.posttask.CeTask) Date(java.util.Date) QualityGate(org.sonar.api.ce.posttask.QualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) Project(org.sonar.api.ce.posttask.Project) ProjectAnalysis(org.sonar.server.webhook.ProjectAnalysis) Branch(org.sonar.api.ce.posttask.Branch) Analysis(org.sonar.server.webhook.Analysis) ProjectAnalysis(org.sonar.server.webhook.ProjectAnalysis) LogStatistics(org.sonar.api.ce.posttask.PostProjectAnalysisTask.LogStatistics) Supplier(java.util.function.Supplier) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate)

Aggregations

Supplier (java.util.function.Supplier)2 EvaluatedQualityGate (org.sonar.server.qualitygate.EvaluatedQualityGate)2 Analysis (org.sonar.server.webhook.Analysis)2 Date (java.util.Date)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Branch (org.sonar.api.ce.posttask.Branch)1 CeTask (org.sonar.api.ce.posttask.CeTask)1 PostProjectAnalysisTask (org.sonar.api.ce.posttask.PostProjectAnalysisTask)1 LogStatistics (org.sonar.api.ce.posttask.PostProjectAnalysisTask.LogStatistics)1 Project (org.sonar.api.ce.posttask.Project)1 QualityGate (org.sonar.api.ce.posttask.QualityGate)1 QGToEvaluatedQG (org.sonar.ce.task.projectanalysis.api.posttask.QGToEvaluatedQG)1 Condition (org.sonar.server.qualitygate.Condition)1 EvaluatedCondition (org.sonar.server.qualitygate.EvaluatedCondition)1 Branch (org.sonar.server.webhook.Branch)1 CeTask (org.sonar.server.webhook.CeTask)1 Project (org.sonar.server.webhook.Project)1 ProjectAnalysis (org.sonar.server.webhook.ProjectAnalysis)1 WebHooks (org.sonar.server.webhook.WebHooks)1