|
| 1 | +"""Make a pull request |
| 2 | +""" |
| 3 | +import subprocess |
| 4 | +from app.query_bedrock import query_bedrock |
| 5 | +from app.extract_bash import extract_bash_commands_no_line_split |
| 6 | + |
| 7 | +def get_current_branch(): |
| 8 | + return subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode().strip() |
| 9 | + |
| 10 | +def get_pr_number(): |
| 11 | + try: |
| 12 | + pr_info = subprocess.check_output(["gh", "pr", "view", "--json", "number"]).decode() |
| 13 | + return int(pr_info.split(":")[1].strip()[:-1]) |
| 14 | + except subprocess.CalledProcessError: |
| 15 | + return None |
| 16 | + |
| 17 | +def get_pr_diff(pr_number): |
| 18 | + try: |
| 19 | + diff = subprocess.check_output(["gh", "pr", "diff", str(pr_number)]).decode() |
| 20 | + return diff |
| 21 | + except subprocess.CalledProcessError: |
| 22 | + print(f"Error: Unable to get the diff for PR #{pr_number}.") |
| 23 | + return None |
| 24 | + |
| 25 | +def review_pull_request(pr_number): |
| 26 | + pr_diff = get_pr_diff(pr_number) |
| 27 | + if not pr_diff: |
| 28 | + return |
| 29 | + prompt = f"""Review the following pull request diff and provide a concise review comment. |
| 30 | + Include any potential issues, suggestions for improvement, and overall assessment. |
| 31 | + If you have any specific code change suggestions, please include them as well. |
| 32 | +
|
| 33 | + {pr_diff} |
| 34 | +
|
| 35 | + Respond with only the review comment, enclosed in triple backticks (```). For example: |
| 36 | + ``` |
| 37 | + The changes look good overall. Consider adding more unit tests for the new functionality. |
| 38 | + |
| 39 | + Suggestion for improvement: |
| 40 | + In file.py, line 42, consider changing: |
| 41 | + if x == None: |
| 42 | + to: |
| 43 | + if x is None: |
| 44 | + |
| 45 | + This is more idiomatic Python and avoids potential issues with custom __eq__ methods. |
| 46 | + ``` |
| 47 | + """ |
| 48 | + |
| 49 | + review_comment = query_bedrock(prompt) |
| 50 | + review_comment = extract_bash_commands_no_line_split(review_comment)[0] |
| 51 | + |
| 52 | + try: |
| 53 | + result = subprocess.run([ |
| 54 | + "gh", "pr", "review", str(pr_number), |
| 55 | + "--body", review_comment, |
| 56 | + "--comment" |
| 57 | + ]) |
| 58 | + |
| 59 | + if result.returncode == 0: |
| 60 | + print(f"✅ Review submitted successfully for PR #{pr_number}!") |
| 61 | + else: |
| 62 | + print(f"❌ Failed to submit review for PR #{pr_number}. Please try again.") |
| 63 | + except subprocess.CalledProcessError: |
| 64 | + print(f"Error: Unable to submit review for PR #{pr_number}.") |
| 65 | + |
| 66 | +def main(): |
| 67 | + # Check if GitHub CLI (gh) is installed |
| 68 | + try: |
| 69 | + subprocess.run(["gh", "--version"], check=True, capture_output=True) |
| 70 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 71 | + print("GitHub CLI (gh) is not installed or not found. Please install it to review pull requests.") |
| 72 | + print("Visit https://cli.github.com/ for installation instructions.") |
| 73 | + return |
| 74 | + |
| 75 | + current_branch = get_current_branch() |
| 76 | + pr_number = get_pr_number() |
| 77 | + |
| 78 | + if pr_number is None: |
| 79 | + print(f"No pull request found for the current branch '{current_branch}'.") |
| 80 | + create_pr = input("Would you like to create a pull request? (y/n): ").lower() |
| 81 | + if create_pr == 'y': |
| 82 | + subprocess.run(["python", "app/make_pull_request.py"]) |
| 83 | + pr_number = get_pr_number() |
| 84 | + else: |
| 85 | + print("Exiting without creating a pull request.") |
| 86 | + return |
| 87 | + |
| 88 | + if pr_number: |
| 89 | + review_pull_request(pr_number) |
| 90 | + else: |
| 91 | + print("Unable to find or create a pull request. Please check your branch and try again.") |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments