def add_labels(gh: github.Github, issue_number: int, affecting_changes: list, repo: Repository, project_board: Project): try: issue = repo.get_issue(issue_number) except github.GithubException: LOG.warning(f'Issue #{issue_number} not found for project') return # Assume these conditions and prove otherwise by iterating over affecting changes is_wip = False is_closed = True for change in affecting_changes: if 'WIP' in change['commitMessage'] or 'DNM' in change['commitMessage']: is_wip = True if change['status'] == 'NEW': is_closed = False if is_closed: LOG.debug(f'Issue #{issue_number} is closed, removing labels.') remove_label(issue, 'wip') remove_label(issue, 'ready for review') elif is_wip: Log.debug(f'Issue #{issue_number} is WIP, adding the "wip" label and removing ' \ f'the "ready for review" label.') remove_label(issue, 'ready for review') add_label(issue, 'wip') move_issue(project_board, issue, 'In Progress') else: Log.debug(f'Issue #{issue_number} is ready to be reviewed, adding the "ready ' \ f'for review" label and removing the "wip" label.') remove_label(issue, 'wip') add_label(issue, 'ready for review') move_issue(project_board, issue, 'Submitted on Gerrit')
def add_comments(gh: github.Github, change: dict, affected_issues: dict, repo: Repository, skip_approvals: bool = False): for key, issues_list in affected_issues.items(): for issue_number in issues_list: try: issue = repo.get_issue(issue_number) except github.GithubException: LOG.warning(f'Issue #{issue_number} not found for project') return comment_msg = get_issue_comment(change, key, skip_approvals) # Disable this feature to reopen issue on any gerrit activity on closed issue # Issues identified: # 1. When an old PS (tagged with closed issue number) is abandoned, it reopens the issue # 2. post/promote job events are also considered as some gerrit activity on a # closed issue and is reopened in the next immediate run of bot #if issue.state == 'closed': # LOG.debug(f'Issue #{issue_number} was closed, reopening...') # NOTE(howell): Reopening a closed issue will move it from the # "Done" column to the "In Progress" column on the project # board via Github automation. # issue.edit(state='open') # comment_msg += '\n\nIssue reopened due to new activity on Gerrit.' bot_comment = github_issues.get_bot_comment(issue, gh.get_user().login, change['number']) if not bot_comment: LOG.debug(f'Comment to post on #{issue_number}: {comment_msg}') issue.create_comment(comment_msg) LOG.info(f'Comment posted to issue #{issue_number}') else: LOG.debug(f'Comment to edit on #{issue_number}: {comment_msg}') bot_comment.edit(comment_msg) LOG.info(f'Comment edited to issue #{issue_number}')
def update_milestone(r: Repository, pr: PullRequest, m: Milestone): # PR in Github API does not have a way to update milestone. It should be opened as issue, # and then it can be updated ¯\_(ツ)_/¯ r.get_issue(pr.number).edit(milestone=m)
def process_change(gh: github.Github, change: dict, repo: Repository, skip_approvals: bool = False): issue_numbers_dict = github_issues.parse_issue_number( change['commitMessage']) issue_numbers_dict = github_issues.remove_duplicated_issue_numbers( issue_numbers_dict) if not issue_numbers_dict: LOG.warning(f'No issue tag found for change #{change["number"]}') return for key, issues_list in issue_numbers_dict.items(): for issue_number in issues_list: try: issue = repo.get_issue(issue_number) except github.GithubException: LOG.warning(f'Issue #{issue_number} not found for project') return bot_comment = github_issues.get_bot_comment( issue, gh.get_user().login, change['number']) if issue.state == 'closed' and not bot_comment: LOG.debug(f'Issue #{issue_number} was closed, reopening...') issue.edit(state='open') issue.create_comment( 'Issue reopened due to new activity on Gerrit.\n\n') labels = [str(l.name) for l in list(issue.get_labels())] if 'WIP' in change['commitMessage'] or 'DNM' in change[ 'commitMessage']: if 'wip' not in labels: LOG.debug(f'add `wip` to #{issue_number}') issue.add_to_labels('wip') if 'ready for review' in labels: try: LOG.debug(f'rm `ready for review` to #{issue_number}') issue.remove_from_labels('ready for review') except github.GithubException: LOG.debug( f'`ready for review` tag does not exist on issue #{issue_number}' ) else: if 'ready for review' not in labels: LOG.debug(f'add `ready for review` to #{issue_number}') issue.add_to_labels('ready for review') if 'wip' in labels: try: LOG.debug(f'rm `wip` to #{issue_number}') issue.remove_from_labels('wip') except github.GithubException: LOG.debug( f'`wip` tag does not exist on issue #{issue_number}' ) comment_msg = get_issue_comment(change, key, skip_approvals) if not bot_comment: if key == 'closes': comment_msg += '\n\nThis change will close this issue when merged.' LOG.debug(f'Comment to post on #{issue_number}: {comment_msg}') issue.create_comment(comment_msg) LOG.info(f'Comment posted to issue #{issue_number}') else: LOG.debug(f'Comment to edit on #{issue_number}: {comment_msg}') comment = github_issues.get_bot_comment( issue, gh.get_user().login, change['number']) comment.edit(comment_msg) LOG.info(f'Comment edited to issue #{issue_number}')