FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
.github/scripts/beta_continuity.py at main · durable-workflow/.github · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
durable-workflow
/
.github
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
.github
/
scripts
/
beta_continuity.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
3002 lines (2782 loc) · 126 KB
Breadcrumbs
.github
/
scripts
/
beta_continuity.py
Copy path
File metadata and controls
3002 lines (2782 loc) · 126 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Drive the workspace-unavailable beta continuity drill from GitHub authority."""
from
__future__
import
annotations
import
argparse
import
datetime
as
dt
import
hashlib
import
json
import
os
import
re
import
subprocess
import
sys
import
tempfile
import
tomllib
import
urllib
.
error
import
urllib
.
parse
import
urllib
.
request
from
pathlib
import
Path
from
typing
import
Any
if
__package__
in
{
None
,
""
}:
sys
.
path
.
insert
(
0
,
str
(
Path
(
__file__
).
resolve
().
parent
.
parent
))
from
scripts
.
beta_candidate
import
(
COMPONENTS
,
VERSION_PATTERN
,
CandidateError
,
PublicClient
,
canonical_json
,
fetch_existing_record
,
manifest_digest
,
read_record_file
,
resolve_github_tag
,
run_git
,
validate_recorded_verification
,
write_github_output
,
)
from
scripts
.
beta_conformance
import
(
EXPERIMENTS
as
CONFORMANCE_EXPERIMENTS
,
)
from
scripts
.
beta_conformance
import
(
LEGACY_PLAN_SCHEMA
,
LEGACY_SUITE_RESULT_SCHEMA
,
ConformanceError
,
validate_public_evidence_strings
,
validate_recorded_experiment_result
,
validate_recorded_plan
,
)
from
scripts
.
beta_conformance
import
(
PLAN_SCHEMA
as
CONFORMANCE_PLAN_SCHEMA
,
)
from
scripts
.
beta_conformance
import
(
SUITE_RESULT_SCHEMA
as
CONFORMANCE_SUITE_RESULT_SCHEMA
,
)
from
scripts
.
release_plan
import
(
CONTINUITY_RESOLUTION_SCHEMA
,
EXPECTED_DEFAULT_BRANCHES
,
FOUNDATION_COMMIT
,
FOUNDATION_TAG
,
PLAN_TAG_PREFIX
,
candidate_manifest
,
continuity_resolution_tag
,
discover_plan
,
read_public_record
,
resolve_tag
,
validate_continuity_resolution_authority
,
validate_plan
,
)
from
scripts
.
release_plan
import
(
LEGACY_SCHEMA
as
LEGACY_RELEASE_PLAN_SCHEMA
,
)
from
scripts
.
release_plan
import
(
SCHEMA
as
RELEASE_PLAN_SCHEMA
,
)
from
scripts
.
release_plan
import
(
validate_recorded_plan
as
validate_recorded_release_plan
,
)
SCHEMA
=
"durable-workflow.beta-continuity.config/v1"
EVIDENCE_SCHEMA
=
"durable-workflow.beta-continuity.evidence/v1"
SELECTION_SCHEMA
=
"durable-workflow.beta-continuity.selection/v1"
CONTROL_REPOSITORY
=
"durable-workflow/.github"
PHASE_TAG_PREFIX
=
"beta-continuity/"
SELECTION_TAG_PREFIX
=
"beta-continuity-selection/"
RELEASE_WORKFLOW
=
"release-plan.yml"
CONTINUITY_WORKFLOW
=
"beta-continuity.yml"
OBSERVER_WORKFLOW
=
"release-plan-observer.yml"
CANDIDATE_WORKFLOW
=
"beta-candidate.yml"
BETA_CANDIDATE_WORKFLOW_NAME
=
"Beta candidate"
BETA_CANDIDATE_WORKFLOW_PATH
=
f".github/workflows/
{
CANDIDATE_WORKFLOW
}
"
CONFORMANCE_WORKFLOW
=
"beta-conformance.yml"
CONFORMANCE_RETENTION_WORKFLOW
=
"beta-conformance-retention.yml"
CONTINUITY_RESOLUTION_SELECTION_SCHEMA
=
(
"durable-workflow.release-plan-continuity-resolution-selection/v1"
)
WORK_ID_PATTERN
=
re
.
compile
(
r"^[a-z0-9][a-z0-9._-]{0,79}$"
)
BETA_WORK_ID_MARKER_PREFIX
=
"<!-- beta-work-id: "
BETA_WORK_ID_MARKER
=
re
.
compile
(
r"<!-- beta-work-id: (?P<work_id>[a-z0-9][a-z0-9._-]{0,79}) -->"
)
DURABLE_WORK_ID_MARKER_PREFIX
=
"<!-- durable-workflow-work-id: "
DURABLE_WORK_ID_MARKER
=
re
.
compile
(
r"<!-- durable-workflow-work-id: (?P<work_id>[a-z0-9][a-z0-9._-]{0,79}) -->"
)
PLAN_PREFIX_PATTERN
=
re
.
compile
(
r"^[a-z0-9][a-z0-9-]{0,35}$"
)
COMMIT_PATTERN
=
re
.
compile
(
r"^[0-9a-f]{40}$"
)
PUBLIC_REPOSITORY_PATTERN
=
re
.
compile
(
r"^durable-workflow/[A-Za-z0-9._-]+$"
)
STABLE_VERSION_PATTERN
=
re
.
compile
(
r"^(0\.[0-9]+\.)([0-9]+)$"
)
ALPHA_VERSION_PATTERN
=
re
.
compile
(
r"^(2\.0\.0-alpha\.)([1-9][0-9]*)$"
)
SOURCE_MANIFESTS
=
{
"sdk-python"
: (
"pyproject.toml"
,
"project"
,
"durable-workflow"
),
"sdk-rust"
: (
"Cargo.toml"
,
"package"
,
"durable-workflow"
),
}
PHASES
=
(
"accepted"
,
"interrupted"
,
"resumed"
,
"conformance-requested"
,
"complete"
,
"no-op-confirmed"
,
)
ROUTED_BLOCKER_AUTHORITY_LABELS
=
(
"authority:github"
,
"beta:blocker"
,
"kind:release-blocker"
,
"priority:P1"
,
)
ROUTED_BLOCKER_LIFECYCLE_LABELS
=
{
"status:ready"
,
"status:done"
}
ROUTED_BLOCKER_LABELS
=
(
*
ROUTED_BLOCKER_AUTHORITY_LABELS
,
"status:ready"
,
)
ISSUE_KIND_LABELS
=
{
"kind:defect"
,
"kind:feature"
,
"kind:release-blocker"
,
"kind:cross-repository"
}
ROUTED_BLOCKER_MARKER
=
re
.
compile
(
r"<!-- beta-continuity-blocker: "
r"(?P<component>[a-z0-9][a-z0-9-]*)-(?P<reason>source-version|occupied-version)-"
r"(?P<version>[^ ]+) -->"
)
ROUTED_BLOCKER_MARKER_PREFIX
=
"<!-- beta-continuity-blocker: "
QUALIFICATION_EVIDENCE_SCHEMA
=
"durable-workflow.github-target-qualification/v1"
GITHUB_RELEASE_PAGE_SIZE
=
100
GITHUB_RELEASE_PAGE_LIMIT
=
100
class
ContinuityError
(
RuntimeError
):
"""The public continuity drill cannot safely advance."""
class
PlanBlocked
(
ContinuityError
):
"""The next public release plan has focused component blockers."""
def
__init__
(
self
,
blockers
:
list
[
dict
[
str
,
str
]])
->
None
:
self
.
blockers
=
blockers
super
().
__init__
(
"; "
.
join
(
blocker
[
"reason"
]
for
blocker
in
blockers
))
def
replace_issue_kind
(
labels
:
set
[
str
],
kind
:
str
,
location
:
str
)
->
set
[
str
]:
"""Replace an incompatible kind at an issue writer boundary."""
if
kind
not
in
ISSUE_KIND_LABELS
:
raise
ContinuityError
(
f"
{
location
}
requested an unsupported issue kind
{
kind
!r
}
"
)
replacement
=
{
label
for
label
in
labels
if
not
label
.
startswith
(
"kind:"
)}
replacement
.
add
(
kind
)
return
replacement
def
require_exact_issue_kind
(
labels
:
set
[
str
],
location
:
str
)
->
str
:
kinds
=
{
label
for
label
in
labels
if
label
.
startswith
(
"kind:"
)}
if
len
(
kinds
)
!=
1
or
not
kinds
<=
ISSUE_KIND_LABELS
:
raise
ContinuityError
(
f"
{
location
}
must write exactly one supported kind:* label, got
{
sorted
(
kinds
)
}
"
)
return
next
(
iter
(
kinds
))
class
GitHubWriter
:
"""Small bounded client for authenticated GitHub mutations and run discovery."""
def
__init__
(
self
,
token
:
str
,
api_url
:
str
=
"https://api.github.com"
)
->
None
:
if
not
token
:
raise
ContinuityError
(
"GitHub continuity authority token is unavailable"
)
self
.
api_url
=
api_url
.
rstrip
(
"/"
)
self
.
headers
=
{
"Accept"
:
"application/vnd.github+json"
,
"Authorization"
:
f"Bearer
{
token
}
"
,
"User-Agent"
:
"durable-workflow-beta-continuity/1"
,
"X-GitHub-Api-Version"
:
"2022-11-28"
,
}
def
request
(
self
,
method
:
str
,
path
:
str
,
payload
:
Any
|
None
=
None
)
->
Any
:
if
(
method
in
{
"PATCH"
,
"POST"
}
and
re
.
fullmatch
(
r"/repos/.+/issues(?:/[1-9][0-9]*)?"
,
path
)
and
isinstance
(
payload
,
dict
)
and
isinstance
(
payload
.
get
(
"labels"
),
list
)
):
require_exact_issue_kind
(
set
(
payload
[
"labels"
]),
f"GitHub issue writer
{
path
}
"
)
data
=
None
if
payload
is
None
else
json
.
dumps
(
payload
).
encode
()
request
=
urllib
.
request
.
Request
(
f"
{
self
.
api_url
}
{
path
}
"
,
data
=
data
,
headers
=
self
.
headers
,
method
=
method
,
)
try
:
with
urllib
.
request
.
urlopen
(
request
,
timeout
=
30
)
as
response
:
raw
=
response
.
read
()
except
urllib
.
error
.
HTTPError
as
error
:
detail
=
error
.
read
(
4096
).
decode
(
errors
=
"replace"
)
raise
ContinuityError
(
f"GitHub
{
method
}
{
path
}
failed (
{
error
.
code
}
):
{
detail
}
"
)
from
error
except
urllib
.
error
.
URLError
as
error
:
raise
ContinuityError
(
f"GitHub
{
method
}
{
path
}
failed:
{
error
.
reason
}
"
)
from
error
if
not
raw
:
return
None
try
:
return
json
.
loads
(
raw
)
except
json
.
JSONDecodeError
as
error
:
raise
ContinuityError
(
f"GitHub
{
method
}
{
path
}
returned invalid JSON"
)
from
error
def
get
(
self
,
path
:
str
)
->
Any
:
return
self
.
request
(
"GET"
,
path
)
def
list
(
self
,
path
:
str
)
->
list
[
dict
[
str
,
Any
]]:
records
:
list
[
dict
[
str
,
Any
]]
=
[]
separator
=
"&"
if
"?"
in
path
else
"?"
for
page
in
range
(
1
,
11
):
payload
=
self
.
get
(
f"
{
path
}
{
separator
}
per_page=100&page=
{
page
}
"
)
if
not
isinstance
(
payload
,
list
):
raise
ContinuityError
(
f"GitHub collection
{
path
}
has an invalid shape"
)
records
.
extend
(
payload
)
if
len
(
payload
)
<
100
:
return
records
raise
ContinuityError
(
f"GitHub collection
{
path
}
exceeded the pagination bound"
)
def
dispatch
(
self
,
repository
:
str
,
workflow
:
str
,
ref
:
str
,
inputs
:
dict
[
str
,
str
])
->
None
:
encoded
=
urllib
.
parse
.
quote
(
workflow
,
safe
=
""
)
self
.
request
(
"POST"
,
f"/repos/
{
repository
}
/actions/workflows/
{
encoded
}
/dispatches"
,
{
"ref"
:
ref
,
"inputs"
:
inputs
},
)
def
utc_now
()
->
str
:
return
dt
.
datetime
.
now
(
dt
.
UTC
).
replace
(
microsecond
=
0
).
isoformat
().
replace
(
"+00:00"
,
"Z"
)
def
load_json
(
path
:
Path
,
label
:
str
)
->
Any
:
try
:
return
json
.
loads
(
path
.
read_text
(
encoding
=
"utf-8"
))
except
(
OSError
,
json
.
JSONDecodeError
)
as
error
:
raise
ContinuityError
(
f"cannot read
{
label
}
{
path
}
:
{
error
}
"
)
from
error
def
validate_resolution_source
(
run
:
Any
,
workflow
:
Any
,
*
,
expected_run_id
:
int
,
expected_run_attempt
:
int
,
)
->
dict
[
str
,
Any
]:
if
(
not
isinstance
(
workflow
,
dict
)
or
type
(
workflow
.
get
(
"id"
))
is
not
int
or
workflow
[
"id"
]
<
1
or
workflow
.
get
(
"name"
)
!=
BETA_CANDIDATE_WORKFLOW_NAME
or
workflow
.
get
(
"path"
)
!=
BETA_CANDIDATE_WORKFLOW_PATH
or
workflow
.
get
(
"state"
)
!=
"active"
):
raise
ContinuityError
(
"trusted Beta candidate workflow metadata is invalid"
)
if
not
isinstance
(
run
,
dict
):
raise
ContinuityError
(
"continuity resolution qualification is absent"
)
repository
=
run
.
get
(
"repository"
)
head_repository
=
run
.
get
(
"head_repository"
)
if
(
type
(
run
.
get
(
"id"
))
is
not
int
or
run
.
get
(
"id"
)
!=
expected_run_id
or
type
(
run
.
get
(
"run_attempt"
))
is
not
int
or
run
.
get
(
"run_attempt"
)
!=
expected_run_attempt
or
not
isinstance
(
repository
,
dict
)
or
repository
.
get
(
"full_name"
)
!=
CONTROL_REPOSITORY
or
not
isinstance
(
head_repository
,
dict
)
or
head_repository
.
get
(
"full_name"
)
!=
CONTROL_REPOSITORY
or
run
.
get
(
"workflow_id"
)
!=
workflow
[
"id"
]
or
run
.
get
(
"path"
)
not
in
{
BETA_CANDIDATE_WORKFLOW_PATH
,
f"
{
BETA_CANDIDATE_WORKFLOW_PATH
}
@main"
,
f"
{
BETA_CANDIDATE_WORKFLOW_PATH
}
@refs/heads/main"
,
}
or
run
.
get
(
"event"
)
!=
"push"
or
run
.
get
(
"head_branch"
)
!=
"main"
):
raise
ContinuityError
(
"continuity resolution qualification is from an untrusted workflow"
)
head_sha
=
run
.
get
(
"head_sha"
)
if
not
isinstance
(
head_sha
,
str
)
or
not
COMMIT_PATTERN
.
fullmatch
(
head_sha
):
raise
ContinuityError
(
"continuity resolution qualification has an invalid source revision"
)
if
run
.
get
(
"status"
)
!=
"completed"
:
raise
ContinuityError
(
"continuity resolution qualification is pending"
)
if
run
.
get
(
"conclusion"
)
==
"cancelled"
:
raise
ContinuityError
(
"continuity resolution qualification was cancelled"
)
if
run
.
get
(
"conclusion"
)
!=
"success"
:
raise
ContinuityError
(
"continuity resolution qualification failed"
)
return
{
"repository"
:
CONTROL_REPOSITORY
,
"workflow"
:
BETA_CANDIDATE_WORKFLOW_PATH
,
"event"
:
"push"
,
"head_branch"
:
"main"
,
"head_sha"
:
head_sha
,
"run_id"
:
expected_run_id
,
"run_attempt"
:
expected_run_attempt
,
"status"
:
"completed"
,
"conclusion"
:
"success"
,
}
def
qualified_resolution_source
(
expected_run_id
:
int
,
expected_run_attempt
:
int
,
token
:
str
,
)
->
dict
[
str
,
Any
]:
if
expected_run_id
<
1
or
expected_run_attempt
<
1
:
raise
ContinuityError
(
"continuity resolution qualification identity must be positive"
)
writer
=
GitHubWriter
(
token
,
os
.
environ
.
get
(
"GITHUB_API_URL"
,
"https://api.github.com"
))
run
=
writer
.
get
(
f"/repos/
{
CONTROL_REPOSITORY
}
/actions/runs/
{
expected_run_id
}
/"
f"attempts/
{
expected_run_attempt
}
"
)
workflow
=
writer
.
get
(
f"/repos/
{
CONTROL_REPOSITORY
}
/actions/workflows/
{
CANDIDATE_WORKFLOW
}
"
)
return
validate_resolution_source
(
run
,
workflow
,
expected_run_id
=
expected_run_id
,
expected_run_attempt
=
expected_run_attempt
,
)
def
resolution_source_command
(
expected_run_id
:
int
,
expected_run_attempt
:
int
,
output
:
Path
|
None
,
)
->
dict
[
str
,
Any
]:
source
=
qualified_resolution_source
(
expected_run_id
,
expected_run_attempt
,
os
.
environ
.
get
(
"GH_TOKEN"
,
""
),
)
write_github_output
(
output
,
{
"source_head_sha"
:
source
[
"head_sha"
],
"source_run_attempt"
:
str
(
source
[
"run_attempt"
]),
"source_run_id"
:
str
(
source
[
"run_id"
]),
},
)
return
source
def
load_config
(
path
:
Path
)
->
dict
[
str
,
Any
]:
value
=
load_json
(
path
,
"continuity config"
)
if
not
isinstance
(
value
,
dict
)
or
set
(
value
)
!=
{
"$schema"
,
"authority_issue"
,
"channel"
,
"drill"
,
"evidence_work_items"
,
"first_component"
,
"plan_prefix"
,
"public_issue_budget"
,
"required_issue_labels"
,
"superseded_interruption"
,
}:
raise
ContinuityError
(
"continuity config has an invalid top-level shape"
)
if
value
.
get
(
"$schema"
)
!=
"./schema.json"
or
value
.
get
(
"channel"
)
!=
"alpha"
:
raise
ContinuityError
(
"continuity config must select the alpha prerelease channel and local schema"
)
if
not
isinstance
(
value
.
get
(
"drill"
),
str
)
or
not
WORK_ID_PATTERN
.
fullmatch
(
value
[
"drill"
]):
raise
ContinuityError
(
"continuity drill has an invalid identity"
)
issue
=
value
.
get
(
"authority_issue"
)
if
not
isinstance
(
issue
,
dict
)
or
set
(
issue
)
!=
{
"number"
,
"repository"
,
"work_id"
}:
raise
ContinuityError
(
"continuity authority issue has an invalid shape"
)
if
(
issue
.
get
(
"repository"
)
!=
CONTROL_REPOSITORY
or
type
(
issue
.
get
(
"number"
))
is
not
int
or
issue
[
"number"
]
<
1
or
not
isinstance
(
issue
.
get
(
"work_id"
),
str
)
or
not
WORK_ID_PATTERN
.
fullmatch
(
issue
[
"work_id"
])
):
raise
ContinuityError
(
"continuity authority issue is invalid"
)
work_items
=
value
.
get
(
"evidence_work_items"
)
if
not
isinstance
(
work_items
,
list
)
or
not
work_items
:
raise
ContinuityError
(
"continuity evidence work items are invalid"
)
work_item_locations
:
set
[
tuple
[
str
,
int
]]
=
set
()
for
work_item
in
work_items
:
if
(
not
isinstance
(
work_item
,
dict
)
or
set
(
work_item
)
!=
{
"number"
,
"repository"
,
"required_labels"
,
"work_id"
}
or
not
isinstance
(
work_item
.
get
(
"repository"
),
str
)
or
not
PUBLIC_REPOSITORY_PATTERN
.
fullmatch
(
work_item
[
"repository"
])
or
type
(
work_item
.
get
(
"number"
))
is
not
int
or
work_item
[
"number"
]
<
1
or
not
isinstance
(
work_item
.
get
(
"work_id"
),
str
)
or
not
WORK_ID_PATTERN
.
fullmatch
(
work_item
[
"work_id"
])
or
not
isinstance
(
work_item
.
get
(
"required_labels"
),
list
)
or
not
all
(
isinstance
(
label
,
str
)
and
label
for
label
in
work_item
[
"required_labels"
])
or
len
(
set
(
work_item
[
"required_labels"
]))
!=
len
(
work_item
[
"required_labels"
])
or
not
{
"authority:github"
,
"beta:blocker"
,
"completion:evidence-required"
,
"status:ready"
,
}
<=
set
(
work_item
[
"required_labels"
])
):
raise
ContinuityError
(
"continuity evidence work item authority is invalid"
)
require_exact_issue_kind
(
set
(
work_item
[
"required_labels"
]),
f"continuity evidence work item
{
work_item
[
'repository'
]
}
#
{
work_item
[
'number'
]
}
"
,
)
location
=
(
work_item
[
"repository"
],
work_item
[
"number"
])
if
location
in
work_item_locations
or
location
==
(
issue
[
"repository"
],
issue
[
"number"
]):
raise
ContinuityError
(
"continuity evidence work item inventory contains a duplicate authority"
)
work_item_locations
.
add
(
location
)
if
not
isinstance
(
value
.
get
(
"plan_prefix"
),
str
)
or
not
PLAN_PREFIX_PATTERN
.
fullmatch
(
value
[
"plan_prefix"
]):
raise
ContinuityError
(
"continuity plan prefix has an invalid identity"
)
budget
=
value
.
get
(
"public_issue_budget"
)
if
budget
!=
{
"max_open_actionable"
:
1
,
"stale_age_seconds"
:
604800
}:
raise
ContinuityError
(
"continuity public issue budget is invalid"
)
if
value
.
get
(
"first_component"
)
not
in
COMPONENTS
:
raise
ContinuityError
(
"continuity first component is unknown"
)
labels
=
value
.
get
(
"required_issue_labels"
)
if
not
isinstance
(
labels
,
list
)
or
not
labels
or
not
all
(
isinstance
(
label
,
str
)
and
label
for
label
in
labels
):
raise
ContinuityError
(
"continuity required issue labels are invalid"
)
require_exact_issue_kind
(
set
(
labels
),
"continuity parent authority"
)
superseded
=
value
.
get
(
"superseded_interruption"
)
if
(
not
isinstance
(
superseded
,
dict
)
or
set
(
superseded
)
!=
{
"reason"
,
"tag"
}
or
not
isinstance
(
superseded
.
get
(
"reason"
),
str
)
or
not
WORK_ID_PATTERN
.
fullmatch
(
superseded
[
"reason"
])
or
not
isinstance
(
superseded
.
get
(
"tag"
),
str
)
or
not
superseded
[
"tag"
].
startswith
(
PHASE_TAG_PREFIX
)
or
not
superseded
[
"tag"
].
endswith
(
"/interrupted"
)
):
raise
ContinuityError
(
"superseded continuity interruption is invalid"
)
return
value
def
optional_public_json
(
client
:
PublicClient
,
url
:
str
)
->
Any
|
None
:
try
:
return
client
.
json
(
url
)
except
CandidateError
as
error
:
if
"(404)"
in
str
(
error
):
return
None
raise
def
has_exact_work_id
(
issue
:
dict
[
str
,
Any
],
work_id
:
str
)
->
bool
:
body
=
str
(
issue
.
get
(
"body"
,
""
))
return
body
.
count
(
BETA_WORK_ID_MARKER_PREFIX
)
==
1
and
BETA_WORK_ID_MARKER
.
findall
(
body
)
==
[
work_id
]
def
has_exact_durable_work_id
(
issue
:
dict
[
str
,
Any
],
work_id
:
str
)
->
bool
:
body
=
str
(
issue
.
get
(
"body"
,
""
))
return
body
.
count
(
DURABLE_WORK_ID_MARKER_PREFIX
)
==
1
and
DURABLE_WORK_ID_MARKER
.
findall
(
body
)
==
[
work_id
]
def
authority_issue
(
config
:
dict
[
str
,
Any
],
client
:
PublicClient
,
*
,
allow_completed
:
bool
=
False
)
->
dict
[
str
,
Any
]:
specification
=
config
[
"authority_issue"
]
issue
=
client
.
json
(
f"https://api.github.com/repos/
{
specification
[
'repository'
]
}
/issues/
{
specification
[
'number'
]
}
"
)
labels
=
{
label
.
get
(
"name"
)
for
label
in
issue
.
get
(
"labels"
, [])
if
isinstance
(
label
,
dict
)
and
label
.
get
(
"name"
)}
missing
=
set
(
config
[
"required_issue_labels"
])
-
labels
exact_work_id
=
has_exact_work_id
(
issue
,
specification
[
"work_id"
])
required_authority
=
{
label
for
label
in
config
[
"required_issue_labels"
]
if
not
label
.
startswith
(
"status:"
)
and
not
label
.
startswith
(
"completion:"
)
}
completed
=
(
allow_completed
and
issue
.
get
(
"state"
)
==
"closed"
and
required_authority
<=
labels
and
{
"status:done"
,
"completion:evidence-verified"
}
<=
labels
)
if
(
issue
.
get
(
"state"
)
!=
"open"
and
not
completed
)
or
(
missing
and
not
completed
)
or
not
exact_work_id
:
raise
ContinuityError
(
f"authority issue is not ready: state=
{
issue
.
get
(
'state'
)
}
, missing_labels=
{
sorted
(
missing
)
}
, "
f"work_id_marker=
{
exact_work_id
}
"
)
return
{
"number"
:
specification
[
"number"
],
"repository"
:
specification
[
"repository"
],
"url"
:
issue
.
get
(
"html_url"
),
"labels"
:
sorted
(
labels
),
"state"
:
issue
[
"state"
],
"updated_at"
:
issue
.
get
(
"updated_at"
),
"work_id"
:
specification
[
"work_id"
],
}
def
parse_manifest_version
(
component
:
str
,
raw
:
bytes
)
->
str
:
path
,
table
,
package
=
SOURCE_MANIFESTS
[
component
]
try
:
document
=
tomllib
.
loads
(
raw
.
decode
(
"utf-8"
))
except
(
UnicodeDecodeError
,
tomllib
.
TOMLDecodeError
)
as
error
:
raise
ContinuityError
(
f"
{
component
}
{
path
}
is not valid TOML:
{
error
}
"
)
from
error
section
=
document
.
get
(
table
)
if
not
isinstance
(
section
,
dict
)
or
section
.
get
(
"name"
)
!=
package
:
raise
ContinuityError
(
f"
{
component
}
{
path
}
does not declare
{
package
}
in [
{
table
}
]"
)
version
=
section
.
get
(
"version"
)
if
not
isinstance
(
version
,
str
):
raise
ContinuityError
(
f"
{
component
}
{
path
}
has no exact package version"
)
return
version
def
next_version
(
component
:
str
,
tags
:
list
[
str
])
->
str
:
pattern
=
ALPHA_VERSION_PATTERN
if
component
in
{
"workflow"
,
"waterline"
}
else
STABLE_VERSION_PATTERN
matches
=
[
match
for
tag
in
tags
if
(
match
:=
pattern
.
fullmatch
(
tag
))]
if
not
matches
:
raise
ContinuityError
(
f"
{
component
}
has no public version baseline"
)
latest
=
max
(
matches
,
key
=
lambda
match
:
int
(
match
.
group
(
2
)))
return
f"
{
latest
.
group
(
1
)
}
{
int
(
latest
.
group
(
2
))
+
1
}
"
def
public_releases
(
client
:
PublicClient
,
repository
:
str
)
->
list
[
dict
[
str
,
Any
]]:
endpoint
=
f"https://api.github.com/repos/
{
repository
}
/releases"
releases
:
list
[
dict
[
str
,
Any
]]
=
[]
full_page_digests
:
set
[
str
]
=
set
()
seen_tags
:
set
[
str
]
=
set
()
for
page
in
range
(
1
,
GITHUB_RELEASE_PAGE_LIMIT
+
1
):
page_releases
=
client
.
json
(
f"
{
endpoint
}
?per_page=
{
GITHUB_RELEASE_PAGE_SIZE
}
&page=
{
page
}
"
)
if
(
not
isinstance
(
page_releases
,
list
)
or
len
(
page_releases
)
>
GITHUB_RELEASE_PAGE_SIZE
or
any
(
not
isinstance
(
release
,
dict
)
or
not
isinstance
(
release
.
get
(
"tag_name"
),
str
)
or
not
release
[
"tag_name"
]
or
type
(
release
.
get
(
"draft"
))
is
not
bool
for
release
in
page_releases
)
):
raise
ContinuityError
(
f"
{
repository
}
releases response page
{
page
}
is invalid"
)
page_tags
=
{
release
[
"tag_name"
]
for
release
in
page_releases
}
if
len
(
page_tags
)
!=
len
(
page_releases
)
or
seen_tags
&
page_tags
:
raise
ContinuityError
(
f"
{
repository
}
release pagination did not advance"
)
seen_tags
.
update
(
page_tags
)
releases
.
extend
(
page_releases
)
if
len
(
page_releases
)
<
GITHUB_RELEASE_PAGE_SIZE
:
return
releases
page_digest
=
hashlib
.
sha256
(
canonical_json
(
page_releases
)).
hexdigest
()
if
page_digest
in
full_page_digests
:
raise
ContinuityError
(
f"
{
repository
}
release pagination did not advance"
)
full_page_digests
.
add
(
page_digest
)
raise
ContinuityError
(
f"
{
repository
}
release history exceeded the pagination bound"
)
def
public_release_tags
(
client
:
PublicClient
,
repository
:
str
)
->
list
[
str
]:
return
[
release
[
"tag_name"
]
for
release
in
public_releases
(
client
,
repository
)
if
not
release
[
"draft"
]]
def
require_unoccupied_fresh_versions
(
client
:
PublicClient
,
versions
:
dict
[
str
,
str
])
->
None
:
for
name
,
component
in
COMPONENTS
.
items
():
version
=
versions
[
name
]
encoded
=
urllib
.
parse
.
quote
(
version
,
safe
=
""
)
tag
=
optional_public_json
(
client
,
f"https://api.github.com/repos/
{
component
.
repository
}
/git/ref/tags/
{
encoded
}
"
,
)
release
=
optional_public_json
(
client
,
f"https://api.github.com/repos/
{
component
.
repository
}
/releases/tags/
{
encoded
}
"
,
)
occupied_by
=
[
label
for
label
,
record
in
((
"tag"
,
tag
), (
"release"
,
release
))
if
record
is
not
None
]
if
occupied_by
:
raise
ContinuityError
(
f"fresh continuity version
{
component
.
repository
}
@
{
version
}
is already occupied by public "
f"
{
' and '
.
join
(
occupied_by
)
}
authority"
)
def
has_routed_blocker_authority
(
issue
:
dict
[
str
,
Any
])
->
bool
:
labels
=
issue
.
get
(
"labels"
)
if
not
isinstance
(
labels
,
list
):
return
False
names
:
set
[
str
]
=
set
()
for
label
in
labels
:
if
not
isinstance
(
label
,
dict
)
or
not
isinstance
(
label
.
get
(
"name"
),
str
)
or
not
label
[
"name"
]:
return
False
names
.
add
(
label
[
"name"
])
lifecycle
=
{
name
for
name
in
names
if
name
.
startswith
(
"status:"
)}
return
(
set
(
ROUTED_BLOCKER_AUTHORITY_LABELS
)
<=
names
and
len
(
lifecycle
)
==
1
and
lifecycle
<=
ROUTED_BLOCKER_LIFECYCLE_LABELS
)
def
is_exact_routed_blocker
(
config
:
dict
[
str
,
Any
],
issue
:
dict
[
str
,
Any
],
component_name
:
str
,
version
:
str
,
)
->
bool
:
body
=
issue
.
get
(
"body"
)
if
not
isinstance
(
body
,
str
)
or
not
has_routed_blocker_dependency
(
config
,
issue
):
return
False
markers
=
list
(
ROUTED_BLOCKER_MARKER
.
finditer
(
body
))
return
(
body
.
count
(
ROUTED_BLOCKER_MARKER_PREFIX
)
==
1
and
len
(
markers
)
==
1
and
markers
[
0
].
group
(
"component"
)
==
component_name
and
markers
[
0
].
group
(
"version"
)
==
version
and
VERSION_PATTERN
.
fullmatch
(
version
)
is
not
None
)
def
has_routed_blocker_dependency
(
config
:
dict
[
str
,
Any
],
issue
:
dict
[
str
,
Any
])
->
bool
:
authority
=
config
[
"authority_issue"
]
dependency
=
f"Blocks https://github.com/
{
authority
[
'repository'
]
}
/issues/
{
authority
[
'number'
]
}
."
return
dependency
in
str
(
issue
.
get
(
"body"
,
""
))
def
selection_plan_name
(
config
:
dict
[
str
,
Any
],
versions
:
dict
[
str
,
str
])
->
str
:
digest
=
hashlib
.
sha256
(
canonical_json
(
versions
)).
hexdigest
()[:
12
]
return
f"
{
config
[
'plan_prefix'
]
}
-
{
digest
}
"
def
validate_selection
(
config
:
dict
[
str
,
Any
],
selection
:
Any
)
->
None
:
expected_keys
=
{
"schema"
,
"drill"
,
"plan"
,
"channel"
,
"versions"
}
if
not
isinstance
(
selection
,
dict
)
or
set
(
selection
)
!=
expected_keys
:
raise
ContinuityError
(
"continuity selection has an invalid top-level shape"
)
if
(
selection
.
get
(
"schema"
)
!=
SELECTION_SCHEMA
or
selection
.
get
(
"drill"
)
!=
config
[
"drill"
]
or
selection
.
get
(
"channel"
)
!=
config
[
"channel"
]
):
raise
ContinuityError
(
"continuity selection does not match this drill"
)
versions
=
selection
.
get
(
"versions"
)
if
not
isinstance
(
versions
,
dict
)
or
set
(
versions
)
!=
set
(
COMPONENTS
):
raise
ContinuityError
(
f"continuity selection versions must be exactly
{
sorted
(
COMPONENTS
)
}
"
)
for
name
,
version
in
versions
.
items
():
pattern
=
ALPHA_VERSION_PATTERN
if
name
in
{
"workflow"
,
"waterline"
}
else
STABLE_VERSION_PATTERN
if
not
isinstance
(
version
,
str
)
or
not
pattern
.
fullmatch
(
version
):
raise
ContinuityError
(
f"continuity selection version for
{
name
}
is invalid"
)
if
selection
.
get
(
"plan"
)
!=
selection_plan_name
(
config
,
versions
):
raise
ContinuityError
(
"continuity selection plan identity does not match its version tuple"
)
def
select_versions
(
config
:
dict
[
str
,
Any
],
client
:
PublicClient
)
->
dict
[
str
,
Any
]:
versions
=
{
name
:
next_version
(
name
,
public_release_tags
(
client
,
component
.
repository
))
for
name
,
component
in
COMPONENTS
.
items
()
}
selection
=
{
"schema"
:
SELECTION_SCHEMA
,
"drill"
:
config
[
"drill"
],
"plan"
:
selection_plan_name
(
config
,
versions
),
"channel"
:
config
[
"channel"
],
"versions"
:
versions
,
}
validate_selection
(
config
,
selection
)
require_unoccupied_fresh_versions
(
client
,
versions
)
return
selection
def
selection_tag
(
config
:
dict
[
str
,
Any
])
->
str
:
return
f"
{
SELECTION_TAG_PREFIX
}
{
config
[
'drill'
]
}
"
def
public_selection
(
config
:
dict
[
str
,
Any
],
client
:
PublicClient
)
->
tuple
[
dict
[
str
,
Any
],
dict
[
str
,
str
]]
|
None
:
tag
=
selection_tag
(
config
)
commit
=
resolve_tag
(
client
,
CONTROL_REPOSITORY
,
tag
)
if
commit
is
None
:
return
None
selection
=
read_public_json_file
(
client
,
commit
,
"continuity-selection.json"
)
validate_selection
(
config
,
selection
)
return
selection
, {
"tag"
:
tag
,
"commit"
:
commit
,
"sha256"
:
manifest_digest
(
selection
)}
def
build_plan
(
config
:
dict
[
str
,
Any
],
client
:
PublicClient
,
selection
:
dict
[
str
,
Any
]
|
None
=
None
,
)
->
tuple
[
dict
[
str
,
Any
],
dict
[
str
,
str
]]:
selection
=
selection
or
select_versions
(
config
,
client
)
validate_selection
(
config
,
selection
)
components
:
dict
[
str
,
dict
[
str
,
str
]]
=
{}
expected_commits
:
dict
[
str
,
str
]
=
{}
blockers
:
list
[
dict
[
str
,
str
]]
=
[]
for
name
,
component
in
COMPONENTS
.
items
():
version
=
selection
[
"versions"
][
name
]
commit
=
resolve_tag
(
client
,
component
.
repository
,
version
)
if
commit
is
None
:
branch
=
EXPECTED_DEFAULT_BRANCHES
[
name
]
branch_record
=
client
.
json
(
f"https://api.github.com/repos/
{
component
.
repository
}
/branches/
{
urllib
.
parse
.
quote
(
branch
,
safe
=
''
)
}
"
)
commit
=
branch_record
.
get
(
"commit"
, {}).
get
(
"sha"
)
if
not
isinstance
(
commit
,
str
)
or
not
COMMIT_PATTERN
.
fullmatch
(
commit
):
raise
ContinuityError
(
f"
{
component
.
repository
}
@
{
version
}
did not resolve to an exact source commit"
)
if
name
in
SOURCE_MANIFESTS
:
path
,
_table
,
_package
=
SOURCE_MANIFESTS
[
name
]
encoded_path
=
urllib
.
parse
.
quote
(
path
,
safe
=
"/"
)
raw
=
client
.
bytes
(
f"https://api.github.com/repos/
{
component
.
repository
}
/contents/
{
encoded_path
}
?ref=
{
commit
}
"
,
accept
=
"application/vnd.github.raw+json"
,
)
declared
=
parse_manifest_version
(
name
,
raw
)
if
declared
!=
version
:
blockers
.
append
(
{
"component"
:
name
,
"reason"
: (
f"
{
component
.
repository
}
@
{
commit
}
declares
{
declared
}
in
{
path
}
; "
f"the retained continuity version is
{
version
}
"
),
"repository"
:
component
.
repository
,
"slug"
:
f"
{
name
}
-source-version-
{
version
}
"
,
"version"
:
version
,
}
)
components
[
name
]
=
{
"commit"
:
commit
,
"version"
:
version
}
expected_commits
[
name
]
=
commit
if
blockers
:
raise
PlanBlocked
(
blockers
)
plan
=
{
"schema"
:
RELEASE_PLAN_SCHEMA
,
"plan"
:
selection
[
"plan"
],
"channel"
:
config
[
"channel"
],
"foundation"
: {
"tag"
:
FOUNDATION_TAG
,
"commit"
:
FOUNDATION_COMMIT
},
"components"
:
components
,
"beta_authorization"
:
None
,
}
validate_plan
(
plan
)
return
plan
,
expected_commits
def
phase_tag
(
plan
:
dict
[
str
,
Any
],
phase
:
str
)
->
str
:
if
phase
not
in
PHASES
:
raise
ContinuityError
(
f"unknown continuity phase
{
phase
}
"
)
return
f"
{
PHASE_TAG_PREFIX
}
{
plan
[
'plan'
]
}
/
{
phase
}
"
def
read_public_json_file
(
client
:
PublicClient
,
commit
:
str
,
filename
:
str
)
->
Any
:
encoded
=
urllib
.
parse
.
quote
(
filename
,
safe
=
"/"
)
raw
=
client
.
bytes
(
f"https://api.github.com/repos/
{
CONTROL_REPOSITORY
}
/contents/
{
encoded
}
?ref=
{
commit
}
"
,
accept
=
"application/vnd.github.raw+json"
,
)
try
:
return
json
.
loads
(
raw
)
except
json
.
JSONDecodeError
as
error
:
raise
ContinuityError
(
f"
{
CONTROL_REPOSITORY
}
@
{
commit
}
:
{
filename
}
is not valid JSON"
)
from
error
def
superseded_interruption
(
config
:
dict
[
str
,
Any
],
client
:
PublicClient
)
->
dict
[
str
,
str
]:
specification
=
config
[
"superseded_interruption"
]
tag
=
specification
[
"tag"
]
commit
=
resolve_tag
(
client
,
CONTROL_REPOSITORY
,
tag
)
if
commit
is
None
:
raise
ContinuityError
(
f"superseded interruption
{
tag
}
is not retained"
)
evidence
=
read_public_json_file
(
client
,
commit
,
"continuity-evidence.json"
)
plan
=
read_public_json_file
(
client
,
commit
,
"release-plan.json"
)
validate_recorded_release_plan
(
plan
)
if
evidence
.
get
(
"phase"
)
!=
"interrupted"
or
phase_tag
(
plan
,
"interrupted"
)
!=
tag
:
raise
ContinuityError
(
f"superseded interruption
{
tag
}
has invalid diagnostic evidence"
)
return
{
"commit"
:
commit
,
"evidence_sha256"
:
manifest_digest
(
evidence
),
"plan_sha256"
:
manifest_digest
(
plan
),
"reason"
:
specification
[
"reason"
],
"tag"
:
tag
,
}
def
accepted_plan
(
config
:
dict
[
str
,
Any
],
client
:
PublicClient
)
->
dict
[
str
,
Any
]
|
None
:
prefix
=
f"
{
PHASE_TAG_PREFIX
}
{
config
[
'plan_prefix'
]
}
-"
encoded
=
urllib
.
parse
.
quote
(
prefix
,
safe
=
"/"
)
refs
=
client
.
json
(
f"https://api.github.com/repos/
{
CONTROL_REPOSITORY
}
/git/matching-refs/tags/
{
encoded
}
"
)
accepted
=
[
ref
for
ref
in
refs
if
str
(
ref
.
get
(
"ref"
,
""
)).
endswith
(
"/accepted"
)]
if
len
(
accepted
)
>
1
:
raise
ContinuityError
(
"multiple accepted continuity plans exist for this drill"
)
if
not
accepted
:
return
None
commit
=
accepted
[
0
].
get
(
"object"
, {}).
get
(
"sha"
)
if
not
isinstance
(
commit
,
str
)
or
not
COMMIT_PATTERN
.
fullmatch
(
commit
):
raise
ContinuityError
(
"accepted continuity phase does not resolve to a commit"
)
plan
=
read_public_json_file
(
client
,
commit
,
"release-plan.json"
)
validate_recorded_release_plan
(
plan
)
return
plan
def
plan_command
(
config_path
:
Path
,
plan_path
:
Path
,
expected_path
:
Path
,
state_path
:
Path
,
output
:
Path
|
None
,
expected_plan_tag
:
str
|
None
=
None
,
)
->
None
:
config
=
load_config
(
config_path
)
client
=
PublicClient
(
os
.
environ
.
get
(
"GITHUB_TOKEN"
))
issue
=
authority_issue
(
config
,
client
,
allow_completed
=
True
)
accepted
=
accepted_plan
(
config
,
client
)
if
expected_plan_tag
:
actual_plan_tag
=
f"
{
PLAN_TAG_PREFIX
}
{
accepted
[
'plan'
]
}
"
if
accepted
is
not
None
else
None
if
actual_plan_tag
!=
expected_plan_tag
:
raise
ContinuityError
(
f"continuity callback requested
{
expected_plan_tag
}
, but exact accepted plan is "
f"
{
actual_plan_tag
or
'unavailable'
}
"
)
selected
=
public_selection
(
config
,
client
)
state
:
dict
[
str
,
Any
]
=
{
"schema"
:
EVIDENCE_SCHEMA
,
"drill"
:
config
[
"drill"
],
"phase"
:
"planning"
,
"observed_at"
:
utc_now
(),
"authority_issue"
:
issue
,
}
try
:
if
accepted
is
None
:
if
selected
is
None
:
selection
=
select_versions
(
config
,
client
)
selection_record
=
record_selection
(
Path
.
cwd
(),
config
,
selection
)
else
:
selection
,
selection_record
=
selected
state
[
"selection"
]
=
{
**
selection_record
,
"plan"
:
selection
[
"plan"
],
"sha256"
:
manifest_digest
(
selection
),
"versions"
:
selection
[
"versions"
],
}
plan
,
expected
=
build_plan
(
config
,
client
,
selection
)
controller_commit
=
os
.
environ
.
get
(
"GITHUB_SHA"
)
if
controller_commit
:
if
not
COMMIT_PATTERN
.
fullmatch
(
controller_commit
):
raise
ContinuityError
(
"GitHub controller revision is not an exact commit"
)
expected
[
"github-control-plane"
]
=
controller_commit
needs_qualification
=
"true"
else
:
plan
=
accepted
if
selected
is
not
None
:
selection
,
selection_record
=
selected
if
(
plan
[
"plan"
]
!=
selection
[
"plan"
]
or
{
name
:
identity
[
"version"
]
for
name
,
identity
in
plan
[
"components"
].
items
()}
!=
selection
[
"versions"
]
):
raise
ContinuityError
(
"accepted continuity plan differs from its immutable version selection"
)
state
[
"selection"
]
=
{
**
selection_record
,
"plan"
:
selection
[
"plan"
]}
expected
=
{
name
:
identity
[
"commit"
]
for
name
,
identity
in
plan
[
"components"
].
items
()}
needs_qualification
=
"false"
state
.
update
(
{
"outcome"
:
"ready"
,
"plan"
: {
"tag"
:
f"
{
PLAN_TAG_PREFIX
}
{
plan
[
'plan'
]
}
"
,
"sha256"
:
manifest_digest
(
plan
)},
}
)
except
PlanBlocked
as
error
:
state
.
update
({
"outcome"
:
"blocked"
,
"blockers"
:
error
.
blockers
})
state_path
.
write_bytes
(
canonical_json
(
state
))
raise
plan_path
.
write_bytes
(
canonical_json
(
plan
))
expected_path
.
write_bytes
(
canonical_json
(
expected
))
state_path
.
write_bytes
(
canonical_json
(
state
))
write_github_output
(
output
,
{
"needs_qualification"
:
needs_qualification
,
"plan"
:
plan
[
"plan"
],
"plan_tag"
:
f"
{
PLAN_TAG_PREFIX
}
{
plan
[
'plan'
]
}
"
,
},
)
def
record_immutable_tag
(
repository
:
Path
,
tag
:
str
,
files
:
list
[
tuple
[
str
,
bytes
]],
message
:
str
,
label
:
str
,
*
,
remote
:
str
,
)
->
dict
[
str
,
str
]:
existing_ref
=
fetch_existing_record
(
repository
,
remote
,
tag
)
if
existing_ref
:
for
filename
,
content
in
files
:
if
read_record_file
(
repository
,
existing_ref
,
filename
)
!=
content
:
raise
ContinuityError
(
f"immutable
{
label
}
{
tag
}
differs from the requested record"
)
return
{
"status"
:
"existing"
,
"tag"
:
tag
,
"commit"
:
run_git
([
"rev-parse"
,
existing_ref
],
cwd
=
repository
)}
with
tempfile
.
NamedTemporaryFile
(
prefix
=
"beta-continuity-index-"
,
delete
=
False
)
as
index
:
index_path
=
Path
(
index
.
name
)
try
:
env
=
os
.
environ
.
copy
()
env
[
"GIT_INDEX_FILE"
]
=
str
(
index_path
)
index_path
.
unlink
(
missing_ok
=
True
)
run_git
([
"read-tree"
,
"--empty"
],
cwd
=
repository
,
env
=
env
)
for
filename
,
content
in
files
:
blob
=
(
subprocess
.
run
(
[
"git"
,
"hash-object"
,
"-w"
,
"--stdin"
],
cwd
=
repository
,
env
=
env
,
input
=
content
,
check
=
True
,
stdout
=
subprocess
.
PIPE
,
)
.
stdout
.
decode
()
.
strip
()
)
run_git
(
[
"update-index"
,
"--add"
,
"--cacheinfo"
,
f"100644,
{
blob
}
,
{
filename
}
"
],
cwd
=
repository
,
env
=
env
,
)
tree
=
run_git
([
"write-tree"
],
cwd
=
repository
,
env
=
env
)
commit_env
=
env
|
{
"GIT_AUTHOR_NAME"
:
"Durable Workflow Continuity"
,
"GIT_AUTHOR_EMAIL"
:
"support@durable-workflow.com"
,
"GIT_COMMITTER_NAME"
:
"Durable Workflow Continuity"
,
"GIT_COMMITTER_EMAIL"
:
"support@durable-workflow.com"
,
}
commit
=
subprocess
.
run
(
[
"git"
,
"commit-tree"
,
tree
],
cwd
=
repository
,
env
=
commit_env
,
input
=
f"
{
message
}
\n
"
,
check
=
True
,
text
=
True
,
capture_output
=
True
,
).
stdout
.
strip
()
pushed
=
subprocess
.
run
(
[
"git"
,
"push"
,
remote
,
f"
{
commit
}
:refs/tags/
{
tag
}
"
],
cwd
=
repository
,
check
=
False
,
text
=
True
,
capture_output
=
True
,
)
if
pushed
.
returncode
:
recovered
=
fetch_existing_record
(
repository
,
remote
,
tag
)
if
not
recovered
:
raise
ContinuityError
(
f"cannot publish immutable
{
label
}
:
{
pushed
.
stderr
.
strip
()
}
"
)
for
filename
,
content
in
files
:
if
read_record_file
(
repository
,
recovered
,
filename
)
!=
content
:
raise
ContinuityError
(
f"concurrent
{
label
}
{
tag
}
has different evidence"
)
commit
=
run_git
([
"rev-parse"
,
recovered
],
cwd
=
repository
)
return
{
"status"
:
"existing"
,
"tag"
:
tag
,
"commit"
:
commit
}
return
{
"status"
:
"created"
,
"tag"
:
tag
,
"commit"
:
commit
}
finally
:
index_path
.
unlink
(
missing_ok
=
True
)
def
record_selection
(
repository
:
Path
,
config
:
dict
[
str
,
Any
],
selection
:
dict
[
str
,
Any
],
*
,
remote
:
str
=
"origin"
,
)
->
dict
[
str
,
str
]:
validate_selection
(
config
,
selection
)
tag
=
selection_tag
(
config
)
return
record_immutable_tag
(
repository
,
tag
,
[(
"continuity-selection.json"
,
canonical_json
(
selection
))],
f"Select versions for continuity drill
{
config
[
'drill'
]
}
"
,
"continuity selection"
,
remote
=
remote
,
)
def
record_phase
(
repository
:
Path
,
plan
:
dict
[
str
,
Any
],
phase
:
str
,
evidence
:
dict
[
str
,
Any
],
*
,
qualification_path
:
Path
|
None
=
None
,
remote
:
str
=
"origin"
,
)
->
dict
[
str
,
str
]:
tag
=
phase_tag
(
plan
,
phase
)
files
:
list
[
tuple
[
str
,
bytes
]]
=
[
(
"continuity-evidence.json"
,
canonical_json
(
evidence
)),
(
"release-plan.json"
,
canonical_json
(
plan
)),
]
if
qualification_path
is
not
None
and
qualification_path
.
exists
():
qualification
=
load_json
(
qualification_path
,
"target qualification evidence"
)
files
.
append
((
"target-qualification-evidence.json"
,
canonical_json
(
qualification
)))
return
record_immutable_tag
(
repository
,
tag
,
files
,
f"Record
{
phase
}
continuity phase for
{
plan
[
'plan'
]
}
"
,
"continuity phase"
,
remote
=
remote
,
)
def
record_continuity_resolution
(
repository
:
Path
,
selection_path
:
Path
,
*
,
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL