88from devolv .drift .issues import create_approval_issue , wait_for_sync_choice
99from devolv .drift .github_approvals import create_github_pr
1010from devolv .drift .report import detect_and_print_drift
11+ from github import Github # Needed for auto-close
1112
1213app = typer .Typer ()
1314
@@ -74,13 +75,13 @@ def drift(
7475 typer .echo (f"✅ AWS policy { policy_arn } updated with local changes (append-only)." )
7576
7677 elif choice == "aws->local" :
77- _update_local_and_create_pr (aws_doc , policy_file , repo_full_name , policy_name , issue_num , description = "from AWS policy" )
78+ _update_local_and_create_pr (aws_doc , policy_file , repo_full_name , policy_name , issue_num , token , "from AWS policy" )
7879
7980 elif choice == "aws<->local" :
8081 superset_doc = build_superset_policy (local_doc , aws_doc )
8182 _update_aws_policy (iam , policy_arn , superset_doc )
8283 typer .echo (f"✅ AWS policy { policy_arn } updated with superset of local + AWS." )
83- _update_local_and_create_pr (superset_doc , policy_file , repo_full_name , policy_name , issue_num , description = "with superset of local + AWS" )
84+ _update_local_and_create_pr (superset_doc , policy_file , repo_full_name , policy_name , issue_num , token , "with superset of local + AWS" )
8485
8586 else :
8687 typer .echo ("⏭ No synchronization performed (skip)." )
@@ -96,16 +97,27 @@ def _update_aws_policy(iam, policy_arn, policy_doc):
9697 SetAsDefault = True
9798 )
9899
99- def _update_local_and_create_pr (doc , policy_file , repo_full_name , policy_name , issue_num , description = "" ):
100+ def _update_local_and_create_pr (doc , policy_file , repo_full_name , policy_name , issue_num , token , description = "" ):
100101 new_content = json .dumps (doc , indent = 2 )
101102 with open (policy_file , "w" ) as f :
102103 f .write (new_content )
103104
104- branch = f"{ description .replace (' ' , '-' )} -policy-{ policy_name } " .strip ("-" )
105+ branch = (
106+ f"{ description .replace (' ' , '-' ).replace ('+' , 'plus' ).replace ('/' , '-' )} -policy-{ policy_name } "
107+ .strip ("-" )
108+ .lower ()
109+ )
105110 push_branch (branch )
106111
107112 pr_title = f"Update { policy_file } { description } " .strip ()
108113 pr_body = f"This PR updates `{ policy_file } ` { description } .\n \n Linked to issue #{ issue_num } ." .strip ()
109114 pr_num , pr_url = create_github_pr (repo_full_name , branch , pr_title , pr_body , issue_num = issue_num )
110115
111116 typer .echo (f"✅ Created PR #{ pr_num } : { pr_url } " )
117+
118+ # Auto-close issue
119+ gh = Github (token )
120+ repo = gh .get_repo (repo_full_name )
121+ issue = repo .get_issue (number = issue_num )
122+ issue .create_comment (f"✅ PR created and linked: { pr_url } . Closing issue." )
123+ issue .edit (state = "closed" )
0 commit comments