Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with not showing code coverage metrics #692

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.jenkinsci.plugins.prometheus;

import hudson.model.Job;
import hudson.model.Run;
import io.jenkins.plugins.coverage.metrics.steps.CoverageBuildAction;
import io.prometheus.client.Collector;
import jenkins.model.Jenkins;
import org.apache.commons.collections.CollectionUtils;
import org.jenkinsci.plugins.prometheus.collectors.CollectorFactory;
import org.jenkinsci.plugins.prometheus.collectors.CollectorType;
import org.jenkinsci.plugins.prometheus.collectors.MetricCollector;
Expand Down Expand Up @@ -36,25 +34,12 @@
return Collections.emptyList();
}

List<List<MetricFamilySamples>> samples = new ArrayList<>();
Jobs.forEachJob(job -> CollectionUtils.addIgnoreNull(samples, collectCoverageMetricForJob(job)));
List<MetricCollector<Run<?,?>, ? extends Collector>> collectors = createCollectors();


return samples.stream().flatMap(Collection::stream).collect(Collectors.toList());
return collectCoverageMetricForJob(collectors);
}

private List<MetricFamilySamples> collectCoverageMetricForJob(Job<?,?> job) {

Run<?,?> lastBuild = job.getLastBuild();
if (lastBuild == null || lastBuild.isBuilding()) {
return Collections.emptyList();
}

CoverageBuildAction coverageBuildAction = lastBuild.getAction(CoverageBuildAction.class);
if (coverageBuildAction == null) {
return Collections.emptyList();
}

private List<MetricCollector<Run<?, ?>, ? extends Collector>> createCollectors() {
CollectorFactory factory = new CollectorFactory();
List<MetricCollector<Run<?,?>, ? extends Collector>> collectors = new ArrayList<>();

Expand Down Expand Up @@ -85,13 +70,31 @@
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_LINE_TOTAL, new String[]{jobAttributeName}));
collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_LINE_PERCENT, new String[]{jobAttributeName}));

collectors.forEach(c -> c.calculateMetric(lastBuild, new String[]{job.getFullName()}));
return collectors;
}

private List<MetricFamilySamples> collectCoverageMetricForJob(List<MetricCollector<Run<?, ?>, ? extends Collector>> collectors) {

Jobs.forEachJob(job -> {
Run<?,?> lastBuild = job.getLastBuild();
if (lastBuild == null || lastBuild.isBuilding()) {

Check warning on line 80 in src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 80 is only partially covered, 2 branches are missing
return;

Check warning on line 81 in src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 81 is not covered by tests
}

CoverageBuildAction coverageBuildAction = lastBuild.getAction(CoverageBuildAction.class);
if (coverageBuildAction == null) {

Check warning on line 85 in src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 85 is only partially covered, one branch is missing
return;

Check warning on line 86 in src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 86 is not covered by tests
}

collectors.forEach(c -> c.calculateMetric(lastBuild, new String[]{job.getFullName()}));
});

return collectors.stream()
.map(MetricCollector::collect)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

private boolean isCoveragePluginLoaded() {
return Jenkins.get().getPlugin("coverage") != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,52 +59,6 @@ void shouldNotProduceMetricsWhenItIsNotConfigured() {
}
}

@Test
void shouldNotProduceMetricsWhenJobIsBuilding() {
try (
MockedStatic<Jenkins> jenkinsStatic = mockStatic(Jenkins.class);
MockedStatic<PrometheusConfiguration> configurationStatic = mockStatic(PrometheusConfiguration.class);
) {
jenkinsStatic.when(Jenkins::get).thenReturn(jenkins);
configurationStatic.when(PrometheusConfiguration::get).thenReturn(config);
Job jobUnderTest = mock(Job.class);
Run lastBuild = mock(Run.class);
when(lastBuild.isBuilding()).thenReturn(true);
when(jobUnderTest.getLastBuild()).thenReturn(lastBuild);
when(jenkins.getAllItems(Job.class)).thenReturn(List.of(jobUnderTest));
when(jenkins.getPlugin("coverage")).thenReturn(new Plugin.DummyImpl());
when(config.isCollectCodeCoverage()).thenReturn(true);

CodeCoverageCollector sut = new CodeCoverageCollector();

List<Collector.MetricFamilySamples> collect = sut.collect();
assertEquals(0, collect.size());
}
}

@Test
void shouldNotProduceMetricsWhenJobHasNoCoverageBuildAction() {
try (
MockedStatic<Jenkins> jenkinsStatic = mockStatic(Jenkins.class);
MockedStatic<PrometheusConfiguration> configurationStatic = mockStatic(PrometheusConfiguration.class);
) {
jenkinsStatic.when(Jenkins::get).thenReturn(jenkins);
configurationStatic.when(PrometheusConfiguration::get).thenReturn(config);
Job jobUnderTest = mock(Job.class);
Run lastBuild = mock(Run.class);
when(lastBuild.isBuilding()).thenReturn(false);
when(jobUnderTest.getLastBuild()).thenReturn(lastBuild);
when(jenkins.getAllItems(Job.class)).thenReturn(List.of(jobUnderTest));
when(jenkins.getPlugin("coverage")).thenReturn(new Plugin.DummyImpl());
when(config.isCollectCodeCoverage()).thenReturn(true);

CodeCoverageCollector sut = new CodeCoverageCollector();

List<Collector.MetricFamilySamples> collect = sut.collect();
assertEquals(0, collect.size());
}
}

@Test
void shouldProduceMetricsWhenJobHasCoverageBuildAction() {
try (
Expand Down
Loading