package org.jenkinsci.plugins.p4.trigger; import hudson.Extension; import hudson.Util; import hudson.console.AnnotatedLargeText; import hudson.model.Action; import hudson.model.Item; import hudson.model.Job; import hudson.scm.PollingResult; import hudson.scm.SCM; import hudson.triggers.SCMTrigger.SCMTriggerCause; import hudson.triggers.Trigger; import hudson.triggers.TriggerDescriptor; import hudson.util.StreamTaskListener; import jenkins.model.ParameterizedJobMixIn; import jenkins.triggers.SCMTriggerItem; import org.apache.commons.jelly.XMLOutput; import org.jenkinsci.plugins.p4.PerforceScm; import org.jenkinsci.plugins.p4.client.ConnectionHelper; import org.jenkinsci.plugins.p4.credentials.P4BaseCredentials; import org.kohsuke.stapler.DataBoundConstructor; import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.nio.charset.Charset; import java.util.Collection; import java.util.Collections; import java.util.logging.Logger; public class P4Trigger extends Trigger<Job<?, ?>> { @DataBoundConstructor public P4Trigger() { } @Extension public static class DescriptorImpl extends TriggerDescriptor { @Override public boolean isApplicable(Item item) { return item instanceof Job; } @Override public String getDisplayName() { return "Perforce triggered build."; } } public void poke(Job<?, ?> job, String port) throws IOException { // exit early if job does not match trigger if (!matchServer(job, port)) { return; } LOGGER.info("P4: poking: " + job.getName()); StreamTaskListener listener = new StreamTaskListener(getLogFile(job)); try { PrintStream log = listener.getLogger(); SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job); if (item == null) { LOGGER.severe("Trigger item not found."); return; } PollingResult pollResult = item.poll(listener); if (pollResult != null && pollResult.hasChanges()) { log.println("Changes found"); build(job); } else { log.println("No changes"); } } catch (Exception e) { String msg = "P4: Failed to record P4 trigger: "; e.printStackTrace(listener.error(msg)); LOGGER.severe(msg + e); } finally { listener.close(); } } /** * Schedule build * * @param job Jenkins Job * @throws IOException push up stack */ private void build(final Job<?, ?> job) throws IOException { SCMTriggerCause cause = new SCMTriggerCause(getLogFile(job)); @SuppressWarnings("rawtypes") ParameterizedJobMixIn pJob = new ParameterizedJobMixIn() { @Override protected Job asJob() { return job; } }; pJob.scheduleBuild(cause); } public File getLogFile(Job<?, ?> job) { if (job == null) { return null; } return new File(job.getRootDir(), "p4trigger.log"); } private boolean matchServer(Job<?, ?> job, String port) { //Get all the trigger for this Job SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job); if (item != null) { //As soon as we find a match, return for (SCM scmTrigger : item.getSCMs()) { PerforceScm p4scm = PerforceScm.convertToPerforceScm(scmTrigger); if (p4scm != null) { String id = p4scm.getCredential(); P4BaseCredentials credential = ConnectionHelper.findCredential(id); if (credential != null && credential.getP4port() != null && port.equals(credential.getP4port())) { return true; } } } } return false; } final static Logger LOGGER = Logger.getLogger(P4Trigger.class.getName()); /** * Perforce Trigger Log Action */ public final class P4TriggerAction implements Action { public Job<?, ?> getOwner() { return job; } public String getIconFileName() { return "clipboard.png"; } public String getDisplayName() { return "P4 Trigger Log"; } public String getUrlName() { return "P4TriggerLog"; } public String getLog() throws IOException { return Util.loadFile(getLogFile(job)); } /** * Writes the annotated log to the given output. * * @param out XML output * @throws IOException pudh up stack */ public void writeLogTo(XMLOutput out) throws IOException { new AnnotatedLargeText<P4TriggerAction>(getLogFile(job), Charset.defaultCharset(), true, this).writeHtmlTo(0, out.asWriter()); } } @Override public Collection<? extends Action> getProjectActions() { return Collections.singleton(new P4TriggerAction()); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 22326 | Paul Allen | Merging down using p4-jenkins | ||
#1 | 21940 | Paul Allen | Branching using p4-jenkins | ||
//guest/perforce_software/p4jenkins/main/src/main/java/org/jenkinsci/plugins/p4/trigger/P4Trigger.java | |||||
#9 | 21132 | Paul Allen | FindBugs: Null Pointer Checks | ||
#8 | 20226 | Paul Allen |
Support for Multiple SCMs Plugin. Optional dependency on multiple-scms and helper method for safely casting SCM object to PerforceSCM instance, especially in the scenario where the SCM object is a MultiSCM instance. JENKINS-32064 |
||
#7 | 20179 | Paul Allen | Javadoc fixes for java 8 builds. | ||
#6 | 19641 | Paul Allen |
Merge pull request #26 from Dohbedoh/hotfix/JENKINS-25249 [JENKINS-25249]: Fixed Null Pointer dereference |
||
#5 | 19630 | Paul Allen |
Merge pull request #24 from Dohbedoh/JENKINS-25249 Changes to make the P4 Trigger pipeline compatible. Added some tests of the P4 trigger for both Pipeline/Non Pipeline jobs. |
||
#4 | 19593 | Paul Allen | More minor fixes to satisfy FindBugs Analysis. | ||
#3 | 19294 | Paul Allen |
Schedule build on trigger for subscribed Jobs. JENKINS-33858 |
||
#2 | 15656 | Paul Allen |
Updated credentials to extend BaseStandardCredentials. Allows users to set the ID at creation. JENKINS-29702 |
||
#1 | 15403 | Paul Allen |
Perforce triggered polling BETA. Perforce triggers on a change-submit and sends a POST to the endpoint http://${JENKINS}/p4/change with the data: {"change":"12345","p4port":"localhost:1666"} Note: ‘change’ is not used (yet), but ‘p4port’ MUST match the credential in the Jenkins Job. JENKINS-25249 |