void cancelBuilds(PullRequestData pullRequestData) {
   final String pullRequestUrl = pullRequestData.pullRequestUrl.toString();
   List<AbstractBuild> pullRequestBuilds = new ArrayList<AbstractBuild>();
   for (Object b : project.getBuilds()) {
     if (b instanceof AbstractBuild) {
       AbstractBuild build = (AbstractBuild) b;
       if (build.isBuilding() && isPullRequestBuild(build, pullRequestUrl)) {
         pullRequestBuilds.add(build);
       }
     }
   }
   if (!pullRequestBuilds.isEmpty()) {
     String[] buildNumbers = new String[pullRequestBuilds.size()];
     for (int i = 0; i < pullRequestBuilds.size(); i++) {
       buildNumbers[i] = String.valueOf(pullRequestBuilds.get(i).getNumber());
     }
     LOGGER.info(
         MessageFormat.format(
             "Aborting the following builds of pull request {0}: {1}",
             pullRequestUrl, StringUtils.join(buildNumbers, ", ")));
     for (AbstractBuild build : pullRequestBuilds) {
       Executor executor = build.getExecutor();
       if (executor != null) {
         executor.interrupt();
       }
     }
   } else {
     LOGGER.fine(
         "There is no previous running builds to cancel for Pull Request: " + pullRequestUrl);
   }
 }
Example #2
0
 @Override
 public boolean cancel(boolean mayInterruptIfRunning) {
   Queue q = Jenkins.getInstance().getQueue();
   synchronized (q) {
     synchronized (this) {
       if (!executors.isEmpty()) {
         if (mayInterruptIfRunning) for (Executor e : executors) e.interrupt();
         return mayInterruptIfRunning;
       }
       return q.cancel(task);
     }
   }
 }
        public void doRun() {
          // timed out
          long effectiveTimeoutMinutes = MINUTES.convert(effectiveTimeout, MILLISECONDS);
          String msg;
          if (failBuild) {
            msg = Messages.Timeout_Message(effectiveTimeoutMinutes, Messages.Timeout_Failed());
          } else {
            msg = Messages.Timeout_Message(effectiveTimeoutMinutes, Messages.Timeout_Aborted());
          }

          listener.getLogger().println(msg);
          if (writingDescription) {
            try {
              build.setDescription(msg);
            } catch (IOException e) {
              listener.getLogger().println("failed to write to the build description!");
            }
          }

          timeout = true;
          Executor e = build.getExecutor();
          if (e != null) e.interrupt(failBuild ? Result.FAILURE : Result.ABORTED);
        }