/** * @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; }
private boolean hasBeenBuilt(BuildData data, ObjectId sha1, Run build) { try { for (Build b : data.getBuildsByBranchName().values()) { if (b.getBuildNumber() == build.number && b.marked.getSha1().equals(sha1)) return true; } return false; } catch (Exception ex) { return false; } }