Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-24994

Poor error reporting when an anonymous Cause is used

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • core
    • envinject-plugin master branch

      EnvInject throws a StringIndexOutOfBoundsException in BuildCauseRetriever.getTriggeredCause if the class that triggered it was anonymous. This is because getTriggerName uses cause.getClass().getSimpleName(), which returns an empty string for anonymous classes, empty strings are skipped when getTriggeredCause iterates over build causes, but it always assumes that there is at least one non-empty build cause.

      To reproduce, create a trigger that uses an anonymous Cause like this:

      private static void scheduleBuild(BuildableItem job) {
          job.scheduleBuild(new Cause() {
              @Override
              public String getShortDescription() {
                  return "Triggered by XYZ";
              }
          });
      }
      

      To work around this, the above snippet can be rewritten to avoid the anonymous class:

      private static void scheduleBuild(BuildableItem job) {
          job.scheduleBuild(new XyzCause());
      }
      
      private static class XyzCause extends Cause {
          @Override
          public String getShortDescription() {
              return "Triggered by XYZ";
          }
      }
      

      I'm not sure which fix would be most consistent with the intent of the ENV_CAUSE variable for custom triggers. I see these options:

      • Skip setting an ENV_CAUSE, possibly log a warning
      • Use the parent class as the trigger name
      • make the trigger "ANONYMOUSTRIGGER", possibly prefixed by the parent class

          [JENKINS-24994] Poor error reporting when an anonymous Cause is used

          David Eckel created issue -
          David Eckel made changes -
          Description Original: EnvInject throws a StringIndexOutOfBoundsException in BuildCauseRetriever.getTriggeredCause if the class that triggered it was anonymous. This is because getTriggerName uses cause.getClass().getSimpleName(), which returns an empty string for anonymous classes, empty strings are skipped when getTriggeredCause iterates over build causes, but it always assumes that there is at least one non-empty build cause.

          I'm not sure which fix would be most consistent with the intent of the ENV_CAUSE variable for custom triggers. I see these options:
          - Skip setting an ENV_CAUSE, possibly log a warning
          - Use the parent class as the trigger name
          - make the trigger "ANONYMOUSTRIGGER", possibly prefixed by the parent class
          New: EnvInject throws a StringIndexOutOfBoundsException in BuildCauseRetriever.getTriggeredCause if the class that triggered it was anonymous. This is because getTriggerName uses cause.getClass().getSimpleName(), which returns an empty string for anonymous classes, empty strings are skipped when getTriggeredCause iterates over build causes, but it always assumes that there is at least one non-empty build cause.

          To reproduce, create a trigger that uses an anonymous Cause like this:
          {code}
          private static void scheduleBuild(BuildableItem job) {
              job.scheduleBuild(new Cause() {
                  @Override
                  public String getShortDescription() {
                      return "Triggered by XYZ";
                  }
              });
          }
          {code}

          To work around this, the above snippet can be rewritten to avoid the anonymous class:
          {code}
          private static void scheduleBuild(BuildableItem job) {
              job.scheduleBuild(new XyzCause());
          }

          private static class XyzCause extends Cause {
              @Override
              public String getShortDescription() {
                  return "Triggered by XYZ";
              }
          }
          {code}

          I'm not sure which fix would be most consistent with the intent of the ENV_CAUSE variable for custom triggers. I see these options:
          - Skip setting an ENV_CAUSE, possibly log a warning
          - Use the parent class as the trigger name
          - make the trigger "ANONYMOUSTRIGGER", possibly prefixed by the parent class
          Daniel Beck made changes -
          Link New: This issue is related to JENKINS-22665 [ JENKINS-22665 ]
          Jesse Glick made changes -
          Component/s New: core [ 15593 ]
          Component/s Original: envinject [ 15893 ]
          Labels New: api robustness xstream
          Summary Original: StringIndexOutOfBoundsException if build cause class is anonymous New: Poor error reporting when an anonymous Cause is used
          Jesse Glick made changes -
          Assignee Original: Gregory Boissinot [ gbois ]
          R. Tyler Croy made changes -
          Workflow Original: JNJira [ 158890 ] New: JNJira + In-Review [ 179790 ]

            Unassigned Unassigned
            dvdckl David Eckel
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: