package org.jenkinsci.plugins.p4.tagging; import java.util.logging.Logger; import org.jenkinsci.plugins.p4.workspace.Expand; import org.kohsuke.stapler.DataBoundConstructor; import hudson.EnvVars; import hudson.Extension; import hudson.FilePath; import hudson.Launcher; import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.BuildListener; import hudson.model.Result; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.BuildStepMonitor; import hudson.tasks.Notifier; import hudson.tasks.Publisher; import jenkins.model.Jenkins; public class TagNotifier extends Notifier { private static final Logger logger = Logger.getLogger(TagNotifier.class.getName()); public final String rawLabelName; public final String rawLabelDesc; public final boolean onlyOnSuccess; @DataBoundConstructor public TagNotifier(String rawLabelName, String rawLabelDesc, boolean onlyOnSuccess) { this.rawLabelName = rawLabelName; this.rawLabelDesc = rawLabelDesc; this.onlyOnSuccess = onlyOnSuccess; } @Override public BuildStepMonitor getRequiredMonitorService() { return BuildStepMonitor.NONE; } @Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException { // return early if label not required if (onlyOnSuccess && build.getResult() != Result.SUCCESS) { return true; } try { // Expand label name and description EnvVars env = build.getEnvironment(listener); Expand expand = new Expand(env); String name = expand.format(rawLabelName, false); String description = expand.format(rawLabelDesc, false); // Get TagAction and check for promoted builds TagAction tagAction = getTagAction(env, build); // Label with TagAction FilePath workspace = build.getWorkspace(); tagAction.labelBuild(listener, name, description, workspace); } catch (Exception e) { e.printStackTrace(); } return true; } private TagAction getTagAction(EnvVars env, AbstractBuild<?, ?> build) { TagAction tagAction = (TagAction) build.getAction(TagAction.class); // process promoted builds? if (tagAction == null) { String jobName = env.get("PROMOTED_JOB_NAME"); if (jobName == null || jobName.isEmpty()) { logger.warning("No tag information; not a promotion job."); return tagAction; } String buildNumber = env.get("PROMOTED_NUMBER"); if (buildNumber == null || buildNumber.isEmpty()) { logger.warning("No tag information; not a promotion job."); return tagAction; } AbstractProject<?, ?> project; Jenkins j = Jenkins.getInstance(); if (j == null) { logger.warning("Jenkins instance is null!"); return tagAction; } project = j.getItemByFullName(jobName, AbstractProject.class); if (project == null) { logger.warning("No project; is it a valid Perforce job?"); return tagAction; } int buildNum = Integer.parseInt(buildNumber); build = (AbstractBuild<?, ?>) project.getBuildByNumber(buildNum); if (build == null) { logger.warning("No build number; is it a valid Perforce job?"); return tagAction; } tagAction = (TagAction) build.getAction(TagAction.class); if (tagAction == null) { logger.warning("No tag information; is it a valid Perforce job?"); return tagAction; } } return tagAction; } public static DescriptorImpl descriptor() { Jenkins j = Jenkins.getInstance(); if (j != null) { return j.getDescriptorByType(TagNotifier.DescriptorImpl.class); } return null; } @Extension public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> { @Override public boolean isApplicable(Class<? extends AbstractProject> jobType) { return true; } @Override public String getDisplayName() { return "Perforce: Label build"; } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 23685 | Paul Allen | Copy main -> dev | ||
#1 | 20641 | Paul Allen | Copy main -> dev (jenkinsci/p4-plugin) | ||
//guest/perforce_software/p4jenkins/main/src/main/java/org/jenkinsci/plugins/p4/tagging/TagNotifier.java | |||||
#8 | 19880 | Paul Allen | Use system logger to avoid serialisation of listener. | ||
#7 | 19762 | Paul Allen | Missing Override annotations. | ||
#6 | 19593 | Paul Allen | More minor fixes to satisfy FindBugs Analysis. | ||
#5 | 15379 | Paul Allen |
Ground-work for Workflow-DSL (Split out Sven’s DSL changes to the core) @sven_erik_knop |
||
#4 | 13994 | Paul Allen | Make TaskListener as transient. | ||
#3 | 13701 | Paul Allen |
Move Labelling into a Task. Logging improvements. |
||
#2 | 11232 | Paul Allen | Update display name to match publish. | ||
#1 | 9690 | Paul Allen |
[Branching using p4-jenkins] Release 1.0.1 |
||
//guest/paul_allen/dev/p4-jenkins/p4-client/src/main/java/org/jenkinsci/plugins/p4/tagging/TagNotifier.java | |||||
#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/tagging/TagNotifier.java | |||||
#2 | 9077 | Paul Allen |
Added support for automatic Labels as a post-build Action. Ported original code for promoted builds, but not tested. |
||
#1 | 9069 | Paul Allen |
Adding initial support for tagging Jenkins builds as Perforce Automatic Labels. Only implements TagAction (manual labels); TagNotifier and test cases TODO. |