11"""
2- PreProd resolver:
3- - Determines the furthest-ahead successful TEST commit on main (sha + dev-* tag).
2+ Pre-release resolver:
3+ - Determines the furthest-ahead successful ( TEST env deployed) commit on main (sha + dev-* tag).
44- Resolves THIS run's sha + dev-* tag (auto via workflow_run, or manual via input tag).
55- Applies the stale-run guard (auto blocks older; manual requires allow_older=true).
6+ - Fails if the commit being run is not the furthest-ahead successful
67- Emits outputs for later steps.
78
89Outputs (via $GITHUB_OUTPUT):
1617import subprocess
1718import sys
1819from dataclasses import dataclass
19- from typing import List , Optional
20-
20+ from typing import List , Optional , NoReturn
2121
2222WORKFLOW_NAME = os .getenv ("WORKFLOW_NAME" , "3. CD | Deploy to Test" )
23- BRANCH = os .getenv ("BRANCH" , "main" )
24- LIMIT = int (os .getenv ("LIMIT" , "30" ))
23+ BRANCH = os .getenv ("BRANCH" , "main" )
24+ LIMIT = int (os .getenv ("LIMIT" , "30" )) #how many previous successful test deployed commits to consider
2525
26- EVENT_NAME = os .getenv ("EVENT_NAME" , "" )
26+ EVENT_NAME = os .getenv ("EVENT_NAME" , "" )
2727HEAD_SHA_AUTO = os .getenv ("WORKFLOW_RUN_HEAD_SHA" , "" )
28- MANUAL_REF = os .getenv ("MANUAL_REF" , "" )
29- ALLOW_OLDER = os .getenv ("ALLOW_OLDER" , "false " ).lower ()
28+ MANUAL_REF = os .getenv ("MANUAL_REF" , "" )
29+ ALLOW_OLDER = os .getenv ("ALLOW_OLDER" , "true " ).lower ()
3030
3131
3232def fail (msg : str ) -> "NoReturn" :
3333 print (f"::error::{ msg } " , file = sys .stderr )
3434 sys .exit (1 )
3535
3636def run (cmd : List [str ], * , check : bool = True ) -> subprocess .CompletedProcess :
37+ # cp = completed process (will use this to refer)
3738 return subprocess .run (cmd , check = check , capture_output = True , text = True )
3839
3940def git_ok (args : List [str ]) -> bool :
@@ -53,9 +54,9 @@ def gh_json(args: List[str]) -> list:
5354
5455def dev_tag_for_sha (sha : str ) -> Optional [str ]:
5556 cp = run (["git" , "tag" , "--points-at" , sha ], check = False )
56- for t in cp .stdout .splitlines ():
57- if t .startswith ("dev-" ):
58- return t
57+ for tag in cp .stdout .splitlines ():
58+ if tag .startswith ("dev-" ):
59+ return tag
5960 return None
6061
6162def sha_for_tag (tag : str ) -> Optional [str ]:
@@ -74,6 +75,10 @@ def require_token() -> None:
7475 fail ("GH_TOKEN/GITHUB_TOKEN is required" )
7576
7677def list_successful_test_shas () -> List [str ]:
78+ """
79+ These are commits that have been deployed into
80+ the test env successfully
81+ """
7782 data = gh_json ([
7883 "run" , "list" ,
7984 "--workflow" , WORKFLOW_NAME ,
@@ -82,13 +87,13 @@ def list_successful_test_shas() -> List[str]:
8287 "--json" , "headSha" ,
8388 "--limit" , str (LIMIT ),
8489 ])
85- return [it ["headSha" ] for it in data if it .get ("headSha" )]
90+ return [commit ["headSha" ] for commit in data if commit .get ("headSha" )]
8691
8792def pick_furthest_ahead (shas : List [str ]) -> str :
8893 latest : Optional [str ] = None
89- for cand in shas :
90- if latest is None or is_ancestor (latest , cand ):
91- latest = cand
94+ for candidate in shas :
95+ if latest is None or is_ancestor (latest , candidate ):
96+ latest = candidate
9297 return latest or ""
9398
9499def resolve_latest_test () -> RefInfo :
@@ -106,7 +111,7 @@ def resolve_latest_test() -> RefInfo:
106111 fail (f"No dev-* tag found on latest TEST SHA ({ latest_sha } )" )
107112 return RefInfo (sha = latest_sha , ref = latest_ref )
108113
109- def resolve_this_run () -> RefInfo :
114+ def resolve_this_run () -> RefInfo | None :
110115 if EVENT_NAME == "workflow_run" :
111116 if not HEAD_SHA_AUTO :
112117 fail ("WORKFLOW_RUN_HEAD_SHA missing for workflow_run event" )
@@ -118,8 +123,6 @@ def resolve_this_run() -> RefInfo:
118123 if EVENT_NAME == "workflow_dispatch" :
119124 if not MANUAL_REF :
120125 fail ("MANUAL_REF (inputs.ref) is required for manual dispatch" )
121- if not re .match (r"^dev-\d{14}$" , MANUAL_REF ):
122- fail (f"Invalid dev-* tag format: { MANUAL_REF } " )
123126 if not git_ok (["rev-parse" , "-q" , "--verify" , f"refs/tags/{ MANUAL_REF } " ]):
124127 fail (f"Tag not found: { MANUAL_REF } " )
125128 sha = sha_for_tag (MANUAL_REF )
@@ -129,29 +132,26 @@ def resolve_this_run() -> RefInfo:
129132 fail (f"Chosen tag { MANUAL_REF } is not on origin/{ BRANCH } history" )
130133 return RefInfo (sha = sha , ref = MANUAL_REF )
131134
132- fail (f"Unsupported EVENT_NAME: { EVENT_NAME } " )
133- raise AssertionError ("unreachable" )
134-
135- def enforce_guard (this_ : RefInfo , latest : RefInfo ) -> None :
136- older_than_latest = (this_ .sha != latest .sha ) and is_ancestor (this_ .sha , latest .sha )
135+ def enforce_guard (current : RefInfo , latest : RefInfo ) -> None :
136+ older_than_latest = (current .sha != latest .sha ) and is_ancestor (current .sha , latest .sha )
137137
138138 if EVENT_NAME == "workflow_run" :
139139 if older_than_latest :
140140 fail (
141141 f"Stale PreProd approval. Latest tested is { latest .ref } ({ latest .sha } ); "
142- f"this run is { this_ .ref } ({ this_ .sha } )."
142+ f"this run is { current .ref } ({ current .sha } )."
143143 )
144144 else :
145145 if ALLOW_OLDER != "true" and older_than_latest :
146146 fail ("Older than latest tested. Set allow_older=true if you intend to backdeploy." )
147147
148- def write_outputs (this_ : RefInfo , latest : RefInfo ) -> None :
148+ def write_outputs (current : RefInfo , latest : RefInfo ) -> None :
149149 out = os .getenv ("GITHUB_OUTPUT" )
150150 if not out :
151151 return
152152 with open (out , "a" ) as f :
153- f .write (f"this_sha={ this_ .sha } \n " )
154- f .write (f"this_ref={ this_ .ref } \n " )
153+ f .write (f"this_sha={ current .sha } \n " )
154+ f .write (f"this_ref={ current .ref } \n " )
155155 f .write (f"latest_test_sha={ latest .sha } \n " )
156156 f .write (f"latest_test_ref={ latest .ref } \n " )
157157
0 commit comments