Sha256: 500b4f7849abf5a3cf69376c650fc001b2e431a6c79390d019f961a261236fd3

Contents?: true

Size: 1.96 KB

Versions: 5

Compression:

Stored size: 1.96 KB

Contents

require 'gitlab'
require 'modulesync/util'

module ModuleSync
  module PR
    # GitLab creates and manages merge requests on gitlab.com or private GitLab
    # installations.
    class GitLab
      def initialize(token, endpoint)
        @api = Gitlab::Client.new(
          :endpoint => endpoint,
          :private_token => token
        )
      end

      def manage(namespace, module_name, options)
        repo_path = File.join(namespace, module_name)
        branch = options[:remote_branch] || options[:branch]
        head = "#{namespace}:#{branch}"
        target_branch = options[:pr_target_branch] || 'master'

        if options[:noop]
          $stdout.puts \
            "Using no-op. Would submit MR '#{options[:pr_title]}' to #{repo_path} " \
            "- merges #{branch} into #{target_branch}"
          return
        end

        merge_requests = @api.merge_requests(repo_path,
                                             :state => 'opened',
                                             :source_branch => head,
                                             :target_branch => target_branch)
        unless merge_requests.empty?
          # Skip creating the MR if it exists already.
          $stdout.puts "Skipped! #{merge_requests.length} MRs found for branch #{branch}"
          return
        end

        mr_labels = ModuleSync::Util.parse_list(options[:pr_labels])
        mr = @api.create_merge_request(repo_path,
                                       options[:pr_title],
                                       :source_branch => branch,
                                       :target_branch => target_branch,
                                       :labels => mr_labels)
        $stdout.puts \
          "Submitted MR '#{options[:pr_title]}' to #{repo_path} " \
          "- merges #{branch} into #{target_branch}"

        return if mr_labels.empty?
        $stdout.puts "Attached the following labels to MR #{mr.iid}: #{mr_labels.join(', ')}"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
modulesync-2.2.0 lib/modulesync/pr/gitlab.rb
modulesync-2.1.1 lib/modulesync/pr/gitlab.rb
modulesync-2.1.0 lib/modulesync/pr/gitlab.rb
modulesync-2.0.2 lib/modulesync/pr/gitlab.rb
modulesync-2.0.1 lib/modulesync/pr/gitlab.rb