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

Conversation

Turiok
Copy link
Contributor

@Turiok Turiok commented Mar 24, 2023

filtered jenkins jobs with SCMSource of the payload on comment trigger

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests - that demonstrates feature works or fixes the issue

close #256

Be careful with this PR. There isn't a lot of code but the fix seems very important I think.
If several people can test it, It'll be appreciated.

In GitLabMergeRequestCommentTrigger class to find which job build depending on the payload information of the webhook. There isn't link between GItlab Project Id and Jenkins jobs. I add a if between these 2 objects with the job parent Item.

# Verify the projectId is asked
if (gitLabSCMSource.getProjectId() == getPayload().getMergeRequest().getTargetProjectId()
    && isTrustedMember(gitLabSCMSource, sourceContext.getOnlyTrustedMembersCanTrigger())) {
    # search in all jobs of the same owner.  And there isn't another verification with the payload
    for (Job<?, ?> job : owner.getAllJobs()) {

To reproduce the previous problem :

  • Create 2 Gitlab projects with the same Owner.
  • Each project with a MR with the same branch name.
  • Configure webhook settings for Jenkins in GItlab project settings and enable comment event
  • In Jenkins create 2 multibranches pipelines and enable comment rebuild trait
  • On first MR add the comment to launch a webhook.
  • Verify the right pipeline is executed
  • On second MR add the comment to launch a webhook.
  • The pipeline executed is the same that the first one.

Be careful to test it with any other pipeline executed.

@Turiok Turiok requested a review from jetersen as a code owner March 24, 2023 23:19
@@ -56,29 +58,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

Comment on lines +61 to +62
if (MultiBranchProject.class.isAssignableFrom(job.getParent().getClass()) ) {
MultiBranchProject parentJob = (MultiBranchProject) job.getParent();
if (parentJob.getSCMSource(gitLabSCMSource.getId()) == gitLabSCMSource
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

"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

filtered jenkins jobs with SCMSource of the payload on comment trigger
@Turiok Turiok force-pushed the bugfix/issues_256_rebuild_comment_on_wrong_pipeline branch from 66cdf19 to bc67102 Compare March 24, 2023 23:33
Copy link
Member

@jetersen jetersen left a comment

Choose a reason for hiding this comment

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

Seems safer way of handling it.

@jetersen jetersen added the bug Something isn't working label Apr 3, 2023
@jetersen jetersen merged commit ad1ce6d into jenkinsci:master Apr 3, 2023
@Turiok Turiok deleted the bugfix/issues_256_rebuild_comment_on_wrong_pipeline branch May 28, 2023 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MR comment triggers rebuild on wrong project
2 participants