From 898fea7093032e865bffcd91fadcdf0f7e1a7b41 Mon Sep 17 00:00:00 2001 From: Don Naro Date: Thu, 19 Sep 2024 16:15:28 +0100 Subject: [PATCH 1/4] add webhook parser script --- forge-webhook-parser.py | 123 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 forge-webhook-parser.py diff --git a/forge-webhook-parser.py b/forge-webhook-parser.py new file mode 100644 index 0000000..8d5c825 --- /dev/null +++ b/forge-webhook-parser.py @@ -0,0 +1,123 @@ +#!/usr/bin/python3 +""" +Parse a webhook payload file to clone the git repository to +the current directory. +""" + +import argparse +import json +import shutil +import subprocess +from dataclasses import dataclass +from pathlib import Path +from typing import List, Optional + + +@dataclass +class PayloadFields: + """ + Dataclass of details extracted from the payload. + """ + + added: List[str] + removed: List[str] + modified: List[str] + default_repo_url: Optional[str] = None + default_clone_path: Optional[Path] = None + + +PAYLOAD_FILE = "./github-payload.json" + +parser = argparse.ArgumentParser() +parser.add_argument( + "-f", "--file", help="webhook payload file to parse", default=PAYLOAD_FILE +) +args = parser.parse_args() + +with open(args.file, "r", encoding="utf-8") as file: + data = json.load(file) + +repo_url_keys = [] + +if "repository" in data and "clone_url" in data["repository"]: + repo_url_keys.append(data["repository"]["clone_url"]) + +if "project" in data and "git_http_url" in data["project"]: + repo_url_keys.append(data["project"]["git_http_url"]) + +default_repo_url = next((url for url in repo_url_keys if url), "") + + +def extract_project_name() -> str: + """Extract the name of the project from the clone url.""" + if default_repo_url: + remove_git = default_repo_url.rstrip(".git") + project_name = remove_git.split("/")[-1] + return project_name + return "" + + +default_clone_path = Path.cwd() / extract_project_name() + + +def clean_clone_path(clone_path: Path = default_clone_path): + """Remove already cloned repositories if they exist. + + Args: + clone_path (Path): Directory of the cloned repo. + """ + if clone_path.exists(): + shutil.rmtree(clone_path, ignore_errors=True) + + +def clone_repo(repo_url: str = default_repo_url, clone_path: Path = default_clone_path): + """Clone the repository to the current working directory. + + Args: + repo_url (str): Location of the repo. + clone_path (Path): Directory to clone the repo into. + """ + cmd: list[str] = ["git", "clone", repo_url, "--depth=1"] + cmd.append(str(clone_path)) + subprocess.run(cmd, check=True) + + +def parse_commits(commit_data: List[dict]) -> List[PayloadFields]: + """Parse the commit fields in the JSON payload. + + Args: + commit_data (dict): List of commit data from the payload. + + Returns: + List[PayloadFields]: List of changes for each commit. + """ + commit_changes = [] + for commit in commit_data: + changes = PayloadFields( + added=commit.get("added", []), + removed=commit.get("removed", []), + modified=commit.get("modified", []), + ) + commit_changes.append(changes) + return commit_changes + + +def main(): + """ + Main execution function. + """ + clean_clone_path() + clone_repo() + + commits = data.get("commits", []) + commit_changes = parse_commits(commits) + + for i, changes in enumerate(commit_changes, 1): + print(f"Commit {i}:") + print(f"Added: {', '.join(changes.added)}") + print(f"Removed: {', '.join(changes.removed)}") + print(f"Modified: {', '.join(changes.modified)}") + + +if __name__ == "__main__": + main() From 16df504eff5c1df01dd944258426087c1027e88e Mon Sep 17 00:00:00 2001 From: Don Naro Date: Fri, 20 Sep 2024 09:56:09 +0100 Subject: [PATCH 2/4] drop data types from docstrings --- forge-webhook-parser.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/forge-webhook-parser.py b/forge-webhook-parser.py index 8d5c825..55cc90c 100644 --- a/forge-webhook-parser.py +++ b/forge-webhook-parser.py @@ -61,22 +61,13 @@ def extract_project_name() -> str: def clean_clone_path(clone_path: Path = default_clone_path): - """Remove already cloned repositories if they exist. - - Args: - clone_path (Path): Directory of the cloned repo. - """ + """Remove already cloned repositories if they exist.""" if clone_path.exists(): shutil.rmtree(clone_path, ignore_errors=True) def clone_repo(repo_url: str = default_repo_url, clone_path: Path = default_clone_path): - """Clone the repository to the current working directory. - - Args: - repo_url (str): Location of the repo. - clone_path (Path): Directory to clone the repo into. - """ + """Clone the repository to the current working directory.""" cmd: list[str] = ["git", "clone", repo_url, "--depth=1"] cmd.append(str(clone_path)) subprocess.run(cmd, check=True) From ffb960e4883d5d934a56a3f52c995deba156892c Mon Sep 17 00:00:00 2001 From: Don Naro Date: Fri, 20 Sep 2024 14:32:15 +0100 Subject: [PATCH 3/4] don't overcomplicate things --- forge-webhook-parser.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/forge-webhook-parser.py b/forge-webhook-parser.py index 55cc90c..359559c 100644 --- a/forge-webhook-parser.py +++ b/forge-webhook-parser.py @@ -37,15 +37,10 @@ class PayloadFields: with open(args.file, "r", encoding="utf-8") as file: data = json.load(file) -repo_url_keys = [] - if "repository" in data and "clone_url" in data["repository"]: - repo_url_keys.append(data["repository"]["clone_url"]) - -if "project" in data and "git_http_url" in data["project"]: - repo_url_keys.append(data["project"]["git_http_url"]) - -default_repo_url = next((url for url in repo_url_keys if url), "") + default_repo_url = data["repository"]["clone_url"] +elif "project" in data and "git_http_url" in data["project"]: + default_repo_url = data["project"]["git_http_url"] def extract_project_name() -> str: From a89df195264c900906282b3fee1856fc444526e3 Mon Sep 17 00:00:00 2001 From: Don Naro Date: Fri, 20 Sep 2024 14:48:25 +0100 Subject: [PATCH 4/4] add else clause --- forge-webhook-parser.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/forge-webhook-parser.py b/forge-webhook-parser.py index 359559c..f2e26d8 100644 --- a/forge-webhook-parser.py +++ b/forge-webhook-parser.py @@ -41,6 +41,8 @@ class PayloadFields: default_repo_url = data["repository"]["clone_url"] elif "project" in data and "git_http_url" in data["project"]: default_repo_url = data["project"]["git_http_url"] +else: + default_repo_url = "" def extract_project_name() -> str: