git-process-lib 2.0.4 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,7 +18,7 @@ require 'octokit/repository'
18
18
  module GitHub
19
19
 
20
20
  class PullRequest
21
- attr_reader :gitlib, :repo, :remote_name, :client, :configuration
21
+ attr_reader :gitlib, :repo, :remote_name, :configuration
22
22
 
23
23
  MAX_RESEND = 5
24
24
 
@@ -26,24 +26,43 @@ module GitHub
26
26
  @gitlib = lib
27
27
  @repo = repo
28
28
  @remote_name = remote_name
29
- @configuration = GitHubService::Configuration.new(gitlib.config, :user => opts[:user], :password => opts[:password])
29
+ @configuration = GitHubService::Configuration.new(
30
+ gitlib.config,
31
+ :user => opts[:user],
32
+ :password => opts[:password],
33
+ :remote_name => remote_name
34
+ )
30
35
  end
31
36
 
32
37
 
33
38
  def client
34
- @client ||= @configuration.create_client
39
+ @configuration.client
35
40
  end
36
41
 
37
42
 
38
- def pull_requests(state = 'open', opts = {})
39
- @pull_requests ||= client.pull_requests(repo, state, opts)
43
+ def pull_requests(opts = {})
44
+ @pull_requests ||= sym_hash_JSON(client.pull_requests(repo, opts))
40
45
  end
41
46
 
42
47
 
48
+ # Create a pull request
49
+ #
50
+ # @see https://developer.github.com/v3/pulls/#create-a-pull-request
51
+ # @param base [String] The branch (or git ref) you want your changes
52
+ # pulled into. This should be an existing branch on the current
53
+ # repository. You cannot submit a pull request to one repo that requests
54
+ # a merge to a base of another repo.
55
+ # @param head [String] The branch (or git ref) where your changes are implemented.
56
+ # @param title [String] Title for the pull request
57
+ # @param body [String] The body for the pull request (optional). Supports GFM.
58
+ # @return [Hash] The newly created pull request
59
+ # @example
60
+ # @client.create_pull_request("master", "feature-branch",
61
+ # "Pull Request title", "Pull Request body")
43
62
  def create(base, head, title, body)
44
63
  logger.info { "Creating a pull request asking for '#{head}' to be merged into '#{base}' on #{repo}." }
45
64
  begin
46
- client.create_pull_request(repo, base, head, title, body)
65
+ return sym_hash_JSON(client.create_pull_request(repo, base, head, title, body))
47
66
  rescue Octokit::UnprocessableEntity => exp
48
67
  pull = pull_requests.find { |p| p[:head][:ref] == head and p[:base][:ref] == base }
49
68
  if pull
@@ -51,7 +70,7 @@ module GitHub
51
70
  else
52
71
  logger.warn { "UnprocessableEntity: #{exp}" }
53
72
  end
54
- pull
73
+ return pull
55
74
  end
56
75
  end
57
76
 
@@ -62,7 +81,7 @@ module GitHub
62
81
 
63
82
 
64
83
  def pull_request(pr_number)
65
- client.pull_request(repo, pr_number)
84
+ sym_hash_JSON(client.pull_request(repo, pr_number))
66
85
  end
67
86
 
68
87
 
@@ -94,7 +113,9 @@ module GitHub
94
113
  logger.info { "Looking for a pull request asking for '#{head}' to be merged into '#{base}' on #{repo}." }
95
114
 
96
115
  json = pull_requests
97
- pr = json.find { |p| p[:head][:ref] == head and p[:base][:ref] == base }
116
+ pr = json.find do |p|
117
+ p[:head][:ref] == head and p[:base][:ref] == base
118
+ end
98
119
 
99
120
  raise NotFoundError.new(base, head, repo, json) if error_if_missing && pr.nil?
100
121
 
@@ -147,10 +168,43 @@ module GitHub
147
168
 
148
169
  private
149
170
 
