|
| 1 | +require 'spec_helper' |
| 2 | +require 'json' |
| 3 | +require 'billy' |
| 4 | + |
| 5 | +describe 'check' do |
| 6 | + let(:proxy) { Billy::Proxy.new } |
| 7 | + |
| 8 | + before { proxy.start } |
| 9 | + after { proxy.reset } |
| 10 | + |
| 11 | + def check(payload = {}) |
| 12 | + path = ['./assets/check', '/opt/resource/check'].find { |p| File.exist? p } |
| 13 | + payload[:source] = { repo: 'jtarchie/test' } |
| 14 | + |
| 15 | + output = `echo '#{JSON.generate(payload)}' | env http_proxy=#{proxy.url} #{path}` |
| 16 | + JSON.parse(output) |
| 17 | + end |
| 18 | + |
| 19 | + context 'when there are no pull requests' do |
| 20 | + before do |
| 21 | + proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls') |
| 22 | + .and_return(json: []) |
| 23 | + end |
| 24 | + |
| 25 | + it 'returns no versions' do |
| 26 | + expect(check).to eq [] |
| 27 | + end |
| 28 | + |
| 29 | + context 'when there is a last known version' do |
| 30 | + it 'returns no versions' do |
| 31 | + payload = { version: { ref: '1' } } |
| 32 | + |
| 33 | + proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls/1') |
| 34 | + .and_return(json: {}) |
| 35 | + |
| 36 | + expect(check(payload)).to eq [] |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + context 'when there is an open pull request' do |
| 42 | + before do |
| 43 | + proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls') |
| 44 | + .and_return(json: [{ id: '1', head: { sha: 'abcdef' } }]) |
| 45 | + end |
| 46 | + |
| 47 | + context 'that has no status' do |
| 48 | + before do |
| 49 | + proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef') |
| 50 | + .and_return(json: []) |
| 51 | + end |
| 52 | + |
| 53 | + it 'returns SHA of the pull request' do |
| 54 | + expect(check).to eq [{ 'ref' => 'abcdef', 'pr' => '1' }] |
| 55 | + end |
| 56 | + |
| 57 | + context 'and the version is the same as the pull request' do |
| 58 | + it 'returns nothing' do |
| 59 | + payload = { version: { ref: 'abcdef', pr: '1' } } |
| 60 | + |
| 61 | + expect(check(payload)).to eq [] |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + context 'that has a pending status' do |
| 67 | + before do |
| 68 | + proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef') |
| 69 | + .and_return(json: [{ state: 'pending', context: 'concourseci' }]) |
| 70 | + end |
| 71 | + |
| 72 | + it 'returns SHA of the pull request' do |
| 73 | + expect(check).to eq [{ 'ref' => 'abcdef', 'pr' => '1' }] |
| 74 | + end |
| 75 | + |
| 76 | + context 'and the version is the same as the pull request' do |
| 77 | + it 'returns nothing' do |
| 78 | + payload = { version: { ref: 'abcdef', pr: '1' } } |
| 79 | + |
| 80 | + expect(check(payload)).to eq [] |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context 'that has another status' do |
| 86 | + it 'does not return it' do |
| 87 | + proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef') |
| 88 | + .and_return(json: [{ state: 'success', context: 'concourseci' }]) |
| 89 | + |
| 90 | + expect(check).to eq [] |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + context 'when there is more than one open pull request' do |
| 96 | + before do |
| 97 | + proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls') |
| 98 | + .and_return(json: [ |
| 99 | + { id: '2', head: { sha: 'zyxwvu' } }, |
| 100 | + { id: '1', head: { sha: 'abcdef' } } |
| 101 | + ]) |
| 102 | + end |
| 103 | + |
| 104 | + context 'and the version is the same as the older pull request' do |
| 105 | + it 'returns nothing when its still pending' do |
| 106 | + payload = { version: { ref: 'abcdef', pr: '1' } } |
| 107 | + |
| 108 | + proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef') |
| 109 | + .and_return(json: [{ state: 'pending', context: 'concourseci' }]) |
| 110 | + |
| 111 | + expect(check(payload)).to eq [] |
| 112 | + end |
| 113 | + |
| 114 | + it 'returns the latest pull request when the current version is not pending' do |
| 115 | + payload = { version: { ref: 'abcdef', pr: '1' } } |
| 116 | + |
| 117 | + proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef') |
| 118 | + .and_return(json: [{ state: 'success', context: 'concourseci' }]) |
| 119 | + |
| 120 | + proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/zyxwvu') |
| 121 | + .and_return(json: [{ state: 'pending', context: 'concourseci' }]) |
| 122 | + |
| 123 | + expect(check(payload)).to eq [{ 'ref' => 'zyxwvu', 'pr' => '2' }] |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | +end |
0 commit comments