private boolean isFilteredBuild(Pullrequest pullRequest) {
    BitbucketCause cause =
        new BitbucketCause(
            pullRequest.getSource().getBranch().getName(),
            pullRequest.getDestination().getBranch().getName(),
            pullRequest.getSource().getRepository().getOwnerName(),
            pullRequest.getSource().getRepository().getRepositoryName(),
            pullRequest.getId(),
            pullRequest.getDestination().getRepository().getOwnerName(),
            pullRequest.getDestination().getRepository().getRepositoryName(),
            pullRequest.getTitle(),
            pullRequest.getSource().getCommit().getHash(),
            pullRequest.getDestination().getCommit().getHash());

    // @FIXME: Way to iterate over all available SCMSources
    List<SCMSource> sources = new LinkedList<SCMSource>();
    for (SCMSourceOwner owner : SCMSourceOwners.all())
      for (SCMSource src : owner.getSCMSources()) sources.add(src);

    BitbucketBuildFilter filter =
        !this.trigger.getBranchesFilterBySCMIncludes()
            ? BitbucketBuildFilter.InstanceByString(this.trigger.getBranchesFilter())
            : BitbucketBuildFilter.InstanceBySCM(sources, this.trigger.getBranchesFilter());

    return filter.approved(cause);
  }
  private boolean isBuildTarget(Pullrequest pullRequest) {
    if (pullRequest.getState() != null && pullRequest.getState().equals("OPEN")) {
      if (isSkipBuild(pullRequest.getTitle()) || !isFilteredBuild(pullRequest)) {
        return false;
      }

      Pullrequest.Revision source = pullRequest.getSource();
      String sourceCommit = source.getCommit().getHash();
      Pullrequest.Revision destination = pullRequest.getDestination();
      String owner = destination.getRepository().getOwnerName();
      String repositoryName = destination.getRepository().getRepositoryName();

      Pullrequest.Repository sourceRepository = source.getRepository();
      String buildKeyPart = this.builder.getProjectId();

      final boolean commitAlreadyBeenProcessed =
          this.client.hasBuildStatus(
              sourceRepository.getOwnerName(),
              sourceRepository.getRepositoryName(),
              sourceCommit,
              buildKeyPart);
      if (commitAlreadyBeenProcessed)
        logger.log(
            Level.INFO,
            "Commit {0}#{1} has already been processed",
            new Object[] {sourceCommit, buildKeyPart});

      final String id = pullRequest.getId();
      List<Pullrequest.Comment> comments = client.getPullRequestComments(owner, repositoryName, id);

      boolean rebuildCommentAvailable = false;
      if (comments != null) {
        Collection<Pullrequest.Comment> filteredComments = this.filterPullRequestComments(comments);
        for (Pullrequest.Comment comment : filteredComments) {
          String content = comment.getContent();
          if (this.isTTPComment(content)) {
            rebuildCommentAvailable = true;
            logger.log(
                Level.INFO,
                "Rebuild comment available for commit {0} and comment #{1}",
                new Object[] {sourceCommit, comment.getId()});
          }
          rebuildCommentAvailable &= this.processTTPCommentBuildTags(content, buildKeyPart);
          if (!rebuildCommentAvailable) break;
        }
      }
      if (rebuildCommentAvailable)
        this.postBuildTagInTTPComment(id, "TTP build flag", buildKeyPart);

      final boolean canBuildTarget = rebuildCommentAvailable || !commitAlreadyBeenProcessed;
      logger.log(
          Level.INFO,
          "Build target? {0} [rebuild:{1} processed:{2}]",
          new Object[] {canBuildTarget, rebuildCommentAvailable, commitAlreadyBeenProcessed});
      return canBuildTarget;
    }

    return false;
  }
 public void addFutureBuildTasks(Collection<Pullrequest> pullRequests) {
   for (Pullrequest pullRequest : pullRequests) {
     if (this.trigger.getApproveIfSuccess()) {
       deletePullRequestApproval(pullRequest.getId());
     }
     BitbucketCause cause =
         new BitbucketCause(
             pullRequest.getSource().getBranch().getName(),
             pullRequest.getDestination().getBranch().getName(),
             pullRequest.getSource().getRepository().getOwnerName(),
             pullRequest.getSource().getRepository().getRepositoryName(),
             pullRequest.getId(),
             pullRequest.getDestination().getRepository().getOwnerName(),
             pullRequest.getDestination().getRepository().getRepositoryName(),
             pullRequest.getTitle(),
             pullRequest.getSource().getCommit().getHash(),
             pullRequest.getDestination().getCommit().getHash());
     setBuildStatus(cause, BuildState.INPROGRESS, Jenkins.getInstance().getRootUrl());
     this.builder.getTrigger().startJob(cause);
   }
 }