package org.jenkinsci.plugins.p4.tagging; import hudson.AbortException; import hudson.EnvVars; import hudson.Extension; import hudson.FilePath; import hudson.Launcher; import hudson.model.Result; import hudson.model.TaskListener; import hudson.model.AbstractProject; import hudson.model.Job; import hudson.model.Run; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.BuildStepMonitor; import hudson.tasks.Notifier; import hudson.tasks.Publisher; import java.io.IOException; import java.util.logging.Logger; import jenkins.model.Jenkins; import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.p4.workspace.Expand; import org.kohsuke.stapler.DataBoundConstructor; @SuppressWarnings("unchecked") public class TagNotifier extends Notifier implements SimpleBuildStep { protected static final Logger LOGGER = Logger.getLogger(TagNotifier.class .getName()); public final String rawLabelName; public final String rawLabelDesc; public final boolean onlyOnSuccess; private transient TaskListener listener; @DataBoundConstructor public TagNotifier(String rawLabelName, String rawLabelDesc, boolean onlyOnSuccess) { this.rawLabelName = rawLabelName; this.rawLabelDesc = rawLabelDesc; this.onlyOnSuccess = onlyOnSuccess; } public BuildStepMonitor getRequiredMonitorService() { return BuildStepMonitor.NONE; } @Override public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException { // return early if label not required if (onlyOnSuccess && run.getResult() != Result.SUCCESS) { return; } try { // Expand label name and description EnvVars env = run.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, run); // Label with TagAction tagAction.labelBuild(listener, name, description, workspace); } catch (Exception e) { final String err = "P4: Could not label: " + e; log(err); throw new AbortException(err); } } private TagAction getTagAction(EnvVars env, Run<?, ?> run) { TagAction tagAction = (TagAction) run.getAction(TagAction.class); // process promoted builds? if (tagAction == null) { String jobName = env.get("PROMOTED_JOB_NAME"); if (jobName == null || jobName.isEmpty()) { log("No tag information; not a promotion job."); return tagAction; } String buildNumber = env.get("PROMOTED_NUMBER"); if (buildNumber == null || buildNumber.isEmpty()) { log("No tag information; not a promotion job."); return tagAction; } Jenkins j = Jenkins.getInstance(); Job<?, ?> job = j.getItemByFullName(jobName, Job.class); int buildNum = Integer.parseInt(buildNumber); run = job.getBuildByNumber(buildNum); tagAction = run.getAction(TagAction.class); if (tagAction == null) { log("No tag information; is it a valid Perforce job?"); return tagAction; } } return tagAction; } private void log(String msg) { if (listener == null) { return; } listener.getLogger().println(msg); } public static DescriptorImpl descriptor() { return Jenkins.getInstance().getDescriptorByType( TagNotifier.DescriptorImpl.class); } @Extension public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> { @SuppressWarnings("rawtypes") @Override public boolean isApplicable(Class<? extends AbstractProject> jobType) { return true; } @Override public String getDisplayName() { return "Perforce: Label build"; } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#4 | 14132 | Sven Erik Knop | Tagging works from the workflow, URL for the tag object still broken. | ||
#3 | 14012 | Sven Erik Knop | Merging using sknop.p4jenkins-main | ||
#2 | 13760 | Sven Erik Knop | merged changes from main | ||
#1 | 12955 | Sven Erik Knop | Branching using sknop.p4jenkins-main | ||
//guest/perforce_software/p4jenkins/main/src/main/java/org/jenkinsci/plugins/p4/tagging/TagNotifier.java | |||||
#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. |