jenkinsci / badge-plugin

Jenkins Badge plugin
https://plugins.jenkins.io/badge/
MIT License
32 stars 43 forks source link

Can not addShortText() with an id to removeBadges() later, but there is a workaround (to document?) #46

Closed jimklimov closed 4 months ago

jimklimov commented 2 years ago

Related to issue #45 - during the same effort I found that AddShortTextStep seems to extend a plain Step, while other badges inherit Step => AbstractStep (has the id field) => AddBadgeStep => AddInfoBadgeStep (or some other applied badge type).

Consistently, the addShortText() step does not have an "id" argument to remove the left-column text later.

Sadly, all the badge steps seem to require the "icon" argument and so any detailed text is that icon image's alt-text in resulting HTML markup -- not easy to access while monitoring build history, especially as I continuously remove and re-post the badges for a progress-update effect, as detailed in #45.

Looking at the resulting build.xml data however I saw that the badges posted to left-column in build history have very similar markups, including the class in the tag, e.g.:

// addBadge():
    <com.jenkinsci.plugins.badge.action.BadgeAction plugin="badge@1.9">
      <id>Build-progress-badge@2680ed6</id>
      <iconPath>/plugin/badge/images/info.gif</iconPath>
      <text>Build in progress: [STARTED:126, COMPLETED:111, SUCCESS:111]</text>
      <color>#000000</color>
      <background>#FFFF00</background>
      <border>1px</border>
      <borderColor>#C0C000</borderColor>
    </com.jenkinsci.plugins.badge.action.BadgeAction>

vs.

// addShortText():
    <com.jenkinsci.plugins.badge.action.BadgeAction plugin="badge@1.9">
      <text>Running 378 &apos;slow build&apos; dynamatrix stages</text>
      <color>#000000</color>
      <background>#FFFF00</background>
      <border>1px</border>
      <borderColor>#C0C000</borderColor>
    </com.jenkinsci.plugins.badge.action.BadgeAction>

Effectively, the result of addBadge() has the "id" and "iconPath" elements, and the result of addShortText() does not; others overlap.

I took a shot in the dark by calling addBadge(icon: null, ...) et voila, I have the markup with "id" and without "iconPath" which is rendered in build history as the yellow plaque of text, updated as my pipeline rolls though its loop:

    <com.jenkinsci.plugins.badge.action.BadgeAction plugin="badge@1.9">
      <id>Build-progress-badge@6175b31a</id>
      <text>Build in progress: [STARTED:45, COMPLETED:42, SUCCESS:27, FAILURE:7, UNSTABLE:8]</text>
      <color>#000000</color>
      <background>#FFFF00</background>
      <border>1px</border>
      <borderColor>#C0C000</borderColor>
    </com.jenkinsci.plugins.badge.action.BadgeAction>

Example rendered output the way I like it: jenkins-badge--addBadge--like-addShortText

So, to summarize: for the practical purposes I need a way to remove and add the "short wall of text" in the build history. One way to achieve that "perfectly" would be native support for an "id" in the addShortText() step. Another way to achieve that "du-jour" is to (ab)use the basic addBadge() method with a null icon argument. At least until the former is implemented (if ever), I suggest to document the latter in the README, and add comments in the code so nobody removes this ability ;)

jimklimov commented 2 years ago

Another note: I am not sure if addShortText() should have any additional effects/abilities/... compared to a badge (arg-manageable color/border/...?), so maybe this is an opportunity to simplify the codebase by implementing it as yet another badge, just without an icon.

MarkEWaite commented 4 months ago

Resolved by the changes in:

The addShortText now accepts an id.