From 6d792167ccfe87420a35f92ed87a10cfbefe0bf2 Mon Sep 17 00:00:00 2001 From: Marcin Kwiatkowski Date: Thu, 18 Sep 2025 08:37:53 +0200 Subject: [PATCH 1/2] feat: added script for fetching pull requests from github Added script that fetch PRs to directory and afterwords, when I have PRs on my machine, I can summarize them using nitrodigest. --- .../Nitrodigest/src/scripts/fetch_gh_prs.py | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Projects/Nitrodigest/src/scripts/fetch_gh_prs.py diff --git a/Projects/Nitrodigest/src/scripts/fetch_gh_prs.py b/Projects/Nitrodigest/src/scripts/fetch_gh_prs.py new file mode 100644 index 0000000..32b0432 --- /dev/null +++ b/Projects/Nitrodigest/src/scripts/fetch_gh_prs.py @@ -0,0 +1,84 @@ +import subprocess +import os +import json +from pathlib import Path +from argparse import ArgumentParser + + +def main(): + parser = ArgumentParser( + description="Fetch pull requests from Github for repository in the current working directory.", + ) + parser.add_argument( + "--dest", + default="pull-requests", + help="destination directory name" + ) + + args = parser.parse_args() + + prs = _get_prs() + + for pr in prs: + pr_url = pr.get("url") + desc = _get_pr_description(pr_url) + diff = _get_pr_diff(pr_url) + title = pr.get("title") + item_to_save = desc + "\n" + diff + + _save_to_file(item_to_save, args.dest, title) + + +def _fetch_from_gh(command): + try: + result = subprocess.run( + command, capture_output=True, text=True, check=True) + return result.stdout + except subprocess.CalledProcessError as e: + print(f"Error when fetching from Github: {e}") + exit(-1) + + +def _get_prs(): + return json.loads(_fetch_from_gh(["gh", "pr", "list", "--json", "url", "--json", "title"])) + + +def _get_pr_description(pr_handle): + return _fetch_from_gh(["gh", "pr", "view", pr_handle, "--comments"]) + + +def _get_pr_diff(pr_handle): + return _fetch_from_gh(["gh", "pr", "diff", pr_handle]) + + +def _save_to_file(content, directory, filename): + try: + dest_dir = Path(directory) + dest_dir.mkdir(parents=True, exist_ok=True) + + safe_filename = "".join(c for c in filename.strip( + ) if c.isalnum() or c in (' ', '-', '_')).rstrip().lstrip() + + file_path = dest_dir / f"{safe_filename}.md" + + with open(file_path, 'w', encoding='utf-8') as file: + file.write(content) + + print(f"Saved file {file_path}") + return file_path + + except PermissionError: + print(f"Error: Permission denied for directory {directory}") + return None + + except FileNotFoundError: + print(f"Error: Directory {directory} not found") + return None + + except Exception as e: + print(f"Unexpected error: {e}") + return None + + +if __name__ == "__main__": + main() From 6bb990aa5349c09adee0f1ff7620533ee948017a Mon Sep 17 00:00:00 2001 From: Marcin Kwiatkowski Date: Thu, 18 Sep 2025 09:08:01 +0200 Subject: [PATCH 2/2] refactor: fixed small issues --- Projects/Nitrodigest/src/scripts/fetch_gh_prs.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Projects/Nitrodigest/src/scripts/fetch_gh_prs.py b/Projects/Nitrodigest/src/scripts/fetch_gh_prs.py index 32b0432..28e0d93 100644 --- a/Projects/Nitrodigest/src/scripts/fetch_gh_prs.py +++ b/Projects/Nitrodigest/src/scripts/fetch_gh_prs.py @@ -1,6 +1,7 @@ import subprocess import os import json +import sys from pathlib import Path from argparse import ArgumentParser @@ -36,7 +37,7 @@ def _fetch_from_gh(command): return result.stdout except subprocess.CalledProcessError as e: print(f"Error when fetching from Github: {e}") - exit(-1) + return sys.exit(1) def _get_prs(): @@ -57,7 +58,7 @@ def _save_to_file(content, directory, filename): dest_dir.mkdir(parents=True, exist_ok=True) safe_filename = "".join(c for c in filename.strip( - ) if c.isalnum() or c in (' ', '-', '_')).rstrip().lstrip() + ) if c.isalnum() or c in (' ', '-', '_')) file_path = dest_dir / f"{safe_filename}.md" @@ -69,15 +70,11 @@ def _save_to_file(content, directory, filename): except PermissionError: print(f"Error: Permission denied for directory {directory}") - return None - - except FileNotFoundError: - print(f"Error: Directory {directory} not found") - return None + return sys.exit(1) except Exception as e: print(f"Unexpected error: {e}") - return None + return sys.exit(1) if __name__ == "__main__":