Skip to content

Commit

Permalink
Merge pull request #45 from hrshdhgd/closed-or-non-existent-issue
Browse files Browse the repository at this point in the history
Handles closed issues or non existent ones
  • Loading branch information
hrshdhgd authored Dec 2, 2022
2 parents 396f555 + a287939 commit b23be74
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 49 deletions.
13 changes: 7 additions & 6 deletions src/ontobot_change_agent/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ def get_issues(
for issue in issues:
if title_search is None and label_object is None and number == 0:
yield issue
elif title_search and re.match(title_search, issue.title):
yield _extract_info_from_issue_object(issue)
elif label_object and label_object in issue.labels:
yield _extract_info_from_issue_object(issue)
elif number and number == issue.number:
yield _extract_info_from_issue_object(issue)
else:
if title_search and re.match(title_search, issue.title):
yield _extract_info_from_issue_object(issue)
if label_object and label_object in issue.labels:
yield _extract_info_from_issue_object(issue)
if number and number == issue.number:
yield _extract_info_from_issue_object(issue)
yield None


def _extract_info_from_issue_object(issue: Issue) -> dict:
Expand Down
82 changes: 43 additions & 39 deletions src/ontobot_change_agent/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,47 +144,51 @@ def process_issue(input: str, repo: str, label: str, number: int, state: str, ou

for issue in get_issues(repository_name=repo, label=label, number=number, state=state):
# Make sure ontobot_change_agent needs to be triggered or no.
if re.match(r"(.*)ontobot(.*)apply(.*):(.*)", issue[BODY]):
bullet_starters = ["* ", "- "]
KGCL_COMMANDS = []
for bullet in bullet_starters:

KGCL_COMMANDS.extend(
[
str(item).replace(bullet, "")
for item in issue[BODY].splitlines()
if item.startswith(bullet)
]
)
if output:
new_output = output
if issue:
if re.match(r"(.*)ontobot(.*)apply(.*):(.*)", issue[BODY]):
bullet_starters = ["* ", "- "]
KGCL_COMMANDS = []
for bullet in bullet_starters:

KGCL_COMMANDS.extend(
[
str(item).replace(bullet, "")
for item in issue[BODY].splitlines()
if item.startswith(bullet)
]
)
if output:
new_output = output
else:
new_output = input

KGCL_COMMANDS = [x.strip() for x in KGCL_COMMANDS]
if issue["number"] == number and len(KGCL_COMMANDS) > 0: # noqa W503 # noqa W503
process_issue_via_oak(
input=input,
commands=KGCL_COMMANDS,
output=new_output,
)

formatted_body += _list_to_markdown(KGCL_COMMANDS)
formatted_body += "</br>Fixes #" + str(issue["number"])
# TODO: remove `set-output` when env var setting is confirmed.
if os.getenv("GITHUB_ENV"):
with open(os.getenv("GITHUB_ENV"), "a") as env: # type: ignore
print(f"PR_BODY={formatted_body}", file=env)
print(f"PR_TITLE={issue[TITLE]}", file=env)

click.echo(
f"""
PR_BODY={formatted_body}
PR_TITLE={issue[TITLE]}
"""
)
else:
new_output = input

KGCL_COMMANDS = [x.strip() for x in KGCL_COMMANDS]
if issue["number"] == number and len(KGCL_COMMANDS) > 0: # noqa W503 # noqa W503
process_issue_via_oak(
input=input,
commands=KGCL_COMMANDS,
output=new_output,
)

formatted_body += _list_to_markdown(KGCL_COMMANDS)
formatted_body += "</br>Fixes #" + str(issue["number"])
# TODO: remove `set-output` when env var setting is confirmed.
if os.getenv("GITHUB_ENV"):
with open(os.getenv("GITHUB_ENV"), "a") as env: # type: ignore
print(f"PR_BODY={formatted_body}", file=env)
print(f"PR_TITLE={issue[TITLE]}", file=env)

click.echo(
f"""
PR_BODY={formatted_body}
PR_TITLE={issue[TITLE]}
"""
)
click.echo(f"""{issue[TITLE]} does not need ontobot's attention.""")
else:
click.echo(f"""{issue[TITLE]} does not need ontobot's attention.""")
click.echo(f"""Issue number:{number} is either closed or does not exist.""")
break


def _list_to_markdown(list: list) -> str:
Expand Down
10 changes: 6 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def test_get_issues_with_label(self):
"""Test if 'get_issues' returns the correct label."""
issues = []
for issue in get_issues(repository_name=self.repo_name, label=self.label):
issues.append(issue)

if issue:
issues.append(issue)
self.assertEqual(len(issues), 1)
self.assertTrue(type(issues[0]), Issue)
self.assertTrue(issues[0]["number"], 2)
Expand All @@ -35,7 +35,8 @@ def test_get_issues_with_title(self):
"""Test if 'get_issues' returns the correct title."""
issues = []
for issue in get_issues(repository_name=self.repo_name, title_search=self.issue_title):
issues.append(issue)
if issue:
issues.append(issue)

self.assertEqual(len(issues), 1)
self.assertTrue(type(issues[0]), Issue)
Expand All @@ -47,7 +48,8 @@ def test_get_issues_with_number(self):
"""Test return the correct issue by number."""
issues = []
for issue in get_issues(repository_name=self.repo_name, number=2):
issues.append(issue)
if issue:
issues.append(issue)

self.assertEqual(len(issues), 1)

Expand Down

0 comments on commit b23be74

Please sign in to comment.