171
+ #
172
+ # @param [String, Sawyer::Resource] str the String to parse as JSON, or a simple pass through
173
+ #
174
+ # @return [Array, Hash, Sawyer::Resource, nil] an Array/Hash where all the hash keys are Symbols
175
+ #
176
+ def sym_hash_JSON(str)
177
+ return nil if str.nil?
178
+ case str
179
+ when String
180
+ raise ArgumentError.new('Can not parse an empty JSON string') if str.empty?
181
+ to_sym_hash(JSON.parse(str))
182
+ when Array, Hash
183
+ to_sym_hash(str)
184
+ else
185
+ str
186
+ end
187
+ end
188
+
189
+
190
+ def to_sym_hash(source)
191
+ if source.is_a? Hash
192
+ Hash[source.map { |k, v|
193
+ [k.to_sym, to_sym_hash(v)]
194
+ }]
195
+ elsif source.is_a? Array
196
+ source.map { |e| to_sym_hash(e) }
197
+ else
198
+ source
199
+ # raise "Don't know what to do with #{source.class} - #{source}"
200
+ end
201
+ end
202
+
150
203
 
204
+ # @return [Sawyer::Resource]
151
205
  def send_close_req(pull_number, count)
152
206
  begin
153
- client.patch("repos/#{Octokit::Repository.new(repo)}/pulls/#{pull_number}", {:state => 'closed'})
207
+ sym_hash_JSON(client.patch("repos/#{Octokit::Repository.new(repo)}/pulls/#{pull_number}", {:state => 'closed'}))
154
208
  rescue Octokit::UnprocessableEntity => exp
155
209
  if count > MAX_RESEND
156
210
  raise exp
@@ -85,17 +85,17 @@ module GitProc
85
85
 
86
86
 
87
87
  def checkout_pull_request(lib, pr_number, remote_name, repo_name, username, password, logger)
88
- logger.info { 'Getting #pr_number' }
88
+ logger.info { "Getting #{pr_number}" }
89
89
 
90
90
  lib.fetch(remote_name)
91
91
 
92
92
  pr = create_pull_request_client(lib, remote_name, repo_name, username, password)
93
93
  json = pr.pull_request(pr_number)
94
- head_branch_name = json.head.ref
95
- base_branch_name = json.base.ref
94
+ head_branch_name = json[:head][:ref]
95
+ base_branch_name = json[:base][:ref]
96
96
 
97
- remote_head_server_name = match_remote_to_pr_remote(lib, json.head.repo.ssh_url)
98
- remote_base_server_name = match_remote_to_pr_remote(lib, json.base.repo.ssh_url)
97
+ remote_head_server_name = match_remote_to_pr_remote(lib, json[:head][:repo][:ssh_url])
98
+ remote_base_server_name = match_remote_to_pr_remote(lib, json[:base][:repo][:ssh_url])
99
99
  lib.checkout(head_branch_name, :new_branch => "#{remote_head_server_name}/#{head_branch_name}")
100
100
  lib.branch(head_branch_name, :upstream => "#{remote_base_server_name}/#{base_branch_name}")
101
101
  #logger.info(json.to_hash)
@@ -12,9 +12,9 @@
12
12
 
13
13
  module GitProc
14
14
  module Version
15
- MAJOR = 2
15
+ MAJOR = 3
16
16
  MINOR = 0
17
- PATCH = 4
17
+ PATCH = 0
18
18
  BUILD = nil
19
19
 
20
20
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
data/local-build.rb CHANGED
@@ -3,22 +3,26 @@ require File.expand_path('../lib/git-process/version', __FILE__)
3
3
 
4
4
  gems = %w(git-process-lib git-sync git-to-master git-new-fb git-pull-req git-process)
5
5
 
6
+ puts "rake manpage"
6
7
  %x[rake manpage 2>&1]
7
8
 
9
+ puts "uninstall"
8
10
  for gem in gems.reverse
