From e84c5fce379ef8925a749960e90754487b244f82 Mon Sep 17 00:00:00 2001 From: ByteXenon <125568681+ByteXenon@users.noreply.github.com> Date: Mon, 4 Nov 2024 02:22:05 -0700 Subject: [PATCH 1/2] Add `--pull-request` [`-pr`] parameter --- sherlock_project/sherlock.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/sherlock_project/sherlock.py b/sherlock_project/sherlock.py index e36ca6cc..ec5e703f 100644 --- a/sherlock_project/sherlock.py +++ b/sherlock_project/sherlock.py @@ -655,6 +655,14 @@ def main(): default=None, help="Load data from a JSON file or an online, valid, JSON file.", ) + parser.add_argument( + "--pull-request", + "-pr", + action="store", + dest="pull_request", + default=None, + help="Load data.json file from a specific pull request.", + ) parser.add_argument( "--timeout", action="store", @@ -779,12 +787,25 @@ def main(): # Create object with all information about sites we are aware of. try: - if args.local: + if args.local and args.pull_request is None: sites = SitesInformation( os.path.join(os.path.dirname(__file__), "resources/data.json") ) else: - sites = SitesInformation(args.json_file) + # Check for conflicting --json and --pull-request arguments + if args.json_file is not None and args.pull_request is not None: + print("You cannot use both --json and --pull-request at the same time.") + sys.exit(1) + + json_file_location = args.json_file + if args.pull_request is not None: + pull_url = f"https://api.github.com/repos/sherlock-project/sherlock/pulls/{args.pull_request}" + pull_request_raw = requests.get(pull_url).text + pull_request_json = json_loads(pull_request_raw) + head_commit_sha = pull_request_json["head"]["sha"] + json_file_location = f"https://raw.githubusercontent.com/sherlock-project/sherlock/{head_commit_sha}/sherlock_project/resources/data.json" + + sites = SitesInformation(json_file_location) except Exception as error: print(f"ERROR: {error}") sys.exit(1) From 270fbf647312e850132bf9a51f33f0c7e8adee54 Mon Sep 17 00:00:00 2001 From: ByteXenon <125568681+ByteXenon@users.noreply.github.com> Date: Wed, 6 Nov 2024 00:26:14 -0700 Subject: [PATCH 2/2] Overload `--json` to accept pull request data and remove `--pull-request` parameter --- sherlock_project/sherlock.py | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/sherlock_project/sherlock.py b/sherlock_project/sherlock.py index ec5e703f..6779a766 100644 --- a/sherlock_project/sherlock.py +++ b/sherlock_project/sherlock.py @@ -655,14 +655,6 @@ def main(): default=None, help="Load data from a JSON file or an online, valid, JSON file.", ) - parser.add_argument( - "--pull-request", - "-pr", - action="store", - dest="pull_request", - default=None, - help="Load data.json file from a specific pull request.", - ) parser.add_argument( "--timeout", action="store", @@ -787,23 +779,27 @@ def main(): # Create object with all information about sites we are aware of. try: - if args.local and args.pull_request is None: + if args.local: sites = SitesInformation( os.path.join(os.path.dirname(__file__), "resources/data.json") ) else: - # Check for conflicting --json and --pull-request arguments - if args.json_file is not None and args.pull_request is not None: - print("You cannot use both --json and --pull-request at the same time.") - sys.exit(1) - json_file_location = args.json_file - if args.pull_request is not None: - pull_url = f"https://api.github.com/repos/sherlock-project/sherlock/pulls/{args.pull_request}" - pull_request_raw = requests.get(pull_url).text - pull_request_json = json_loads(pull_request_raw) - head_commit_sha = pull_request_json["head"]["sha"] - json_file_location = f"https://raw.githubusercontent.com/sherlock-project/sherlock/{head_commit_sha}/sherlock_project/resources/data.json" + if args.json_file: + # If --json parameter is a number, interpret it as a pull request number + if args.json_file.isnumeric(): + pull_number = args.json_file + pull_url = f"https://api.github.com/repos/sherlock-project/sherlock/pulls/{pull_number}" + pull_request_raw = requests.get(pull_url).text + pull_request_json = json_loads(pull_request_raw) + + # Check if it's a valid pull request + if "message" in pull_request_json: + print(f"ERROR: Pull request #{pull_number} not found.") + sys.exit(1) + + head_commit_sha = pull_request_json["head"]["sha"] + json_file_location = f"https://raw.githubusercontent.com/sherlock-project/sherlock/{head_commit_sha}/sherlock_project/resources/data.json" sites = SitesInformation(json_file_location) except Exception as error: