package org.jenkinsci.plugins.p4; import hudson.AbortException; import hudson.FilePath.FileCallable; import hudson.model.TaskListener; import hudson.remoting.VirtualChannel; import java.io.File; import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; import org.jenkinsci.plugins.p4.client.ClientHelper; import org.jenkinsci.plugins.p4.client.ConnectionHelper; import org.jenkinsci.plugins.p4.credentials.P4StandardCredentials; import org.jenkinsci.plugins.p4.populate.Populate; import org.jenkinsci.plugins.p4.workspace.Workspace; public class CheckoutTask implements FileCallable<Boolean>, Serializable { private static final long serialVersionUID = 1L; private static Logger logger = Logger.getLogger(CheckoutTask.class .getName()); private final P4StandardCredentials credential; private final TaskListener listener; private final String client; private CheckoutStatus status; private int head; private Object buildChange; private int review; private Populate populate; /** * Constructor * * @param config * - Server connection details * @param auth * - Server login details */ public CheckoutTask(String credentialID, Workspace config, TaskListener listener) { this.credential = ConnectionHelper.findCredential(credentialID); this.listener = listener; this.client = config.getFullName(); } public void setBuildOpts(Workspace workspace) throws AbortException { ClientHelper p4 = new ClientHelper(credential, listener, client); try { // setup the client workspace to use for the build. if (!p4.setClient(workspace)) { String err = "Undefined workspace: " + workspace.getFullName(); logger.severe(err); listener.error(err); throw new AbortException(err); } // fetch and calculate change to sync to or review to unshelve. status = getStatus(workspace); head = p4.getClientHead(); review = getReview(workspace); buildChange = getBuildChange(workspace); } catch (Exception e) { String err = "Unable to setup workspace: " + e; logger.severe(err); listener.error(err); throw new AbortException(err); } finally { p4.disconnect(); } } public void setPopulateOpts(Populate populate) { this.populate = populate; } /** * Invoke sync on build node (master or remote node). * * @return true if updated, false if no change. */ public Boolean invoke(File workspace, VirtualChannel channel) throws IOException { boolean success = true; ClientHelper p4 = new ClientHelper(credential, listener, client); try { // Tidy the workspace before sync/build success &= p4.tidyWorkspace(populate); // Sync workspace to label, head or specified change success &= p4.syncFiles(buildChange, populate); // Unshelve review if specified if (status == CheckoutStatus.SHELVED) { success &= p4.unshelveFiles(review); } } catch (Exception e) { String err = "Unable to update workspace: " + e; logger.severe(err); listener.getLogger().println(err); e.printStackTrace(); success = false; } finally { p4.disconnect(); } return success; } /** * Get the build status for the parameter map. * * @param map * @return */ private CheckoutStatus getStatus(Workspace workspace) { CheckoutStatus status = CheckoutStatus.HEAD; String value = workspace.get("status"); if (value != null && !value.isEmpty()) { status = CheckoutStatus.parse(value); } return status; } /** * Get the sync point from the parameter map. Returns the head if no change * found in the map. * * @param map * @return */ private Object getBuildChange(Workspace workspace) { // Use head as the default Object build = this.head; // if change is specified then update String change = workspace.get("change"); if (change != null && !change.isEmpty()) { try { build = Integer.parseInt(change); } catch (NumberFormatException e) { } } // if label is specified then update String label = workspace.get("label"); if (label != null && !label.isEmpty()) { build = label; } return build; } /** * Get the unshelve point from the parameter map. * * @param map * @return */ private int getReview(Workspace workspace) { int review = 0; String value = workspace.get("review"); if (value != null && !value.isEmpty()) { try { review = Integer.parseInt(value); } catch (NumberFormatException e) { } } return review; } public List<Object> getChanges(Object last) { List<Object> changes = new ArrayList<Object>(); // Add changes to this build. ClientHelper p4 = new ClientHelper(credential, listener, client); try { changes = p4.listChanges(last, buildChange); } catch (Exception e) { String err = "Unable to get changes: " + e; logger.severe(err); listener.getLogger().println(err); e.printStackTrace(); } finally { p4.disconnect(); } // Include shelf if a review if (status == CheckoutStatus.SHELVED) { changes.add(review); } return changes; } public CheckoutStatus getStatus() { return status; } public Object getBuildChange() { if (status == CheckoutStatus.SHELVED) { return review; } return buildChange; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 10798 | nakagawat | Add p4-jenkins in my guest storage | ||
//guest/paul_allen/dev/p4-jenkins/p4-client/src/main/java/org/jenkinsci/plugins/p4/CheckoutTask.java | |||||
#8 | 9854 | Paul Allen | Upgrade plugin (1.569) to support Job/Run | ||
#7 | 9840 | Paul Allen |
Minor improvments. - Protection against undefined workspaces - Logging improvments for Perforce - Performance for Perforce connections |
||
#6 | 9834 | Paul Allen |
Support pinned builds with SCM Polling. Supports the Populate option 'Pin build at Perforce Label' (or change), and only triggers a build if the label is changed. |
||
#5 | 9793 | Paul Allen |
Change list reporting. Only report the last change list for the first build; prevent a flood of changes. For later builds; reports submitted changes since the last build. |
||
#4 | 9782 | Paul Allen |
Limit change-lists report on first/clean builds. If during a build the workspace has no sync'ed changes from a previous build (e.g. cstat reports no 'have' entries), then only the last change 'needed' is reported. |
||
#3 | 9750 | Paul Allen |
Add support for Environment variables. Includes cleanup of Perforce exception reporting for Connections and Workspaces. |
||
#2 | 9736 | Paul Allen |
Custom build args for Workspace names. Takes `${parameter}` and replaces it with the value if defined or uses the tag (`parameter` in this case). Includes substitution for a space ` ` with `_` as per P4 behaviour. |
||
#1 | 9672 | Paul Allen | Refactor name from 'p4_client' to 'p4'. | ||
//guest/paul_allen/dev/p4-jenkins/p4-client/src/main/java/org/jenkinsci/plugins/p4_client/CheckoutTask.java | |||||
#14 | 9371 | Paul Allen | Fix issue when label could be empty (called by Swarm or Review URL). | ||
#13 | 9115 | Paul Allen |
Initial implementation of workspace Cleanup and Sync options. - Includes 3 modes: Automatic Clean/Sync, Force Clean/Sync, Sync Only Automatic Clean/Sync Uses reconcile to clean up workspace and sync changes. Force Clean/Sync Force sync of all files (does not remove files yet...) Sync Only Normal sync with no cleanup TODO: - remove of files in Force Clean/Sync mode - Inline help - Update docs - Add unit/functional tests |
||
#12 | 9069 | Paul Allen |
Adding initial support for tagging Jenkins builds as Perforce Automatic Labels. Only implements TagAction (manual labels); TagNotifier and test cases TODO. |
||
#11 | 9055 | Paul Allen |
Label support. Build at a label using the pram 'label'. This includes adding the label to the ChangeEntry, building the change reports and Browser links to Swarm. (TPI-102) |
||
#10 | 8969 | Paul Allen |
Adds all contributing change-lists for the build to the change log (using p4 cstat). - Includes exception logging for server connection to the Jenkins console. |
||
#9 | 8940 | Paul Allen |
Major refactor for the ConnectionHelper class to simplify serialisation. Fixed remote Jenkins JNLP slave connection issue. ClientHelper now extends ConnectionHelper and takes on all methods that require a client workspace. P4StandardCredentials is sent to the remote node instead of Credentials ID due to an issue accessing the Credentials store over a remote connection. For simplicity Client ID (workspace name) is serialised instead of the Workspace object. |
||
#8 | 8926 | Paul Allen |
Bug fix when unshelving files with the Template workspace option. p4-java was unable to switch the worksapce view (without the -f flag) if there were files open. - Improved logging (as this error was hidden) - Added test case for this condition. |
||
#7 | 8924 | Paul Allen | Sync workspace to last change in workspace (not depot). | ||
#6 | 8915 | Paul Allen |
Support for ChangeLog and RepoBrowser. - Added RepoBrowser for Swarm (porting the others should be easy) - ChangeLog XML file now only stores the changelist number all other information is fetched from Perforce |
||
#5 | 8765 | Paul Allen | Update status to include SUBMITTED and sync to change. | ||
#4 | 8762 | Paul Allen |
Console Ouptut logging for SCM build steps. - Removed SLF4J and used old style logger (matching Jenkins) - Set Client Host filed to null, Jenkins sometimes gives an IP address. - Test p4java setps in SCM tasks for success(true/false) |
||
#3 | 8725 | Paul Allen |
Minor fixes: - Improved tidy workspace method - Fix for template clients - After an unshelf revert -k files - Minor bug in CheckoutTask - Clear callback urls to prevet reuse |
||
#2 | 8715 | Paul Allen |
#review-8716 [Basic Swarm-Jenkins cycle DONE] Support Job's build notification and then POST's the pass/fail call-back URLs to Swarm. |
||
#1 | 8694 | Paul Allen | Added support for unshelve and revert -w behaviour for builds. |