@SuppressWarnings("rawtypes")
  public void doQueryRuns(
      StaplerRequest request,
      StaplerResponse response,
      @QueryParameter("oslc.where") String where,
      @QueryParameter("oslc.prefix") String prefixes)
      throws IOException, URISyntaxException {
    requireGET();
    WhereClause whereClause = null;
    if (where != null) {
      whereClause = parseWhere(where, prefixes);
    }

    Collection<Job> jobs = Hudson.getInstance().getItems(Job.class);
    ArrayList<AutomationResult> results = new ArrayList<AutomationResult>();

    for (Job<?, ?> job : jobs) {
      if (jobMatchesRunWhereClause(request, job, whereClause)) {
        Iterator<?> i = job.getBuilds().iterator();
        while (i.hasNext()) {
          Run<?, ?> run = (Run<?, ?>) i.next();
          results.add(toAutomationResult(request, job, run));
        }
      }
    }

    marshalQueryResult(request, response, results);
  }
  /**
   * @param project
   * @param commitSHA1
   * @return
   */
  private Run getBuildBySHA1(Job project, String commitSHA1, boolean triggeredByMergeRequest) {
    List<Run> builds = project.getBuilds();
    for (Run build : builds) {
      BuildData data = build.getAction(BuildData.class);
      MergeRecord mergeRecord = build.getAction(MergeRecord.class);
      if (mergeRecord == null) {
        // Determine if build was triggered by a Merge Request event
        ParametersAction params = build.getAction(ParametersAction.class);

        if (params == null) continue;

        StringParameterValue sourceBranch =
            (StringParameterValue) params.getParameter("gitlabSourceBranch");
        StringParameterValue targetBranch =
            (StringParameterValue) params.getParameter("gitlabTargetBranch");
        boolean isMergeRequestBuild =
            (sourceBranch != null && !sourceBranch.value.equals(targetBranch.value));

        if (!triggeredByMergeRequest) {
          if (isMergeRequestBuild)
            // skip Merge Request builds
            continue;

          if (data.getLastBuiltRevision().getSha1String().contains(commitSHA1)) {
            return build;
          }
        } else {
          if (!isMergeRequestBuild)
            // skip Push builds
            continue;

          if (hasBeenBuilt(data, ObjectId.fromString(commitSHA1), build)) {
            return build;
          }
        }

      } else {
        Build b = data.lastBuild;
        boolean isMergeBuild =
            mergeRecord != null && !mergeRecord.getSha1().equals(b.getMarked().getSha1String());
        if (b != null
            && b.getMarked() != null
            && b.getMarked().getSha1String().equals(commitSHA1)) {
          if (triggeredByMergeRequest == isMergeBuild) {
            LOGGER.log(
                Level.FINE,
                build.getNumber()
                    + " Build found matching "
                    + commitSHA1
                    + " "
                    + (isMergeBuild ? "merge" : "normal")
                    + " build");
            return build;
          }
        }
      }
    }
    return null;
  }
 public void compute(Job job) {
   List<Run> builds = job.getBuilds();
   for (Run build : builds) {
     // a build result can be null if the build is currently building (HUDSON-15067)
     if (build.getResult() != null) {
       if (build.getResult().isBetterOrEqualTo(Result.SUCCESS)) addSuccess();
       else if (build.getResult().isBetterOrEqualTo(Result.UNSTABLE)) addUnstable();
       else if (build.getResult().isBetterOrEqualTo(Result.FAILURE)) addFail();
     }
   }
 }
 /**
  * @param project
  * @param branch
  * @return latest build of the branch specified that is not part of a merge request
  */
 @SuppressWarnings("rawtypes")
 private Run getBuildByBranch(Job project, String branch) {
   RunList<?> builds = project.getBuilds();
   for (Run build : builds) {
     BuildData data = build.getAction(BuildData.class);
     if (data != null && data.lastBuild != null) {
       MergeRecord merge = build.getAction(MergeRecord.class);
       boolean isMergeBuild =
           merge != null && !merge.getSha1().equals(data.lastBuild.getMarked().getSha1String());
       if (data.lastBuild.getRevision() != null && !isMergeBuild) {
         for (Branch b : data.lastBuild.getRevision().getBranches()) {
           if (b.getName().endsWith("/" + branch)) return build;
         }
       }
     }
   }
   return null;
 }