Skip to content

Commit

Permalink
Prevent dormant multi branch project continiously building.
Browse files Browse the repository at this point in the history
JENKINS-63494 and JENKINS-64193

When scanning a multi branch project if the history of changes is outside of the `Head change query limit` then no changes are found.  The old behaviour used the latest change and triggered a build; continuously triggering dormant projects with each new submit.

The new behaviour uses the last build change if no changes are found (preventing a triggered build) or if there was no previous builds (in the case of new projects) then the latest changes is used to triggering a build.
  • Loading branch information
p4paul committed Jan 12, 2021
1 parent d5ba49d commit f4116bc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
33 changes: 16 additions & 17 deletions src/main/java/org/jenkinsci/plugins/p4/client/ClientHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private void updateClient() throws Exception {
sb.append(view.getRight());
sb.append("\n");
}
if ( iclient.getStream() != null ) {
if (iclient.getStream() != null) {
sb.append("... Stream: " + iclient.getStream());
sb.append("\n");
}
Expand Down Expand Up @@ -717,8 +717,8 @@ private List<String> buildPaths(Publish publish, String clientBase) {
}

String[] array = rawPaths.split("\n\\s*");
for(String a : array) {
if(a.startsWith("//")) {
for (String a : array) {
if (a.startsWith("//")) {
list.add(a);
} else {
list.add(clientBase + a);
Expand Down Expand Up @@ -1089,16 +1089,11 @@ public Changelist getChange(long id) throws Exception {
* workspace view up to the specified revision
*
* @param from From revision (change or label)
* @param to To revision (change or label)
* @param to To revision (change or label)
* @return Perforce change
* @throws Exception push up stack
*/
public long getClientHead(P4Ref from, P4Ref to) throws Exception {
// get last change in server
// This will also return shelved CLs
String latestChange = getConnection().getCounter("change");
long change = Long.parseLong(latestChange);

// build file revision spec
String path = "//" + iclient.getName() + "/...";
String revisionPath = buildRevisionLimit(path, from, to);
Expand All @@ -1111,25 +1106,29 @@ public long getClientHead(P4Ref from, P4Ref to) throws Exception {
List<IChangelistSummary> list = getConnection().getChangelists(files, opts);

if (!list.isEmpty() && list.get(0) != null) {
change = list.get(0).getId();
} else if ( to != null) {
change = to.getChange();
log("P4: no revisions under " + revisionPath + " using change: " + change);
long change = list.get(0).getId();
log("P4: found " + change + " revision in " + revisionPath);
return change;
} else {
log("P4: no revisions under " + revisionPath + " using latest change: " + change);
log("P4: no revisions under " + revisionPath);
return 0L;
}
return change;
}

/**
* Get the change number for the last change within the scope of the
* workspace view.
* workspace view. If there are no recent changes use the latest change.
*
* @return Perforce change
* @throws Exception push up stack
*/
public long getClientHead() throws Exception {
return getClientHead(null, null);
// get last change in server, may return shelved CLs
String latestChange = getConnection().getCounter("change");
long latest = Long.parseLong(latestChange);
P4Ref to = new P4ChangeRef(latest);
long head = getClientHead(null, to);
return (head == 0L) ? latest : head;
}

public List<IChangelistSummary> getPendingChangelists(boolean includeLongDescription, String clientName) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ protected List<String> toLines(String value) {
/**
* Get the Latest change for the path specified in P4SCMHead.
*
* @param p4 TempClient instance
* @param p4 TempClient instance
* @param head SCMHead
* @return The latest change as a P4SCMRevision object
* @throws Exception pushed up stack
Expand All @@ -338,12 +338,28 @@ public P4SCMRevision getRevision(TempClientHelper p4, P4SCMHead head) throws Exc
// Fetch last scan
P4SCMRevision last = getLastScan(head);

/* If 'Polling per Change' is disabled then get the latest change; otherwise use the latest change for the
* first scan then the oldest un-built change.
/* Calculate changes to use for SCMRevision:
* If 'Polling per Change' use the next change on the code-line, else the latest change for the code-line.
* If there are no changes within the the scan limits (set by `Head change query limit`) the change will be 0;
* in these cases return the last built change, or for now projects the latest change.
*/
if (last == null || !perChange) {
if (last == null) {
// possibly a new project or broken configuration...
change = findLatestChange(p4, path);
if (change == 0) {
// no changes? - use the latest and trigger a build
change = p4.getClientHead();
}
} else if (!perChange) {
// Typical case...
change = findLatestChange(p4, path);
if (change == 0) {
// JENKINS-63494 and JENKINS-64193
// dormant project outside the change limits; use last built change
change = last.getRef().getChange();
}
} else {
// Polling per Change - get next change.
change = findIncrementalChange(p4, path, last.getRef());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public void initialise() throws AbortException {
long rangeLimit = buildChange.getChange() - p4.getHeadLimit();
rangeLimit = (rangeLimit < 0) ? 1 : rangeLimit;
P4ChangeRef limitRef = (p4.getHeadLimit() == 0) ? null : new P4ChangeRef(rangeLimit);
buildChange = new P4ChangeRef(p4.getClientHead(limitRef, buildChange));
long headChange = p4.getClientHead(limitRef, buildChange);
buildChange = (headChange == 0) ? buildChange : new P4ChangeRef(headChange);
}

// add buildChange to list of changes to builds
Expand Down

0 comments on commit f4116bc

Please sign in to comment.