From ba64dd8f34e5f488ffa52ea58c86f9e21b8b30d6 Mon Sep 17 00:00:00 2001 From: Nick Floyd <139819+nickfloyd@users.noreply.github.com> Date: Mon, 1 Apr 2024 13:15:09 -0500 Subject: [PATCH] feat: Create pr diff script to help analyze the impact of generated SDK PRs (#53) --- scripts/pr-diff.py | 98 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 scripts/pr-diff.py diff --git a/scripts/pr-diff.py b/scripts/pr-diff.py new file mode 100644 index 0000000..a17be58 --- /dev/null +++ b/scripts/pr-diff.py @@ -0,0 +1,98 @@ +import requests +import difflib +import re +import sys + + +def fetch_pr_diff(repo, pr_number): + """Fetch the diff for a given PR from a repository.""" + url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}" + headers = {"Accept": "application/vnd.github.v3.diff"} + response = requests.get(url, headers=headers) + if response.status_code == 200: + return response.text + else: + return None + + +def summarize_diff(diff, verbose=False): + # TODO: This needs to be cleaned up and improved + """Attempt to summarize the diff, added and removed lines.""" + addDictionary = {} + removeDictionary = {} + added_lines = 0 + removed_lines = 0 + add = "" + remove = "" + for line in diff.split("\n"): + if "kiotaVersion" in line and not verbose: + print(line) + + if line.startswith("+") and not line.startswith("+++"): + added_lines += 1 + add = line + item = parse_lines(line) + if item: + addDictionary[item] = True + elif line.startswith("-") and not line.startswith("---"): + removed_lines += 1 + remove = line + item = parse_lines(line) + if item: + removeDictionary[item] = True + elif add and remove and verbose: + print(add + "\n" + remove + "\n") + add, remove = "", "" + + commit_msg = "FEAT:" + if len(addDictionary) > 0: + print("Items added/updated:") + print(", ".join(addDictionary.keys()) + "\n") + commit_msg += "| Added/Updated [" + ", ".join(addDictionary.keys()) + "]" + if len(removeDictionary) > 0: + print("Items removed:") + print(", ".join(removeDictionary.keys()) + "\n") + commit_msg += "| Removed [" + ", ".join(removeDictionary.keys()) + "]" + + print( + "NOTE: Mismatches between items added and removed indicate that there may be breaking changes introduced in this changeset. \n" + ) + + print("Commit message: " + commit_msg) + + summary = f"Added lines: {added_lines}\nRemoved lines: {removed_lines}" + return summary + + +def parse_lines(line): + # This needs to be updated to handle other languages + # C# specific + match = re.search(r"public (\w+)", line) + if match: + return match.group(1) + # Go specific + match = re.search(r"func (\w+)", line) + if match: + return match.group(1) + else: + return "" + + +def compare_lines(line1, line2): + diff = difflib.ndiff(line1, line2) + return "\n".join(list(diff)) + + +if len(sys.argv) == 3: + repo = sys.argv[1] + pr_number = int(sys.argv[2]) + diff = fetch_pr_diff(repo, pr_number) + if diff: + summary = summarize_diff(diff, True) + print(summary) + else: + print("Failed to fetch PR diff.") +else: + print("Usage: python3 ./scripts/pr-diff.py ") + print("Example: python3 ./scripts/pr-diff.py octokit/dotnet-sdk 123") + sys.exit(1)