Skip to content

Commit

Permalink
Merge pull request #19 from LukaszSielski/extract_commons
Browse files Browse the repository at this point in the history
Extract commons
  • Loading branch information
LukaszSielski authored Sep 19, 2024
2 parents 21654f9 + a0f2188 commit 3f5c35d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions scripts/gh_cli.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import re
import subprocess
import json
from typing import TypeVar, Callable

class GitHubCli:

def extract_data_from_pr_body(self, commit_sha: str, pattern: re.Pattern) -> list[str]:
def extract_pr_body(commit_sha: str) -> str:
prDetails = None
try:
prDetails = subprocess.check_output(f'gh pr list --json body --state merged --search {commit_sha}', shell=True, text=True)
except subprocess.CalledProcessError as e:
raise Exception(f'Failed to retrieve PR body! {e.stderr}')
prDetailsJson = json.loads(prDetails)
print(prDetailsJson)
if len(prDetailsJson) == 0:
return ''
finds = re.findall(pattern, prDetailsJson[0]['body'])
a = list(map(lambda e: e.split('#')[1], finds))
return prDetailsJson[0]['body'] if len(prDetailsJson) == 0 else ''

T = TypeVar('T')
def extract_data_from_pr_body(self, commit_sha: str, pattern: re.Pattern, mapping: Callable[[str], T]) -> list[T]:
finds = re.findall(pattern, self.extract_pr_body(commit_sha))
a = list(map(mapping, finds))
print(a)
return a

Expand Down
2 changes: 1 addition & 1 deletion scripts/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def main():
commitSha = args.commit_sha
deploymentEnv = args.deploy_env
tagAdoWorkItem(extractWorkItemIdFromPR(commitSha), deploymentEnv)
GH_CLI.extract_data_from_pr_body(commit_sha=commitSha, pattern=r"AB#\d{1}")
x = GH_CLI.extract_data_from_pr_body(commitSha, r'AB#(\d+)', lambda e: e.split('#')[1])

if __name__ == '__main__':
try:
Expand Down

0 comments on commit 3f5c35d

Please sign in to comment.