Java Code Examples for hudson.model.Run#getCauses()

The following examples show how to use hudson.model.Run#getCauses() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BuildListener.java    From audit-log-plugin with MIT License 6 votes vote down vote up
/**
 * Fired when a build is started, event logged via Log4j-audit.
 *
 * @param run of type Run having the build information
 * @param listener of type TaskListener that the onStarted method expects
 */
@Override
public void onStarted(Run run, TaskListener listener) {
    BuildStart buildStart = LogEventFactory.getEvent(BuildStart.class);

    List causeObjects = run.getCauses();
    List<String> causes = new ArrayList<>(causeObjects.size());
    for (Object cause: causeObjects) {
        Cause c = (Cause)cause;
        causes.add(c.getShortDescription());
    }

    buildStart.setBuildNumber(run.getNumber());
    buildStart.setCause(causes);
    buildStart.setProjectName(run.getParent().getFullName());
    buildStart.setTimestamp(formatDateISO(run.getStartTimeInMillis()));
    User user = User.current();
    if(user != null)
        buildStart.setUserId(user.getId());
    else
        buildStart.setUserId(null);

    buildStart.logEvent();
}
 
Example 2
Source File: PipelineTriggerService.java    From pipeline-maven-plugin with MIT License 6 votes vote down vote up
/**
 * Check NO infinite loop of job triggers caused by {@link hudson.model.Cause.UpstreamCause}.
 *
 * @param initialBuild
 * @throws IllegalStateException if an infinite loop is detected
 */
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);
                }
            }
        }
    }
}
 
Example 3
Source File: UpstreamBuildUtil.java    From jira-ext-plugin with Apache License 2.0 6 votes vote down vote up
private static Cause.UpstreamCause getUpstreamCause(Run run)
{
    if (run == null)
    {
        return null;
    }
    List<Cause> causes = run.getCauses();
    for (Cause cause : causes)
    {
        if (cause instanceof Cause.UpstreamCause)
        {
            return (Cause.UpstreamCause)cause;
        }
    }
    return null;
}
 
Example 4
Source File: BuildListener.java    From audit-log-plugin with MIT License 5 votes vote down vote up
/**
 * Fired when a build is completed, event logged via Log4j-audit.
 *
 * @param run of type Run having the build information
 * @param listener of type TaskListener that the onCompleted method expects
 */
@Override
public void onCompleted(Run run, TaskListener listener) {
    BuildFinish buildFinish = LogEventFactory.getEvent(BuildFinish.class);

    List causeObjects = run.getCauses();
    List<String> causes = new ArrayList<>(causeObjects.size());
    for (Object cause: causeObjects) {
        Cause c = (Cause)cause;
        causes.add(c.getShortDescription());
    }
    buildFinish.setBuildNumber(run.getNumber());
    buildFinish.setCause(causes);
    buildFinish.setProjectName(run.getParent().getFullName());

    Instant start = Instant.ofEpochMilli(run.getStartTimeInMillis());
    Instant finish = start.plusMillis(run.getDuration());
    buildFinish.setTimestamp(formatDateISO(finish.toEpochMilli()));

    User user = User.current();
    if(user != null)
        buildFinish.setUserId(user.getId());
    else
        buildFinish.setUserId(null);

    buildFinish.logEvent();
}
 
Example 5
Source File: DeflakeListener.java    From flaky-test-handler-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void onStarted(Run build, TaskListener listener) {
  List<Cause> causesList = build.getCauses();
  try {
    for (Cause cause : causesList) {
      if (cause instanceof DeflakeCause) {
        build.setDisplayName(build.getDisplayName() + ": " + cause.getShortDescription());
        return;
      }
    }
  } catch (IOException e) {
    LOGGER.log(Level.SEVERE, "Failed to set deflake build name: " + e.getMessage());
  }
}