From 43661c2bf166cc8304244c1d786e75087e6e627e Mon Sep 17 00:00:00 2001 From: Lukasz Sielski Date: Mon, 23 Sep 2024 10:08:56 +0200 Subject: [PATCH] added excceptions --- scripts/gh_cli.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/scripts/gh_cli.py b/scripts/gh_cli.py index 0ec5d1e..562d161 100644 --- a/scripts/gh_cli.py +++ b/scripts/gh_cli.py @@ -10,19 +10,24 @@ class PrState(str, Enum): ALL = 'all' class GhCliException(Exception): - def __init__(self, message): - super().__init__(message) + def __init__(self, error): + super().__init__(f'Failed to execute GH CLI command! Error: {error}') + +class PrNotFoundException(Exception): + def __init__(self, commit_sha: str): + super().__init__(f"Could not find pull request for commit identitifed by {commit_sha} SHA") def extract_pr_body(commit_sha: str, pr_state: PrState) -> str: prDetails = None try: prDetails = check_output(f'gh pr list --json body --state {pr_state.value} --search {commit_sha}', shell=True, text=True, stderr=STDOUT) except CalledProcessError as e: - raise GhCliException(f'Failed to execute GH CLI command! Error: {e.output}') + raise GhCliException(e.output) + prDetailsJson = json.loads(prDetails) - print(prDetails) - print(prDetailsJson) - return prDetailsJson[0]['body'] if len(prDetailsJson) != 0 else '' + if len(prDetailsJson) == 0: + raise PrNotFoundException(commit_sha) + return prDetailsJson[0]['body'] def extract_data_by_pattern_from_pr_body(commit_sha: str, pr_state: PrState, pattern: re.Pattern) -> list[str]: return re.findall(pattern, extract_pr_body(commit_sha, pr_state)) \ No newline at end of file