diff --git a/assets/lib/check.rb b/assets/lib/check.rb index 27b5fbb..39a8a07 100755 --- a/assets/lib/check.rb +++ b/assets/lib/check.rb @@ -11,7 +11,11 @@ if input['source']['every'] json!(repo.pull_requests.map(&:as_json)) else - next_pull_request = repo.next_pull_request(id: input['version']['pr'], sha: input['version']['ref']) + next_pull_request = repo.next_pull_request( + id: input['version']['pr'], + sha: input['version']['ref'], + base: input['source']['base'] + ) if next_pull_request json!([next_pull_request.as_json]) else diff --git a/assets/lib/common.rb b/assets/lib/common.rb index c156896..bd79ba4 100644 --- a/assets/lib/common.rb +++ b/assets/lib/common.rb @@ -79,8 +79,8 @@ def initialize(name:) @name = name end - def pull_requests - @pull_requests ||= Octokit.pulls(name, state: 'open', sort: 'updated', direction: 'desc').map do |pr| + def pull_requests(args = {}) + @pull_requests ||= Octokit.pulls(name, pulls_options(args)).map do |pr| PullRequest.new(repo: self, pr: pr) end end @@ -90,8 +90,8 @@ def pull_request(id:) PullRequest.new(repo: self, pr: pr) end - def next_pull_request(id: nil, sha: nil) - return if pull_requests.empty? + def next_pull_request(id: nil, sha: nil, base: nil) + return if pull_requests(base: base).empty? if id && sha current = pull_requests.find { |pr| pr.equals?(id: id, sha: sha) } @@ -102,6 +102,16 @@ def next_pull_request(id: nil, sha: nil) pr != current && pr.ready? end end + + private + + def pulls_options(base: nil) + base ? default_opts.merge(base: base) : default_opts + end + + def default_opts + { state: 'open', sort: 'updated', direction: 'desc' } + end end def input diff --git a/spec/integration/check_spec.rb b/spec/integration/check_spec.rb index 245be42..ff3028a 100644 --- a/spec/integration/check_spec.rb +++ b/spec/integration/check_spec.rb @@ -19,6 +19,19 @@ end end + context 'when targetting a base branch other than master' do + must_stub_query_params + + before do + proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls?base=my-base-branch&direction=desc&per_page=100&sort=updated&state=open') + .and_return(json: [{ number: 1, head: { sha: 'abcdef' } }]) + end + + it 'retrieves pull requests for the specified base branch' do + expect(check(source: { repo: 'jtarchie/test', base: 'my-base-branch' })).to eq [{ 'ref' => 'abcdef', 'pr' => '1' }] + end + end + context 'with check for `version: every`' do context 'when there are no pull requests' do before do @@ -43,8 +56,10 @@ end context 'when there is an open pull request' do + must_stub_query_params + before do - proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls') + proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls?direction=desc&per_page=100&sort=updated&state=open') .and_return(json: [{ number: 1, head: { sha: 'abcdef' } }]) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 311929a..57defcc 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -71,3 +71,12 @@ def with_resource FileUtils.cp_r(dest_dir, File.join(tmp_dir, 'resource')) yield(tmp_dir) end + +def must_stub_query_params + around do |example| + old_strip_params = Billy.config.strip_query_params + Billy.config.strip_query_params = false + example.run + Billy.config.strip_query_params = old_strip_params + end +end