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

Add support for GitLab merge requests (MRs) #175

Merged
merged 5 commits into from
Jun 2, 2020

Conversation

chamer81
Copy link

Adds an option to create a gitlab MR.

Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

I didn't look too closely at the code, but I wonder if we should lazy load the Gitlab gem + configure it (and the same for Github). That should keep start up time lower.

.vscode/launch.json Outdated Show resolved Hide resolved
bin/bundle Outdated Show resolved Hide resolved
bin/rspec Outdated Show resolved Hide resolved
.rubocop_todo.yml Outdated Show resolved Hide resolved
lib/modulesync.rb Outdated Show resolved Hide resolved
@bittner bittner marked this pull request as draft May 18, 2020 11:57
@bittner bittner changed the title Feature/add gitlab support Add support for GitLab merge requests (MRs) May 18, 2020
@bittner
Copy link
Contributor

bittner commented May 18, 2020

Thank you @ekohl for the quick feedback. We'll take that immediately into account. We're still at fixing some rough edges of the implementation and doing internal review.

@bittner
Copy link
Contributor

bittner commented May 18, 2020

Relates to #80 (comment).

@chamer81 chamer81 force-pushed the feature/add-gitlab-support branch from db4812d to cd3ef1e Compare May 19, 2020 13:20
@bastelfreak bastelfreak requested a review from ekohl May 19, 2020 19:30
@bittner bittner force-pushed the feature/add-gitlab-support branch from 9ee05c7 to 442c9ca Compare May 19, 2020 19:31
@bittner
Copy link
Contributor

bittner commented May 19, 2020

We have cleaned up the commits and I've consolidated the changes into a single commit.

There are still a few topics open: (at least from a first, quick glance)

  • Apart from GitLab.com, we need to allow GitLab support for private instances (config.endpoint is set for this); presumably, this needs to be configurable
  • The added / updated tests don't seem complete
  • The lazy-loading of the GitHub and GitLab dependencies is still pending realization

Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

I think this and the Github PR code should live in lib/modulesync/pr.rb. You'd have something like:

module ModuleSync
  class PullRequest
    def initialize(implementation, token)
      @implementation = implementation
      case @implementation
      when :github
        @client = Octokit::Client.new(:access_token => token)
      when :gitlab
        @client = Gitlab::Client.new(:private_token => token)
      else
        raise "Unknown implementation #{implementation}"
      end
    end

    def has_pr?(repo_path, source, target)
      requests = case @implementation
                 when :github
                   @client.pull_requests()
                 when :gitlab
                   @client.merge_requests()
                 end

      requests.any?
    end

    def create_pr(repo_path, source, target)
      case @implementation
      when :github
        @client.pull_request()
      when :gitlab
        @client.create_merge_request()
      end
    end

    def add_labels_to_pr(repo_path, pr, labels)
      # ....
    end
  end
end

You can also implement PullRequest and MergeRequest that implement the same interface.

This keeps lib/modulesync.rb small. It's already way too big now. An additional benefit is that you cache the client instance which I hope keeps the HTTP connection open. Not sure it does that right now.

.gitignore Outdated
Comment on lines 4 to 10
.ruby-version
.vscode/
bin/bundle
bin/rspec
modules/
tmp/
vendor/
Copy link
Member

Choose a reason for hiding this comment

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

This would typically be a separate commit.

Gemfile Outdated
@@ -2,5 +2,7 @@ source ENV['GEM_SOURCE'] || 'https://rubygems.org'

gemspec

gem 'aruba'
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed, looks redundant:

spec.add_development_dependency 'aruba', '~> 0.14'

Gemfile Outdated
gem 'cucumber', '< 3.0' if RUBY_VERSION < '2.1'
gem 'gitlab' # gem 'gitlab', github: 'NARKOZ/gitlab'
gem 'octokit', '~> 4.9'
Copy link
Member

Choose a reason for hiding this comment

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

This was there, but I can't explain why because this should be sufficent:

spec.add_runtime_dependency 'octokit', '~>4.0'

Gemfile Outdated
gem 'cucumber', '< 3.0' if RUBY_VERSION < '2.1'
gem 'gitlab' # gem 'gitlab', github: 'NARKOZ/gitlab'
Copy link
Member

Choose a reason for hiding this comment

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

This is redundant since it's in the gemspec

@mhutter mhutter force-pushed the feature/add-gitlab-support branch from 442c9ca to e4ff51f Compare May 25, 2020 14:33
@mhutter
Copy link
Contributor

