Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Polling Fix for use with quiet period.
Switched all uses of change/label to P4Revision object and implemented
Comparable.  The changes to build are now calculated at build time
(after the quiet period) not during the polling phase.

JENKINS-36883
  • Loading branch information
p4paul committed Sep 30, 2016
1 parent e78a41a commit f4c24f7
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 91 deletions.
39 changes: 30 additions & 9 deletions src/main/java/org/jenkinsci/plugins/p4/PerforceScm.java
Expand Up @@ -42,6 +42,7 @@
import org.jenkinsci.plugins.p4.client.ConnectionHelper;
import org.jenkinsci.plugins.p4.credentials.P4CredentialsImpl;
import org.jenkinsci.plugins.p4.filters.Filter;
import org.jenkinsci.plugins.p4.filters.FilterPerChangeImpl;
import org.jenkinsci.plugins.p4.filters.FilterPollMasterImpl;
import org.jenkinsci.plugins.p4.matrix.MatrixOptions;
import org.jenkinsci.plugins.p4.populate.Populate;
Expand Down Expand Up @@ -77,7 +78,7 @@ public class PerforceScm extends SCM {
private final Populate populate;
private final P4Browser browser;

private transient List<Integer> changes;
private transient List<P4Revision> incrementalChanges;
private transient P4Revision parentChange;

/**
Expand Down Expand Up @@ -107,8 +108,8 @@ public P4Browser getBrowser() {
return browser;
}

public List<Integer> getChanges() {
return changes;
public List<P4Revision> getIncrementalChanges() {
return incrementalChanges;
}

/**
Expand Down Expand Up @@ -331,10 +332,10 @@ private PollingResult pollWorkspace(EnvVars envVars, TaskListener listener, File
task.setLimit(pin);

// Execute remote task
changes = buildWorkspace.act(task);
incrementalChanges = buildWorkspace.act(task);

// Report changes
if (!changes.isEmpty()) {
if (!incrementalChanges.isEmpty()) {
return PollingResult.BUILD_NOW;
}
return PollingResult.NO_CHANGES;
Expand All @@ -360,14 +361,16 @@ public void checkout(Run<?, ?> run, Launcher launcher, FilePath buildWorkspace,
// Get workspace used for the Task
Workspace ws = task.setEnvironment(run, workspace, buildWorkspace);

// Set changes to build (used by polling), MUST clear after use.
ws = task.setNextChange(ws, changes);
changes = new ArrayList<Integer>();

// Set the Workspace and initialise
task.setWorkspace(ws);
task.initialise();

// Override build change if polling per change, MUST clear after use.
if(isIncremental()) {
task.setIncrementalChanges(incrementalChanges);
}
incrementalChanges = new ArrayList<P4Revision>();

// Add tagging action to build, enabling label support.
TagAction tag = new TagAction(run);
tag.setCredential(credential);
Expand Down Expand Up @@ -821,4 +824,22 @@ private static Node workspaceToNode(FilePath workspace) {
Jenkins jenkins = Jenkins.getInstance();
return jenkins;
}

/**
* Incremental polling filter is set
*
* @return true if set
*/
private boolean isIncremental() {
if (filter != null) {
for (Filter f : filter) {
if (f instanceof FilterPerChangeImpl) {
if (((FilterPerChangeImpl) f).isPerChange()) {
return true;
}
}
}
}
return false;
}
}
31 changes: 26 additions & 5 deletions src/main/java/org/jenkinsci/plugins/p4/changes/P4Revision.java
@@ -1,14 +1,13 @@
package org.jenkinsci.plugins.p4.changes;

import java.io.Serializable;

import org.jenkinsci.plugins.p4.client.ClientHelper;

import com.perforce.p4java.client.IClient;
import com.perforce.p4java.core.IChangelistSummary;
import com.perforce.p4java.exception.P4JavaException;
import org.jenkinsci.plugins.p4.client.ClientHelper;

