|
| 1 | +from . import settings |
| 2 | +from .github import GitHub |
| 3 | +from .utils import create_event |
| 4 | + |
| 5 | +github = GitHub(settings.token) |
| 6 | + |
| 7 | + |
| 8 | +def parse_comment(body): |
| 9 | + if not body.startswith(settings.bot_name): |
| 10 | + return None, None |
| 11 | + offset = len(settings.bot_name) |
| 12 | + for dr in settings.doc_requests: |
| 13 | + if body.startswith(dr, offset): |
| 14 | + offset += len(dr) |
| 15 | + break |
| 16 | + else: |
| 17 | + return None, 'Invalid request type.' |
| 18 | + if not body.startswith(settings.title_header, offset): |
| 19 | + return None, 'Title not found.' |
| 20 | + offset += len(settings.title_header) |
| 21 | + pos = body.find('\n', offset) |
| 22 | + if pos == -1: |
| 23 | + pos = len(body) |
| 24 | + return {'title': body[offset:pos], |
| 25 | + 'description': body[pos:]}, None |
| 26 | + |
| 27 | + |
| 28 | +def process_issue_comment(body, issue_state, issue_api) -> str: |
| 29 | + action = body['action'] |
| 30 | + if (action != 'created' and action != 'edited') or \ |
| 31 | + issue_state != 'open': |
| 32 | + return 'Not needed.' |
| 33 | + comment = body['comment'] |
| 34 | + author = comment['user']['login'] |
| 35 | + comment, error = parse_comment(comment['body']) |
| 36 | + if error: |
| 37 | + print('Error during request processing: {}'.format(error)) |
| 38 | + github.send_comment(error, issue_api, author) |
| 39 | + elif comment: |
| 40 | + print('Request is processed ok') |
| 41 | + if action == 'edited': |
| 42 | + github.send_comment('Accept edited.', issue_api, author) |
| 43 | + else: |
| 44 | + github.send_comment('Accept.', issue_api, author) |
| 45 | + else: |
| 46 | + print('Ignore non-request comments') |
| 47 | + return 'Doc request is processed.' |
| 48 | + |
| 49 | + |
| 50 | +def process_issue_state_change(body, issue_api, issue_url, doc_repo_url): |
| 51 | + action = body['action'] |
| 52 | + if action != 'closed': |
| 53 | + return 'Not needed.' |
| 54 | + comments = github.get_comments(issue_api) |
| 55 | + for c in comments: |
| 56 | + comment, error = parse_comment(c['body']) |
| 57 | + if comment: |
| 58 | + github.create_issue(comment["title"], comment["description"], |
| 59 | + issue_url, c['user']['login'], doc_repo_url) |
| 60 | + return 'Issue is processed.' |
| 61 | + |
| 62 | + |
| 63 | +def process_commit(c, is_master_push, doc_repo_url): |
| 64 | + body = c['message'] |
| 65 | + request_pos = body.find(settings.bot_name) |
| 66 | + if request_pos == -1: |
| 67 | + return |
| 68 | + request = body[request_pos:] |
| 69 | + author = c['author']['username'] |
| 70 | + comment, error = parse_comment(request) |
| 71 | + if error: |
| 72 | + print('Error during request processing: {}'.format(error)) |
| 73 | + create_event(author, 'process_commit', error) |
| 74 | + else: |
| 75 | + create_event(author, 'process_commit', 'Accept') |
| 76 | + if is_master_push: |
| 77 | + github.create_issue(comment['title'], |
| 78 | + comment['description'], c['url'], |
| 79 | + author, doc_repo_url) |
0 commit comments