Skip to content

Commit

Permalink
Merge pull request #712 from jenkinsci/reference-job
Browse files Browse the repository at this point in the history
[JENKINS-72015] Make reference build computation more flexible
  • Loading branch information
uhafner committed Feb 23, 2024
2 parents 5071402 + 06fde4c commit aedc532
Show file tree
Hide file tree
Showing 29 changed files with 378 additions and 421 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
check-latest: true
cache: 'maven'
- name: Set up Maven
uses: stCarolas/setup-maven@v4.5
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.6
- name: Build with Maven
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
cache: maven

- name: Set up Maven
uses: stCarolas/setup-maven@v4.5
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.6

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
check-latest: true
cache: 'maven'
- name: Set up Maven
uses: stCarolas/setup-maven@v4.5
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.6
- name: Generate coverage with JaCoCo
Expand Down
5 changes: 4 additions & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>analysis-pom</artifactId>
<version>6.14.0</version>
<version>7.0.0</version>
<relativePath />
</parent>

Expand All @@ -30,6 +30,7 @@
<!-- Maven Surefire ArgLine -->
<argLine>-Djava.awt.headless=true -Xmx1024m -Djenkins.test.timeout=1000 --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.util.concurrent=ALL-UNNAMED</argLine>

<forensics-api.version>2.4.0</forensics-api.version>
</properties>

