@@ -24,14 +24,12 @@ def push_branch(branch_name: str):
2424 subprocess .run (["git" , "config" , "user.name" , "github-actions" ], check = True )
2525 subprocess .run (["git" , "add" , "." ], check = True )
2626 subprocess .run (["git" , "commit" , "-m" , f"Update policy: { branch_name } " ], check = True )
27-
2827 try :
2928 subprocess .run (["git" , "push" , "--set-upstream" , "origin" , branch_name ], check = True )
3029 except subprocess .CalledProcessError :
3130 typer .echo ("⚠️ Initial push failed. Attempting rebase + push..." )
3231 subprocess .run (["git" , "pull" , "--rebase" , "origin" , branch_name ], check = True )
3332 subprocess .run (["git" , "push" , "--set-upstream" , "origin" , branch_name ], check = True )
34-
3533 typer .echo (f"✅ Pushed branch { branch_name } to origin." )
3634 except subprocess .CalledProcessError as e :
3735 typer .echo (f"❌ Git command failed: { e } " )
@@ -78,9 +76,6 @@ def drift(
7876 aws_doc = get_aws_policy_document (policy_arn )
7977 drift_detected = detect_drift (local_doc , aws_doc )
8078
81- if drift_detected :
82- print_drift_diff (local_doc , aws_doc )
83-
8479 if not drift_detected :
8580 try :
8681 _update_aws_policy (iam , policy_arn , local_doc )
@@ -93,62 +88,63 @@ def drift(
9388 typer .echo ("✅ No forced approval requested. Exiting." )
9489 return
9590
96- repo_full_name = repo_full_name or os .getenv ("GITHUB_REPOSITORY" )
97- token = os .getenv ("GITHUB_TOKEN" )
98-
99- if not repo_full_name :
100- typer .echo ("❌ GitHub repo not specified. Use --repo or set GITHUB_REPOSITORY." )
101- raise typer .Exit (1 )
102- if not token :
103- typer .echo ("❌ GITHUB_TOKEN not set in environment." )
104- raise typer .Exit (1 )
91+ if drift_detected or approval_anyway :
92+ repo_full_name = repo_full_name or os .getenv ("GITHUB_REPOSITORY" )
93+ token = os .getenv ("GITHUB_TOKEN" )
10594
106- assignees = [a .strip () for a in approvers .split ("," ) if a .strip ()]
107- issue_num , _ = create_approval_issue (
108- repo_full_name , token , policy_name , assignees = assignees , approval_anyway = approval_anyway
109- )
110- issue_url = f"https://github.com/{ repo_full_name } /issues/{ issue_num } "
111- typer .echo (f"✅ Approval issue created: { issue_url } " )
112-
113- choice = wait_for_sync_choice (
114- repo_full_name , issue_num , token , allowed_approvers = assignees , approval_anyway = approval_anyway
115- )
116-
117- if choice == "local->aws" :
118- merged_doc = merge_policy_documents (local_doc , aws_doc )
119- try :
120- _update_aws_policy (iam , policy_arn , merged_doc )
121- except ValueError as ve :
122- typer .echo (str (ve ))
95+ if not repo_full_name :
96+ typer .echo ("❌ GitHub repo not specified. Use --repo or set GITHUB_REPOSITORY." )
12397 raise typer .Exit (1 )
124- typer .echo (f"✅ AWS policy { policy_arn } updated with local changes (append-only)." )
125- close_issue (repo_full_name , token , issue_num , "✅ AWS updated with local changes. Closing issue." )
126-
127- elif choice == "aws->local" :
128- _update_local_and_create_pr (aws_doc , policy_file , repo_full_name , policy_name , issue_num , token , "from AWS policy" )
129-
130- elif choice == "aws<->local" :
131- superset_doc = build_superset_policy (local_doc , aws_doc )
132- try :
133- _update_aws_policy (iam , policy_arn , superset_doc )
134- except ValueError as ve :
135- typer .echo (str (ve ))
98+ if not token :
99+ typer .echo ("❌ GITHUB_TOKEN not set in environment." )
136100 raise typer .Exit (1 )
137- typer .echo (f"✅ AWS policy { policy_arn } updated with superset of local + AWS." )
138- _update_local_and_create_pr (superset_doc , policy_file , repo_full_name , policy_name , issue_num , token , "with superset of local + AWS" )
139-
140- elif choice == "approve" :
141- typer .echo ("✅ Approved without sync action. Closing issue." )
142- close_issue (repo_full_name , token , issue_num , "✅ Approved without sync action. Closing issue." )
143101
144- elif choice == "reject" :
145- typer .echo ("❌ Approval rejected. Closing issue." )
146- close_issue (repo_full_name , token , issue_num , "❌ Approval rejected. Closing issue." )
147- raise typer .Exit (1 )
102+ assignees = [a .strip () for a in approvers .split ("," ) if a .strip ()]
103+ issue_num , _ = create_approval_issue (
104+ repo_full_name , token , policy_name , assignees = assignees , approval_anyway = approval_anyway
105+ )
106+ issue_url = f"https://github.com/{ repo_full_name } /issues/{ issue_num } "
107+ typer .echo (f"✅ Approval issue created: { issue_url } " )
108+
109+ choice = wait_for_sync_choice (
110+ repo_full_name , issue_num , token , allowed_approvers = assignees , approval_anyway = approval_anyway
111+ )
112+
113+ if choice == "local->aws" :
114+ merged_doc = merge_policy_documents (local_doc , aws_doc )
115+ try :
116+ _update_aws_policy (iam , policy_arn , merged_doc )
117+ except ValueError as ve :
118+ typer .echo (str (ve ))
119+ raise typer .Exit (1 )
120+ typer .echo (f"✅ AWS policy { policy_arn } updated with local changes (append-only)." )
121+ close_issue (repo_full_name , token , issue_num , "✅ AWS updated with local changes. Closing issue." )
122+
123+ elif choice == "aws->local" :
124+ _update_local_and_create_pr (aws_doc , policy_file , repo_full_name , policy_name , issue_num , token , "from AWS policy" )
125+
126+ elif choice == "aws<->local" :
127+ superset_doc = build_superset_policy (local_doc , aws_doc )
128+ try :
129+ _update_aws_policy (iam , policy_arn , superset_doc )
130+ except ValueError as ve :
131+ typer .echo (str (ve ))
132+ raise typer .Exit (1 )
133+ typer .echo (f"✅ AWS policy { policy_arn } updated with superset of local + AWS." )
134+ _update_local_and_create_pr (superset_doc , policy_file , repo_full_name , policy_name , issue_num , token , "with superset of local + AWS" )
135+
136+ elif choice == "approve" :
137+ typer .echo ("✅ Approved without sync action. Closing issue." )
138+ close_issue (repo_full_name , token , issue_num , "✅ Approved without sync action. Closing issue." )
139+
140+ elif choice == "reject" :
141+ typer .echo ("❌ Approval rejected. Closing issue." )
142+ close_issue (repo_full_name , token , issue_num , "❌ Approval rejected. Closing issue." )
143+ raise typer .Exit (1 )
148144
149- else :
150- typer .echo ("⏭ No synchronization performed (skip)." )
151- close_issue (repo_full_name , token , issue_num , "⏭ No sync chosen. Closing issue." )
145+ else :
146+ typer .echo ("⏭ No synchronization performed (skip)." )
147+ close_issue (repo_full_name , token , issue_num , "⏭ No sync chosen. Closing issue." )
152148
153149def _update_aws_policy (iam , policy_arn , policy_doc ):
154150 sids = [stmt .get ("Sid" ) for stmt in policy_doc .get ("Statement" , []) if "Sid" in stmt ]
0 commit comments