diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..2be05c55 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,7 @@ +Contributing +=============== + +* If you want to add a new feature or to fix a defect, just submit a pull request. + The [pull request template](.github/PULL_REQUEST_TEMPLATE.md) documents usual expectations from pull requests in this project +* Making new releases is covered in [the "Hosting Plugins" wiki page](https://wiki.jenkins-ci.org/display/JENKINS/Hosting+Plugins). +* There are a lot a issues and features that need attention, the JIRA bug tracker is listed on [the wiki page](https://wiki.jenkins-ci.org/display/JENKINS/Promoted+Builds+Plugin). diff --git a/Jenkinsfile b/Jenkinsfile index bf836741..b3b7d450 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,2 +1,2 @@ // Builds a module using https://github.com/jenkins-infra/pipeline-library -buildPlugin() +buildPlugin(configurations: buildPlugin.recommendedConfigurations()) diff --git a/README.md b/README.md index 0b113f8d..66cdf9c2 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ -See the wiki at: +Promoted Builds Plugin +====================== + +[![Join the chat at https://gitter.im/jenkinsci/promoted-builds-plugin](https://badges.gitter.im/jenkinsci/promoted-builds-plugin.svg)](https://gitter.im/jenkinsci/promoted-builds-plugin?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Jenkins Plugin](https://img.shields.io/jenkins/plugin/v/promoted-builds.svg)](https://plugins.jenkins.io/promoted-builds) +[![GitHub release](https://img.shields.io/github/release/jenkinsci/promoted-builds-plugin.svg?label=release)](https://github.com/jenkinsci/promoted-builds-plugin/releases/latest) +[![Jenkins Plugin Installs](https://img.shields.io/jenkins/plugin/i/promoted-builds.svg?color=blue)](https://plugins.jenkins.io/promoted-builds) This plugin allows you to distinguish good builds from bad builds by introducing the notion of 'promotion'.Put simply, a promoted build is a @@ -179,12 +185,6 @@ freeStyleJob('test-job') { } ``` -## Contributing - -* Making new releases is covered in [the "Hosting Plugins" wiki page](https://wiki.jenkins-ci.org/display/JENKINS/Hosting+Plugins). -* If you want to send pull requests or work on some fixes, check [this document](http://jenkins-ci.org/pull-request-greeting). -* There are a lot a issues and features that need attention, the JIRA bug tracker is listed on [the wiki page](https://wiki.jenkins-ci.org/display/JENKINS/Promoted+Builds+Plugin). - [1]: https://wiki.jenkins-ci.org/display/JENKINS/Copy+Artifact+Plugin [2]: https://github.com/jenkinsci/promoted-builds-plugin/blob/master/src/main/java/hudson/plugins/promoted_builds/Promotion.java diff --git a/README.txt b/README.txt deleted file mode 100644 index 77dacce4..00000000 --- a/README.txt +++ /dev/null @@ -1,3 +0,0 @@ -This plugin allows you to distinguish good builds from bad builds by introducing the notion of 'promotion'. - -See http://wiki.jenkins-ci.org/display/JENKINS/Promoted+Builds+Plugin diff --git a/pom.xml b/pom.xml index 300bc398..a8d0454f 100644 --- a/pom.xml +++ b/pom.xml @@ -4,20 +4,21 @@ org.jenkins-ci.plugins plugin - 3.4 + 3.45 promoted-builds - 3.3-SNAPSHOT + 3.4-SNAPSHOT hpi Jenkins promoted builds plugin https://wiki.jenkins.io/display/JENKINS/Promoted+Builds+Plugin - 1.625.3 - 7 + 2.138.4 + 8 Max + 3.0 @@ -61,58 +62,54 @@ - - - - org.jenkins-ci.tools - maven-hpi-plugin - true - - - 3.0 - - - - - org.jenkins-ci.main maven-plugin - 3.0 + 3.2 true + + + commons-net + commons-net + + org.jenkins-ci.plugins apache-httpcomponents-client-4-api - 4.5.3-2.0 + 4.5.3-2.1 org.jenkins-ci.plugins junit 1.20 + + org.jenkins-ci.plugins + mailer + 1.20 + com.sonyericsson.hudson.plugins.rebuild rebuild - 1.25 + 1.31 true org.jenkins-ci.plugins structs - 1.7 + 1.19 org.hamcrest hamcrest-core - 1.3 + 2.1 test org.mockito mockito-core - 1.8.5 test @@ -121,6 +118,16 @@ 2.0 true + + org.jenkins-ci.plugins + credentials + 2.1.4 + + + org.jenkins-ci.plugins + ssh-credentials + 1.13 + hudson.plugins project-inheritance @@ -130,13 +137,19 @@ org.jenkins-ci.plugins job-dsl - 1.44 + 1.74 true + + + org.codehaus.groovy + groovy-all + + org.jenkins-ci.plugins script-security - 1.26 + 1.54 org.jvnet.hudson @@ -174,8 +187,38 @@ org.jenkins-ci.plugins matrix-project - 1.11 + 1.12 + test + + + org.jenkins-ci.plugins.workflow + workflow-step-api + 2.20 + true + + + org.jenkins-ci.plugins.workflow + workflow-support + 3.2 + true + + + org.jenkins-ci.plugins.workflow + workflow-cps + 2.9 + test + + + org.jenkins-ci.plugins.workflow + workflow-job + 2.32 + test + + + org.jenkins-ci.plugins.workflow + workflow-api + 2.32 test - + \ No newline at end of file diff --git a/src/main/java/hudson/plugins/promoted_builds/AddPromotionBadge.java b/src/main/java/hudson/plugins/promoted_builds/AddPromotionBadge.java new file mode 100644 index 00000000..e100e16d --- /dev/null +++ b/src/main/java/hudson/plugins/promoted_builds/AddPromotionBadge.java @@ -0,0 +1,120 @@ +package hudson.plugins.promoted_builds; + +import hudson.EnvVars; +import hudson.Extension; +import hudson.FilePath; +import hudson.Launcher; +import hudson.model.*; +import hudson.tasks.*; +import jenkins.tasks.SimpleBuildStep; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.DataBoundSetter; + +import java.io.IOException; +import java.io.PrintStream; + +public class AddPromotionBadge extends Recorder implements SimpleBuildStep { + + private String boxName = "Promotion Badges"; + private boolean gold; + private boolean silver; + + @Extension + public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); + + @DataBoundConstructor + public AddPromotionBadge(String boxName, boolean gold, boolean silver){ + this.boxName = boxName; + this.gold = gold; + this.silver = silver; + } + + public String getBoxName(){ + return boxName; + } + + @DataBoundSetter + public void setBoxName(String boxName){ + this.boxName = boxName; + } + + + public boolean getGold(){ + return gold; + } + + @DataBoundSetter + public void setGold(boolean gold){ + this.gold = gold; + } + + public boolean getSilver(){ + return silver; + } + + @DataBoundSetter + public void setSilver(boolean silver){ + this.silver = silver; + } + + public BuildStepMonitor getRequiredMonitorService(){ + return BuildStepMonitor.NONE; + } + + + @Override + public void perform(Run build, FilePath workspace, Launcher launcher, final TaskListener listener) + throws InterruptedException, IOException { + + PrintStream logger = listener.getLogger(); + logger.println("Started initialization"); + + Result buildResult = build.getResult(); + + if (Result.SUCCESS.equals(buildResult)) { + logger.println("The pipeline build is successful!!"); + + if(gold == true){ + // How to assign actual badge icons?? + } + + } + + String temp; + + if(build instanceof AbstractBuild){ + EnvVars vars = build.getEnvironment(listener); + vars.overrideAll(((AbstractBuild)build).getBuildVariables()); + temp = vars.expand(boxName); + }else{ + temp = boxName; + } + + logger.println("Starting the pipeline build"); + logger.println("Will assign badges once the pipeline build is successful in promotion"); + + } + + + public static final class DescriptorImpl extends BuildStepDescriptor{ + + private DescriptorImpl(){ + super(AddPromotionBadge.class); + } + + public String getDisplayName(){ + return "Add Promotion Badges"; + } + + + @Override + public boolean isApplicable(Class a){ + return true; + } + + } + + + + +} diff --git a/src/main/java/hudson/plugins/promoted_builds/JobPropertyImpl.java b/src/main/java/hudson/plugins/promoted_builds/JobPropertyImpl.java index 5413d0ce..b12dadf0 100644 --- a/src/main/java/hudson/plugins/promoted_builds/JobPropertyImpl.java +++ b/src/main/java/hudson/plugins/promoted_builds/JobPropertyImpl.java @@ -294,7 +294,7 @@ public List getActiveItems() { /** @see ItemGroupMixIn#createProjectFromXML */ public PromotionProcess createProcessFromXml(final String name, InputStream xml) throws IOException { owner.checkPermission(Item.CONFIGURE); // CREATE is ItemGroup-scoped and owner is not an ItemGroup - Jenkins.getInstance().getProjectNamingStrategy().checkName(name); + Jenkins.get().getProjectNamingStrategy().checkName(name); if (getItem(name) != null) { throw new IllegalArgumentException(owner.getDisplayName() + " already contains an item '" + name + "'"); } diff --git a/src/main/java/hudson/plugins/promoted_builds/Promotion.java b/src/main/java/hudson/plugins/promoted_builds/Promotion.java index 394e1adc..31c6c3a5 100644 --- a/src/main/java/hudson/plugins/promoted_builds/Promotion.java +++ b/src/main/java/hudson/plugins/promoted_builds/Promotion.java @@ -23,6 +23,7 @@ import hudson.model.Run; import hudson.model.User; import hudson.plugins.promoted_builds.conditions.ManualCondition; +import hudson.plugins.promoted_builds.pipeline.PromotionRun; import hudson.plugins.promoted_builds.util.JenkinsHelper; import hudson.security.Permission; import hudson.security.PermissionGroup; @@ -60,11 +61,11 @@ import org.kohsuke.stapler.StaplerResponse; /** - * Records a promotion process. + * Records a promotion process for {@link AbstractProject}s. * * @author Kohsuke Kawaguchi */ -public class Promotion extends AbstractBuild { +public class Promotion extends AbstractBuild implements PromotionRun { public Promotion(PromotionProcess job) throws IOException { super(job); @@ -112,7 +113,8 @@ public EnvVars getEnvironment(TaskListener listener) throws IOException, Interru EnvVars e = super.getEnvironment(listener); // Augment environment with target build's information - String rootUrl = JenkinsHelper.getInstance().getRootUrl(); + String rootUrl = Jenkins.get().getRootUrl(); + //TODO: Refactor to Run AbstractBuild target = getTarget(); if(rootUrl!=null) e.put("PROMOTED_URL",rootUrl+target.getUrl()); @@ -162,7 +164,7 @@ public EnvVars getEnvironment(TaskListener listener) throws IOException, Interru } // Allow the promotion status to contribute to build environment - getStatus().buildEnvVars(this, e); + getStatus().buildEnvVars(this, e, listener); return e; } @@ -211,11 +213,11 @@ public String getUserName() { */ @Nonnull public String getUserId() { - // Deprecated, but we still want to support it in order to maintain the compatiibility + // Deprecated, but we still want to support it in order to maintain the compatibility // We try to convert the cause to the user ID by using a search by the full name, not reliable final UserCause userCause = getCause(UserCause.class); final String nameFromUserCause = userCause != null ? userCause.getUserName(): null; - final User user = nameFromUserCause != null ? User.get(nameFromUserCause, false, null) : null; + final User user = nameFromUserCause != null ? User.get(nameFromUserCause, false, Collections.emptyMap()) : null; if (user != null) { return user.getId(); } @@ -239,6 +241,21 @@ public String getUserId() { return User.getUnknown().getId(); } + @Nonnull + @Override + public Run getPromotedRun() { + return getTarget(); + } + + + @Nonnull + @Override + public Run getPromotionRun() { + return this; + } + + //TODO: move to a default method + @Override public List getParameterValues(){ List values=new ArrayList(); ParametersAction parametersAction=getParametersActions(this); @@ -422,7 +439,7 @@ protected void post2(BuildListener listener) throws Exception { } // tickle PromotionTriggers - for (AbstractProject p : JenkinsHelper.getInstance().getAllItems(AbstractProject.class)) { + for (AbstractProject p : Jenkins.get().getAllItems(AbstractProject.class)) { PromotionTrigger pt = p.getTrigger(PromotionTrigger.class); if (pt!=null) pt.consider(Promotion.this); diff --git a/src/main/java/hudson/plugins/promoted_builds/PromotionBadge.java b/src/main/java/hudson/plugins/promoted_builds/PromotionBadge.java index 2a527cb6..79fee47c 100644 --- a/src/main/java/hudson/plugins/promoted_builds/PromotionBadge.java +++ b/src/main/java/hudson/plugins/promoted_builds/PromotionBadge.java @@ -2,8 +2,12 @@ import hudson.EnvVars; import hudson.model.AbstractBuild; +import hudson.model.Run; +import hudson.model.TaskListener; import org.kohsuke.stapler.export.ExportedBean; +import javax.annotation.Nonnull; + /** * Captures the information about how/when the promotion criteria is satisfied. * @@ -18,11 +22,22 @@ public abstract class PromotionBadge { /** * Called by {@link Status} to allow promotion badges to contribute environment variables. * - * @param build - * The calling build. Never null. + * @param run + * The calling run. * @param env * Environment variables should be added to this map. */ + public void buildEnvVars(@Nonnull Run run, EnvVars env, TaskListener listener) { + // Default implementation when the method is not overridden + if (run instanceof AbstractBuild) { + buildEnvVars((AbstractBuild)run, env); + } + } + + /** + * @deprecated Use {@link #buildEnvVars(Run, EnvVars, TaskListener)} + */ + @Deprecated public void buildEnvVars(AbstractBuild build, EnvVars env) { // by default don't contribute any variables } diff --git a/src/main/java/hudson/plugins/promoted_builds/PromotionCondition.java b/src/main/java/hudson/plugins/promoted_builds/PromotionCondition.java index 781b6c78..986cf0ab 100644 --- a/src/main/java/hudson/plugins/promoted_builds/PromotionCondition.java +++ b/src/main/java/hudson/plugins/promoted_builds/PromotionCondition.java @@ -5,8 +5,6 @@ import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.Describable; -import hudson.model.Hudson; -import hudson.plugins.promoted_builds.util.JenkinsHelper; import java.util.ArrayList; import java.util.List; @@ -53,14 +51,14 @@ public PromotionBadge isMet(PromotionProcess promotionProcess, AbstractBuild all() { - return JenkinsHelper.getInstance().getDescriptorList(PromotionCondition.class); + return Jenkins.get().getDescriptorList(PromotionCondition.class); } /** diff --git a/src/main/java/hudson/plugins/promoted_builds/PromotionConditionDescriptor.java b/src/main/java/hudson/plugins/promoted_builds/PromotionConditionDescriptor.java index be1a7c23..5eb129eb 100644 --- a/src/main/java/hudson/plugins/promoted_builds/PromotionConditionDescriptor.java +++ b/src/main/java/hudson/plugins/promoted_builds/PromotionConditionDescriptor.java @@ -2,6 +2,10 @@ import hudson.model.AbstractProject; import hudson.model.Descriptor; +import hudson.model.Job; +import hudson.model.TaskListener; + +import javax.annotation.Nonnull; /** * {@link Descriptor} for {@link PromotionCondition}. @@ -25,5 +29,13 @@ protected PromotionConditionDescriptor() { * @return * true to allow user to configure this promotion condition for the given project. */ + public boolean isApplicable(@Nonnull Job item,@Nonnull TaskListener listener){ + if(item instanceof AbstractProject){ + return isApplicable((AbstractProject)item); + } + return isApplicable((Job) item, listener); + } + + @Deprecated public abstract boolean isApplicable(AbstractProject item); } diff --git a/src/main/java/hudson/plugins/promoted_builds/PromotionProcess.java b/src/main/java/hudson/plugins/promoted_builds/PromotionProcess.java index b22c6b75..af97adb0 100644 --- a/src/main/java/hudson/plugins/promoted_builds/PromotionProcess.java +++ b/src/main/java/hudson/plugins/promoted_builds/PromotionProcess.java @@ -35,7 +35,6 @@ import hudson.model.labels.LabelAtom; import hudson.model.labels.LabelExpression; import hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval; -import hudson.plugins.promoted_builds.util.JenkinsHelper; import hudson.security.ACL; import hudson.tasks.BuildStep; import hudson.tasks.BuildStepDescriptor; @@ -260,7 +259,7 @@ public String getAssignedLabelString() { // not just the same label.. but at least this works if job is tied to one node: if (assignedLabel == null) return getOwner().getAssignedLabel(); - return JenkinsHelper.getInstance().getLabel(assignedLabel); + return Jenkins.get().getLabel(assignedLabel); } @Override public JDK getJDK() { @@ -326,7 +325,7 @@ private static EnvVars getDefaultParameterValuesAsEnvVars(AbstractProject owner) ParameterValue defaultParameterValue = parameterDefinition.getDefaultParameterValue(); if (defaultParameterValue!=null){ if (defaultParameterValue instanceof StringParameterValue){ - envVars.put(parameterDefinition.getName(), ((StringParameterValue)defaultParameterValue).value); + envVars.put(parameterDefinition.getName(), (String)defaultParameterValue.getValue()); } } } @@ -565,7 +564,7 @@ public Future scheduleBuild2(@Nonnull AbstractBuild build, @Nonn } public boolean isInQueue(@Nonnull AbstractBuild build) { - for (Item item : JenkinsHelper.getInstance().getQueue().getItems(this)) + for (Item item : Jenkins.get().getQueue().getItems(this)) if (item.getAction(PromotionTargetAction.class).resolve(this)==build) return true; return false; @@ -628,7 +627,7 @@ public String getId() { } public DescriptorImpl getDescriptor() { - return (DescriptorImpl)JenkinsHelper.getInstance().getDescriptorOrDie(getClass()); + return (DescriptorImpl)Jenkins.get().getDescriptorOrDie(getClass()); } @Override @@ -658,14 +657,14 @@ public FormValidation doCheckLabelString(@QueryParameter String value) { Messages.JobPropertyImpl_LabelString_InvalidBooleanExpression(e.getMessage())); } // TODO: if there's an atom in the expression that is empty, report it - if (JenkinsHelper.getInstance().getLabel(value).isEmpty()) + if (Jenkins.get().getLabel(value).isEmpty()) return FormValidation.warning(Messages.JobPropertyImpl_LabelString_NoMatch()); return FormValidation.ok(); } public AutoCompletionCandidates doAutoCompleteAssignedLabelString(@QueryParameter String value) { AutoCompletionCandidates c = new AutoCompletionCandidates(); - Set