Check commits array is not empty

This commit is contained in:
alstr 2021-08-26 19:09:28 +01:00
parent 4b6c80501f
commit 1169721b22

77
main.py
View File

@ -67,7 +67,7 @@ class GitHubClient(object):
def get_last_diff(self): def get_last_diff(self):
"""Get the last diff.""" """Get the last diff."""
if self.before != '0000000000000000000000000000000000000000' or len(self.commits) == 0: if self.before != '0000000000000000000000000000000000000000':
# There is a valid before SHA to compare with, or this is a release being created # There is a valid before SHA to compare with, or this is a release being created
diff_url = f'{self.repos_url}{self.repo}/compare/{self.before}...{self.sha}' diff_url = f'{self.repos_url}{self.repo}/compare/{self.before}...{self.sha}'
elif len(self.commits) == 1: elif len(self.commits) == 1:
@ -275,6 +275,7 @@ class GitHubClient(object):
else: else:
print('Issue card could not be added to project') print('Issue card could not be added to project')
class TodoParser(object): class TodoParser(object):
"""Parser for extracting information from a given diff file.""" """Parser for extracting information from a given diff file."""
FILE_HUNK_PATTERN = r'(?<=diff)(.*?)(?=diff\s--git\s)' FILE_HUNK_PATTERN = r'(?<=diff)(.*?)(?=diff\s--git\s)'
@ -603,42 +604,44 @@ class TodoParser(object):
projects = list(filter(None, projects.split(','))) projects = list(filter(None, projects.split(',')))
return projects return projects
if __name__ == "__main__": if __name__ == "__main__":
# Create a basic client for communicating with GitHub, automatically initialised with environment variables. # Create a basic client for communicating with GitHub, automatically initialised with environment variables.
client = GitHubClient() client = GitHubClient()
# Get the diff from the last pushed commit. if len(client.commits) != 0:
last_diff = StringIO(client.get_last_diff()) # Get the diff from the last pushed commit.
# Parse the diff for TODOs and create an Issue object for each. last_diff = StringIO(client.get_last_diff())
raw_issues = TodoParser().parse(last_diff) # Parse the diff for TODOs and create an Issue object for each.
# This is a simple, non-perfect check to filter out any TODOs that have just been moved. raw_issues = TodoParser().parse(last_diff)
# It looks for items that appear in the diff as both an addition and deletion. # This is a simple, non-perfect check to filter out any TODOs that have just been moved.
# It is based on the assumption that TODOs will not have identical titles in identical files. # It looks for items that appear in the diff as both an addition and deletion.
issues_to_process = [] # It is based on the assumption that TODOs will not have identical titles in identical files.
for values, similar_issues in itertools.groupby(raw_issues, key=operator.attrgetter('title', 'file_name', issues_to_process = []
'markdown_language')): for values, similar_issues in itertools.groupby(raw_issues, key=operator.attrgetter('title', 'file_name',
similar_issues = list(similar_issues) 'markdown_language')):
if (len(similar_issues) == 2 and ((similar_issues[0].status == LineStatus.ADDED and similar_issues = list(similar_issues)
similar_issues[1].status == LineStatus.DELETED) or if (len(similar_issues) == 2 and ((similar_issues[0].status == LineStatus.ADDED and
(similar_issues[1].status == LineStatus.ADDED and similar_issues[1].status == LineStatus.DELETED) or
similar_issues[0].status == LineStatus.DELETED))): (similar_issues[1].status == LineStatus.ADDED and
print(f'Issue "{values[0]}" appears as both addition and deletion. ' similar_issues[0].status == LineStatus.DELETED))):
f'Assuming this issue has been moved so skipping.') print(f'Issue "{values[0]}" appears as both addition and deletion. '
continue f'Assuming this issue has been moved so skipping.')
issues_to_process.extend(similar_issues) continue
# Cycle through the Issue objects and create or close a corresponding GitHub issue for each. issues_to_process.extend(similar_issues)
for j, raw_issue in enumerate(issues_to_process): # Cycle through the Issue objects and create or close a corresponding GitHub issue for each.
print(f'Processing issue {j + 1} of {len(issues_to_process)}') for j, raw_issue in enumerate(issues_to_process):
if raw_issue.status == LineStatus.ADDED: print(f'Processing issue {j + 1} of {len(issues_to_process)}')
status_code = client.create_issue(raw_issue) if raw_issue.status == LineStatus.ADDED:
if status_code == 201: status_code = client.create_issue(raw_issue)
print('Issue created') if status_code == 201:
else: print('Issue created')
print('Issue could not be created') else:
elif raw_issue.status == LineStatus.DELETED and os.getenv('INPUT_CLOSE_ISSUES', 'true') == 'true': print('Issue could not be created')
status_code = client.close_issue(raw_issue) elif raw_issue.status == LineStatus.DELETED and os.getenv('INPUT_CLOSE_ISSUES', 'true') == 'true':
if status_code == 201: status_code = client.close_issue(raw_issue)
print('Issue closed') if status_code == 201:
else: print('Issue closed')
print('Issue could not be closed') else:
# Stagger the requests to be on the safe side. print('Issue could not be closed')
sleep(1) # Stagger the requests to be on the safe side.
sleep(1)