Skip to content
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

Adds a Modern Mode handling for merge requests. #285

Closed
wants to merge 20 commits into from

Conversation

pbendersky
Copy link

In Modern Mode, the plugin will fetch Merge Requests from origin instead of using parameterized builds.

This supersedes #252 and works with the refactored 1.2 codebase.

@pbendersky
Copy link
Author

Here's the original comment I wrote on #252:

This PR enhances gitlab-plugin by adding two operation modes: Modern and Legacy.
Legacy is what we currently have, that works with older versions of GitLab.

Modern works with GitLab 8.1 or better, and provides an easier configuration of projects in Jenkins (a single Git repository is enough) and much better handling of Merge Requests (MR are fetched from origin and matched by SHA1 instead of comparing branch names).

This PR fixes the two issues I reported: #239 and #155.

@omehegan
Copy link
Member

omehegan commented May 2, 2016

@pbendersky thanks a lot for doing this work!

@@ -5,6 +5,7 @@
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabApi;
import com.dabsquared.gitlabjenkins.gitlab.api.model.BuildState;
import hudson.EnvVars;
import hudson.model.AbstractBuild;
Copy link
Member

Choose a reason for hiding this comment

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

Are you using AbstractBuild here? Note that, in order to support Pipeline jobs, AbstractJob and AbstractBuild can't be used. See https://github.com/jenkinsci/pipeline-plugin/blob/master/DEVGUIDE.md

Copy link
Author

Choose a reason for hiding this comment

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

I'm not. I'll add a commit removing it.

@pbendersky
Copy link
Author

@coder-hugo @omehegan I'm keeping this PR updated with master, so it's easier to merge when you want to.
Please let me know if you want to do additional changes, or discuss any decisions I made.

@omehegan
Copy link
Member

omehegan commented May 5, 2016

@pbendersky thanks! I'm going to try to test it in my environment tomorrow, where we use GitLab CE 7.14. That's probably the most I can do with it. @coder-hugo said that he will look through it as soon as he can.

pbendersky added 3 commits May 9, 2016 15:25
# Conflicts:
#	src/main/java/com/dabsquared/gitlabjenkins/cause/CauseData.java
#	src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerLegacyImpl.java
…ild was initiated manually instead of through a GitLab trigger.
@omehegan
Copy link
Member

@pbendersky I'm working on testing this. The only merge conflict is with changes to the README, so it should be easy to resolve that.

@pbendersky
Copy link
Author

Nice! Let me know if you need anything, or you have comments on the code.

@omehegan
Copy link
Member

@pbendersky there seems to be a bug in the existing HEAD of master, I'm getting an NPE when I trigger a build with that code. Unrelated to your change. I'll see if I can track it down.

@tommyminds
Copy link

Is there any update on the status of this PR? Or on the next release (1.2.3) in general? There are some fixes in master that would be good to have released.

@omehegan
Copy link
Member

I determined yesterday that the current master has a bug that breaks compatibility with GitLab 7.14 and possibly everything before GitLab 8.5. I have a patch for it that just needs cleanup. I will merge that this week and then I can test this change.

@omehegan
Copy link
Member

OK, I have successfully tested this with GitLab 7.14, using the legacy mode, and it correctly triggers a build based on push or MR, and checks out the right branch. So that much looks good to me.