9
11
  %x[gem uninstall #{gem} -x -v #{GitProc::Version::STRING} 2>&1]
10
12
  end
11
13
 
12
14
  for gem in gems
13
- puts %x(a2x -f manpage -D man docs/#{gem}.1.adoc)
14
- SystemExit.new($?) if $?.exitstatus
15
+ # puts %x(a2x -f manpage -D man docs/#{gem}.1.adoc)
16
+ # SystemExit.new($?) if $?.exitstatus
15
17
 
18
+ puts "gem build #{gem}"
16
19
  %x[gem build #{gem}.gemspec]
17
20
  SystemExit.new($?) if $?.exitstatus
18
21
 
19
22
  # puts %x(gem install ./#{gem}-#{GitProc::Version::STRING}.gem -l -u)
20
23
  # SystemExit.new($?) if $?.exitstatus
21
24
 
25
+ puts "gem push #{gem}"
22
26
  puts %x(gem push ./#{gem}-#{GitProc::Version::STRING}.gem)
23
27
  SystemExit.new($?) if $?.exitstatus
24
28
  end
@@ -110,7 +110,7 @@ describe ChangeFileHelper, :git_repo_helper do
110
110
 
111
111
  change_file_helper.offer_to_help_uncommitted_changes
112
112
 
113
- gitlib.status.clean?.should be_true
113
+ gitlib.status.clean?.should be true
114
114
 
115
115
  gitlib.stash_pop
116
116
 
@@ -21,6 +21,41 @@ describe GitProc::GitBranch do
21
21
  end
22
22
 
23
23
 
24
+ describe 'comparison' do
25
+
26
+ it 'should handle another branch' do
27
+ base_branch = gitlib.branches.current
28
+ current = gitlib.branches.current
29
+ gitlib.checkout('fb', :new_branch => base_branch.name)
30
+ fb = gitlib.branches['fb']
31
+ expect( current <=> fb ).to eq(1)
32
+ expect( fb <=> current ).to eq(-1)
33
+ end
34
+
35
+
36
+ it 'should handle a String' do
37
+ current = gitlib.branches.current
38
+ expect( current <=> 'fb' ).to eq(1)
39
+ expect( 'fb' <=> current ).to eq(-1)
40
+ end
41
+
42
+
43
+ it 'should handle a nil' do
44
+ current = gitlib.branches.current
45
+ expect( current <=> nil ).to be_nil
46
+ expect( nil <=> current ).to be_nil
47
+ end
48
+
49
+
50
+ it 'should handle an unknown' do
51
+ current = gitlib.branches.current
52
+ expect( current <=> {} ).to be_nil
53
+ expect( {} <=> current ).to be_nil
54
+ end
55
+
56
+ end
57
+
58
+
24
59
  describe 'contains_all_of' do
25
60
 
26
61
  it 'should handle the trivial case' do
data/spec/git_lib_spec.rb CHANGED
@@ -46,18 +46,41 @@ describe GitLib, :git_repo_helper do
46
46
  end
47
47
 
48
48
 
49
+ describe 'rev_parse' do
50
+
51
+ it 'parses a known branch name' do
52
+ expect(gitlib.rev_parse('master')).not_to be_empty
53
+ end
54
+
55
+
56
+ it 'does not try to parse a file name as a revision name' do
57
+ file = gitlib.workdir + '/fooble.txt'
58
+ FileUtils.touch(file, :verbose => false)
59
+ gitlib.add file
60
+ gitlib.commit 'test'
61
+ expect(gitlib.rev_parse('fooble.txt')).to be_nil
62
+ end
63
+
64
+
65
+ it 'empty for an unknown branch name' do
66
+ expect(gitlib.rev_parse('skdfklsjhdfklerouwh')).to be_nil
67
+ end
68
+
69
+ end
70
+
71
+
49
72
  describe 'set_upstream_branch' do
50
73
  include GitRepoHelper
51
74
 
52
75
  it 'updates for remote branch' do
53
- gitlib.branch('ba/bb', :base_branch => 'master')
54
- clone_repo do |gl|
55
- gl.checkout('new_branch', :new_branch => 'origin/master')
56
- gl.set_upstream_branch('new_branch', 'origin/ba/bb')
76
+ gitlib.branch('ba/bb', :base_branch => 'master')
77
+ clone_repo do |gl|
78
+ gl.checkout('new_branch', :new_branch => 'origin/master')
79
+ gl.set_upstream_branch('new_branch', 'origin/ba/bb')
57
80
 
58
- gl.config['branch.new_branch.remote'].should == 'origin'
59
- gl.config['branch.new_branch.merge'].should == 'refs/heads/ba/bb'
60
- end
81
+ gl.config['branch.new_branch.remote'].should == 'origin'
82
+ gl.config['branch.new_branch.merge'].should == 'refs/heads/ba/bb'
83
+ end
61
84
  end
62
85
 
63
86
 
@@ -86,7 +109,7 @@ describe GitLib, :git_repo_helper do
86
109
  describe 'fetch' do
87
110
 
88
111
  it 'parse the list of changes' do
89
- output = '''
112
+ output = '' '
90
113
  remote: Counting objects: 1028, done.
91
114
  remote: Compressing objects: 100% (301/301), done.
92
115
  remote: Total 699 (delta 306), reused 654 (delta 273)
@@ -111,7 +134,7 @@ From remote.system.com:tuser/test-proj
111
134
  b9797f8..dd24a9f webcms-2135 -> origin/webcms-2135
112
135
  * [new branch] webcms-831-faq-web-service -> origin/webcms-831-faq-web-service
113
136
  x [deleted] (none) -> origin/webcms-1315-masthead
114
- '''
137
+ ' ''
115
138
  changes = gitlib.fetch_changes(output)
116
139
 
117
140
  changes[:new_branch].size().should == 6
@@ -1,5 +1,6 @@
1
- require 'git-process/git_process'
1
+ require File.dirname(__FILE__) + '/../lib/git-process/git_process'
2
2
  require 'GitRepoHelper'
3
+ require 'climate_control'
3
4
  require 'fileutils'
4
5
 
5
6
  describe GitProc::Process do
@@ -21,6 +22,13 @@ describe GitProc::Process do
21
22
  rm_rf(gitlib.workdir)
22
23
  end
23
24
 
25
+ around do |example|
26
+ # make sure there aren't side-effects testing from the testing user's .gitconfig
27
+ ClimateControl.modify HOME: '/path_that_does_not_exist' do
28
+ example.run
29
+ end
30
+ end
31
+
24
32
 
25
33
  describe 'run lifecycle' do
26
34
 
@@ -35,13 +43,13 @@ describe GitProc::Process do
35
43
  end
36
44
 
37
45
 
38
- it "should call 'cleanup' even if there's an error" do
46
+ it 'should call "cleanup" even if there is an error' do
39
47
  proc = GitProc::Process.new(gitlib)
40
48
  proc.should_receive(:verify_preconditions)
41
- proc.should_receive(:runner).and_raise(GitProc::GitProcessError.new("Error!"))
49
+ proc.should_receive(:runner).and_raise(GitProc::GitProcessError.new('Error!'))
42
50
  proc.should_receive(:cleanup)
43
51
  proc.should_receive(:exit)
44
- proc.should_receive(:puts).with("Error!")
52
+ proc.should_receive(:puts).with('Error!')
45
53
 
46
54
  proc.run
47
55
  end
@@ -49,9 +57,9 @@ describe GitProc::Process do
49
57
  end
50
58
 
51
59
 
52
- describe "validate local integration branch" do
60
+ describe 'validate local integration branch' do
53
61
 
54
- it "should use remove the int-branch if not on it and not blocked" do
62
+ it 'should use remove the int-branch if not on it and not blocked' do
55
63
  clone_repo('master') do |gl|
56
64
  gl.checkout('fb', :new_branch => 'master')
57
65
 
@@ -60,12 +68,12 @@ describe GitProc::Process do
60
68
 
61
69
  gp.verify_preconditions
62
70
 
63
- gl.branches.include?('master').should be_false
71
+ gl.branches.include?('master').should be false
64
72
  end
65
73
  end
66
74
 
67
75
 
68
- it "should ask use remove the int-branch if not on it and not blocked" do
76
+ it 'should ask to remove the int-branch if not on it and not blocked' do
69
77
  clone_repo('master') do |gl|
70
78
  gl.checkout('fb', :new_branch => 'master')
71
79
 
@@ -74,12 +82,12 @@ describe GitProc::Process do
74
82
 
75
83
  gp.verify_preconditions
76
84
 
77
- gl.branches.include?('master').should be_false
85
+ gl.branches.include?('master').should be false
78
86
  end
79
87
  end
80
88
 
81
89
 
82
- it "should ask use remove the int-branch if not on it and not blocked and not remove if answered no" do
90
+ it 'should ask to remove the int-branch if not on it and not blocked and not remove if answered no' do
83
91
  clone_repo('master') do |gl|
84
92
  gl.checkout('fb', :new_branch => 'master')
85
93
 
@@ -88,22 +96,22 @@ describe GitProc::Process do
88
96
 
89
97
  gp.verify_preconditions
90
98
 
91
- gl.branches.include?('master').should be_true
99
+ gl.branches.include?('master').should be true
92
100
  end
93
101
  end
94
102
 
95
103
 
96
- it "should not remove the int-branch if on it" do
104
+ it 'should not remove the int-branch if on it' do
97
105
  clone_repo('master') do |gl|
98
106
  gp = GitProc::Process.new(gl)
99
107
  gp.verify_preconditions
100
108
 
101
- gl.branches.include?('master').should be_true
109
+ gl.branches.include?('master').should be true
102
110
  end
103
111
  end
104
112
 
105
113
 
106
- it "should not remove the int-branch if blocked" do
114
+ it 'should not remove the int-branch if blocked' do
107
115
  clone_repo('master') do |gl|
108
116
  gl.config['gitProcess.keepLocalIntegrationBranch'] = 'true'
109
117
  gl.checkout('fb', :new_branch => 'master')
@@ -111,12 +119,12 @@ describe GitProc::Process do
111
119
  gp = GitProc::Process.new(gl)
112
120
  gp.verify_preconditions
113
121
 
114
- gl.branches.include?('master').should be_true
122
+ gl.branches.include?('master').should be true
115
123
  end
116
124
  end
117
125
 
118
126
 
119
- describe "local vs remote branch status" do
127
+ describe 'local vs remote branch status' do
120
128
 
121
129
  before(:each) do
122
130
  change_file_and_commit('a.txt', 'a content', gitlib)
@@ -124,7 +132,7 @@ describe GitProc::Process do
124
132
  end
125
133
 
126
134
 
127
- it "should not remove if both have changes" do
135
+ it 'should not remove if both have changes' do
128
136
  clone_repo('master') do |gl|
129
137
  change_file_and_commit('c.txt', 'c on origin/master', gitlib)
130
138
  change_file_and_commit('d.txt', 'd on master', gl)
@@ -136,12 +144,12 @@ describe GitProc::Process do
136
144
  gp = GitProc::Process.new(gl)
137
145
  gp.verify_preconditions
138
146
 
139
- gl.branches.include?('master').should be_true
147
+ gl.branches.include?('master').should be true
140
148
  end
141
149
  end
142
150
 
143
151
 
144
- it "should remove if server changed but not local" do
152
+ it 'should remove if server changed but not local' do
145
153
  clone_repo('master') do |gl|
146
154
  gp = GitProc::Process.new(gl)
147
155
  gp.stub(:ask_about_removing_master).and_return(true)
@@ -154,12 +162,12 @@ describe GitProc::Process do
154
162
 
155
163
  gp.verify_preconditions
156
164
 
157
- gl.branches.include?('master').should be_false
165
+ gl.branches.include?('master').should be false
158
166
  end
159
167
  end
160
168
 
161
169
 
162
- it "should not remove if server did not change but local did" do
170
+ it 'should not remove if server did not change but local did' do
163
171
  clone_repo('master') do |gl|
164
172
  change_file_and_commit('c.txt', 'c on master', gl)
165
173
 
@@ -170,12 +178,12 @@ describe GitProc::Process do
170
178
  gp = GitProc::Process.new(gl)
171
179
  gp.verify_preconditions
172
180
 
173
- gl.branches.include?('master').should be_true
181
+ gl.branches.include?('master').should be true
174
182
  end
175
183
  end
176
184
 
177
185
 
178
- it "should remove if server and local are the same" do
186
+ it 'should remove if server and local are the same' do
179
187
  change_file_and_commit('c.txt', 'c on origin/master', gitlib)
180
188
 
181
189
  clone_repo('master') do |gl|
@@ -186,21 +194,21 @@ describe GitProc::Process do
186
194
  gl.fetch
187
195
  gp.verify_preconditions
188
196
 
189
- gl.branches.include?('master').should be_false
197
+ gl.branches.include?('master').should be false
190
198
  end
191
199
  end
192
200
 
193
201
  end
194
202
 
195
203
 
196
- it "should not remove the int-branch if not a clone" do
204
+ it 'should not remove the int-branch if not a clone' do
197
205
  gitlib.config['gitProcess.keepLocalIntegrationBranch'] = 'false'
198
206
  gitlib.checkout('fb', :new_branch => 'master')
199
207
 
200
208
  gitprocess = GitProc::Process.new(gitlib)
201
209
  gitprocess.verify_preconditions
202
210
 
203
- gitlib.branches.include?('master').should be_true
211
+ gitlib.branches.include?('master').should be true
204
212
  end
205
213
 
206
214
  end