|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import os |
| 4 | + |
| 5 | +from github import Auth, Github, Issue |
| 6 | + |
| 7 | +from consts import OrgSchemaIds, NEW_ORG_ISSUE_DEFAULT_TITLE, NEW_ORG_SCHEMA_FILENAME |
| 8 | +from labels import Label |
| 9 | +from parsers import FormDataParser |
| 10 | +from pullers import OrgDataPuller |
| 11 | +from utils import has_label |
| 12 | +from validators import OrgValidator |
| 13 | + |
| 14 | +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
| 15 | +logger = logging.getLogger(__file__) |
| 16 | + |
| 17 | +GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") |
| 18 | +GITHUB_REPOSITORY = os.getenv("GITHUB_REPOSITORY") |
| 19 | + |
| 20 | +auth = Auth.Token(GITHUB_TOKEN) |
| 21 | +g = Github(auth=auth) |
| 22 | +repo = g.get_repo(GITHUB_REPOSITORY) |
| 23 | + |
| 24 | + |
| 25 | +def process_new_org_issue(issue: Issue, data: FormDataParser): |
| 26 | + if has_label(issue, Label.AUTO_VERIFIED): |
| 27 | + issue.remove_from_labels(Label.AUTO_VERIFIED) |
| 28 | + |
| 29 | + validator = OrgValidator(data, issue) |
| 30 | + if not validator.validate(): |
| 31 | + logger.error("Validation failed") |
| 32 | + return |
| 33 | + |
| 34 | + if not (org := OrgDataPuller.get_org_by_krs(issue, data.get(OrgSchemaIds.krs))): |
| 35 | + logger.error("KRS db validation failed") |
| 36 | + return |
| 37 | + |
| 38 | + # Update issue title |
| 39 | + if issue.title == NEW_ORG_ISSUE_DEFAULT_TITLE: |
| 40 | + logger.info("Updating issue title") |
| 41 | + issue.edit(title=f"{NEW_ORG_ISSUE_DEFAULT_TITLE} {org.name or data.get(OrgSchemaIds.name)}") |
| 42 | + |
| 43 | + if not has_label(issue, Label.AUTO_VERIFIED): |
| 44 | + logger.info("Adding auto-verified label") |
| 45 | + issue.create_comment(f"@{issue.user.login}, dziękujemy za podanie informacji. " |
| 46 | + f"Przyjęliśmy zgłoszenie dodania nowej organizacji. " |
| 47 | + f"Wkrótce skontaktujemy się celem weryfikacji zgłoszenia.") |
| 48 | + issue.add_to_labels(Label.AUTO_VERIFIED) |
| 49 | + |
| 50 | + |
| 51 | +def main(): |
| 52 | + github_form_json = os.getenv("GITHUB_FORM_JSON") |
| 53 | + github_issue_number = int(os.getenv("GITHUB_ISSUE_NUMBER")) |
| 54 | + |
| 55 | + issue = repo.get_issue(github_issue_number) |
| 56 | + data = FormDataParser( |
| 57 | + json.loads(github_form_json), |
| 58 | + NEW_ORG_SCHEMA_FILENAME |
| 59 | + ) |
| 60 | + process_new_org_issue(issue, data) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + main() |
0 commit comments