<licenses>
Expand Down Expand Up @@ -68,6 +69,7 @@
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>forensics-api</artifactId>
<version>${forensics-api.version}</version>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
Expand All @@ -86,6 +88,7 @@
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>forensics-api</artifactId>
<version>2.3.0</version>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static class BlameCallback extends AbstractRepositoryCallback<RemoteResultWrappe
@Override @SuppressWarnings("PMD.UseTryWithResources")
public RemoteResultWrapper<Blames> invoke(final Repository repository, final VirtualChannel channel)
throws InterruptedException {
try {
try (repository) {
RemoteResultWrapper<Blames> log = new RemoteResultWrapper<>(blames, "Errors while running Git blame:");
log.logInfo("-> Git commit ID = '%s'", headCommit.getName());
log.logInfo("-> Git working tree = '%s'", getWorkTree(repository));
Expand All @@ -148,9 +148,6 @@ public RemoteResultWrapper<Blames> invoke(final Repository repository, final Vir

return log;
}
finally {
repository.close();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.Edit;
import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.errors.LargeObjectException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.LargeObjectException;
import org.eclipse.jgit.lib.ObjectDatabase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.IOException;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.util.FilteredLog;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

Expand All @@ -29,28 +31,29 @@ public class GitDeltaCalculator extends DeltaCalculator {
static final String EMPTY_COMMIT_ERROR = "Calculating the Git code delta is not possible due to an unknown commit ID";

private final GitClient git;
private final String scmKey;

/**
* Constructor for an instance of {@link DeltaCalculator} which can be used for Git.
*
* @param git
* The {@link GitClient}
* @param scmKey
* the key of the SCM repository (substring that must be part of the SCM key)
*/
public GitDeltaCalculator(final GitClient git) {
public GitDeltaCalculator(final GitClient git, final String scmKey) {
super();

this.git = git;
this.scmKey = scmKey;
}

@Override
public Optional<Delta> calculateDelta(final Run<?, ?> build, final Run<?, ?> referenceBuild,
final String scmKeyFilter, final FilteredLog log) {
log.logInfo("-> Obtaining commits in SCM '%s' for current build '%s'",
scmKeyFilter, build.getFullDisplayName());
Optional<GitCommitsRecord> buildCommits = GitCommitsRecord.findRecordForScm(build, scmKeyFilter);
log.logInfo("-> Obtaining commits in SCM '%s' for reference build '%s'",
scmKeyFilter, referenceBuild.getFullDisplayName());
Optional<GitCommitsRecord> referenceCommits = GitCommitsRecord.findRecordForScm(referenceBuild, scmKeyFilter);
String scm = StringUtils.defaultIfEmpty(scmKeyFilter, scmKey);
Optional<GitCommitsRecord> buildCommits = GitCommitsRecord.findRecordForScm(build, scm);
Optional<GitCommitsRecord> referenceCommits = GitCommitsRecord.findRecordForScm(referenceBuild, scm);
if (buildCommits.isPresent() && referenceCommits.isPresent()) {
String currentCommit = getLatestCommit(build.getFullDisplayName(), buildCommits.get(), log);
String referenceCommit = getLatestCommit(referenceBuild.getFullDisplayName(), referenceCommits.get(), log);
Expand All @@ -77,11 +80,11 @@ public Optional<Delta> calculateDelta(final Run<?, ?> build, final Run<?, ?> ref
* Returns the latest commit of the {@link GitCommitsRecord commits record} of a Git repository.
*
* @param buildName
* The name of the build the commits record corresponds to
* the name of the build the commits record corresponds to
* @param record
* The commits record
* the commits record
* @param log
* The log
* the log
*
* @return the latest commit
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Optional<DeltaCalculator> createDeltaCalculator(final SCM scm, final Run<
GitClient client = validator.createClient();
logger.logInfo("-> Git delta calculator successfully created in working tree '%s'",
new PathUtil().getAbsolutePath(client.getWorkTree().getRemote()));
return Optional.of(new GitDeltaCalculator(client));
return Optional.of(new GitDeltaCalculator(client, scm.getKey()));
}
logger.logInfo("-> Git Delta Calculator could not be created for SCM '%s' in working tree '%s'", scm,
workspace);
Expand Down

This file was deleted.

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

import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.verb.POST;
import org.jenkinsci.Symbol;
import hudson.Extension;
import hudson.model.BuildableItem;
import hudson.model.Item;
import hudson.model.Run;
import hudson.util.ComboBoxModel;
import hudson.util.FormValidation;
import jenkins.scm.api.SCMHead;

import io.jenkins.plugins.forensics.reference.ReferenceRecorder;
Expand Down Expand Up @@ -50,8 +43,8 @@ public GitReferenceRecorder() {

/**
* Sets the maximal number of additional commits in the reference job that will be considered during the
* comparison with the current branch. In order to avoid an indefinite scanning of the build history until a
* matching reference has been found this value is used as a stop criteria.
* comparison with the current branch. To avoid an indefinite scanning of the build history until a
* matching reference has been found, this value is used as a stop criteria.
*
* @param maxCommits
* maximal number of commits
Expand Down Expand Up @@ -87,23 +80,19 @@ public boolean isSkipUnknownCommits() {
final FilteredLog log) {
Optional<GitCommitsRecord> referenceCommit
= GitCommitsRecord.findRecordForScm(lastCompletedBuildOfReferenceJob, getScm());

Optional<Run<?, ?>> referenceBuild;
if (referenceCommit.isPresent()) {
Optional<GitCommitsRecord> thisCommit = GitCommitsRecord.findRecordForScm(owner, getScm());
if (thisCommit.isPresent()) {
GitCommitsRecord commitsRecord = thisCommit.get();
Optional<Run<?, ?>> referencePoint = commitsRecord.getReferencePoint(
referenceCommit.get(), getMaxCommits(), isSkipUnknownCommits(), log);
if (referencePoint.isPresent()) {
log.logInfo("-> found build '%s' in reference job with matching commits",
referencePoint.get().getDisplayName());

return referencePoint;
}
}
referenceBuild = findByCommits(owner, referenceCommit.get(), log);
}
else {
log.logInfo("-> selected build '%s' of reference job does not yet contain a `GitCommitsRecord`",
lastCompletedBuildOfReferenceJob.getDisplayName());
referenceBuild = Optional.empty();
}

if (referenceBuild.isPresent()) {
return referenceBuild;
}

Optional<SCMHead> targetBranchHead = findTargetBranchHead(owner.getParent());
Expand All @@ -117,6 +106,30 @@ public boolean isSkipUnknownCommits() {
return Optional.empty();
}

private Optional<Run<?, ?>> findByCommits(final Run<?, ?> owner, final GitCommitsRecord referenceCommit,
final FilteredLog log) {
Optional<GitCommitsRecord> ownerCommits = GitCommitsRecord.findRecordForScm(owner, getScm());
if (ownerCommits.isPresent()) {
GitCommitsRecord commitsRecord = ownerCommits.get();
Optional<Run<?, ?>> referencePoint = commitsRecord.getReferencePoint(
referenceCommit, getMaxCommits(), isSkipUnknownCommits(), log);
if (referencePoint.isPresent()) {
Run<?, ?> referenceBuild = referencePoint.get();
log.logInfo("-> found build '%s' in reference job with matching commits",
referenceBuild.getDisplayName());

return referencePoint;
}
else {
log.logInfo("-> found no build with matching commits");
}
}
else {
log.logInfo("-> found no `GitCommitsRecord` in current build '%s'", owner.getDisplayName());
}
return Optional.empty();
}

@Override
@SuppressFBWarnings("BC")
public Descriptor getDescriptor() {
Expand All @@ -129,66 +142,22 @@ public Descriptor getDescriptor() {
@Extension
@Symbol("discoverGitReferenceBuild")
public static class Descriptor extends SimpleReferenceRecorderDescriptor {
private final JenkinsFacade jenkins;

/**
* Creates a new descriptor with the concrete services.
* Creates a new instance of {@link Descriptor}.
*/
@SuppressWarnings("unused") // Required for extension point
public Descriptor() {
this(new JenkinsFacade(), new GitReferenceJobModelValidation());
this(new JenkinsFacade());
}

@VisibleForTesting
Descriptor(final JenkinsFacade jenkins, final GitReferenceJobModelValidation model) {
super();

this.jenkins = jenkins;
this.model = model;
Descriptor(final JenkinsFacade jenkins) {
super(jenkins);
}

@NonNull
@Override
public String getDisplayName() {
return Messages.Recorder_DisplayName();
}

private final GitReferenceJobModelValidation model;

/**
* Returns the model with the possible reference jobs.
*
* @param project
* the project that is configured
*
* @return the model with the possible reference jobs
*/
@POST
public ComboBoxModel doFillReferenceJobItems(@AncestorInPath final BuildableItem project) {
if (jenkins.hasPermission(Item.CONFIGURE, project)) {
return model.getAllJobs();
}
return new ComboBoxModel();
}

/**
* Performs on-the-fly validation of the reference job.
*
* @param project
* the project that is configured
* @param referenceJob
* the reference job
*
* @return the validation result
*/
@POST
@SuppressWarnings("unused") // Used in jelly validation
public FormValidation doCheckReferenceJob(@AncestorInPath final BuildableItem project,
@QueryParameter final String referenceJob) {
if (!jenkins.hasPermission(Item.CONFIGURE, project)) {
return FormValidation.ok();
}
return model.validateJob(referenceJob);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ public Optional<CommitDecorator> createCommitDecorator(final SCM scm, final Filt
}
return Optional.empty();
}

}
Loading

0 comments on commit aedc532

Please sign in to comment.