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.



7
8
9
10
11
12
# File 'lib/git_helper/pull_request.rb', line 7

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

Instance Attribute Details

#base_branchObject

Returns the value of attribute base_branch.



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

def base_branch
  @base_branch
end

#highlineObject

Returns the value of attribute highline.



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

def highline
  @highline
end

#local_branchObject

Returns the value of attribute local_branch.



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

def local_branch
  @local_branch
end

#local_codeObject

Returns the value of attribute local_code.



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

def local_code
  @local_code
end

#local_repoObject

Returns the value of attribute local_repo.



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

def local_repo
  @local_repo
end

#new_pr_titleObject

Returns the value of attribute new_pr_title.



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

def new_pr_title
  @new_pr_title
end

Instance Method Details

#create(options) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/git_helper/pull_request.rb', line 16

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

  options = {
    head: local_branch,
    base: base_branch,
    body: new_pr_body,
    title: new_pr_title
  }

  puts "Creating pull request: #{new_pr_title}"
  pr = github_client.create_pull_request(local_repo, options)

  raise StandardError, pr.errors.first['message'] if pr.html_url.nil?

  puts "Pull request successfully created: #{pr.html_url}"
rescue StandardError => e
  puts 'Could not create pull request:'

  if e.message.include?('A 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
end

#mergeObject

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/git_helper/pull_request.rb', line 49

def merge
  pr_id

  options = {
    merge_method: merge_method,
    commit_title: existing_pr.title
  }

  puts "Merging pull request: #{pr_id}"
  merge = github_client.merge_pull_request(local_repo, pr_id, options)

  raise StandardError, merge.message if merge.sha.nil?

  puts "Pull request successfully merged: #{merge.sha}"
rescue StandardError => e
  puts 'Could not merge pull request:'

  if e.message.include?('Not found')
    puts '  Could not a locate a pull request to merge with given ID'
  else
    puts "  #{e.message}"
  end
end