code: public void checkNoInfiniteLoopOfUpstreamCause(@Nonnull Run initialBuild) throws IllegalStateException {
          java.util.Queue<Run> builds = new LinkedList<>(Collections.singleton(initialBuild));
          Run currentBuild;
          while ((currentBuild = builds.poll()) != null) {
              for (Cause cause : ((List<Cause>) currentBuild.getCauses())) {
                  if (cause instanceof Cause.UpstreamCause) {
                      Cause.UpstreamCause upstreamCause = (Cause.UpstreamCause) cause;
                      Run<?, ?> upstreamBuild = upstreamCause.getUpstreamRun();
                      if (upstreamBuild == null) {
                          // Can be Authorization, build deleted on the file system...
                      } else if (Objects.equals(upstreamBuild.getParent().getFullName(), initialBuild.getParent().getFullName())) {
                          throw new IllegalStateException("Infinite loop of job triggers ");
                      } else {
                          builds.add(upstreamBuild);
                      }
                  }
              }
          }
      }
docs: Check NO infinite loop of job triggers caused by {@link hudson.model.Cause.UpstreamCause}.
  
  @param initialBuild
  @throws IllegalStateException if an infinite loop is detected