Class: GitHelper::GitLabMergeRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/git_helper/merge_request.rb

Instance Method Summary collapse

Instance Method Details

#createObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/git_helper/merge_request.rb', line 7

def create
  begin
    # Ask these questions right away
    base_branch
    new_mr_title
    options = {
      source_branch: local_branch,
      target_branch: base_branch,
      squash: squash_merge_request,
      remove_source_branch: remove_source_branch,
      description: new_mr_body
    }

    puts "Creating merge request: #{new_mr_title}"
    mr = gitlab_client.create_merge_request(local_project, new_mr_title, options)

    if mr.diff_refs.base_sha == mr.diff_refs.head_sha
      puts "Merge request was created, but no commits have been pushed to GitLab: #{mr.web_url}"
    else
      puts "Merge request successfully created: #{mr.web_url}"
    end
  rescue Gitlab::Error::Conflict => e
    puts 'Could not create merge request:'
    puts '  A merge request already exists for this branch'
  rescue Exception => e
    puts 'Could not create merge request:'
    puts e.message
  end
end

#mergeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/git_helper/merge_request.rb', line 37

def merge
  begin
    # Ask these questions right away
    mr_id
    options = {}
    options[:should_remove_source_branch] = existing_mr.should_remove_source_branch || existing_mr.force_remove_source_branch
    options[:squash] = existing_mr.squash
    options[:squash_commit_message] = existing_mr.title

    puts "Merging merge request: #{mr_id}"
    merge = gitlab_client.accept_merge_request(local_project, mr_id, options)

    if merge.merge_commit_sha.nil?
      options[:squash] = true
      merge = gitlab_client.accept_merge_request(local_project, mr_id, options)
    end

    puts "Merge request successfully merged: #{merge.merge_commit_sha}"
  rescue Gitlab::Error::MethodNotAllowed => e
    puts 'Could not merge merge request:'
    puts '  The merge request is not mergeable'
  rescue Gitlab::Error::NotFound => e
    puts 'Could not merge merge request:'
    puts "  Could not a locate a merge request to merge with ID #{mr_id}"
  rescue Exception => e
    puts 'Could not merge merge request:'
    puts e.message
  end
end