@@ -444,19 +444,62 @@ private AbstractBuild getBuildBySHA1(AbstractProject project, String commitSHA1,
444444 List <AbstractBuild > builds = project .getBuilds ();
445445 for (AbstractBuild build : builds ) {
446446 BuildData data = build .getAction (BuildData .class );
447- Build b = data .lastBuild ;
448- MergeRecord merge = build .getAction (MergeRecord .class );
449- boolean isMergeBuild = merge !=null && !merge .getSha1 ().equals (b .getMarked ().getSha1String ());
450- if (b !=null && b .getMarked ()!=null && b .getMarked ().getSha1String ().equals (commitSHA1 )){
451- if (triggeredByMergeRequest == isMergeBuild ){
452- LOGGER .log (Level .FINE , build .getNumber ()+" Build found matching " +commitSHA1 +" " +(isMergeBuild ? "merge" :"normal" )+" build" );
453- return build ;
447+ MergeRecord mergeRecord = build .getAction (MergeRecord .class );
448+ if (mergeRecord == null ) {
449+ //Determine if build was triggered by a Merge Request event
450+ ParametersAction params = build .getAction (ParametersAction .class );
451+
452+ if (params == null ) continue ;
453+
454+ StringParameterValue sourceBranch = (StringParameterValue ) params .getParameter ("gitlabSourceBranch" );
455+ StringParameterValue targetBranch = (StringParameterValue ) params .getParameter ("gitlabTargetBranch" );
456+ boolean isMergeRequestBuild = (sourceBranch != null && !sourceBranch .value .equals (targetBranch .value ));
457+
458+ if (!triggeredByMergeRequest ) {
459+ if (isMergeRequestBuild )
460+ // skip Merge Request builds
461+ continue ;
462+
463+ if (data .getLastBuiltRevision ().getSha1String ().contains (commitSHA1 )) {
464+ return build ;
465+ }
466+ } else {
467+ if (!isMergeRequestBuild )
468+ // skip Push builds
469+ continue ;
470+
471+ if (hasBeenBuilt (data , ObjectId .fromString (commitSHA1 ), build )) {
472+ return build ;
473+ }
454474 }
475+
476+ } else {
477+ Build b = data .lastBuild ;
478+ boolean isMergeBuild = mergeRecord !=null && !mergeRecord .getSha1 ().equals (b .getMarked ().getSha1String ());
479+ if (b !=null && b .getMarked ()!=null && b .getMarked ().getSha1String ().equals (commitSHA1 )){
480+ if (triggeredByMergeRequest == isMergeBuild ){
481+ LOGGER .log (Level .FINE , build .getNumber ()+" Build found matching " +commitSHA1 +" " +(isMergeBuild ? "merge" :"normal" )+" build" );
482+ return build ;
483+ }
484+ }
455485 }
456486 }
457487 return null ;
458488 }
459489
490+ private boolean hasBeenBuilt (BuildData data , ObjectId sha1 , AbstractBuild build ) {
491+ try {
492+ for (Build b : data .getBuildsByBranchName ().values ()) {
493+ if (b .getBuildNumber () == build .number
494+ && b .marked .getSha1 ().equals (sha1 ))
495+ return true ;
496+ }
497+ return false ;
498+ } catch (Exception ex ) {
499+ return false ;
500+ }
501+ }
502+
460503 /**
461504 *
462505 * @param project
0 commit comments