mirror of
https://github.com/ditkrg/todo-to-issue-action.git
synced 2026-01-22 22:06:43 +00:00
Improve checks for existing issues
This commit is contained in:
parent
27e63bb04e
commit
4ed02ed621
59
main.py
59
main.py
@ -8,6 +8,7 @@ import json
|
|||||||
from time import sleep
|
from time import sleep
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from ruamel.yaml import YAML
|
from ruamel.yaml import YAML
|
||||||
|
import hashlib
|
||||||
|
|
||||||
base_url = 'https://api.github.com/repos/'
|
base_url = 'https://api.github.com/repos/'
|
||||||
|
|
||||||
@ -36,6 +37,17 @@ def main():
|
|||||||
yaml = YAML(typ='safe')
|
yaml = YAML(typ='safe')
|
||||||
languages_dict = yaml.load(languages_data)
|
languages_dict = yaml.load(languages_data)
|
||||||
|
|
||||||
|
# Get the current issues so we can check we're not duplicating any, and so we can close any that have been removed.
|
||||||
|
issues_url = f'{base_url}{repo}/issues'
|
||||||
|
issue_headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': f'token {token}'
|
||||||
|
}
|
||||||
|
current_issues = []
|
||||||
|
list_issues_request = requests.get(issues_url, headers=issue_headers)
|
||||||
|
if list_issues_request.status_code == 200:
|
||||||
|
current_issues = list_issues_request.json()
|
||||||
|
|
||||||
diff_request = requests.get(url=diff_url, headers=diff_headers)
|
diff_request = requests.get(url=diff_url, headers=diff_headers)
|
||||||
if diff_request.status_code == 200:
|
if diff_request.status_code == 200:
|
||||||
diff = diff_request.text
|
diff = diff_request.text
|
||||||
@ -122,7 +134,7 @@ def main():
|
|||||||
deletion = deletion_search.group(0)
|
deletion = deletion_search.group(0)
|
||||||
todo_search = todo_pattern.search(deletion)
|
todo_search = todo_pattern.search(deletion)
|
||||||
if todo_search:
|
if todo_search:
|
||||||
closed_issues.append(todo_search.group(0))
|
closed_issues.append(todo_search.group(0).lstrip())
|
||||||
else:
|
else:
|
||||||
lines.append(cleaned_line[1:])
|
lines.append(cleaned_line[1:])
|
||||||
|
|
||||||
@ -143,11 +155,7 @@ def main():
|
|||||||
new_issues.append(curr_issue)
|
new_issues.append(curr_issue)
|
||||||
|
|
||||||
# Create new issues for any newly added TODOs.
|
# Create new issues for any newly added TODOs.
|
||||||
issues_url = f'{base_url}{repo}/issues'
|
print('Start creating issues')
|
||||||
issue_headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': f'token {token}'
|
|
||||||
}
|
|
||||||
for i, issue in enumerate(new_issues):
|
for i, issue in enumerate(new_issues):
|
||||||
title = issue['todo']
|
title = issue['todo']
|
||||||
# Truncate the title if it's longer than 50 chars.
|
# Truncate the title if it's longer than 50 chars.
|
||||||
@ -171,8 +179,18 @@ def main():
|
|||||||
body += '\n\n' + '```' + markdown_language + '\n' + '\n'.join(hunk) + '\n' + '```'
|
body += '\n\n' + '```' + markdown_language + '\n' + '\n'.join(hunk) + '\n' + '```'
|
||||||
else:
|
else:
|
||||||
body += '\n\n' + '```' + '\n'.join(hunk) + '\n' + '```'
|
body += '\n\n' + '```' + '\n'.join(hunk) + '\n' + '```'
|
||||||
|
|
||||||
|
# Check if the current issue already exists - if so, skip it.
|
||||||
|
issue_id = hashlib.sha1(body.encode('utf-8')).hexdigest()
|
||||||
|
body += '\n\n' + issue_id
|
||||||
|
for current_issue in current_issues:
|
||||||
|
if issue_id in current_issue['body']:
|
||||||
|
print(f'Skipping issue {i + 1} of {len(new_issues)} (already exists)')
|
||||||
|
break
|
||||||
|
else:
|
||||||
new_issue_body = {'title': title, 'body': body, 'labels': ['todo']}
|
new_issue_body = {'title': title, 'body': body, 'labels': ['todo']}
|
||||||
new_issue_request = requests.post(url=issues_url, headers=issue_headers, data=json.dumps(new_issue_body))
|
new_issue_request = requests.post(url=issues_url, headers=issue_headers,
|
||||||
|
data=json.dumps(new_issue_body))
|
||||||
print(f'Creating issue {i + 1} of {len(new_issues)}')
|
print(f'Creating issue {i + 1} of {len(new_issues)}')
|
||||||
# TODO Investigate result not being printed on last loop when adding multiple new TODOs
|
# TODO Investigate result not being printed on last loop when adding multiple new TODOs
|
||||||
# See https://github.com/alstr/todo-to-issue-action/issues/7#issuecomment-630694673
|
# See https://github.com/alstr/todo-to-issue-action/issues/7#issuecomment-630694673
|
||||||
@ -183,23 +201,27 @@ def main():
|
|||||||
print('Issue could not be created')
|
print('Issue could not be created')
|
||||||
# Don't add too many issues too quickly.
|
# Don't add too many issues too quickly.
|
||||||
sleep(1)
|
sleep(1)
|
||||||
|
print('Creating issues complete')
|
||||||
|
|
||||||
if len(closed_issues) > 0:
|
# Close issues for removed TODOs.
|
||||||
# Get the list of current issues.
|
print('Start closing issues')
|
||||||
list_issues_request = requests.get(issues_url, headers=issue_headers)
|
|
||||||
if list_issues_request.status_code == 200:
|
|
||||||
current_issues = list_issues_request.json()
|
|
||||||
for i, closed_issue in enumerate(closed_issues):
|
for i, closed_issue in enumerate(closed_issues):
|
||||||
title = closed_issue
|
title = closed_issue
|
||||||
if len(title) > 50:
|
matched = 0
|
||||||
title = closed_issue[:50] + '...'
|
issue_number = None
|
||||||
|
|
||||||
# Compare the title of each closed issue with each issue in the issues list.
|
# Compare the title of each closed issue with each issue in the issues list.
|
||||||
for current_issue in current_issues:
|
for current_issue in current_issues:
|
||||||
if current_issue['title'] == title:
|
if current_issue['body'].startswith(title):
|
||||||
# The titles match, so we will try and close the issue.
|
matched += 1
|
||||||
|
# If there are multiple issues with similar titles, don't try and close any.
|
||||||
|
if matched > 1:
|
||||||
|
print(f'Skipping issue {i + 1} of {len(closed_issues)} (multiple matches)')
|
||||||
|
break
|
||||||
issue_number = current_issue['number']
|
issue_number = current_issue['number']
|
||||||
|
else:
|
||||||
|
if issue_number is None:
|
||||||
|
continue
|
||||||
|
# The titles match, so we will try and close the issue.
|
||||||
update_issue_url = f'{base_url}{repo}/issues/{issue_number}'
|
update_issue_url = f'{base_url}{repo}/issues/{issue_number}'
|
||||||
body = {'state': 'closed'}
|
body = {'state': 'closed'}
|
||||||
requests.patch(update_issue_url, headers=issue_headers, data=json.dumps(body))
|
requests.patch(update_issue_url, headers=issue_headers, data=json.dumps(body))
|
||||||
@ -215,6 +237,7 @@ def main():
|
|||||||
print('Issue could not be closed')
|
print('Issue could not be closed')
|
||||||
# Don't update too many issues too quickly.
|
# Don't update too many issues too quickly.
|
||||||
sleep(1)
|
sleep(1)
|
||||||
|
print('Closing issues complete')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user