public enum GitLabPluginMode {
LEGACY,
MODERN;
/*
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to keep all this commented out stuff?

@omehegan
Copy link
Member

I posted some minor comments on the code. Also wanted to note that I tested with a Pipeline job and it works; this should imply that it will work with Freestyle jobs as well.

@coder-hugo pending comments from you before we merge this. I've reviewed as much as I think I can.

- Addressed comments in PR
@pbendersky
Copy link
Author

pbendersky commented May 19, 2016

@omehegan Just fixed these minor issues, and merged from upstream, to make things easier.

@@ -21,7 +21,11 @@
- [Parameterized builds](#parameterized-builds)
- [Branch filtering](#branch-filtering)
- [Build Tags](#build-tags)
<<<<<<< HEAD
Copy link
Member

Choose a reason for hiding this comment

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

Looks like you committed the merge conflicts of README...

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, just added a new commit fixing it.

@omehegan
Copy link
Member

omehegan commented Jun 1, 2016

@coder-hugo ping

List<PushHookTriggerHandler> result = new ArrayList<PushHookTriggerHandler>();
if (triggerOnPush) {
result.add(new PushHookTriggerHandlerImpl());
}
if (triggerOpenMergeRequestOnPush == TriggerOpenMergeRequest.both) {
if (gitLabPluginMode == GitLabPluginMode.LEGACY && triggerOpenMergeRequestOnPush == TriggerOpenMergeRequest.both) {
Copy link
Contributor

Choose a reason for hiding this comment

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

With this you disable the functionality to rebuild open MRs if the target branch has changed for the modern mode. Why you are doing this?

Copy link
Author

Choose a reason for hiding this comment

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

Because that case is handled by the Merge Request hook instead (if a MR is created and a commit added, the MR hook is called again).

Copy link
Contributor

Choose a reason for hiding this comment

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

Nope. I just tested this with GitLab 8.7:

  1. I configured the job in Jenkins as you described in the README
  2. I configured my GitLab repo to send MR events to the Jenkins
  3. I created a MR for a test branch into the master -> a new build was triggerd
  4. I pushed some changes to the master (target branch) -> no build was triggered as no event was sent by GitLab

Copy link
Author

Choose a reason for hiding this comment

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

You are right. The case I was referring was new commits on the source branch (the user's fork). That gets handled by the MR trigger. I'll take a look at this, it might be a limitation of the "Modern" approach (if that's the case, I'll document it to reflect it).

Copy link
Author

Choose a reason for hiding this comment

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

@coder-hugo just looked a bit at this... seems to be a limitation of the Modern mode. Since the user only has one repo configured, a new commit to master in the target branch can't be merged to the MR by Jenkins.
I suggest:

  1. Updating the Readme to reflect this.
  2. Ideally (I'll check how can it be done) change the drop down when selecting Modern mode, so the user can only select "Source branch" on when to re-build.
    Thoughts?

…ing open MR when pushing to source branch.

- Changed the settings so when switching the plugin Mode the options for "triggerOpenMergeRequest" are refreshed and show only the possible values.
@pbendersky
Copy link
Author

@coder-hugo just added a two commits:

  • I clarified in the Readme that in Modern mode only pushes to the target repo will trigger a rebuild.
  • I added a target value to TriggerOpenMergeRequest.java
  • I changed GitLabPushTrigger.java so it reloads the options for TriggerOpenMergeRequest when you change the plugin mode and only shows the valid values for each mode.
    Hope this makes it clearer to users.

I also merged the latest changes from master, so it passes the checks here.

@coder-hugo
Copy link
Contributor

@pbendersky it looks like you have interchanged the meaning of source and target branch of a MR. The source branch is the one that contains the changes that shall be merged to the target branch. So the modern mode supports re-building open MRs by pushes to the source but not to the target branch. And as the target branch is part of the configured repository it should be possible to re-build open MRs also for pushes to the target branch.

@pbendersky
Copy link
Author

Oh... I'll revert the source / target then.

So, a better approach (that would cover all the cases) would be to instead of fetching from merge-requests/x to fetch from origin and merge the contents of merge-requests/x. That way, commits added to either source or target would get applied. Would that work? (of course, respecting the user settings for this).

@pbendersky
Copy link
Author

@coder-hugo I see one problem, however. Let's say there are 3 merge requests open, and a user commits to master in the upstream repository. How many builds should that trigger? If this works as you suggest, we would need to trigger four builds: one for the push to the upstream repo, and 3 for the updated merge requests. Is that correct?

@omorillo
Copy link
Contributor

omorillo commented Jun 7, 2016

@pbendersky I don't see any problem there. You would like to know if the master is still stable but also if the source branches can be safely merged to the target branch.

@pbendersky
Copy link
Author

@omorillo @coder-hugo I'm trying to wrap this up... Pending tasks (as discussed):

  1. Merge origin with the merge request before build.
  2. Add ability to trigger multiple builds on open merge requests when pushing to the target repo.

Now I need some help, and maybe you can give me a pointer... We currently create a RevisionParameterAction to tell Jenkins what revision to checkout. Is there any Action I can use to tell it to merge two repositories? I could not find it, and I'm not that familiar with Jenkins' API yet.

@coder-hugo
Copy link
Contributor

@pbendersky you don't need to add a special Action to the build for merging two branches. This will be configured as additional behavior (Merge before build) of the git SCM config. (see here)

@pbendersky
Copy link
Author

@coder-hugo right, but it loses some of the charm. As it is now, you simply set up origin on Jenkins and it will know how to fetch the merge requests from it. I'd like to keep that simplicity if possible. Maybe adding a second repository on build time? Other idea?

@coder-hugo
Copy link
Contributor

@pbendersky you can try to extend the PreBuildMerge of the git-plugin and pass the required UserMergeOptions for GitLab within the constructor to the super constructor. This extended class should then appear as a new additional behavior for the git SCM. This should keep the job setup as simple as possible.

# Conflicts:
#	README.md
#	src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java
#	src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerFactory.java
#	src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerLegacyImpl.java
#	src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/push/PushHookTriggerHandlerFactory.java
#	src/main/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdater.java
#	src/test/java/com/dabsquared/gitlabjenkins/publisher/GitLabCommitStatusPublisherTest.java
#	src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerImplTest.java
@pbendersky
Copy link
Author

@coder-hugo @omehegan please take a look at #370. While I'm not giving up on this PR (I still think the setup with a single repository is easier to manage), the minor changes in configuration explained in #370 achieve the same result.

@pabloa
Copy link

pabloa commented Aug 3, 2016

Hello, any news with this Pull Request? I am interested because #98

@pbendersky
Copy link
Author

I think it's a dead end at this point... With the configuration now in the README, I don't think this is needed. Did you try that?

@omehegan
Copy link
Member

Should we make a call on this one? It sounds like we should close it. Agreed?

@pbendersky
Copy link
Author

Agreed.

@pbendersky pbendersky closed this Sep 16, 2016
exceed-alae pushed a commit to exceed-alae/gitlab-plugin that referenced this pull request May 20, 2022
exceed-alae pushed a commit to exceed-alae/gitlab-plugin that referenced this pull request May 20, 2022
Merge pull request jenkinsci#285 from jenkinsci/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants