Class: GitProc::PullRequest

Inherits:
Process
  • Object
show all
Defined in:
lib/git-process/pull_request.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Process

#cleanup, #config, #fetch_remote_changes, #gitlib, #is_parked?, #logger, #master_branch, #remote, #run, #verify_preconditions, #workdir

Constructor Details

#initialize(dir, opts) ⇒ PullRequest

Returns a new instance of PullRequest.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/git-process/pull_request.rb', line 24

def initialize(dir, opts)
  super
  @base_branch = opts[:base_branch]
  @head_branch = opts[:head_branch]
  @_repo_name = opts[:repo_name]
  @_remote_name = opts[:server]
  @pr_number = opts[:prNumber]
  @title = opts[:title]
  @description = opts[:description]
  @user = opts[:user]
  @password = opts[:password]
end

Class Method Details

.checkout_pull_request(lib, pr_number, remote_name, repo_name, username, password, logger) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/git-process/pull_request.rb', line 87

def checkout_pull_request(lib, pr_number, remote_name, repo_name, username, password, logger)
  logger.info { 'Getting #pr_number' }

  lib.fetch(remote_name)

  pr = create_pull_request_client(lib, remote_name, repo_name, username, password)
  json = pr.pull_request(pr_number)
  head_branch_name = json.head.ref
  base_branch_name = json.base.ref

  remote_head_server_name = match_remote_to_pr_remote(lib, json.head.repo.ssh_url)
  remote_base_server_name = match_remote_to_pr_remote(lib, json.base.repo.ssh_url)
  lib.checkout(head_branch_name, :new_branch => "#{remote_head_server_name}/#{head_branch_name}")
  lib.branch(head_branch_name, :upstream => "#{remote_base_server_name}/#{base_branch_name}")
  #logger.info(json.to_hash)

  lib.fetch(remote_base_server_name) if remote_head_server_name != remote_base_server_name
  Syncer.rebase_sync(lib, true)
end

.create_pull_request(lib, server_name, remote_name, repo_name, current_branch, base_branch, head_branch, title, description, logger, username, password) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/git-process/pull_request.rb', line 76

def create_pull_request(lib, server_name, remote_name, repo_name, current_branch, base_branch, head_branch, title, description, logger, username, password)
  if base_branch == head_branch
    raise PullRequestError.new("Can not create a pull request where the base branch and head branch are the same: #{base_branch}")
  end

  lib.push(server_name, current_branch, current_branch, :force => false)
  pr = create_pull_request_client(lib, remote_name, repo_name, username, password)
  pr.create(base_branch, head_branch, title, description)
end

.create_pull_request_client(lib, remote_name, repo_name, username, password) ⇒ Object



71
72
73
# File 'lib/git-process/pull_request.rb', line 71

def create_pull_request_client(lib, remote_name, repo_name, username, password)
  GitHub::PullRequest.new(lib, remote_name, repo_name, {:user => username, :password => password})
end

.match_remote_to_pr_remote(lib, pr_remote) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/git-process/pull_request.rb', line 108

def match_remote_to_pr_remote(lib, pr_remote)
  pr_url = lib.remote.expanded_url(nil, pr_remote)
  servers = lib.remote.remote_names
  server_urls = servers.collect { |s| {:server_name => s, :url => lib.remote.expanded_url(s)} }

  pair = server_urls.find do |su|
    url = su[:url]
    uri = URI.parse(url)
    host = uri.host
    path = uri.path

    pr_uri = URI.parse(lib.remote.expanded_url(nil, pr_url))
    pr_host = pr_uri.host
    pr_path = pr_uri.path

    pr_host == host and pr_path == path
  end

  if pair.nil?
    raise GitHubService::NoRemoteRepository.new("Could not match pull request url (#{pr_url}) to any of the registered remote urls: #{server_urls.map {|s| '{' + s[:server_name] + ': ' + s[:url] + '}'}.join(', ')}")
  end

  pair[:server_name]
end

Instance Method Details

#checkout_pull_requestObject



64
65
66
# File 'lib/git-process/pull_request.rb', line 64

def checkout_pull_request
  PullRequest.checkout_pull_request(gitlib, @pr_number, remote.name, remote.repo_name, @user, @password, logger)
end

#create_pull_requestObject



53
54
55
56
57
58
59
60
61
# File 'lib/git-process/pull_request.rb', line 53

def create_pull_request
  current_branch = gitlib.branches.current.name
  base_branch = @base_branch || config.master_branch
  head_branch = @head_branch || current_branch
  title = @title || current_branch
  description = @description || ''

  PullRequest.create_pull_request(gitlib, remote.name, remote.name, remote.repo_name, current_branch, base_branch, head_branch, title, description, logger, @user, @password)
end

#create_pull_request_client(remote_name, repo_name) ⇒ Object



48
49
50
# File 'lib/git-process/pull_request.rb', line 48

def create_pull_request_client(remote_name, repo_name)
  PullRequest.create_pull_request_client(self, remote_name, repo_name, @user, @password)
end

#runnerObject



38
39
40
41
42
43
44
45
# File 'lib/git-process/pull_request.rb', line 38

def runner
  if @pr_number.nil? or @pr_number.empty?
    pr = create_pull_request
    logger.info { "Created pull request at #{pr.html_url}" }
  else
    checkout_pull_request
  end
end