Skip to content

Commit

Permalink
Merge pull request #44 from LukaszSielski/extract_commons
Browse files Browse the repository at this point in the history
added excceptions
  • Loading branch information
LukaszSielski authored Sep 23, 2024
2 parents 1cc94e5 + 43661c2 commit 5e6f00c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions scripts/gh_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 5e6f00c

Please sign in to comment.