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

[JENKINS-72015] Make reference build computation more flexible #519

Merged
merged 18 commits into from
Feb 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
1 change: 0 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ def configurations = [

def params = [
failFast: false,
pit: [skip: false, sourceCodeRetention: 'MODIFIED'],
configurations: configurations,
checkstyle: [qualityGates: [[threshold: 1, type: 'NEW', unstable: true]]],
pmd: [qualityGates: [[threshold: 1, type: 'NEW', unstable: true]]],
Expand Down
88 changes: 56 additions & 32 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -8,12 +9,12 @@
<relativePath />
</parent>

<artifactId>forensics-api</artifactId>
<groupId>io.jenkins.plugins</groupId>
<packaging>hpi</packaging>
<name>Forensics API Plugin</name>
<artifactId>forensics-api</artifactId>
<version>${revision}${changelist}</version>
<packaging>hpi</packaging>

<name>Forensics API Plugin</name>
<description>Defines an API for Jenkins to mine and analyze data from a source code repository.</description>
<url>https://github.com/jenkinsci/forensics-api-plugin</url>

Expand Down Expand Up @@ -43,6 +44,24 @@
</developer>
</developers>

<scm>
<connection>scm:git:https://github.com/jenkinsci/forensics-api-plugin.git</connection>
<developerConnection>scm:git:[email protected]:jenkinsci/forensics-api-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/forensics-api-plugin</url>
<tag>${scmTag}</tag>
</scm>

<properties>
<revision>2.4.0</revision>
<changelist>-SNAPSHOT</changelist>

<module.name>${project.groupId}.forensics.api</module.name>

<!-- Library Dependencies Versions -->
<testcontainers.version>1.19.5</testcontainers.version>

</properties>

<dependencies>

<dependency>
Expand Down Expand Up @@ -97,8 +116,8 @@
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>plugin-util-api</artifactId>
<scope>test</scope>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand All @@ -110,6 +129,21 @@
<artifactId>ssh-slaves</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-definition</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
Expand All @@ -125,6 +159,23 @@

</dependencies>

<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
<repository>
<id>incrementals.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/incrementals/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -170,31 +221,4 @@

</plugins>
</build>

<scm>
<connection>scm:git:https://github.com/jenkinsci/forensics-api-plugin.git</connection>
<developerConnection>scm:git:[email protected]:jenkinsci/forensics-api-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/forensics-api-plugin</url>
<tag>${scmTag}</tag>
</scm>

<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
<repository>
<id>incrementals.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/incrementals/</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

</project>

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import org.kohsuke.stapler.StaplerProxy;
import hudson.model.Result;
import hudson.model.Run;
import jenkins.model.RunAction2;

Expand All @@ -20,8 +21,8 @@
import static j2html.TagCreator.*;

/**
* Stores the reference build for a given build. The reference build is a build in a different Jenkins job that can be
* used to compute delta reports.
* Stores the selected reference build for a given build. The reference build is a build in a different (or same)
* Jenkins job that can be used to compute delta reports.
*
* @author Ullrich Hafner
* @see ReferenceRecorder
Expand All @@ -31,7 +32,7 @@ public class ReferenceBuild implements RunAction2, Serializable, StaplerProxy {

/**
* Indicates that no reference build has been found. Note that this value is not used when the build has been found
* initially but has been deleted afterwards.
* initially but has been deleted afterward.
*/
public static final String NO_REFERENCE_BUILD = "-";

Expand All @@ -58,51 +59,100 @@ public static String getReferenceBuildLink(final String referenceBuildId) {
}

private final String referenceBuildId;
private Result requiredResult; // @since 2.4.0
private final JenkinsFacade jenkinsFacade;
private final List<String> messages;

@SuppressFBWarnings(value = "SE", justification = "transient field owner ist restored using a Jenkins callback")
private transient Run<?, ?> owner;

/**
* Creates a new instance of {@link ReferenceBuild} that indicates that no reference build has been found.
* Creates a new instance of {@link ReferenceBuild} that indicates that no reference build has been found.
*
* @param owner
* the current run as owner of this action
* the current run as the owner of this action
* @param messages
* messages that show the steps the resolution process
* @param requiredResult
* the required build result of the chosen reference build
*/
public ReferenceBuild(final Run<?, ?> owner, final List<String> messages, final Result requiredResult) {
this(owner, messages, NO_REFERENCE_BUILD, requiredResult);
}

/**
* Creates a new instance of {@link ReferenceBuild} that points to the specified reference build.
*
* @param owner
* the current build as the owner of this action
* @param messages
* messages that show the steps of the resolution process
* @param requiredResult
* the required build result of the chosen reference build
* @param referenceBuild
* the found reference build
*/
public ReferenceBuild(final Run<?, ?> owner, final List<String> messages, final Result requiredResult,
final Run<?, ?> referenceBuild) {
this(owner, messages, referenceBuild.getExternalizableId(), requiredResult);
}

/**
* Creates a new instance of {@link ReferenceBuild} that indicates that no reference build has been found.
*
* @param owner
* the current run as the owner of this action
* @param messages
* messages that show the steps the resolution process
* @deprecated use {@link #ReferenceBuild(Run, List, Result)} instead
*/
@Deprecated
public ReferenceBuild(final Run<?, ?> owner, final List<String> messages) {
this(owner, messages, NO_REFERENCE_BUILD);
this(owner, messages, NO_REFERENCE_BUILD, Result.UNSTABLE);
}

/**
* Creates a new instance of {@link ReferenceBuild} that points to the specified reference build.
*
* @param owner
* the current build as owner of this action
* the current build as the owner of this action
* @param messages
* messages that show the steps of the resolution process
* @param referenceBuild
* the found reference build
* @deprecated use {@link #ReferenceBuild(Run, List, Result, Run)} instead
*/
@Deprecated
public ReferenceBuild(final Run<?, ?> owner, final List<String> messages, final Run<?, ?> referenceBuild) {
this(owner, messages, referenceBuild.getExternalizableId());
this(owner, messages, referenceBuild.getExternalizableId(), Result.UNSTABLE);
}

private ReferenceBuild(final Run<?, ?> owner, final List<String> messages, final String referenceBuildId) {
this(owner, messages, referenceBuildId, new JenkinsFacade());
private ReferenceBuild(final Run<?, ?> owner, final List<String> messages, final String referenceBuildId, final Result requiredResult) {
this(owner, messages, referenceBuildId, requiredResult, new JenkinsFacade());
}

@VisibleForTesting
ReferenceBuild(final Run<?, ?> owner, final List<String> messages, final String referenceBuildId,
final JenkinsFacade jenkinsFacade) {
final Result requiredResult, final JenkinsFacade jenkinsFacade) {
this.owner = owner;
this.messages = new ArrayList<>(messages);
this.referenceBuildId = referenceBuildId;
this.requiredResult = requiredResult;
this.jenkinsFacade = jenkinsFacade;
}

/**
* Called after deserialization to retain backward compatibility.
*
* @return this
*/
protected Object readResolve() {
if (requiredResult == null) {
requiredResult = Result.UNSTABLE;
}
return this;
}

@Override
public void onAttached(final Run<?, ?> run) {
this.owner = run;
Expand Down Expand Up @@ -174,6 +224,15 @@ public String getReferenceBuildId() {
return Optional.empty();
}

/**
* Returns the required build result of the chosen reference build.
*
* @return the required build result
*/
public Result getRequiredResult() {
return requiredResult;
}

@Override
public String getDisplayName() {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.jenkins.plugins.forensics.reference;

import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.util.VisibleForTesting;

import hudson.util.ComboBoxModel;
Expand Down Expand Up @@ -45,7 +47,8 @@ public ComboBoxModel getAllJobs() {
* @return the validation result
*/
public FormValidation validateJob(final String referenceJobName) {
if (jenkins.getJob(referenceJobName).isPresent()) {
if (StringUtils.isEmpty(referenceJobName)
|| jenkins.getJob(referenceJobName).isPresent()) {
return FormValidation.ok();
}
return FormValidation.error(Messages.FieldValidator_Error_ReferenceJobDoesNotExist());
Expand Down
Loading
Loading