forked from jtarchie/github-pullrequest-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_spec.rb
215 lines (171 loc) · 7.4 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
214
215
require 'spec_helper'
require 'json'
describe 'check' do
let(:proxy) { Billy::Proxy.new }
before { proxy.start }
after { proxy.reset }
context 'when working with an external API' do
it 'makes requests with respect to that endpoint' do
proxy.stub('https://test.example.com:443/repos/jtarchie/test/pulls')
.and_return(json: [])
expect(check(source: {
repo: 'jtarchie/test',
api_endpoint: 'https://test.example.com'
})).to eq []
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
proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls')
.and_return(json: [])
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 } }
proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls/1')
.and_return(json: {})
expect(check(payload)).to eq []
end
end
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?direction=desc&per_page=100&sort=updated&state=open')
.and_return(json: [{ 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
proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls')
.and_return(json: [
{ number: 2, head: { sha: 'zyxwvu' } },
{ number: 1, head: { sha: 'abcdef' } }
])
end
it 'returns all the pull request SHAs' do
expect(check(source: { repo: 'jtarchie/test', every: true }, version: {})).to eq [
{ 'ref' => 'zyxwvu', 'pr' => '2' },
{ 'ref' => 'abcdef', 'pr' => '1' }
]
end
end
end
context 'with current check steps' do
context 'when there are no pull requests' do
before do
proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls')
.and_return(json: [])
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' } }
proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls/1')
.and_return(json: {})
expect(check(payload)).to eq []
end
end
end
context 'when there is an open pull request' do
before do
proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls')
.and_return(json: [{ number: 1, head: { sha: 'abcdef' } }])
end
context 'that has no status' do
before do
proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef')
.and_return(json: [])
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
proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef')
.and_return(json: [{ 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
proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef')
.and_return(json: [
{ 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
proxy.stub('https://api.github.com:443/repos/jtarchie/test/pulls')
.and_return(json: [
{ number: 2, head: { sha: 'zyxwvu' } },
{ number: 1, head: { sha: 'abcdef' } }
])
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' } }
proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef')
.and_return(json: [{ state: 'pending', context: 'concourse-ci' }])
proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/zyxwvu')
.and_return(json: [])
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' } }
proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/abcdef')
.and_return(json: [{ state: 'success', context: 'concourse-ci' }])
proxy.stub('https://api.github.com:443/repos/jtarchie/test/statuses/zyxwvu')
.and_return(json: [{ state: 'pending', context: 'concourse-ci' }])
expect(check(payload)).to eq []
end
end
end
end
end