Skip to content

Correction of rebuild on comment on wrong pipeline #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import jenkins.branch.MultiBranchProject;
import jenkins.model.ParameterizedJobMixIn;
import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.SCMSource;
Expand Down Expand Up @@ -56,29 +57,33 @@ public void isMatch() {
if (gitLabSCMSource.getProjectId() == getPayload().getMergeRequest().getTargetProjectId()
&& isTrustedMember(gitLabSCMSource, sourceContext.getOnlyTrustedMembersCanTrigger())) {
for (Job<?, ?> job : owner.getAllJobs()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this for, we loop to all jobs. Even out of the projectId. And there isn't more check after with the payload

if (mergeRequestJobNamePattern.matcher(job.getName()).matches()) {
String expectedCommentBody = sourceContext.getCommentBody();
Pattern pattern = Pattern.compile(expectedCommentBody,
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
if (commentBody == null || pattern.matcher(commentBody)
.matches()) {
ParameterizedJobMixIn.scheduleBuild2(job, 0,
new CauseAction(
new GitLabMergeRequestCommentCause(commentUrl, getPayload())));
LOGGER.log(Level.INFO,
"Triggered build for {0} due to MR comment on {1}",
new Object[] {
job.getFullName(),
getPayload().getProject().getPathWithNamespace()
});
} else {
LOGGER.log(Level.INFO,
"MR comment does not match the trigger build string ({0}) for {1}",
new Object[] { expectedCommentBody, job.getFullName() });
if (MultiBranchProject.class.isAssignableFrom(job.getParent().getClass()) ) {
MultiBranchProject parentJob = (MultiBranchProject) job.getParent();
if (parentJob.getSCMSource(gitLabSCMSource.getId()) == gitLabSCMSource
Comment on lines +60 to +62
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to check the link between the source of the payload and the job in the for.

I don't know if there is a better solution

&& mergeRequestJobNamePattern.matcher(job.getName()).matches()) {
String expectedCommentBody = sourceContext.getCommentBody();
Pattern pattern = Pattern.compile(expectedCommentBody,
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
if (commentBody == null || pattern.matcher(commentBody)
.matches()) {
ParameterizedJobMixIn.scheduleBuild2(job, 0,
new CauseAction(
new GitLabMergeRequestCommentCause(commentUrl, getPayload())));
LOGGER.log(Level.INFO,
"Triggered build for {0} due to MR comment on {1}",
new Object[] {
job.getFullName(),
getPayload().getProject().getPathWithNamespace()
});
} else {
LOGGER.log(Level.INFO,
"MR comment does not match the trigger build string ({0}) for {1}",
new Object[] { expectedCommentBody, job.getFullName() });
}
break;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this break, only one job can be executed.

I'm wondering if it is possible to have 2 multi branch pipelines with the same project?
Because if it can, this break stop the wake up of the second pipeline in case of comment

}
break;
jobFound = true;
}
jobFound = true;
}
}
}
Expand Down