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

Job.BuildNumberAssigner #9846

Merged
merged 2 commits into from
Nov 2, 2024
Merged
Changes from 1 commit
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
48 changes: 43 additions & 5 deletions core/src/main/java/hudson/model/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import hudson.BulkChange;
import hudson.EnvVars;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.FeedAdapter;
import hudson.PermalinkList;
Expand Down Expand Up @@ -346,12 +347,49 @@
}

/**
* Allocates a new buildCommand number.
* Allocates a new build number.
* @see BuildNumberAssigner
*/
public synchronized int assignBuildNumber() throws IOException {
int r = nextBuildNumber++;
saveNextBuildNumber();
return r;
public int assignBuildNumber() throws IOException {
var bna = ExtensionList.lookupFirst(BuildNumberAssigner.class).assignBuildNumber(this);
if (bna.needsSave) {

Check warning on line 355 in core/src/main/java/hudson/model/Job.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 355 is only partially covered, one branch is missing
saveNextBuildNumber();
}
return bna.assignedNumber;
}

/**
* Alternate strategy for assigning build numbers.
* @see #assignBuildNumber()
*/
@Restricted(Beta.class)
public interface BuildNumberAssigner extends ExtensionPoint {
/**
* Strategy outcome.
* @param assignedNumber the return value for {@link #assignBuildNumber()}
* @param needsSave whether {@link #saveNextBuildNumber} should be called afterwards
*/
record BuildNumberAssignment(int assignedNumber, boolean needsSave) {};

/**
* Assign a unique number for a new build of a given job.
* @see #getNextBuildNumber
* @see #fastUpdateNextBuildNumber
*/
BuildNumberAssignment assignBuildNumber(Job<?, ?> job) throws IOException;
jglick marked this conversation as resolved.
Show resolved Hide resolved
}

@Restricted(DoNotUse.class)
@Extension(ordinal = -1000)
public static final class DefaultBuildNumberAssigner implements BuildNumberAssigner {
@Override
public BuildNumberAssignment assignBuildNumber(Job<?, ?> job) throws IOException {
synchronized (job) {
int r = job.nextBuildNumber++;
job.saveNextBuildNumber();
return new BuildNumberAssignment(r, false);
}
}
}

/**
Expand Down
Loading