|
13 | 13 | app = typer.Typer() |
14 | 14 |
|
15 | 15 | def push_branch(branch_name: str): |
| 16 | + import subprocess |
| 17 | + import typer |
| 18 | + |
16 | 19 | try: |
| 20 | + # Create or switch to branch safely |
17 | 21 | subprocess.run(["git", "checkout", "-B", branch_name], check=True) |
| 22 | + |
| 23 | + # Ensure Git identity is set |
18 | 24 | subprocess.run(["git", "config", "user.email", "github-actions@users.noreply.github.com"], check=True) |
19 | 25 | subprocess.run(["git", "config", "user.name", "github-actions"], check=True) |
| 26 | + |
| 27 | + # Add, commit |
20 | 28 | subprocess.run(["git", "add", "."], check=True) |
21 | 29 | subprocess.run(["git", "commit", "-m", f"Update policy: {branch_name}"], check=True) |
22 | 30 |
|
| 31 | + # Try pushing |
23 | 32 | try: |
24 | 33 | subprocess.run(["git", "push", "--set-upstream", "origin", branch_name], check=True) |
25 | 34 | except subprocess.CalledProcessError: |
26 | | - typer.echo("⚠️ Push failed. Trying to pull + re-push...") |
| 35 | + typer.echo("⚠️ Initial push failed. Attempting rebase + push...") |
27 | 36 | subprocess.run(["git", "pull", "--rebase", "origin", branch_name], check=True) |
28 | 37 | subprocess.run(["git", "push", "--set-upstream", "origin", branch_name], check=True) |
29 | 38 |
|
30 | 39 | typer.echo(f"✅ Pushed branch {branch_name} to origin.") |
| 40 | + |
31 | 41 | except subprocess.CalledProcessError as e: |
32 | 42 | typer.echo(f"❌ Git command failed: {e}") |
33 | 43 | raise typer.Exit(1) |
@@ -105,28 +115,36 @@ def _update_aws_policy(iam, policy_arn, policy_doc): |
105 | 115 | ) |
106 | 116 |
|
107 | 117 | def _update_local_and_create_pr(doc, policy_file, repo_full_name, policy_name, issue_num, token, description=""): |
| 118 | + import json |
| 119 | + from github import Github |
| 120 | + from devolv.drift.github_approvals import create_github_pr |
| 121 | + |
108 | 122 | new_content = json.dumps(doc, indent=2) |
109 | 123 | with open(policy_file, "w") as f: |
110 | 124 | f.write(new_content) |
111 | 125 |
|
112 | | - branch_base = ( |
| 126 | + # Clean branch name |
| 127 | + branch = ( |
113 | 128 | f"{description.replace(' ', '-').replace('+', 'plus').replace('/', '-')}-policy-{policy_name}" |
114 | 129 | .strip("-") |
115 | 130 | .lower() |
116 | 131 | ) |
117 | | - branch_name = push_branch(branch_base) |
| 132 | + |
| 133 | + push_branch(branch) |
118 | 134 |
|
119 | 135 | pr_title = f"Update {policy_file} {description}".strip() |
120 | 136 | pr_body = f"This PR updates `{policy_file}` {description}.\n\nLinked to issue #{issue_num}.".strip() |
121 | | - pr_num, pr_url = create_github_pr(repo_full_name, branch_name, pr_title, pr_body, issue_num=issue_num) |
| 137 | + |
| 138 | + # ✅ Pass correct branch name |
| 139 | + pr_num, pr_url = create_github_pr(repo_full_name, branch, pr_title, pr_body, issue_num=issue_num) |
122 | 140 |
|
123 | 141 | typer.echo(f"✅ Created PR #{pr_num}: {pr_url}") |
124 | 142 |
|
125 | | - # Close issue *immediately after PR link is posted* |
| 143 | + # ✅ Auto-close issue immediately |
126 | 144 | gh = Github(token) |
127 | 145 | repo = gh.get_repo(repo_full_name) |
128 | 146 | issue = repo.get_issue(number=issue_num) |
129 | 147 | issue.create_comment(f"✅ PR created and linked: {pr_url}. Closing issue.") |
130 | 148 | issue.edit(state="closed") |
131 | | - typer.echo(f"💬 Commented on and closed issue #{issue_num}") |
| 149 | + |
132 | 150 |
|
0 commit comments