Class: GitHelper::GitHubPullRequest

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ GitHubPullRequest

Returns a new instance of GitHubPullRequest.



5
6
7
8
9
10
# File 'lib/git_helper/pull_request.rb', line 5

def initialize(options)
  @local_repo = options[:local_project]
  @local_branch = options[:local_branch]
  @local_code = options[:local_code]
  @cli = options[:cli]
end

Instance Attribute Details

#base_branchObject

Returns the value of attribute base_branch.



3
4
5
# File 'lib/git_helper/pull_request.rb', line 3

def base_branch
  @base_branch
end

#cliObject

Returns the value of attribute cli.



3
4
5
# File 'lib/git_helper/pull_request.rb', line 3

def cli
  @cli
end

#local_branchObject

Returns the value of attribute local_branch.



3
4
5
# File 'lib/git_helper/pull_request.rb', line 3

def local_branch
  @local_branch
end

#local_codeObject

Returns the value of attribute local_code.



3
4
5
# File 'lib/git_helper/pull_request.rb', line 3

def local_code
  @local_code
end

#local_repoObject

Returns the value of attribute local_repo.



3
4
5
# File 'lib/git_helper/pull_request.rb', line 3

def local_repo
  @local_repo
end

#new_pr_titleObject

Returns the value of attribute new_pr_title.



3
4
5
# File 'lib/git_helper/pull_request.rb', line 3

def new_pr_title
  @new_pr_title
end

Instance Method Details

#create(options) ⇒ Object



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/pull_request.rb', line 12

def create(options)
  @base_branch = options[:base_branch]
  @new_pr_title = options[:new_title]

  begin
    new_pr_body

    puts "Creating pull request: #{new_pr_title}"
    pr = octokit_client.create_pull_request(local_repo, base_branch, local_branch, new_pr_title, new_pr_body)
    puts "Pull request successfully created: #{pr.html_url}"
  rescue Octokit::UnprocessableEntity => e
    puts 'Could not create pull request:'
    if e.message.include?('pull request already exists')
      puts '  A pull request already exists for this branch'
    elsif e.message.include?('No commits between')
      puts '  No commits have been pushed to GitHub'
    else
      puts e.message
    end
  rescue Exception => e
    puts 'Could not create pull 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
66
67
68
69
70
71
72
# File 'lib/git_helper/pull_request.rb', line 37

def merge
  begin
    pr_id
    merge_method

    puts "Merging pull request: #{pr_id}"
    merge = octokit_client.merge_pull_request(local_repo, pr_id, existing_pr.title, { merge_method: merge_method })
    puts "Pull request successfully merged: #{merge.sha}"
  rescue Octokit::UnprocessableEntity => e
    puts 'Could not merge pull request:'
    puts e.message
  rescue Octokit::NotFound => e
    puts 'Could not merge pull request:'
    puts "  Could not a locate a pull request to merge with ID #{pr_id}"
  rescue Octokit::MethodNotAllowed => e
    puts 'Could not merge pull request:'
    if e.message.include?('405 - Required status check')
      puts '  A required status check has not passed'
    elsif e.message.include?('405 - Base branch was modified')
      puts '  The base branch has been modified'
    elsif e.message.include?('405 - Pull Request is not mergeable')
      puts '  The pull request is not mergeable'
    elsif e.message.include?('405 - Rebase merges are not allowed on this repository')
      puts '  Rebase merges are not allowed on this repository'
    elsif e.message.include?('405 - Merge commits are not allowed on this repository')
      puts '  Merge commits are not allowed on this repository'
    elsif e.message.include?('405 - Squash commits are not allowed on this repository')
      puts '  Squash merges are not allowed on this repository'
    else
      puts e.message
    end
  rescue Exception => e
    puts 'Could not merge pull request:'
    puts e.message
  end
end