Skip to content

Commit

Permalink
Merge pull request #181 from zenml-io/michael/todo-comment-improvements
Browse files Browse the repository at this point in the history
Include Github URL of TODO comment in issue
  • Loading branch information
schustmi authored Nov 19, 2021
2 parents b4fdf16 + 068688b commit 63f6a45
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/update_todos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
JIRA_ISSUE_TYPE_ID: 10017
JIRA_DONE_STATUS_CATEGORY_ID: 3
JIRA_ISSUE_LABEL: todo_comment
JIRA_GITHUB_URL_FIELD_NAME: customfield_10029
run: python scripts/update_todos.py src/zenml
- name: Commit report
run: |
Expand Down
37 changes: 34 additions & 3 deletions scripts/update_todos.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
JIRA_ISSUE_TYPE_ID = os.environ["JIRA_ISSUE_TYPE_ID"]
JIRA_DONE_STATUS_CATEGORY_ID = int(os.environ["JIRA_DONE_STATUS_CATEGORY_ID"])
JIRA_ISSUE_LABEL = os.environ["JIRA_ISSUE_LABEL"]
JIRA_GITHUB_URL_FIELD_NAME = os.environ["JIRA_GITHUB_URL_FIELD_NAME"]

GITHUB_REPOSITORY = os.environ["GITHUB_REPOSITORY"]
GITHUB_SHA = os.environ["GITHUB_SHA"]


class Todo:
Expand All @@ -33,11 +37,15 @@ def __init__(
self,
filepath: str,
description: str,
line_number: int,
line_count: int,
priority: Optional[str] = None,
issue_key: Optional[str] = None,
):
self.filepath = filepath
self.description = description
self.line_number = line_number
self.line_count = line_count
self.priority = priority
self.issue_key = issue_key

Expand All @@ -54,13 +62,21 @@ def issue_payload_for_todo(todo: Todo) -> Dict[str, Any]:
"""Returns the http request data required to create a JIRA issue for the
given Todo object."""
title = f"Autogenerated issue from TODO comment in file {todo.filepath}."

last_line_number = todo.line_number + todo.line_count - 1
github_url = (
f"https://github.com/{GITHUB_REPOSITORY}/tree/{GITHUB_SHA}/"
f"{todo.filepath}#L{todo.line_number}-L{last_line_number}"
)

return {
"fields": {
"summary": title,
"issuetype": {"id": f"{JIRA_ISSUE_TYPE_ID}"},
"project": {"id": f"{JIRA_BOARD_ID}"},
"priority": {"id": f"{PRIORITIES[todo.priority]}"},
"labels": [JIRA_ISSUE_LABEL],
JIRA_GITHUB_URL_FIELD_NAME: github_url,
"description": {
"type": "doc",
"version": 1,
Expand Down Expand Up @@ -171,7 +187,7 @@ def find_todos(file: Path) -> Tuple[List[Todo], List[Todo]]:
"""
file_content = file.read_text()

matches = re.findall(
matches = re.finditer(
r"(^[ \t]*#) TODO ?\[(LOWEST|LOW|MEDIUM|HIGH|HIGHEST|[A-Z]*?-[0-9]*?)\]"
r" ?:(.*$\n(\1 {2}.*$\n)*)",
file_content,
Expand All @@ -181,13 +197,28 @@ def find_todos(file: Path) -> Tuple[List[Todo], List[Todo]]:
todos_without_issue = []
todos_with_issue = []

for _, priority_or_key, description, _ in matches:
line_number = 1
character_index = 0

for match in matches:
priority_or_key = match.group(2)
description = match.group(3)

# remove whitespace and leading '#' from description
lines = description.strip().split("\n")
lines = [line.strip().lstrip("#").strip() for line in lines]
description = " ".join(lines)

todo = Todo(filepath=str(file), description=description)
# Increase line number by number of lines since last match
line_number += file_content.count("\n", character_index, match.start())
character_index = match.start()

todo = Todo(
filepath=str(file),
description=description,
line_number=line_number,
line_count=len(lines),
)
if priority_or_key in PRIORITIES:
todo.priority = priority_or_key
todos_without_issue.append(todo)
Expand Down
32 changes: 27 additions & 5 deletions tests/scripts/test_update_todos.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def setup_environment():
os.environ["JIRA_ISSUE_TYPE_ID"] = ""
os.environ["JIRA_DONE_STATUS_CATEGORY_ID"] = "-1"
os.environ["JIRA_ISSUE_LABEL"] = ""
os.environ["JIRA_GITHUB_URL_FIELD_NAME"] = ""

os.environ["GITHUB_REPOSITORY"] = ""
os.environ["GITHUB_SHA"] = ""


def test_todo_detection(tmp_path):
Expand Down Expand Up @@ -50,6 +54,8 @@ def test_todo_detection(tmp_path):
assert todos_with_issue[0].issue_key == "ABC-123"
assert todos_with_issue[0].filepath == str(file)
assert todos_with_issue[0].description == "valid issue key"
assert todos_with_issue[0].line_number == 14
assert todos_with_issue[0].line_count == 1

assert len(todos_without_issue) == 5
expected_priorities = ["LOW", "MEDIUM", "HIGH", "LOWEST", "HIGHEST"]
Expand All @@ -60,13 +66,21 @@ def test_todo_detection(tmp_path):
"",
"invalid multiline",
]

for todo, expected_priority, expected_description in zip(
todos_without_issue, expected_priorities, expected_descriptions
expected_line_numbers = [2, 8, 11, 18, 19]
expected_line_counts = [1, 2, 1, 1, 1]

for todo, priority, description, line_number, line_count in zip(
todos_without_issue,
expected_priorities,
expected_descriptions,
expected_line_numbers,
expected_line_counts,
):
assert todo.priority == expected_priority
assert todo.priority == priority
assert todo.filepath == str(file)
assert todo.description == expected_description
assert todo.description == description
assert todo.line_number == line_number
assert todo.line_count == line_count


def test_todo_issue_key_insertion(tmp_path):
Expand All @@ -92,12 +106,16 @@ def test_todo_issue_key_insertion(tmp_path):
Todo(
filepath=str(file),
description="some valid todo",
line_number=2,
line_count=1,
priority="LOW",
issue_key="TEST-1",
),
Todo(
filepath=str(file),
description="another valid todo",
line_number=8,
line_count=2,
priority="HIGHEST",
issue_key="TEST-2",
),
Expand Down Expand Up @@ -146,11 +164,15 @@ def test_removing_of_closed_todos(tmp_path):
Todo(
filepath=str(file),
description="todo for closed issue",
line_number=4,
line_count=2,
issue_key="TEST-1",
),
Todo(
filepath=str(file),
description="todo with open issue",
line_number=8,
line_count=1,
issue_key="TEST-2",
),
]
Expand Down

0 comments on commit 63f6a45

Please sign in to comment.