From 1c2cb2f1889c3ddde64d967f1ffc856853a22430 Mon Sep 17 00:00:00 2001 From: bakito Date: Thu, 29 Mar 2018 18:41:51 +0200 Subject: [PATCH] add removeBadges methods https://issues.jenkins-ci.org/browse/JENKINS-50442 https://github.com/jenkinsci/badge-plugin/issues/4 update readme add unit tests https://issues.jenkins-ci.org/browse/JENKINS-50442 https://github.com/jenkinsci/badge-plugin/issues/4 extend documentation / support remove for summary https://issues.jenkins-ci.org/browse/JENKINS-50442 https://github.com/jenkinsci/badge-plugin/issues/4 fix indent --- README.md | 162 ++++++++++++++++-- .../badge/action/AbstractBadgeAction.java | 46 +++++ .../plugins/badge/action/BadgeAction.java | 5 +- .../badge/action/BadgeSummaryAction.java | 8 +- .../plugins/badge/action/HtmlBadgeAction.java | 5 +- .../badge/annotations/OptionalParam.java | 43 +++++ .../plugins/badge/annotations/Param.java | 48 ++++++ .../badge/dsl/AbstractRemoveBadgesStep.java | 79 +++++++++ .../plugins/badge/dsl/AbstractStep.java | 46 +++++ .../plugins/badge/dsl/AddBadgeStep.java | 22 ++- .../plugins/badge/dsl/AddErrorBadgeStep.java | 7 +- .../plugins/badge/dsl/AddHtmlBadgeStep.java | 19 +- .../plugins/badge/dsl/AddInfoBadgeStep.java | 7 +- .../plugins/badge/dsl/AddShortTextStep.java | 10 +- .../badge/dsl/AddWarningBadgeStep.java | 7 +- .../plugins/badge/dsl/CreateSummaryStep.java | 20 ++- .../plugins/badge/dsl/RemoveBadgesStep.java | 61 +++++++ .../badge/dsl/RemoveHtmlBadgesStep.java | 62 +++++++ .../badge/dsl/RemoveSummariesStep.java | 62 +++++++ .../badge/dsl/AddBadgeStep/config.jelly | 3 + .../badge/dsl/AddErrorBadgeStep/config.jelly | 3 + .../badge/dsl/AddHtmlBadgeStep/config.jelly | 3 + .../badge/dsl/AddInfoBadgeStep/config.jelly | 3 + .../badge/dsl/AddShortTextStep/config.jelly | 3 + .../dsl/AddWarningBadgeStep/config.jelly | 3 + .../badge/dsl/CreateSummaryStep/config.jelly | 3 + .../plugins/badge/dsl/AbstractBadgeTest.java | 38 ++++ .../plugins/badge/dsl/AddBadgeStepTest.java | 7 +- .../badge/dsl/AddHtmlBadgeStepTest.java | 11 +- .../badge/dsl/CreateSummaryStepTest.java | 7 +- .../badge/dsl/RemoveBadgesStepTest.java | 76 ++++++++ .../badge/dsl/RemoveSummariesStepTest.java | 65 +++++++ .../plugins/badge/dsl/ShortTextStepTest.java | 9 +- .../plugins/badge/readme/DescribeStepTag.java | 97 ++++++++--- src/test/resources/readme/README.tmpl | 20 +++ 35 files changed, 949 insertions(+), 121 deletions(-) create mode 100644 src/main/java/com/jenkinsci/plugins/badge/action/AbstractBadgeAction.java create mode 100644 src/main/java/com/jenkinsci/plugins/badge/annotations/OptionalParam.java create mode 100644 src/main/java/com/jenkinsci/plugins/badge/annotations/Param.java create mode 100644 src/main/java/com/jenkinsci/plugins/badge/dsl/AbstractRemoveBadgesStep.java create mode 100644 src/main/java/com/jenkinsci/plugins/badge/dsl/AbstractStep.java create mode 100644 src/main/java/com/jenkinsci/plugins/badge/dsl/RemoveBadgesStep.java create mode 100644 src/main/java/com/jenkinsci/plugins/badge/dsl/RemoveHtmlBadgesStep.java create mode 100644 src/main/java/com/jenkinsci/plugins/badge/dsl/RemoveSummariesStep.java create mode 100644 src/test/java/com/jenkinsci/plugins/badge/dsl/AbstractBadgeTest.java create mode 100644 src/test/java/com/jenkinsci/plugins/badge/dsl/RemoveBadgesStepTest.java create mode 100644 src/test/java/com/jenkinsci/plugins/badge/dsl/RemoveSummariesStepTest.java diff --git a/README.md b/README.md index 4220d6f..9beeaa2 100644 --- a/README.md +++ b/README.md @@ -20,41 +20,109 @@ This method allows to add build badge icons. // addBadge // ------------------------------------------ -// minimal params +/** + * minimal params + * + * icon: The icon for this badge + * text: The text for this badge + */ addBadge(icon, text) -// all params -addBadge(icon, text, link) +/** + * all params + * + * icon: The icon for this badge + * text: The text for this badge + * id: (optional) The id for this badge. This id can be used to selectively delete badges. + * link: (optional) The link to be added to this badge + */ +addBadge(icon, text, id, link) // addInfoBadge // ------------------------------------------ -// minimal params +/** + * minimal params + * + * text: The info text for this badge + */ addInfoBadge(text) -// all params -addInfoBadge(text, link) +/** + * all params + * + * text: The info text for this badge + * id: (optional) The id for this badge. This id can be used to selectively delete badges. + * link: (optional) The link to be added to this badge + */ +addInfoBadge(text, id, link) // addWarningBadge // ------------------------------------------ -// minimal params +/** + * minimal params + * + * text: The text for this warning badge + */ addWarningBadge(text) -// all params -addWarningBadge(text, link) +/** + * all params + * + * text: The text for this warning badge + * id: (optional) The id for this badge. This id can be used to selectively delete badges. + * link: (optional) The link to be added to this badge + */ +addWarningBadge(text, id, link) // addErrorBadge // ------------------------------------------ -// minimal params +/** + * minimal params + * + * text: The text for this error badge + */ addErrorBadge(text) -// all params -addErrorBadge(text, link) +/** + * all params + * + * text: The text for this error badge + * id: (optional) The id for this badge. This id can be used to selectively delete badges. + * link: (optional) The link to be added to this badge + */ +addErrorBadge(text, id, link) + +``` + + +## removeBadges + +Removes badges + +```groovy + +// removes badges. If no id is provided all are removed, otherwise only the badges with a matching id +// removeBadges +// ------------------------------------------ + +/** + * minimal params + * + */ +removeBadges() + +/** + * all params + * + * id: (optional) The id for this badge. This id can be used to selectively delete badges. + */ +removeBadges(id) ``` @@ -68,9 +136,44 @@ Puts a badge with custom html // addHtmlBadge // ------------------------------------------ -// params +/** + * minimal params + * + * html: The html content to be used for this badge + */ addHtmlBadge(html) +/** + * all params + * + * html: The html content to be used for this badge + * id: (optional) The id for this badge. This id can be used to selectively delete badges. + */ +addHtmlBadge(html, id) + +``` +## removeHtmlBadges + +Removes html badges + +```groovy + +// removes html badges. If no id is provided all are removed, otherwise only the badges with a matching id +// removeHtmlBadges +// ------------------------------------------ + +/** + * minimal params + * + */ +removeHtmlBadges() + +/** + * all params + * + * id: (optional) The id for this badge. This id can be used to selectively delete badges. + */ +removeHtmlBadges(id) ``` @@ -86,10 +189,23 @@ Puts a badge with a short text // addShortText // ------------------------------------------ -// minimal params +/** + * minimal params + * + * text: The text to add fot this badge + */ addShortText(text) -// all params +/** + * all params + * + * text: The text to add fot this badge + * background: (optional) The background-color for this short text + * border: (optional) The border width for this short text + * borderColor: (optional) The order color for this short text + * color: (optional) The color for this short text + * link: (optional) The link for this short text + */ addShortText(text, background, border, borderColor, color, link) ``` @@ -108,11 +224,21 @@ Puts a badge with a short text // createSummary // ------------------------------------------ -// minimal params +/** + * minimal params + * + * icon: The icon for this summary + */ createSummary(icon) -// all params -createSummary(icon, text) +/** + * all params + * + * icon: The icon for this summary + * id: (optional) The id for this badge. This id can be used to selectively delete badges. + * text: (optional) The title text for this summary + */ +createSummary(icon, id, text) def summary = createSummary(icon) diff --git a/src/main/java/com/jenkinsci/plugins/badge/action/AbstractBadgeAction.java b/src/main/java/com/jenkinsci/plugins/badge/action/AbstractBadgeAction.java new file mode 100644 index 0000000..6a4fd9d --- /dev/null +++ b/src/main/java/com/jenkinsci/plugins/badge/action/AbstractBadgeAction.java @@ -0,0 +1,46 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Serban Iordache + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.jenkinsci.plugins.badge.action; + +import hudson.model.BuildBadgeAction; +import org.kohsuke.stapler.export.Exported; + +import java.io.Serializable; +import java.lang.annotation.Documented; + +/** + * An abstract badge action providing a badge id + */ +public abstract class AbstractBadgeAction implements BuildBadgeAction, Serializable { + private String id; + + public void setId(String id) { + this.id = id; + } + + @Exported + public String getId() { + return id; + } +} diff --git a/src/main/java/com/jenkinsci/plugins/badge/action/BadgeAction.java b/src/main/java/com/jenkinsci/plugins/badge/action/BadgeAction.java index efcdecb..0923907 100644 --- a/src/main/java/com/jenkinsci/plugins/badge/action/BadgeAction.java +++ b/src/main/java/com/jenkinsci/plugins/badge/action/BadgeAction.java @@ -25,18 +25,15 @@ import com.jenkinsci.plugins.badge.BadgePlugin; import hudson.PluginWrapper; -import hudson.model.Action; -import hudson.model.BuildBadgeAction; import hudson.model.Hudson; import jenkins.model.Jenkins; import org.kohsuke.stapler.export.Exported; import org.kohsuke.stapler.export.ExportedBean; import java.io.File; -import java.io.Serializable; @ExportedBean(defaultVisibility = 2) -public class BadgeAction implements BuildBadgeAction, Serializable { +public class BadgeAction extends AbstractBadgeAction { private static final long serialVersionUID = 1L; private final String iconPath; private final String text; diff --git a/src/main/java/com/jenkinsci/plugins/badge/action/BadgeSummaryAction.java b/src/main/java/com/jenkinsci/plugins/badge/action/BadgeSummaryAction.java index 7cc28c9..1f074a4 100644 --- a/src/main/java/com/jenkinsci/plugins/badge/action/BadgeSummaryAction.java +++ b/src/main/java/com/jenkinsci/plugins/badge/action/BadgeSummaryAction.java @@ -23,17 +23,13 @@ */ package com.jenkinsci.plugins.badge.action; -import java.io.Serializable; - import org.apache.commons.lang.StringEscapeUtils; import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted; import org.kohsuke.stapler.export.Exported; import org.kohsuke.stapler.export.ExportedBean; -import hudson.model.Action; - @ExportedBean(defaultVisibility = 2) -public class BadgeSummaryAction implements Action, Serializable { +public class BadgeSummaryAction extends AbstractBadgeAction { private static final long serialVersionUID = 1L; private final String iconPath; @@ -88,7 +84,7 @@ public void appendText(String text, boolean escapeHtml, boolean bold, boolean it summaryText += ""; } if (color != null) { - summaryText += ""; + summaryText += ""; } if (escapeHtml) { text = StringEscapeUtils.escapeHtml(text); diff --git a/src/main/java/com/jenkinsci/plugins/badge/action/HtmlBadgeAction.java b/src/main/java/com/jenkinsci/plugins/badge/action/HtmlBadgeAction.java index 26b6900..697dc2d 100644 --- a/src/main/java/com/jenkinsci/plugins/badge/action/HtmlBadgeAction.java +++ b/src/main/java/com/jenkinsci/plugins/badge/action/HtmlBadgeAction.java @@ -23,14 +23,11 @@ */ package com.jenkinsci.plugins.badge.action; -import hudson.model.BuildBadgeAction; import org.kohsuke.stapler.export.Exported; import org.kohsuke.stapler.export.ExportedBean; -import java.io.Serializable; - @ExportedBean(defaultVisibility = 2) -public class HtmlBadgeAction implements BuildBadgeAction, Serializable { +public class HtmlBadgeAction extends AbstractBadgeAction { private static final long serialVersionUID = 1L; private final String html; diff --git a/src/main/java/com/jenkinsci/plugins/badge/annotations/OptionalParam.java b/src/main/java/com/jenkinsci/plugins/badge/annotations/OptionalParam.java new file mode 100644 index 0000000..ed99f28 --- /dev/null +++ b/src/main/java/com/jenkinsci/plugins/badge/annotations/OptionalParam.java @@ -0,0 +1,43 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Serban Iordache + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.jenkinsci.plugins.badge.annotations; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * An optional badge param + */ + +@Documented +@Retention(RUNTIME) +@Target({ElementType.METHOD, ElementType.PARAMETER}) +public @interface OptionalParam { + + String description(); +} diff --git a/src/main/java/com/jenkinsci/plugins/badge/annotations/Param.java b/src/main/java/com/jenkinsci/plugins/badge/annotations/Param.java new file mode 100644 index 0000000..ce7307c --- /dev/null +++ b/src/main/java/com/jenkinsci/plugins/badge/annotations/Param.java @@ -0,0 +1,48 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Serban Iordache + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.jenkinsci.plugins.badge.annotations; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * A required badge param + */ + +@Documented +@Retention(RUNTIME) +@Target({ElementType.FIELD, ElementType.PARAMETER}) +public @interface Param { + + /** + * The name. + */ + String name(); + + String description(); +} diff --git a/src/main/java/com/jenkinsci/plugins/badge/dsl/AbstractRemoveBadgesStep.java b/src/main/java/com/jenkinsci/plugins/badge/dsl/AbstractRemoveBadgesStep.java new file mode 100644 index 0000000..135aaab --- /dev/null +++ b/src/main/java/com/jenkinsci/plugins/badge/dsl/AbstractRemoveBadgesStep.java @@ -0,0 +1,79 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Serban Iordache + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.jenkinsci.plugins.badge.dsl; + +import com.jenkinsci.plugins.badge.action.AbstractBadgeAction; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import hudson.model.Action; +import hudson.model.Run; +import org.jenkinsci.plugins.workflow.steps.StepContext; +import org.jenkinsci.plugins.workflow.steps.StepExecution; +import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution; + +import java.io.IOException; + +/** + * Abstract class to remove badges + */ +abstract class AbstractRemoveBadgesStep extends AbstractStep { + @Override + public StepExecution start(StepContext context) { + return new Execution(context, getActionClass(), getId()); + } + + + protected abstract Class getActionClass(); + + + public static class Execution extends SynchronousStepExecution { + + @SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "Only used when starting.") + private final Class actionClass; + + @SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "Only used when starting.") + private final String id; + + Execution(StepContext context, Class actionClass, String id) { + super(context); + this.actionClass = actionClass; + this.id = id; + } + + @Override + protected Void run() throws IOException, InterruptedException { + Run run = getContext().get(Run.class); + if (run != null) { + run.getAllActions().stream().filter(this::matches).forEach(run::removeAction); + } + return null; + } + + private boolean matches(Action a) { + return actionClass.isAssignableFrom(a.getClass()) && (id == null || id.equals(((AbstractBadgeAction) a).getId())); + } + + private static final long serialVersionUID = 1L; + + } +} diff --git a/src/main/java/com/jenkinsci/plugins/badge/dsl/AbstractStep.java b/src/main/java/com/jenkinsci/plugins/badge/dsl/AbstractStep.java new file mode 100644 index 0000000..222c8ad --- /dev/null +++ b/src/main/java/com/jenkinsci/plugins/badge/dsl/AbstractStep.java @@ -0,0 +1,46 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Serban Iordache + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.jenkinsci.plugins.badge.dsl; + +import com.jenkinsci.plugins.badge.annotations.OptionalParam; +import org.jenkinsci.plugins.workflow.steps.Step; +import org.kohsuke.stapler.DataBoundSetter; + +/** + * An abstract Step implementation providing an id for the step. + */ +abstract class AbstractStep extends Step { + private String id; + + @DataBoundSetter + @OptionalParam(description = "The id for this badge. This id can be used to selectively delete badges.") + public void setId(String id) { + this.id = id; + } + + public String getId() { + return id; + } +} + diff --git a/src/main/java/com/jenkinsci/plugins/badge/dsl/AddBadgeStep.java b/src/main/java/com/jenkinsci/plugins/badge/dsl/AddBadgeStep.java index bd30814..8550fd8 100644 --- a/src/main/java/com/jenkinsci/plugins/badge/dsl/AddBadgeStep.java +++ b/src/main/java/com/jenkinsci/plugins/badge/dsl/AddBadgeStep.java @@ -24,28 +24,30 @@ package com.jenkinsci.plugins.badge.dsl; import com.jenkinsci.plugins.badge.action.BadgeAction; +import com.jenkinsci.plugins.badge.annotations.OptionalParam; +import com.jenkinsci.plugins.badge.annotations.Param; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import hudson.Extension; import hudson.model.Run; -import org.jenkinsci.plugins.workflow.steps.Step; import org.jenkinsci.plugins.workflow.steps.StepContext; import org.jenkinsci.plugins.workflow.steps.StepExecution; import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; +import sun.security.krb5.internal.crypto.Des; -import javax.inject.Named; import java.io.Serializable; /** * Add a badge. */ -public class AddBadgeStep extends Step { +public class AddBadgeStep extends AbstractStep { private final Badge badge; @DataBoundConstructor - public AddBadgeStep(@Named("icon") String icon, @Named("text") String text) { + public AddBadgeStep(@Param(name = "icon", description = "The icon for this badge") String icon, + @Param(name = "text", description = "The text for this badge") String text) { this.badge = new Badge(icon, text); } @@ -62,13 +64,14 @@ public String getLink() { } @DataBoundSetter + @OptionalParam(description = "The link to be added to this badge") public void setLink(String link) { badge.setLink(link); } @Override public StepExecution start(StepContext context) { - return new Execution(badge, context); + return new Execution(badge, getId(), context); } protected Badge getBadge() { @@ -123,15 +126,20 @@ public static class Execution extends SynchronousStepExecution { @SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "Only used when starting.") private transient final Badge badge; + @SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "Only used when starting.") + private transient final String id; - Execution(Badge badge, StepContext context) { + Execution(Badge badge, String id, StepContext context) { super(context); this.badge = badge; + this.id = id; } @Override protected Void run() throws Exception { - getContext().get(Run.class).addAction(newBatchAction(badge)); + BadgeAction action = newBatchAction(badge); + action.setId(id); + getContext().get(Run.class).addAction(action); return null; } diff --git a/src/main/java/com/jenkinsci/plugins/badge/dsl/AddErrorBadgeStep.java b/src/main/java/com/jenkinsci/plugins/badge/dsl/AddErrorBadgeStep.java index 9813120..293a2ff 100644 --- a/src/main/java/com/jenkinsci/plugins/badge/dsl/AddErrorBadgeStep.java +++ b/src/main/java/com/jenkinsci/plugins/badge/dsl/AddErrorBadgeStep.java @@ -24,20 +24,19 @@ package com.jenkinsci.plugins.badge.dsl; import com.jenkinsci.plugins.badge.action.BadgeAction; +import com.jenkinsci.plugins.badge.annotations.Param; import hudson.Extension; import org.jenkinsci.plugins.workflow.steps.StepContext; import org.jenkinsci.plugins.workflow.steps.StepExecution; import org.kohsuke.stapler.DataBoundConstructor; -import javax.inject.Named; - /** * Add an error badge. */ public class AddErrorBadgeStep extends AddBadgeStep { @DataBoundConstructor - public AddErrorBadgeStep(@Named("text") String text) { + public AddErrorBadgeStep(@Param(name = "text", description = "The text for this error badge") String text) { super(null, text); } @@ -57,7 +56,7 @@ public String getDisplayName() { @Override public StepExecution start(StepContext context) { - return new Execution(getBadge(), context) { + return new Execution(getBadge(), getId(), context) { @Override protected BadgeAction newBatchAction(Badge badge) { diff --git a/src/main/java/com/jenkinsci/plugins/badge/dsl/AddHtmlBadgeStep.java b/src/main/java/com/jenkinsci/plugins/badge/dsl/AddHtmlBadgeStep.java index 1f0cf2c..0e71889 100644 --- a/src/main/java/com/jenkinsci/plugins/badge/dsl/AddHtmlBadgeStep.java +++ b/src/main/java/com/jenkinsci/plugins/badge/dsl/AddHtmlBadgeStep.java @@ -24,26 +24,24 @@ package com.jenkinsci.plugins.badge.dsl; import com.jenkinsci.plugins.badge.action.HtmlBadgeAction; +import com.jenkinsci.plugins.badge.annotations.Param; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import hudson.Extension; import hudson.model.Run; -import org.jenkinsci.plugins.workflow.steps.Step; import org.jenkinsci.plugins.workflow.steps.StepContext; import org.jenkinsci.plugins.workflow.steps.StepExecution; import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution; import org.kohsuke.stapler.DataBoundConstructor; -import javax.inject.Named; - /** * Create a short text. */ -public class AddHtmlBadgeStep extends Step { +public class AddHtmlBadgeStep extends AbstractStep { private final String html; @DataBoundConstructor - public AddHtmlBadgeStep(@Named("html") String html) { + public AddHtmlBadgeStep(@Param(name = "html", description = "The html content to be used for this badge") String html) { this.html = html; } @@ -53,7 +51,7 @@ public String getHtml() { @Override public StepExecution start(StepContext context) { - return new Execution(html, context); + return new Execution(html, getId(), context); } @Extension @@ -75,15 +73,20 @@ public static class Execution extends SynchronousStepExecution { @SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "Only used when starting.") private transient final String html; + @SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "Only used when starting.") + private transient final String id; - Execution(String html, StepContext context) { + Execution(String html, String id, StepContext context) { super(context); this.html = html; + this.id = id; } @Override protected Void run() throws Exception { - getContext().get(Run.class).addAction(HtmlBadgeAction.createHtmlBadge(html)); + HtmlBadgeAction htmlBadge = HtmlBadgeAction.createHtmlBadge(html); + htmlBadge.setId(id); + getContext().get(Run.class).addAction(htmlBadge); return null; } diff --git a/src/main/java/com/jenkinsci/plugins/badge/dsl/AddInfoBadgeStep.java b/src/main/java/com/jenkinsci/plugins/badge/dsl/AddInfoBadgeStep.java index f319d41..54381a4 100644 --- a/src/main/java/com/jenkinsci/plugins/badge/dsl/AddInfoBadgeStep.java +++ b/src/main/java/com/jenkinsci/plugins/badge/dsl/AddInfoBadgeStep.java @@ -24,20 +24,19 @@ package com.jenkinsci.plugins.badge.dsl; import com.jenkinsci.plugins.badge.action.BadgeAction; +import com.jenkinsci.plugins.badge.annotations.Param; import hudson.Extension; import org.jenkinsci.plugins.workflow.steps.StepContext; import org.jenkinsci.plugins.workflow.steps.StepExecution; import org.kohsuke.stapler.DataBoundConstructor; -import javax.inject.Named; - /** * Add an error badge. */ public class AddInfoBadgeStep extends AddBadgeStep { @DataBoundConstructor - public AddInfoBadgeStep(@Named("text") String text) { + public AddInfoBadgeStep(@Param(name = "text", description = "The info text for this badge") String text) { super(null, text); } @@ -57,7 +56,7 @@ public String getDisplayName() { @Override public StepExecution start(StepContext context) { - return new Execution(getBadge(), context) { + return new Execution(getBadge(), getId(), context) { @Override protected BadgeAction newBatchAction(Badge badge) { diff --git a/src/main/java/com/jenkinsci/plugins/badge/dsl/AddShortTextStep.java b/src/main/java/com/jenkinsci/plugins/badge/dsl/AddShortTextStep.java index a687178..4b5ec8c 100644 --- a/src/main/java/com/jenkinsci/plugins/badge/dsl/AddShortTextStep.java +++ b/src/main/java/com/jenkinsci/plugins/badge/dsl/AddShortTextStep.java @@ -24,6 +24,8 @@ package com.jenkinsci.plugins.badge.dsl; import com.jenkinsci.plugins.badge.action.BadgeAction; +import com.jenkinsci.plugins.badge.annotations.OptionalParam; +import com.jenkinsci.plugins.badge.annotations.Param; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import hudson.Extension; import hudson.model.Run; @@ -34,7 +36,6 @@ import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; -import javax.inject.Named; import java.io.Serializable; /** @@ -45,7 +46,7 @@ public class AddShortTextStep extends Step { private final ShortText shortText; @DataBoundConstructor - public AddShortTextStep(@Named("text") String text) { + public AddShortTextStep(@Param(name = "text", description = "The text to add fot this badge") String text) { this.shortText = new ShortText(text); } @@ -58,6 +59,7 @@ public String getColor() { } @DataBoundSetter + @OptionalParam(description = "The color for this short text") public void setColor(String color) { this.shortText.setColor(color); } @@ -67,6 +69,7 @@ public String getBackground() { } @DataBoundSetter + @OptionalParam(description = "The background-color for this short text") public void setBackground(String background) { this.shortText.setBackground(background); } @@ -76,6 +79,7 @@ public Integer getBorder() { } @DataBoundSetter + @OptionalParam(description = "The border width for this short text") public void setBorder(Integer border) { this.shortText.setBorder(border); } @@ -85,11 +89,13 @@ public String getBorderColor() { } @DataBoundSetter + @OptionalParam(description = "The order color for this short text") public void setBorderColor(String borderColor) { this.shortText.setBorderColor(borderColor); } @DataBoundSetter + @OptionalParam(description = "The link for this short text") public void setLink(String link) { this.shortText.setLink(link); } diff --git a/src/main/java/com/jenkinsci/plugins/badge/dsl/AddWarningBadgeStep.java b/src/main/java/com/jenkinsci/plugins/badge/dsl/AddWarningBadgeStep.java index dd64592..6436989 100644 --- a/src/main/java/com/jenkinsci/plugins/badge/dsl/AddWarningBadgeStep.java +++ b/src/main/java/com/jenkinsci/plugins/badge/dsl/AddWarningBadgeStep.java @@ -24,20 +24,19 @@ package com.jenkinsci.plugins.badge.dsl; import com.jenkinsci.plugins.badge.action.BadgeAction; +import com.jenkinsci.plugins.badge.annotations.Param; import hudson.Extension; import org.jenkinsci.plugins.workflow.steps.StepContext; import org.jenkinsci.plugins.workflow.steps.StepExecution; import org.kohsuke.stapler.DataBoundConstructor; -import javax.inject.Named; - /** * Add an error badge. */ public class AddWarningBadgeStep extends AddBadgeStep { @DataBoundConstructor - public AddWarningBadgeStep(@Named("text") String text) { + public AddWarningBadgeStep(@Param(name = "text", description = "The text for this warning badge") String text) { super(null, text); } @@ -57,7 +56,7 @@ public String getDisplayName() { @Override public StepExecution start(StepContext context) { - return new Execution(getBadge(), context) { + return new Execution(getBadge(), getId(), context) { @Override protected BadgeAction newBatchAction(Badge badge) { diff --git a/src/main/java/com/jenkinsci/plugins/badge/dsl/CreateSummaryStep.java b/src/main/java/com/jenkinsci/plugins/badge/dsl/CreateSummaryStep.java index d27e26c..cc6781b 100644 --- a/src/main/java/com/jenkinsci/plugins/badge/dsl/CreateSummaryStep.java +++ b/src/main/java/com/jenkinsci/plugins/badge/dsl/CreateSummaryStep.java @@ -23,30 +23,29 @@ */ package com.jenkinsci.plugins.badge.dsl; -import org.apache.commons.lang.StringUtils; import com.jenkinsci.plugins.badge.action.BadgeSummaryAction; +import com.jenkinsci.plugins.badge.annotations.OptionalParam; +import com.jenkinsci.plugins.badge.annotations.Param; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import hudson.Extension; import hudson.model.Run; -import org.jenkinsci.plugins.workflow.steps.Step; +import org.apache.commons.lang.StringUtils; import org.jenkinsci.plugins.workflow.steps.StepContext; import org.jenkinsci.plugins.workflow.steps.StepExecution; import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; -import javax.inject.Named; - /** * Create a summary text. */ -public class CreateSummaryStep extends Step { +public class CreateSummaryStep extends AbstractStep { private final String icon; private String text; @DataBoundConstructor - public CreateSummaryStep(@Named("icon") String icon) { + public CreateSummaryStep(@Param(name = "icon", description = "The icon for this summary") String icon) { this.icon = icon; } @@ -59,13 +58,14 @@ public String getText() { } @DataBoundSetter + @OptionalParam(description = "The title text for this summary") public void setText(String text) { this.text = text; } @Override public StepExecution start(StepContext context) { - return new Execution(icon, text, context); + return new Execution(icon, text, getId(), context); } @Extension @@ -89,11 +89,14 @@ public static class Execution extends SynchronousStepExecution getActionClass() { + return BadgeAction.class; + } + + + @Extension + public static class DescriptorImpl extends AbstractTaskListenerDescriptor { + + @Override + public String getFunctionName() { + return "removeBadges"; + } + + @Override + public String getDisplayName() { + return "Remove Badges"; + } + + } +} diff --git a/src/main/java/com/jenkinsci/plugins/badge/dsl/RemoveHtmlBadgesStep.java b/src/main/java/com/jenkinsci/plugins/badge/dsl/RemoveHtmlBadgesStep.java new file mode 100644 index 0000000..53e5352 --- /dev/null +++ b/src/main/java/com/jenkinsci/plugins/badge/dsl/RemoveHtmlBadgesStep.java @@ -0,0 +1,62 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Serban Iordache + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.jenkinsci.plugins.badge.dsl; + +import com.jenkinsci.plugins.badge.action.AbstractBadgeAction; +import com.jenkinsci.plugins.badge.action.HtmlBadgeAction; +import hudson.Extension; +import org.kohsuke.stapler.DataBoundConstructor; + +/** + * Removes all html badges or the badges with a given id. + */ +public class RemoveHtmlBadgesStep extends AbstractRemoveBadgesStep { + + + @DataBoundConstructor + public RemoveHtmlBadgesStep() { + } + + @Override + protected Class getActionClass() { + return HtmlBadgeAction.class; + } + + @Extension + public static class DescriptorImpl extends AbstractTaskListenerDescriptor { + + @Override + public String getFunctionName() { + return "removeHtmlBadges"; + } + + @Override + public String getDisplayName() { + return "Remove Html Badges"; + } + + } + + +} diff --git a/src/main/java/com/jenkinsci/plugins/badge/dsl/RemoveSummariesStep.java b/src/main/java/com/jenkinsci/plugins/badge/dsl/RemoveSummariesStep.java new file mode 100644 index 0000000..b333548 --- /dev/null +++ b/src/main/java/com/jenkinsci/plugins/badge/dsl/RemoveSummariesStep.java @@ -0,0 +1,62 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Serban Iordache + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.jenkinsci.plugins.badge.dsl; + +import com.jenkinsci.plugins.badge.action.AbstractBadgeAction; +import com.jenkinsci.plugins.badge.action.BadgeSummaryAction; +import hudson.Extension; +import org.kohsuke.stapler.DataBoundConstructor; + +/** + * Removes all summaries or the summaries with a given id. + */ +public class RemoveSummariesStep extends AbstractRemoveBadgesStep { + + + @DataBoundConstructor + public RemoveSummariesStep() { + } + + @Override + protected Class getActionClass() { + return BadgeSummaryAction.class; + } + + @Extension + public static class DescriptorImpl extends AbstractTaskListenerDescriptor { + + @Override + public String getFunctionName() { + return "removeSummaries"; + } + + @Override + public String getDisplayName() { + return "Remove Summaries"; + } + + } + + +} diff --git a/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddBadgeStep/config.jelly b/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddBadgeStep/config.jelly index c0ce138..e7baed1 100644 --- a/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddBadgeStep/config.jelly +++ b/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddBadgeStep/config.jelly @@ -34,4 +34,7 @@ THE SOFTWARE. + + + \ No newline at end of file diff --git a/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddErrorBadgeStep/config.jelly b/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddErrorBadgeStep/config.jelly index c333cf3..a4fc2d0 100644 --- a/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddErrorBadgeStep/config.jelly +++ b/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddErrorBadgeStep/config.jelly @@ -28,4 +28,7 @@ THE SOFTWARE. + + + \ No newline at end of file diff --git a/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddHtmlBadgeStep/config.jelly b/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddHtmlBadgeStep/config.jelly index d0d769e..0c8d850 100644 --- a/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddHtmlBadgeStep/config.jelly +++ b/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddHtmlBadgeStep/config.jelly @@ -28,4 +28,7 @@ THE SOFTWARE. + + + \ No newline at end of file diff --git a/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddInfoBadgeStep/config.jelly b/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddInfoBadgeStep/config.jelly index c333cf3..a4fc2d0 100644 --- a/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddInfoBadgeStep/config.jelly +++ b/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddInfoBadgeStep/config.jelly @@ -28,4 +28,7 @@ THE SOFTWARE. + + + \ No newline at end of file diff --git a/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddShortTextStep/config.jelly b/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddShortTextStep/config.jelly index ab7f9a5..3329633 100644 --- a/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddShortTextStep/config.jelly +++ b/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddShortTextStep/config.jelly @@ -43,4 +43,7 @@ THE SOFTWARE. + + + \ No newline at end of file diff --git a/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddWarningBadgeStep/config.jelly b/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddWarningBadgeStep/config.jelly index c333cf3..a4fc2d0 100644 --- a/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddWarningBadgeStep/config.jelly +++ b/src/main/resources/com/jenkinsci/plugins/badge/dsl/AddWarningBadgeStep/config.jelly @@ -28,4 +28,7 @@ THE SOFTWARE. + + + \ No newline at end of file diff --git a/src/main/resources/com/jenkinsci/plugins/badge/dsl/CreateSummaryStep/config.jelly b/src/main/resources/com/jenkinsci/plugins/badge/dsl/CreateSummaryStep/config.jelly index 8003cbe..bbdb9c4 100644 --- a/src/main/resources/com/jenkinsci/plugins/badge/dsl/CreateSummaryStep/config.jelly +++ b/src/main/resources/com/jenkinsci/plugins/badge/dsl/CreateSummaryStep/config.jelly @@ -28,4 +28,7 @@ THE SOFTWARE. + + + \ No newline at end of file diff --git a/src/test/java/com/jenkinsci/plugins/badge/dsl/AbstractBadgeTest.java b/src/test/java/com/jenkinsci/plugins/badge/dsl/AbstractBadgeTest.java new file mode 100644 index 0000000..def5a27 --- /dev/null +++ b/src/test/java/com/jenkinsci/plugins/badge/dsl/AbstractBadgeTest.java @@ -0,0 +1,38 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Serban Iordache + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.jenkinsci.plugins.badge.dsl; + +import org.junit.ClassRule; +import org.junit.Rule; +import org.jvnet.hudson.test.BuildWatcher; +import org.jvnet.hudson.test.JenkinsRule; + +public abstract class AbstractBadgeTest { + + @ClassRule + public static BuildWatcher buildWatcher = new BuildWatcher(); + @Rule + public JenkinsRule r = new JenkinsRule(); + +} diff --git a/src/test/java/com/jenkinsci/plugins/badge/dsl/AddBadgeStepTest.java b/src/test/java/com/jenkinsci/plugins/badge/dsl/AddBadgeStepTest.java index e3653f9..fad7fb3 100644 --- a/src/test/java/com/jenkinsci/plugins/badge/dsl/AddBadgeStepTest.java +++ b/src/test/java/com/jenkinsci/plugins/badge/dsl/AddBadgeStepTest.java @@ -42,12 +42,7 @@ import hudson.model.BuildBadgeAction; -public class AddBadgeStepTest { - - @ClassRule - public static BuildWatcher buildWatcher = new BuildWatcher(); - @Rule - public JenkinsRule r = new JenkinsRule(); +public class AddBadgeStepTest extends AbstractBadgeTest { @Test public void addBadge() throws Exception { diff --git a/src/test/java/com/jenkinsci/plugins/badge/dsl/AddHtmlBadgeStepTest.java b/src/test/java/com/jenkinsci/plugins/badge/dsl/AddHtmlBadgeStepTest.java index 9761dea..b1a4573 100644 --- a/src/test/java/com/jenkinsci/plugins/badge/dsl/AddHtmlBadgeStepTest.java +++ b/src/test/java/com/jenkinsci/plugins/badge/dsl/AddHtmlBadgeStepTest.java @@ -28,23 +28,14 @@ import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; -import org.junit.ClassRule; -import org.junit.Rule; import org.junit.Test; -import org.jvnet.hudson.test.BuildWatcher; -import org.jvnet.hudson.test.JenkinsRule; import java.util.List; import java.util.UUID; import static org.junit.Assert.assertEquals; -public class AddHtmlBadgeStepTest { - - @ClassRule - public static BuildWatcher buildWatcher = new BuildWatcher(); - @Rule - public JenkinsRule r = new JenkinsRule(); +public class AddHtmlBadgeStepTest extends AbstractBadgeTest { @Test public void addHtmlBadge() throws Exception { diff --git a/src/test/java/com/jenkinsci/plugins/badge/dsl/CreateSummaryStepTest.java b/src/test/java/com/jenkinsci/plugins/badge/dsl/CreateSummaryStepTest.java index e8744f9..b07f64d 100644 --- a/src/test/java/com/jenkinsci/plugins/badge/dsl/CreateSummaryStepTest.java +++ b/src/test/java/com/jenkinsci/plugins/badge/dsl/CreateSummaryStepTest.java @@ -41,12 +41,7 @@ import com.jenkinsci.plugins.badge.action.BadgeSummaryAction; -public class CreateSummaryStepTest { - - @ClassRule - public static BuildWatcher buildWatcher = new BuildWatcher(); - @Rule - public JenkinsRule r = new JenkinsRule(); +public class CreateSummaryStepTest extends AbstractBadgeTest { @Test public void createSummary_plain() throws Exception { diff --git a/src/test/java/com/jenkinsci/plugins/badge/dsl/RemoveBadgesStepTest.java b/src/test/java/com/jenkinsci/plugins/badge/dsl/RemoveBadgesStepTest.java new file mode 100644 index 0000000..62b2966 --- /dev/null +++ b/src/test/java/com/jenkinsci/plugins/badge/dsl/RemoveBadgesStepTest.java @@ -0,0 +1,76 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Serban Iordache + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.jenkinsci.plugins.badge.dsl; + +import com.jenkinsci.plugins.badge.action.AbstractBadgeAction; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class RemoveBadgesStepTest extends AbstractBadgeTest { + + @Test + public void removeBadges_by_id() throws Exception { + removeBadges("addInfoBadge(text: 'a'", "removeBadges(id:'a')", "b"); + } + + @Test + public void removeBadges_all() throws Exception { + removeBadges("addInfoBadge(text: 'a'", "removeBadges()"); + } + + @Test + public void removeHtmlBadges_by_id() throws Exception { + removeBadges("addHtmlBadge(html: 'a'", "removeHtmlBadges(id:'a')", "b"); + } + + @Test + public void removeHtmlBadges_all() throws Exception { + removeBadges("addHtmlBadge(html: 'a'", "removeHtmlBadges()"); + } + + private void removeBadges(String badgeScriptPrefix, String removeScript, String... remainingBadgeIds) throws Exception { + WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); + String script = badgeScriptPrefix + ", id: 'a')\n" + + badgeScriptPrefix + ", id: 'b')\n" + + removeScript; + + p.setDefinition(new CpsFlowDefinition(script, true)); + WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0)); + + List badgeActions = b.getActions(AbstractBadgeAction.class); + + assertEquals(remainingBadgeIds.length, badgeActions.size()); + + for (int i = 0; i < remainingBadgeIds.length; i++) { + assertEquals(remainingBadgeIds[i], badgeActions.get(i).getId()); + } + + } +} diff --git a/src/test/java/com/jenkinsci/plugins/badge/dsl/RemoveSummariesStepTest.java b/src/test/java/com/jenkinsci/plugins/badge/dsl/RemoveSummariesStepTest.java new file mode 100644 index 0000000..1436af2 --- /dev/null +++ b/src/test/java/com/jenkinsci/plugins/badge/dsl/RemoveSummariesStepTest.java @@ -0,0 +1,65 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Serban Iordache + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.jenkinsci.plugins.badge.dsl; + +import com.jenkinsci.plugins.badge.action.BadgeSummaryAction; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.Test; + +import java.util.List; + +import static java.util.UUID.randomUUID; +import static org.junit.Assert.assertEquals; + +public class RemoveSummariesStepTest extends AbstractBadgeTest { + + @Test + public void removeSummaries_by_id() throws Exception { + removeSummaries("removeSummaries(id:'a')", "b"); + } + + @Test + public void removeSummaries_all() throws Exception { + removeSummaries("removeSummaries()"); + } + + + private void removeSummaries(String removeScript, String... remainingBadgeIds) throws Exception { + String icon = randomUUID().toString(); + + WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); + p.setDefinition(new CpsFlowDefinition("def summaryA = createSummary(id:'a', icon:\"" + icon + "\")\n" + + "def summaryB = createSummary(id:'b', icon:\"" + icon + "\")\n" + + removeScript, true)); + WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0)); + List summaryActions = b.getActions(BadgeSummaryAction.class); + assertEquals(remainingBadgeIds.length, summaryActions.size()); + + for (int i = 0; i < remainingBadgeIds.length; i++) { + assertEquals(remainingBadgeIds[i], summaryActions.get(i).getId()); + } + } +} diff --git a/src/test/java/com/jenkinsci/plugins/badge/dsl/ShortTextStepTest.java b/src/test/java/com/jenkinsci/plugins/badge/dsl/ShortTextStepTest.java index 6fd5706..44a436f 100644 --- a/src/test/java/com/jenkinsci/plugins/badge/dsl/ShortTextStepTest.java +++ b/src/test/java/com/jenkinsci/plugins/badge/dsl/ShortTextStepTest.java @@ -24,6 +24,7 @@ package com.jenkinsci.plugins.badge.dsl; import com.jenkinsci.plugins.badge.action.BadgeAction; +import hudson.model.Action; import hudson.model.BuildBadgeAction; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; @@ -41,12 +42,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -public class ShortTextStepTest { - - @ClassRule - public static BuildWatcher buildWatcher = new BuildWatcher(); - @Rule - public JenkinsRule r = new JenkinsRule(); +public class ShortTextStepTest extends AbstractBadgeTest { @Test public void addShortText() throws Exception { @@ -62,6 +58,7 @@ public void addShortText() throws Exception { + borderColor + "\", link:\"" + link + "\")", true)); WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0)); + List allActions = b.getAllActions(); List badgeActions = b.getBadgeActions(); assertEquals(1, badgeActions.size()); diff --git a/src/test/java/com/jenkinsci/plugins/badge/readme/DescribeStepTag.java b/src/test/java/com/jenkinsci/plugins/badge/readme/DescribeStepTag.java index 03df32b..5b566ee 100644 --- a/src/test/java/com/jenkinsci/plugins/badge/readme/DescribeStepTag.java +++ b/src/test/java/com/jenkinsci/plugins/badge/readme/DescribeStepTag.java @@ -4,15 +4,16 @@ import com.hubspot.jinjava.lib.tag.Tag; import com.hubspot.jinjava.tree.TagNode; import com.hubspot.jinjava.util.HelperStringTokenizer; +import com.jenkinsci.plugins.badge.annotations.OptionalParam; +import com.jenkinsci.plugins.badge.annotations.Param; import hudson.Extension; +import org.apache.commons.lang3.builder.CompareToBuilder; import org.jenkinsci.plugins.workflow.steps.StepDescriptor; import org.kohsuke.stapler.DataBoundConstructor; -import org.kohsuke.stapler.DataBoundSetter; -import javax.inject.Named; import java.lang.reflect.Constructor; +import java.lang.reflect.Method; import java.lang.reflect.Parameter; -import java.util.Arrays; import java.util.Optional; import static java.util.Arrays.sort; @@ -50,68 +51,78 @@ public String interpret(TagNode tagNode, JinjavaInterpreter jinjavaInterpreter) String functionName = getFunctionName(declaredClasses); - Optional constructorParameterNames = getConstructorParameterNames(constructors); + Optional constructorParameterNames = getConstructorParameterNames(constructors); if (!constructorParameterNames.isPresent()) { throw new IllegalStateException("No constructor parameters found"); } - String[] constructorParams = constructorParameterNames.get(); + MethodParameter[] constructorParams = constructorParameterNames.get(); - String[] optionalParameterNames = getOptionalParams(clazz); + MethodParameter[] optionalParameterNames = getOptionalParams(clazz); sb.append("// ").append(functionName).append("\n"); sb.append("// ------------------------------------------\n\n"); + sb.append("/**\n"); if (optionalParameterNames.length != 0) { - sb.append("// minimal params\n"); + sb.append(" * minimal params"); } else { - sb.append("// params\n"); + sb.append(" * params"); } + sb.append("\n * \n"); + + stream(constructorParams).forEach(sb::append); + sb.append(" */\n"); + + sb.append(functionName).append("(").append(stream(constructorParams).map(MethodParameter::getName) + .collect(joining(", "))).append(")\n\n"); - sb.append(functionName).append("(").append(stream(constructorParams).collect(joining(", "))).append(")\n\n"); if (optionalParameterNames.length != 0) { - sb.append("// all params\n"); - sb.append(functionName).append("(").append(concat(stream(constructorParams), stream(optionalParameterNames)).collect(joining(", "))).append(")\n"); + sb.append("/**\n * all params\n * \n"); + concat(stream(constructorParams), stream(optionalParameterNames)).forEach(sb::append); + + sb.append(" */\n"); + + sb.append(functionName).append("(").append(concat(stream(constructorParams), stream(optionalParameterNames)) + .map(MethodParameter::getName).collect(joining(", "))).append(")\n"); } } catch (Exception e) { - e.printStackTrace(); throw new RuntimeException(e); } return sb.toString(); } - private String[] getOptionalParams(Class clazz) { - return stream(clazz.getMethods()).filter(m -> m.getAnnotation(DataBoundSetter.class) != null).map(m -> { - String name = m.getName().substring(3); - return name.substring(0, 1).toLowerCase() + name.substring(1); - }).sorted().toArray(String[]::new); + private MethodParameter[] getOptionalParams(Class clazz) { + return stream(clazz.getMethods()).filter(m -> m.getAnnotation(OptionalParam.class) != null) + .map(m -> new MethodParameter(m.getAnnotation(OptionalParam.class), m)).sorted().toArray(MethodParameter[]::new); } /** - * The constructor parameters need to have @Named in order to get the name of the parameter + * The constructor parameters need to have @Param in order to get the name of the parameter + * * @param constructors * @return */ - private Optional getConstructorParameterNames(Constructor[] constructors) { + private Optional getConstructorParameterNames(Constructor[] constructors) { return stream(constructors).filter(c -> c.getAnnotation(DataBoundConstructor.class) != null).map(c -> { - String[] names = new String[c.getParameterCount()]; + MethodParameter[] names = new MethodParameter[c.getParameterCount()]; for (int i = 0; i < c.getParameterCount(); i++) { Parameter parameter = c.getParameters()[i]; - Named named = parameter.getAnnotation(Named.class); + Param param = parameter.getAnnotation(Param.class); - if (named == null) { - throw new IllegalStateException("Constructor argument " + i + " of class " + c.getDeclaringClass().getName() + " needs to have @" + Named.class.getName() + " annotation."); + if (param == null) { + throw new IllegalStateException("Constructor argument " + i + " of class " + c.getDeclaringClass().getName() + " needs to have @" + Param.class.getName() + " annotation."); } - names[i] = named.value(); + names[i] = new MethodParameter(param); } @@ -136,4 +147,42 @@ public String getEndTagName() { return null; } + private static class MethodParameter implements Comparable { + final String name; + final String description; + final boolean optional; + + MethodParameter(Param param) { + this.name = param.name(); + this.description = param.description(); + this.optional = false; + } + + MethodParameter(OptionalParam param, Method method) { + + String methodName = method.getName().substring(3); + this.name = methodName.substring(0, 1).toLowerCase() + methodName.substring(1); + this.description = param.description(); + this.optional = true; + } + + private String getName() { + return name; + } + + private boolean isOptional() { + return optional; + } + + @Override + public String toString() { + return " * " + name + ": " + (optional ? "(optional) " : "") + description + "\n"; + } + + @Override + public int compareTo(MethodParameter o) { + return new CompareToBuilder().append(this.optional, o.isOptional()).append(this.name, o.getName()).toComparison(); + } + } + } diff --git a/src/test/resources/readme/README.tmpl b/src/test/resources/readme/README.tmpl index 7f2177c..a84c0c2 100644 --- a/src/test/resources/readme/README.tmpl +++ b/src/test/resources/readme/README.tmpl @@ -26,6 +26,17 @@ This method allows to add build badge icons. {% describe_step com.jenkinsci.plugins.badge.dsl.AddErrorBadgeStep %} ``` + +## removeBadges + +Removes badges + +```groovy + +// removes badges. If no id is provided all are removed, otherwise only the badges with a matching id +{% describe_step com.jenkinsci.plugins.badge.dsl.RemoveBadgesStep %} +``` + ## addHtmlBadge Puts a badge with custom html @@ -35,6 +46,15 @@ Puts a badge with custom html // puts a badge with a custom html content. {% describe_step com.jenkinsci.plugins.badge.dsl.AddHtmlBadgeStep %} ``` +## removeHtmlBadges + +Removes html badges + +```groovy + +// removes html badges. If no id is provided all are removed, otherwise only the badges with a matching id +{% describe_step com.jenkinsci.plugins.badge.dsl.RemoveHtmlBadgesStep %} +``` ## addShortText