public class P4Revision implements Serializable {
import java.io.Serializable;

public class P4Revision implements Serializable, Comparable {

private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -126,4 +125,26 @@ public int hashCode() {
hash = 89 * hash + (toString().hashCode());
return hash;
}

@Override
public int compareTo(Object obj) {
if(equals(obj)) {
return 0;
}
if (obj instanceof P4Revision) {
P4Revision rev = (P4Revision) obj;


if(rev.isLabel && rev.toString().equals("now"))
return -1;
if(isLabel && label.equals("now"))
return 1;
if(isLabel || rev.isLabel) {
return 0;
} else {
return change - rev.getChange();
}
}
throw new ClassCastException();
}
}
34 changes: 16 additions & 18 deletions src/main/java/org/jenkinsci/plugins/p4/client/ClientHelper.java
Expand Up @@ -893,18 +893,17 @@ public int getClientHead() throws Exception {
* @return List of changes
* @throws Exception push up stack
*/
public List<Integer> listChanges(P4Revision from, P4Revision to) throws Exception {
public List<P4Revision> listChanges(P4Revision from, P4Revision to) throws Exception {
// return empty array, if from and to are equal, or Perforce will report
// a change
if (from.equals(to)) {
return new ArrayList<Integer>();
return new ArrayList<P4Revision>();
}

String ws = "//" + iclient.getName() + "/...@" + from + "," + to;
List<Integer> list = listChanges(ws);
List<P4Revision> list = listChanges(ws);
if (!from.isLabel()) {
Object obj = from.getChange();
list.remove(obj);
list.remove(from);
}
return list;
}
Expand All @@ -917,12 +916,11 @@ public List<Integer> listChanges(P4Revision from, P4Revision to) throws Exceptio
* @return List of changes
* @throws Exception push up stack
*/
public List<Integer> listChanges(P4Revision from) throws Exception {
public List<P4Revision> listChanges(P4Revision from) throws Exception {
String ws = "//" + iclient.getName() + "/...@" + from + ",now";
List<Integer> list = listChanges(ws);
List<P4Revision> list = listChanges(ws);
if (!from.isLabel()) {
Object obj = from.getChange();
list.remove(obj);
list.remove(from);
}
return list;
}
Expand All @@ -933,13 +931,13 @@ public List<Integer> listChanges(P4Revision from) throws Exception {
* @return List of changes
* @throws Exception push up stack
*/
public List<Integer> listChanges() throws Exception {
public List<P4Revision> listChanges() throws Exception {
String ws = "//" + iclient.getName() + "/...";
return listChanges(ws);
}

private List<Integer> listChanges(String ws) throws Exception {
List<Integer> list = new ArrayList<Integer>();
private List<P4Revision> listChanges(String ws) throws Exception {
List<P4Revision> list = new ArrayList<P4Revision>();

List<IFileSpec> spec = FileSpecBuilder.makeFileSpecList(ws);
GetChangelistsOptions opts = new GetChangelistsOptions();
Expand All @@ -951,7 +949,7 @@ private List<Integer> listChanges(String ws) throws Exception {
if (c != null && c.getId() != -1) {
// don't add change entries already in the list
if (!(list.contains(c.getId()))) {
list.add(c.getId());
list.add(new P4Revision(c.getId()));
}
}
}
Expand All @@ -969,7 +967,7 @@ private List<Integer> listChanges(String ws) throws Exception {
* @return List of changes
* @throws Exception push up stack
*/
public List<Integer> listHaveChanges(P4Revision from) throws Exception {
public List<P4Revision> listHaveChanges(P4Revision from) throws Exception {
if (from.getChange() > 0) {
log("P4: Polling with range: " + from + ",now");
return listChanges(from);
Expand All @@ -988,7 +986,7 @@ public List<Integer> listHaveChanges(P4Revision from) throws Exception {
* @return List of changes
* @throws Exception push up stack
*/
public List<Integer> listHaveChanges(P4Revision from, P4Revision changeLimit) throws Exception {
public List<P4Revision> listHaveChanges(P4Revision from, P4Revision changeLimit) throws Exception {
if (from.getChange() > 0) {
log("P4: Polling with range: " + from + "," + changeLimit);
return listChanges(from, changeLimit);
Expand All @@ -999,10 +997,10 @@ public List<Integer> listHaveChanges(P4Revision from, P4Revision changeLimit) th
return listHaveChanges(fileSpec);
}

private List<Integer> listHaveChanges(String fileSpec) throws Exception {
private List<P4Revision> listHaveChanges(String fileSpec) throws Exception {
log("P4: Polling with cstat: " + fileSpec);

List<Integer> haveChanges = new ArrayList<Integer>();
List<P4Revision> haveChanges = new ArrayList<P4Revision>();
Map<String, Object>[] map;
map = connection.execMapCmd("cstat", new String[]{fileSpec}, null);

Expand All @@ -1012,7 +1010,7 @@ private List<Integer> listHaveChanges(String fileSpec) throws Exception {
if (status.startsWith("have")) {
String value = (String) entry.get("change");
int change = Integer.parseInt(value);
haveChanges.add(change);
haveChanges.add(new P4Revision(change));
}
}
}
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/org/jenkinsci/plugins/p4/tasks/AbstractTask.java
Expand Up @@ -8,15 +8,13 @@
import org.jenkinsci.plugins.p4.client.ClientHelper;
import org.jenkinsci.plugins.p4.client.ConnectionHelper;
import org.jenkinsci.plugins.p4.credentials.P4BaseCredentials;
import org.jenkinsci.plugins.p4.review.ReviewProp;
import org.jenkinsci.plugins.p4.workspace.TemplateWorkspaceImpl;
import org.jenkinsci.plugins.p4.workspace.Workspace;

import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

Expand Down Expand Up @@ -128,17 +126,6 @@ public Workspace setEnvironment(Run<?, ?> run, Workspace wsType, FilePath buildW
return ws;
}

public Workspace setNextChange(Workspace ws, List<Integer> changes) {
// Set label for changes to build
if (changes != null) {
if (!changes.isEmpty()) {
String label = Integer.toString(changes.get(0));
ws.getExpand().set(ReviewProp.LABEL.toString(), label);
}
}
return ws;
}

/**
* Remote execute to find hostname.
*
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/org/jenkinsci/plugins/p4/tasks/CheckoutTask.java
Expand Up @@ -206,9 +206,9 @@ private int getReview(Workspace workspace) {
return review;
}

public List<Integer> getChanges(P4Revision last) {
public List<P4Revision> getChanges(P4Revision last) {

List<Integer> changes = new ArrayList<Integer>();
List<P4Revision> changes = new ArrayList<P4Revision>();

// Add changes to this build.
ClientHelper p4 = getConnection();
Expand All @@ -225,7 +225,7 @@ public List<Integer> getChanges(P4Revision last) {

// Include shelf if a review
if (status == CheckoutStatus.SHELVED) {
changes.add(review);
changes.add(new P4Revision(review));
}

return changes;
Expand All @@ -246,10 +246,10 @@ public List<P4ChangeEntry> getChangesFull(P4Revision last) {
}

// add all changes to list
List<Integer> changes = p4.listChanges(last, buildChange);
for (Integer change : changes) {
List<P4Revision> changes = p4.listChanges(last, buildChange);
for (P4Revision change : changes) {
P4ChangeEntry cl = new P4ChangeEntry();
IChangelistSummary summary = p4.getChangeSummary(change);
IChangelistSummary summary = p4.getChangeSummary(change.getChange());
cl.setChange(p4, summary);
changesFull.add(cl);
}
Expand Down Expand Up @@ -304,6 +304,13 @@ public void setBuildChange(P4Revision parentChange) {
buildChange = parentChange;
}

public void setIncrementalChanges(List<P4Revision> changes) {
if (changes != null && !changes.isEmpty()) {
P4Revision lowest = changes.get(changes.size() - 1);
buildChange = lowest;
}
}

public int getReview() {
return review;
}
Expand Down

0 comments on commit f4c24f7

Please sign in to comment.