This repository was archived by the owner on Apr 4, 2018. It is now read-only.
forked from jtarchie/github-pullrequest-resource
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_spec.rb
213 lines (172 loc) · 8.57 KB
/
check_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
require_relative '../../assets/lib/commands/check'
require 'webmock/rspec'
require 'json'
describe Commands::Check do
def check(payload)
payload['source']['no_ssl_verify'] = true
payload['version'] ||= {}
Commands::Check.new(input: payload).output.map &:as_json
end
def stub_json(uri, body)
stub_request(:get, uri)
.to_return(headers: { 'Content-Type' => 'application/json' }, body: body.to_json)
end
context 'when targetting a base branch other than master' do
before do
stub_json('https://api.github.com/repos/jtarchie/test/statuses/abcdef', [])
stub_json('https://api.github.com:443/repos/jtarchie/test/pulls?base=my-base-branch&direction=asc&per_page=100&sort=updated&state=open', [{ 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
stub_json('https://api.github.com/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open', [])
end
it 'returns no versions' do
expect(check('source' => { 'repo' => 'jtarchie/test', 'every' => true })).to eq []
end
context 'when there is a last known version' do
it 'returns no versions' do
payload = { 'version' => { 'ref' => '1' }, 'source' => { 'repo' => 'jtarchie/test', 'every' => true } }
expect(check(payload)).to eq []
end
end
end
context 'when there is an open pull request' do
before do
stub_json('https://api.github.com:443/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open', [{ number: 1, head: { sha: 'abcdef' } }])
end
it 'returns SHA of the pull request' do
expect(check('source' => { 'repo' => 'jtarchie/test', 'every' => true }, 'version' => {})).to eq [{ 'ref' => 'abcdef', 'pr' => '1' }]
end
context 'and the version is the same as the pull request' do
it 'returns that pull request' do
payload = { 'version' => { 'ref' => 'abcdef', 'pr' => '1' }, 'source' => { 'repo' => 'jtarchie/test', 'every' => true } }
expect(check(payload)).to eq [
{ 'ref' => 'abcdef', 'pr' => '1' }
]
end
end
end
context 'when there is more than one open pull request' do
before do
stub_json(
'https://api.github.com:443/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open',
[
{ number: 1, head: { sha: 'abcdef', repo: { full_name: 'jtarchie/test' } }, base: { repo: { full_name: 'jtarchie/test' } } },
{ number: 3, head: { sha: 'ghijkl', repo: { full_name: 'jtarchie/test' } }, base: { repo: { full_name: 'jtarchie/test' } } },
{ number: 2, head: { sha: 'zyxwvu', repo: { full_name: 'someother/repo' } }, base: { repo: { full_name: 'jtarchie/test' } } },
]
)
end
it 'returns all PRs oldest to newest last' do
expect(check('source' => { 'repo' => 'jtarchie/test', 'every' => true }, 'version' => {})).to eq [
{ 'ref' => 'abcdef', 'pr' => '1' },
{ 'ref' => 'ghijkl', 'pr' => '3' },
{ 'ref' => 'zyxwvu', 'pr' => '2' },
]
end
it 'filters out PRs from forked repos when requested' do
expect(check('source' => { 'repo' => 'jtarchie/test', 'every' => true, 'disallow_forks' => true }, 'version' => {})).to eq [
{ 'ref' => 'abcdef', 'pr' => '1' },
{ 'ref' => 'ghijkl', 'pr' => '3' },
]
end
end
end
context 'with current check steps' do
context 'when there are no pull requests' do
before do
stub_json('https://api.github.com/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open', [])
end
it 'returns no versions' do
expect(check('source' => { 'repo' => 'jtarchie/test' })).to eq []
end
context 'when there is a last known version' do
it 'returns no versions' do
payload = { 'version' => { 'ref' => '1' }, 'source' => { 'repo' => 'jtarchie/test' } }
expect(check(payload)).to eq []
end
end
end
context 'when there is an open pull request' do
before do
stub_json('https://api.github.com/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open', [{ number: 1, head: { sha: 'abcdef' } }])
end
context 'that has no status' do
before do
stub_json('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef', [])
end
it 'returns SHA of the pull request' do
expect(check('source' => { 'repo' => 'jtarchie/test' }, 'version' => {})).to eq [{ 'ref' => 'abcdef', 'pr' => '1' }]
end
context 'and the version is the same as the pull request' do
it 'returns no versions' do
payload = { 'version' => { 'ref' => 'abcdef', 'pr' => '1' }, 'source' => { 'repo' => 'jtarchie/test' } }
expect(check(payload)).to eq []
end
end
end
context 'that has a pending status' do
before do
stub_json('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef', [{ state: 'pending', context: 'concourse-ci' }])
end
it 'returns no versions' do
expect(check('source' => { 'repo' => 'jtarchie/test' }, 'version' => {})).to eq []
end
context 'and the version is the same as the pull request' do
it 'returns no versions' do
payload = { 'version' => { 'ref' => 'abcdef', 'pr' => '1' }, 'source' => { 'repo' => 'jtarchie/test' } }
expect(check(payload)).to eq []
end
end
end
context 'that has another status' do
it 'does not return it' do
stub_json('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef', [
{ state: 'pending', context: 'concourse-ci' },
{ state: 'success', context: 'concourse-ci' }
])
expect(check('source' => { 'repo' => 'jtarchie/test' }, 'version' => {})).to eq []
end
end
end
context 'when there is more than one open pull request' do
before do
stub_json('https://api.github.com/repos/jtarchie/test/statuses/abcdef', [])
stub_json('https://api.github.com/repos/jtarchie/test/statuses/zyxwvu', [])
stub_json('https://api.github.com/repos/jtarchie/test/pulls?direction=asc&per_page=100&sort=updated&state=open', [
{ number: 2, head: { sha: 'zyxwvu', repo: { full_name: 'someotherowner/repo' } }, base: { repo: { full_name: 'jtarchie/test' } } },
{ number: 1, head: { sha: 'abcdef', repo: { full_name: 'jtarchie/test' } }, base: { repo: { full_name: 'jtarchie/test' } } },
])
end
context 'and the version is the same as the older pull request' do
it 'returns the latest pull request when the current version is not pending' do
payload = { 'version' => { 'ref' => 'abcdef', 'pr' => '1' }, 'source' => { 'repo' => 'jtarchie/test' } }
stub_json('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef', [{ state: 'pending', context: 'concourse-ci' }])
stub_json('https://api.github.com:443/repos/jtarchie/test/statuses/zyxwvu', [])
expect(check(payload)).to eq [{ 'ref' => 'zyxwvu', 'pr' => '2' }]
end
it 'returns nothing when its still pending' do
payload = { 'version' => { 'ref' => 'abcdef', 'pr' => '1' }, 'source' => { 'repo' => 'jtarchie/test' } }
stub_json('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef', [{ state: 'success', context: 'concourse-ci' }])
stub_json('https://api.github.com:443/repos/jtarchie/test/statuses/zyxwvu', [{ state: 'pending', context: 'concourse-ci' }])
expect(check(payload)).to eq []
end
end
context 'and `disallow_forks` is not set' do
it 'returns the most recently updated pull request' do
expect(check('source' => { 'repo' => 'jtarchie/test' })).to eq [{ 'ref' => 'zyxwvu', 'pr' => '2' }]
end
end
context 'and `disallow_forks` is set to true' do
it 'returns the most recently updated internal pull request' do
expect(check('source' => { 'repo' => 'jtarchie/test', 'disallow_forks' => true })).to eq [{ 'ref' => 'abcdef', 'pr' => '1' }]
end
end
end
end
end