|
| 1 | +from datetime import date |
| 2 | +from typing import Set, List, Dict, Any |
| 3 | +import os |
| 4 | +import logging |
| 5 | +from github import Github |
| 6 | +from github.Issue import Issue |
| 7 | +from subprocess import check_call |
| 8 | + |
| 9 | +_LOG = logging.getLogger(__name__) |
| 10 | + |
| 11 | +_LANGUAGE_OWNER = {'msyyc'} |
| 12 | +_LANGUAGE_REPO = 'Azure/azure-sdk-for-python' |
| 13 | +_FILE_OUT_NAME = 'common.md' |
| 14 | + |
| 15 | + |
| 16 | +class Common: |
| 17 | + """ The class defines some function for all languages to reference |
| 18 | + language_owner = {} # language owner who may handle issue |
| 19 | + repo_name = '' # base repo. For example: Azure/azure-sdk-for-python |
| 20 | + file_out_name = '' # report name. For example: sdk_issue_status_python.md |
| 21 | + issue_html = '' # issue url. For example: https://github.com/Azure/azure-sdk-for-python/issues |
| 22 | +
|
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self, language_owner: Set[str], repo_name: str, file_out_name: str): |
| 26 | + self.language_owner = language_owner |
| 27 | + self.repo_name = repo_name |
| 28 | + self.file_out_name = file_out_name |
| 29 | + self.issue_html = f'https://github.com/{repo_name}/issues' |
| 30 | + self.bot_assignees = {'msftbot[bot]'} |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def push_md_to_storage(): |
| 34 | + cmd_list = ['git add .', 'git commit -m \"update excel\"', 'git push -f origin HEAD'] |
| 35 | + [check_call(cmd, shell=True) for cmd in cmd_list] |
| 36 | + |
| 37 | + def collect_open_issues(self) -> List[Issue]: |
| 38 | + hub = Github(os.getenv('TOKEN')) |
| 39 | + repo = hub.get_repo(self.repo_name) |
| 40 | + mgmt_label = repo.get_label('Mgmt') |
| 41 | + open_issues = repo.get_issues(state='open', labels=[mgmt_label]) |
| 42 | + return [issue for issue in open_issues if not issue.pull_request] |
| 43 | + |
| 44 | + def judge_status(self, issue: Issue) -> str: |
| 45 | + if issue.user.login in self.language_owner: |
| 46 | + return '' |
| 47 | + latest_comments = '' |
| 48 | + comments = [(comment.updated_at.timestamp(), comment.user.login) for comment in issue.get_comments() |
| 49 | + if comment.user.login not in self.bot_assignees] |
| 50 | + comments.sort() |
| 51 | + if comments: |
| 52 | + latest_comments = comments[-1][1] |
| 53 | + if issue.comments == 0: |
| 54 | + return 'new issue' |
| 55 | + elif latest_comments not in self.language_owner: |
| 56 | + return 'new comment' |
| 57 | + else: |
| 58 | + return '' |
| 59 | + |
| 60 | + def extract_info(self, open_issues: List[Issue]) -> List[Dict[str, Any]]: |
| 61 | + issues_info = [] |
| 62 | + _LOG.info(f'collect {len(open_issues)} open issues in {self.issue_html}') |
| 63 | + idx = 1 |
| 64 | + for issue in open_issues: |
| 65 | + assignees = {assignee.login for assignee in issue.assignees} |
| 66 | + if assignees.intersection(self.language_owner): |
| 67 | + issue_info = { |
| 68 | + 'idx': idx, |
| 69 | + 'number': issue.number, |
| 70 | + 'html': f'{self.issue_html}/{issue.number}', |
| 71 | + 'title': issue.title, |
| 72 | + 'labels': [label.name for label in issue.labels], |
| 73 | + 'assignees': assignees, |
| 74 | + 'created_date': str(date.fromtimestamp(issue.created_at.timestamp()).strftime('%Y-%m-%d')), |
| 75 | + 'status': self.judge_status(issue) |
| 76 | + } |
| 77 | + issues_info.append(issue_info) |
| 78 | + idx = idx + 1 |
| 79 | + _LOG.info(f'collect {len(issues_info)} open issues assigned to {str(self.language_owner)}') |
| 80 | + return issues_info |
| 81 | + |
| 82 | + @staticmethod |
| 83 | + def output_line(issue_info: Dict[str, Any]) -> str: |
| 84 | + return '|{No}|[#{number}]({issue_html})|{title}|{labels}|{assignees}|{bot_advice}|{created_date}|\n'.format( |
| 85 | + No=issue_info['idx'], |
| 86 | + number=issue_info['number'], |
| 87 | + issue_html=issue_info['html'], |
| 88 | + title=issue_info['title'], |
| 89 | + labels=', '.join(issue_info['labels']), |
| 90 | + assignees=', '.join(issue_info['assignees']), |
| 91 | + bot_advice=issue_info['status'], |
| 92 | + created_date=issue_info['created_date'], |
| 93 | + ) |
| 94 | + |
| 95 | + def report(self, issues_info: List[Dict[str, Any]]) -> None: |
| 96 | + with open(self.file_out_name, 'w') as file_out: |
| 97 | + file_out.write( |
| 98 | + '| No. | issue | title | labels | assignees | bot advice | created date |\n') |
| 99 | + file_out.write('| ------ | ------ | ------ | ------ | ------ | ------ | :-----: |\n') |
| 100 | + file_out.writelines([Common.output_line(issue_info) for issue_info in issues_info]) |
| 101 | + |
| 102 | + def run(self): |
| 103 | + open_issues = self.collect_open_issues() |
| 104 | + issues_info = self.extract_info(open_issues) |
| 105 | + self.report(issues_info) |
| 106 | + |
| 107 | + |
| 108 | +def common_process(): |
| 109 | + instance = Common(_LANGUAGE_OWNER, _LANGUAGE_REPO, _FILE_OUT_NAME) |
| 110 | + instance.run() |
0 commit comments