Skip to content

Commit

Permalink
Job.BuildNumberAssigner (#9846)
Browse files Browse the repository at this point in the history
* `Job.BuildNumberAssigner`

* Clearer API for `saveNextBuildNumber` #9846 (comment)
  • Loading branch information
jglick authored Nov 2, 2024
1 parent 5db2f01 commit 1892e21
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 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 @@ -337,12 +338,42 @@ public boolean isKeepDependencies() {
}

/**
* 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 {
return ExtensionList.lookupFirst(BuildNumberAssigner.class).assignBuildNumber(this, this::saveNextBuildNumber);
}

/**
* Alternate strategy for assigning build numbers.
*/
@Restricted(Beta.class)
public interface BuildNumberAssigner extends ExtensionPoint {
/**
* Implementation of {@link Job#assignBuildNumber}.
*/
int assignBuildNumber(Job<?, ?> job, SaveNextBuildNumber saveNextBuildNumber) throws IOException;
/**
* Provides an externally accessible alias for {@link Job#saveNextBuildNumber}, which is {@code protected}.
* ({@link #getNextBuildNumber} and {@link #fastUpdateNextBuildNumber} are already accessible.)
*/
interface SaveNextBuildNumber {
void call() throws IOException;
}
}

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

/**
Expand Down

0 comments on commit 1892e21

Please sign in to comment.