Skip to content

Commit ad1ce6d

Browse files
authored
Correction of rebuild on comment on wrong pipeline (#290)
1 parent 0ddadb8 commit ad1ce6d

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestCommentTrigger.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.logging.Level;
99
import java.util.logging.Logger;
1010
import java.util.regex.Pattern;
11+
import jenkins.branch.MultiBranchProject;
1112
import jenkins.model.ParameterizedJobMixIn;
1213
import jenkins.scm.api.SCMHeadObserver;
1314
import jenkins.scm.api.SCMSource;
@@ -56,29 +57,33 @@ public void isMatch() {
5657
if (gitLabSCMSource.getProjectId() == getPayload().getMergeRequest().getTargetProjectId()
5758
&& isTrustedMember(gitLabSCMSource, sourceContext.getOnlyTrustedMembersCanTrigger())) {
5859
for (Job<?, ?> job : owner.getAllJobs()) {
59-
if (mergeRequestJobNamePattern.matcher(job.getName()).matches()) {
60-
String expectedCommentBody = sourceContext.getCommentBody();
61-
Pattern pattern = Pattern.compile(expectedCommentBody,
62-
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
63-
if (commentBody == null || pattern.matcher(commentBody)
64-
.matches()) {
65-
ParameterizedJobMixIn.scheduleBuild2(job, 0,
66-
new CauseAction(
67-
new GitLabMergeRequestCommentCause(commentUrl, getPayload())));
68-
LOGGER.log(Level.INFO,
69-
"Triggered build for {0} due to MR comment on {1}",
70-
new Object[] {
71-
job.getFullName(),
72-
getPayload().getProject().getPathWithNamespace()
73-
});
74-
} else {
75-
LOGGER.log(Level.INFO,
76-
"MR comment does not match the trigger build string ({0}) for {1}",
77-
new Object[] { expectedCommentBody, job.getFullName() });
60+
if (MultiBranchProject.class.isAssignableFrom(job.getParent().getClass()) ) {
61+
MultiBranchProject parentJob = (MultiBranchProject) job.getParent();
62+
if (parentJob.getSCMSource(gitLabSCMSource.getId()) == gitLabSCMSource
63+
&& mergeRequestJobNamePattern.matcher(job.getName()).matches()) {
64+
String expectedCommentBody = sourceContext.getCommentBody();
65+
Pattern pattern = Pattern.compile(expectedCommentBody,
66+
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
67+
if (commentBody == null || pattern.matcher(commentBody)
68+
.matches()) {
69+
ParameterizedJobMixIn.scheduleBuild2(job, 0,
70+
new CauseAction(
71+
new GitLabMergeRequestCommentCause(commentUrl, getPayload())));
72+
LOGGER.log(Level.INFO,
73+
"Triggered build for {0} due to MR comment on {1}",
74+
new Object[] {
75+
job.getFullName(),
76+
getPayload().getProject().getPathWithNamespace()
77+
});
78+
} else {
79+
LOGGER.log(Level.INFO,
80+
"MR comment does not match the trigger build string ({0}) for {1}",
81+
new Object[] { expectedCommentBody, job.getFullName() });
82+
}
83+
break;
7884
}
79-
break;
85+
jobFound = true;
8086
}
81-
jobFound = true;
8287
}
8388
}
8489
}

0 commit comments

Comments
 (0)