refactor: move bulk of main's processing into separate method

No functional change, but enables future changes
will allow more of the code to be exercised by
unittest.
This commit is contained in:
Robert Alonso 2024-11-08 19:22:57 +00:00
parent da6872a340
commit 829b294c5f

49
main.py
View File

@ -8,6 +8,7 @@ from io import StringIO
import itertools
import operator
from collections import defaultdict
import sys
from Client import Client
from GitHubClient import GitHubClient
@ -15,24 +16,9 @@ from LineStatus import LineStatus
from LocalClient import LocalClient
from TodoParser import TodoParser
if __name__ == "__main__":
client: Client | None = None
# Try to create a basic client for communicating with the remote version control server, automatically initialised with environment variables.
try:
# try to build a GitHub client
client = GitHubClient()
except EnvironmentError:
# don't immediately give up
pass
# if needed, fall back to using a local client for testing
client = client or LocalClient()
# Get the diff from the last pushed commit.
last_diff = client.get_last_diff()
if last_diff:
def process_diff(diff, client=Client(), insert_issue_urls=False, parser=TodoParser(), output=sys.stdout):
# Parse the diff for TODOs and create an Issue object for each.
raw_issues = TodoParser().parse(StringIO(last_diff))
raw_issues = parser.parse(diff)
# This is a simple, non-perfect check to filter out any TODOs that have just been moved.
# It looks for items that appear in the diff as both an addition and deletion.
# It is based on the assumption that TODOs will not have identical titles in identical files.
@ -75,16 +61,13 @@ if __name__ == "__main__":
issues_to_process = [issue for issue in issues_to_process if
not (issue.issue_url in update_and_close_issues and issue.status == LineStatus.DELETED)]
# Check to see if we should insert the issue URL back into the linked TODO.
insert_issue_urls = os.getenv('INPUT_INSERT_ISSUE_URLS', 'false') == 'true'
# Cycle through the Issue objects and create or close a corresponding GitHub issue for each.
for j, raw_issue in enumerate(sorted(reversed(sorted(issues_to_process, key = operator.attrgetter('start_line'))), key = operator.attrgetter('file_name'))):
print(f"Processing issue {j + 1} of {len(issues_to_process)}: '{raw_issue.title}' @ {raw_issue.file_name}:{raw_issue.start_line}")
if raw_issue.status == LineStatus.ADDED:
status_code, new_issue_number = client.create_issue(raw_issue)
if status_code == 201:
print(f'Issue created: : #{new_issue_number} @ {client.get_issue_url(new_issue_number)}')
print(f'Issue created: #{new_issue_number} @ {client.get_issue_url(new_issue_number)}')
# Don't insert URLs for comments. Comments do not get updated.
if insert_issue_urls and not (raw_issue.ref and raw_issue.ref.startswith('#')):
line_number = raw_issue.start_line - 1
@ -102,7 +85,7 @@ if __name__ == "__main__":
with open(raw_issue.file_name, 'w') as issue_file:
issue_file.writelines(file_lines)
elif status_code == 200:
print(f'Issue updated: : #{new_issue_number} @ {client.get_issue_url(new_issue_number)}')
print(f'Issue updated: #{new_issue_number} @ {client.get_issue_url(new_issue_number)}')
else:
print('Issue could not be created')
elif raw_issue.status == LineStatus.DELETED and os.getenv('INPUT_CLOSE_ISSUES', 'true') == 'true':
@ -116,3 +99,25 @@ if __name__ == "__main__":
print('Issue could not be closed')
# Stagger the requests to be on the safe side.
sleep(1)
if __name__ == "__main__":
client: Client | None = None
# Try to create a basic client for communicating with the remote version control server, automatically initialised with environment variables.
try:
# try to build a GitHub client
client = GitHubClient()
except EnvironmentError:
# don't immediately give up
pass
# if needed, fall back to using a local client for testing
client = client or LocalClient()
# Get the diff from the last pushed commit.
last_diff = client.get_last_diff()
# process the diff
if last_diff:
# Check to see if we should insert the issue URL back into the linked TODO.
insert_issue_urls = os.getenv('INPUT_INSERT_ISSUE_URLS', 'false') == 'true'
process_diff(StringIO(last_diff), client, insert_issue_urls)