From 300ab3ee0de9ddec8254077aa3ad409d13a0feb6 Mon Sep 17 00:00:00 2001 From: Ian H Pittwood Date: Wed, 26 Feb 2020 16:05:30 -0600 Subject: [PATCH] Test and debug interactions --- gerrit_to_github_issues/__main__.py | 11 +++++++++-- gerrit_to_github_issues/engine.py | 30 +++++++++++++++-------------- gerrit_to_github_issues/gerrit.py | 2 +- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/gerrit_to_github_issues/__main__.py b/gerrit_to_github_issues/__main__.py index b59845a..60255c0 100644 --- a/gerrit_to_github_issues/__main__.py +++ b/gerrit_to_github_issues/__main__.py @@ -24,7 +24,7 @@ LOG = logging.getLogger(__name__) def validate(namespace: argparse.Namespace): arg_dict = vars(namespace) - if not ((arg_dict['github_username'] and arg_dict['github_password']) or arg_dict['github_token']): + if not ((arg_dict['github_user'] and arg_dict['github_password']) or arg_dict['github_token']): raise errors.GithubConfigurationError return arg_dict @@ -45,7 +45,7 @@ if __name__ == '__main__': ) parser.add_argument('-g', '--gerrit-url', action='store', required=True, type=str, default=os.getenv('GERRIT_URL', default=None), help='Target Gerrit URL.') - parser.add_argument('-u', '--github-username', action='store', required=False, type=str, + parser.add_argument('-u', '--github-user', action='store', required=False, type=str, default=os.getenv('GITHUB_USER', default=None), help='Username to use for GitHub Issues integration. Defaults to GITHUB_USER in ' 'environmental variables. Must be used with a password.') @@ -57,8 +57,15 @@ if __name__ == '__main__': default=os.getenv('GITHUB_TOKEN', default=None), help='Token to use for GitHub Issues integration. Defaults to GITHUB_TOKEN in ' 'environmental variables. This will be preferred over a username/password.') + parser.add_argument('-v', '--verbose', action='store_true', required=False, + default=False, help='Enabled DEBUG level logging.') parser.add_argument('gerrit_project_name', action='store', type=str, help='Target Gerrit project.') parser.add_argument('github_project_name', action='store', type=str, help='Target Github project.') ns = parser.parse_args() args = validate(ns) + if args['verbose']: + logging.basicConfig(format=LOG_FORMAT, level=logging.DEBUG) + else: + logging.basicConfig(format=LOG_FORMAT, level=logging.INFO) + args.pop('verbose') update(**args) diff --git a/gerrit_to_github_issues/engine.py b/gerrit_to_github_issues/engine.py index da6747f..dbdeb22 100644 --- a/gerrit_to_github_issues/engine.py +++ b/gerrit_to_github_issues/engine.py @@ -20,11 +20,11 @@ import github_issues LOG = logging.getLogger(__name__) -def update(gerrit_url: str, gerrit_project_name: str, github_project_name: str, github_user: str, github_pw: str, +def update(gerrit_url: str, gerrit_project_name: str, github_project_name: str, github_user: str, github_password: str, github_token: str): - repo = github_issues.get_repo(github_project_name, github_user, github_pw, github_token) + repo = github_issues.get_repo(github_project_name, github_user, github_password, github_token) change_list = gerrit.get_changes(gerrit_url, gerrit_project_name) - for change in change_list: + for change in change_list['data']: if 'commitMessage' in change: process_change(change, repo, gerrit_url) @@ -46,23 +46,25 @@ def process_change(change: dict, repo: Repository, gerrit_url: str): issue.edit(state='open') comment_msg += 'Issue reopened due to new activity on Gerrit.\n\n' if 'WIP' in change['commitMessage'] or 'DNM' in change['commitMessage']: - LOG.debug(f'add `wip` to {issue_number}') - #issue.add_to_labels('wip') + LOG.debug(f'add `wip` to #{issue_number}') + issue.add_to_labels('wip') try: - LOG.debug(f'rm `ready for review` to {issue_number}') - #issue.remove_from_labels('ready for review') + 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: - LOG.debug(f'add `ready for review` to {issue_number}') - #issue.add_to_labels('ready for review') + LOG.debug(f'add `ready for review` to #{issue_number}') + issue.add_to_labels('ready for review') try: - LOG.debug(f'rm `wip` to {issue_number}') - #issue.remove_from_labels('wip') + 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}') if not link_exists: - comment_msg += f'New Related Change: {gerrit_url}' + comment_msg += f'New Related Change: {change_url}\n' \ + f'Authored By: {change["owner"]["name"]} ({change["owner"]["email"]})' if comment_msg: - #issue.create_comment(comment_msg) - LOG.info(f'Comment posted to issue #{gerrit_url}') + issue.create_comment(comment_msg) + LOG.debug(f'Comment to post on #{issue_number}: {comment_msg}') + LOG.info(f'Comment posted to issue #{issue_number}') diff --git a/gerrit_to_github_issues/gerrit.py b/gerrit_to_github_issues/gerrit.py index 94d1a0e..c655422 100644 --- a/gerrit_to_github_issues/gerrit.py +++ b/gerrit_to_github_issues/gerrit.py @@ -14,7 +14,7 @@ import json from fabric import Connection -def get_changes(gerrit_url: str, project_name: str, port: int = 29418) -> list: +def get_changes(gerrit_url: str, project_name: str, port: int = 29418) -> dict: cmd = f'gerrit query --format=JSON status:open project:{project_name}' result = Connection(gerrit_url, port=port).run(cmd) processed_stdout = '{"data":[%s]}' % ','.join(list(filter(None, result.stdout.split('\n'))))