Skip to content

Commit ce793af

Browse files
authored
Implement history table (#116)
1 parent 1333664 commit ce793af

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<dependency>
6565
<groupId>org.jenkins-ci.plugins</groupId>
6666
<artifactId>junit</artifactId>
67+
<version>1.52</version>
6768
</dependency>
6869
<dependency>
6970
<groupId>org.jenkins-ci.plugins.workflow</groupId>

src/main/java/io/jenkins/plugins/junit/storage/database/DatabaseTestResultStorage.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import edu.umd.cs.findbugs.annotations.CheckForNull;
44
import edu.umd.cs.findbugs.annotations.NonNull;
55
import hudson.Extension;
6-
import hudson.ExtensionList;
76
import hudson.Util;
87
import hudson.model.Job;
98
import hudson.model.Run;
@@ -16,6 +15,7 @@
1615
import hudson.tasks.junit.TestResult;
1716
import hudson.tasks.junit.TestResultSummary;
1817
import hudson.tasks.junit.TrendTestResultSummary;
18+
import hudson.tasks.junit.HistoryTestResultSummary;
1919
import io.jenkins.plugins.junit.storage.JunitTestResultStorage;
2020
import io.jenkins.plugins.junit.storage.JunitTestResultStorageDescriptor;
2121
import io.jenkins.plugins.junit.storage.TestResultImpl;
@@ -230,7 +230,10 @@ public TestResultStorage(String job, int build) {
230230
}
231231

232232
private <T> T query(Querier<T> querier) {
233-
try (Connection connection = getConnectionSupplier().connection()) {
233+
try {
234+
// TODO move to try-with-resources, whenever I try close this I get (multiple queries needed):
235+
// org.postgresql.util.PSQLException: This statement has been closed.
236+
Connection connection = getConnectionSupplier().connection();
234237
return querier.run(connection);
235238
} catch (SQLException x) {
236239
throw new RuntimeException(x);
@@ -415,6 +418,35 @@ public List<TestDurationResultSummary> getTestDurationResultSummary() {
415418
});
416419
}
417420

421+
public List<HistoryTestResultSummary> getHistorySummary(int offset) {
422+
return query(connection -> {
423+
try (PreparedStatement statement = connection.prepareStatement(
424+
"SELECT build, sum(duration) as duration, sum(case when errorDetails is not null then 1 else 0 end) as failCount, sum(case when skipped is not null then 1 else 0 end) as skipCount, sum(case when errorDetails is null and skipped is null then 1 else 0 end) as passCount FROM caseResults WHERE job = ? GROUP BY build ORDER BY build DESC LIMIT 25 OFFSET ?;"
425+
)) {
426+
statement.setString(1, job);
427+
statement.setInt(2, offset);
428+
try (ResultSet result = statement.executeQuery()) {
429+
430+
List<HistoryTestResultSummary> historyTestResultSummaries = new ArrayList<>();
431+
while (result.next()) {
432+
int buildNumber = result.getInt("build");
433+
int duration = result.getInt("duration");
434+
int passed = result.getInt("passCount");
435+
int failed = result.getInt("failCount");
436+
int skipped = result.getInt("skipCount");
437+
438+
Job<?, ?> theJob = Jenkins.get().getItemByFullName(getJobName(), Job.class);
439+
if (theJob != null) {
440+
Run<?, ?> run = theJob.getBuildByNumber(buildNumber);
441+
historyTestResultSummaries.add(new HistoryTestResultSummary(run, duration, failed, skipped, passed));
442+
}
443+
}
444+
return historyTestResultSummaries;
445+
}
446+
}
447+
});
448+
}
449+
418450
@Override
419451
public int getCountOfBuildsWithTestResults() {
420452
return query(connection -> {

0 commit comments

Comments
 (0)