-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_github_build_status.py
More file actions
356 lines (317 loc) · 12.1 KB
/
Copy pathtest_github_build_status.py
File metadata and controls
356 lines (317 loc) · 12.1 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from bert_e.git_host.github import AggregatedWorkflowRuns, Client
from pytest import fixture
@fixture
def client():
return Client(
login='login',
password='password',
email='email@org.com',
base_url="http://localhost:4010",
accept_header="application/json"
)
@fixture
def workflow_run_json():
return {
'workflow_runs': [
{
'id': 1,
'head_sha': 'd6fde92930d4715a2b49857d24b940956b26d2d3',
'head_branch': 'q/1',
'status': 'completed',
'event': 'pull_request',
'workflow_id': 1,
'check_suite_id': 1,
'conclusion': 'success',
'pull_requests': [
{
'number': 1
}
],
'repository': {
'full_name': 'octo-org/Hello-World',
'owner': {
'login': 'octo-org'
},
'name': 'Hello-World'
}
},
{
'id': 2,
'head_sha': 'd6fde92930d4715a2b49857d24b940956b26d2d3',
'head_branch': 'q/1',
'workflow_id': 1,
'check_suite_id': 2,
'event': 'pull_request',
'status': 'completed',
'conclusion': 'cancelled',
'pull_requests': [
{
'number': 1
}
],
'repository': {
'full_name': 'octo-org/Hello-World',
'owner': {
'login': 'octo-org'
},
'name': 'Hello-World'
}
},
{
'id': 3,
'head_sha': 'd6fde92930d4715a2b49857d24b940956b26d2d3',
'head_branch': 'q/1',
'workflow_id': 2,
'check_suite_id': 3,
'event': 'pull_request',
'status': 'completed',
'conclusion': 'skipped',
'pull_requests': [
{
'number': 1
}
],
'repository': {
'full_name': 'octo-org/Hello-World',
'owner': {
'login': 'octo-org'
},
'name': 'Hello-World'
}
}
],
'total_count': 3
}
def test_aggregated_workflow_run_api_client(client):
"""Run the workflow run client with the GitHub mock server."""
head_sha = 'acb5820ced9479c074f688cc328bf03f341a511d'
owner = 'octocat'
repo = 'Hello-World'
workflow_runs = AggregatedWorkflowRuns.get(
client=client,
owner=owner,
repo=repo,
params={
'head_sha': head_sha
}
)
assert workflow_runs.state == 'INPROGRESS'
assert workflow_runs.full_repo == f'{owner}/{repo}'
assert workflow_runs.commit == head_sha
assert workflow_runs.owner == owner
assert workflow_runs.repo == repo
assert workflow_runs.branch == 'master'
def test_aggregated_workflow_run(client, workflow_run_json):
workflow_runs = AggregatedWorkflowRuns(client, **workflow_run_json)
full_name = \
workflow_run_json['workflow_runs'][0]['repository']['full_name']
head_sha = workflow_run_json['workflow_runs'][0]['head_sha']
owner = \
workflow_run_json['workflow_runs'][0]['repository']['owner']['login']
repo = workflow_run_json['workflow_runs'][0]['repository']['name']
branch = workflow_run_json['workflow_runs'][0]['head_branch']
url = f"https://github.com/{full_name}/actions?query=branch%3A{branch}"
assert url == workflow_runs.url
assert head_sha == workflow_runs.commit
assert owner == workflow_runs.owner
assert repo == workflow_runs.repo
assert full_name == workflow_runs.full_repo
assert branch == workflow_runs.branch
assert workflow_runs.is_pending() is False
assert workflow_runs.is_queued() is False
workflow_run_json['workflow_runs'][0]['status'] = 'queued'
workflow_run_json['workflow_runs'][0]['conclusion'] = None
workflow_runs = AggregatedWorkflowRuns(client, **workflow_run_json)
assert workflow_runs.is_pending() is False
assert workflow_runs.is_queued() is True
assert workflow_runs.state == "INPROGRESS"
workflow_run_json['workflow_runs'][0]['status'] = 'pending'
workflow_runs = AggregatedWorkflowRuns(client, **workflow_run_json)
assert workflow_runs.is_pending() is True
assert workflow_runs.is_queued() is False
assert workflow_runs.state == "INPROGRESS"
workflow_run_json['workflow_runs'] = []
workflow_run_json['total_count'] = 0
workflow_runs = AggregatedWorkflowRuns(client, **workflow_run_json)
assert workflow_runs.url is None
assert workflow_runs.commit is None
assert workflow_runs.owner is None
assert workflow_runs.repo is None
assert workflow_runs.full_repo is None
assert workflow_runs.branch is None
def test_cancelled_build_same_sha(client, monkeypatch, workflow_run_json):
workflow_runs = AggregatedWorkflowRuns(client, **workflow_run_json)
monkeypatch.setattr(AggregatedWorkflowRuns, 'get',
lambda *args, **kwargs: workflow_runs)
get_workflow_run = AggregatedWorkflowRuns.get(
client=client,
owner=workflow_run_json['workflow_runs'][0]['repository']['owner']['login'], # noqa
repo=workflow_run_json['workflow_runs'][0]['repository']['name'],
ref=workflow_run_json['workflow_runs'][0]['head_sha']
)
assert get_workflow_run.state == 'SUCCESSFUL'
def test_skipped_workflow_treated_as_success(client):
"""Test that a completed workflow with conclusion 'skipped' is
treated as successful.
This reproduces the real scenario where:
- Workflow 1: conclusion='success'
- Workflow 2: conclusion='skipped' (different workflow_id)
Both should be considered successful, not cause a KeyError.
"""
workflow_run_json = {
'workflow_runs': [
{
'id': 1,
'head_sha': 'abc123',
'head_branch': 'feature-branch',
'status': 'completed',
'event': 'pull_request',
'workflow_id': 1,
'check_suite_id': 1,
'conclusion': 'success',
'pull_requests': [{'number': 1}],
'repository': {
'full_name': 'octo-org/Hello-World',
'owner': {'login': 'octo-org'},
'name': 'Hello-World'
}
},
{
'id': 2,
'head_sha': 'abc123',
'head_branch': 'feature-branch',
'workflow_id': 2,
'check_suite_id': 2,
'event': 'pull_request',
'status': 'completed',
'conclusion': 'skipped',
'pull_requests': [{'number': 1}],
'repository': {
'full_name': 'octo-org/Hello-World',
'owner': {'login': 'octo-org'},
'name': 'Hello-World'
}
}
],
'total_count': 2
}
# This should not raise KeyError on 'skipped'
workflow_runs = AggregatedWorkflowRuns(client, **workflow_run_json)
# Both workflows should be kept (different workflow_ids)
assert len(workflow_runs._workflow_runs) == 2
# State should be SUCCESSFUL (both success and skipped are treated
# as success)
assert workflow_runs.state == 'SUCCESSFUL'
assert workflow_runs.is_pending() is False
assert workflow_runs.is_queued() is False
def _make_run(*, run_id, sha, branch, status, conclusion, event='push',
workflow_id):
return {
'id': run_id,
'head_sha': sha,
'head_branch': branch,
'status': status,
'event': event,
'workflow_id': workflow_id,
'check_suite_id': run_id,
'conclusion': conclusion,
'pull_requests': [],
'repository': {
'full_name': 'org/repo',
'owner': {'login': 'org'},
'name': 'repo',
},
}
SHARED_SHA = 'aabb' * 10
W_BRANCH = 'w/4.3/bugfix/BERTE-602'
def test_cross_branch_sha_poisoning_state_property(client):
"""Document the existing .state bug: a sibling branch's SUCCESSFUL run
on the same SHA causes .state to return SUCCESSFUL even though the
w-branch is still in_progress (the PR 5155 hollow-success shape).
"""
data = {
'total_count': 2,
'workflow_runs': [
_make_run(
run_id=1, sha=SHARED_SHA, branch=W_BRANCH,
status='in_progress', conclusion=None, workflow_id=10),
_make_run(
run_id=2, sha=SHARED_SHA, branch='development/4.3',
status='completed', conclusion='success', workflow_id=20),
],
}
runs = AggregatedWorkflowRuns(client, **data)
# The existing .state property returns SUCCESSFUL because development/4.3
# succeeded — this is the source of the stale-cache poisoning.
assert runs.state == 'SUCCESSFUL'
def test_state_for_branch_catches_inprogress(client):
"""state_for_branch filters strictly to the requested branch so an
in-progress run is not masked by a sibling branch's success.
"""
data = {
'total_count': 2,
'workflow_runs': [
_make_run(
run_id=1, sha=SHARED_SHA, branch=W_BRANCH,
status='in_progress', conclusion=None, workflow_id=10),
_make_run(
run_id=2, sha=SHARED_SHA, branch='development/4.3',
status='completed', conclusion='success', workflow_id=20),
],
}
runs = AggregatedWorkflowRuns(client, **data)
assert runs.state_for_branch(W_BRANCH) == 'INPROGRESS'
assert runs.state_for_branch('development/4.3') == 'SUCCESSFUL'
def test_state_for_branch_no_runs_returns_notstarted(client):
data = {
'total_count': 1,
'workflow_runs': [
_make_run(
run_id=1, sha=SHARED_SHA, branch='development/4.3',
status='completed', conclusion='success', workflow_id=20),
],
}
runs = AggregatedWorkflowRuns(client, **data)
assert runs.state_for_branch(W_BRANCH) == 'NOTSTARTED'
def test_state_for_branch_workflow_dispatch_excluded(client):
data = {
'total_count': 1,
'workflow_runs': [
_make_run(
run_id=1, sha=SHARED_SHA, branch=W_BRANCH,
status='completed', conclusion='success',
event='workflow_dispatch', workflow_id=10),
],
}
runs = AggregatedWorkflowRuns(client, **data)
assert runs.state_for_branch(W_BRANCH) == 'NOTSTARTED'
def test_state_for_branch_deduplicates_within_branch(client):
"""Same workflow_id on the same branch: keep the best conclusion."""
data = {
'total_count': 2,
'workflow_runs': [
_make_run(
run_id=1, sha=SHARED_SHA, branch=W_BRANCH,
status='completed', conclusion='cancelled', workflow_id=10),
_make_run(
run_id=2, sha=SHARED_SHA, branch=W_BRANCH,
status='completed', conclusion='success', workflow_id=10),
],
}
runs = AggregatedWorkflowRuns(client, **data)
assert runs.state_for_branch(W_BRANCH) == 'SUCCESSFUL'
def test_state_for_branch_clean_pr_passes(client):
"""A PR where the w-branch completed successfully passes revalidation."""
data = {
'total_count': 2,
'workflow_runs': [
_make_run(
run_id=1, sha=SHARED_SHA, branch=W_BRANCH,
status='completed', conclusion='success', workflow_id=10),
_make_run(
run_id=2, sha=SHARED_SHA, branch=W_BRANCH,
status='completed', conclusion='skipped', workflow_id=20),
],
}
runs = AggregatedWorkflowRuns(client, **data)
assert runs.state_for_branch(W_BRANCH) == 'SUCCESSFUL'