mhutter commented May 25, 2020

  • Updated the first commit to clean up dependencies in Gemfile and gemspec.
  • Added a second commit to refactor the existing code. This (as a side-effect) also implements lazy-loading and client reuse.
  • Moved the .gitignore change into its own commit

Signed-off-by: Manuel Hutter <manuel@vshn.ch>
@mhutter mhutter force-pushed the feature/add-gitlab-support branch from e1d51b5 to d0d1ca0 Compare May 25, 2020 14:48
@mhutter
Copy link
Contributor

mhutter commented May 25, 2020

Still looking into that failing cucumber test, not sure how that's related...

Update: noticed that the expected outcome was changed in this PR; I reverted this (was probably changed during implemented; the original implementation of this PR might have changed error behavior)

bittner and others added 2 commits May 25, 2020 17:18
Adds the capability of creating GitLab merge requests
following the approach currently implemented for GitHub.
We try to make the user experience simply by sticking to calling
the feature "PR" / "--pr" also for GitLab. Which of both options,
GitHub or GitLab, is chosen for processing the user request is
determined by the environment variable being set:
- GITHUB_TOKEN ... a PR on GitHub will be created
- GITLAB_TOKEN ... a MR on GitLab will be created
* Move implementation into separate modules
* Lazy-load dependencies
* Reuse GitHub/GitLab clients
* Add some basic specs for the GitLab implementation

Signed-off-by: Manuel Hutter <manuel@vshn.ch>
@mhutter mhutter force-pushed the feature/add-gitlab-support branch 2 times, most recently from a50a730 to 4b3ad12 Compare May 25, 2020 15:34
This commit moves configuration validation (--pr cannot be used without
--branch) into initialization. This catches the error early on, before
any actual work is done.

Signed-off-by: Manuel Hutter <manuel@vshn.ch>
@mhutter mhutter force-pushed the feature/add-gitlab-support branch from 4b3ad12 to a77b968 Compare May 25, 2020 15:37
@mhutter
Copy link
Contributor

mhutter commented May 25, 2020

One thing that caught my eye when moving code around: The Gitlab implementation will ensure the configured labels are added to the PR when the PR is created, but will NOT add any labels if there is already an open PR around.

The GitHub implementation will always update the existing PR - and fail horribly from looking at the code (note this was already broken before this PR)

@ekohl what is the intended behaviour?

@bittner
Copy link
Contributor

bittner commented May 29, 2020

@ekohl Can you clarify?

Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

I think the current code looks like a good solution and an improvement over the current implementation. Just a small comment about the constants, otherwise 👍

I like that it only loads the Github/Gitlab code if a PR was requested which keeps the start up time lower.

Comment on lines 12 to 13
GITHUB_TOKEN = ENV.fetch('GITHUB_TOKEN', '')

Octokit.configure do |c|
c.api_endpoint = ENV.fetch('GITHUB_BASE_URL', 'https://api.github.com')
end
GITLAB_TOKEN = ENV.fetch('GITLAB_TOKEN', '')
Copy link
Member

Choose a reason for hiding this comment

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

I think we can avoid these constants and only use them inline since they're only used in a single place.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point, totally missed it. Will adjust accordingly

@ekohl
Copy link
Member

ekohl commented Jun 1, 2020

This looks good to me. It's still marked as a draft so let me know when you think it's ready to be merged.

They are not required on a global scope, so remove them from there

Signed-off-by: Manuel Hutter <manuel@vshn.ch>
Signed-off-by: Manuel Hutter <manuel@hutter.io>
@mhutter mhutter force-pushed the feature/add-gitlab-support branch from 355f85f to bf27fd8 Compare June 1, 2020 10:50
@mhutter
Copy link
Contributor

mhutter commented Jun 1, 2020

I broke the check in my last commit, fixed.

I think the PR is ready, can you remove the "Draft" status @chamer81 ?

I'll open a new Issue to discuss the PR label behaviour. Fixing it should be trivial.

@bittner bittner marked this pull request as ready for review June 2, 2020 08:41
@bittner bittner requested a review from ekohl June 2, 2020 08:42
Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

I'm good with this.

@ekohl ekohl merged commit b590050 into voxpupuli:master Jun 2, 2020
@ekohl
Copy link
Member

ekohl commented Jun 2, 2020

Thanks!

@mhutter mhutter deleted the feature/add-gitlab-support branch June 2, 2020 13:30
@bittner
Copy link
Contributor

bittner commented Jun 2, 2020

Thanks everyone for the good collaboration! 🥇 🎊

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