From 809380e31baf764ed34dbc5752cd020ea38a1fa2 Mon Sep 17 00:00:00 2001 From: Bugale Bugalit Date: Mon, 11 Jul 2022 22:08:22 +0300 Subject: [PATCH] Issue #60 - Add support for custom comment tag --- lintly/backends/github.py | 4 ++-- lintly/backends/gitlab.py | 4 ++-- lintly/cli.py | 4 ++++ lintly/config.py | 8 ++++++++ lintly/constants.py | 5 ----- lintly/formatters.py | 8 ++++---- 6 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lintly/backends/github.py b/lintly/backends/github.py index 5368957..1798137 100644 --- a/lintly/backends/github.py +++ b/lintly/backends/github.py @@ -7,7 +7,7 @@ from github import GithubException, UnknownObjectException, Github -from lintly.constants import LINTLY_IDENTIFIER +from lintly.config import Config from lintly.formatters import ( build_pr_review_line_comment, build_pr_review_body, @@ -124,7 +124,7 @@ def __init__(self, token, project, context): self.context = context def _should_delete_comment(self, comment): - return LINTLY_IDENTIFIER in comment.body + return Config.LINTLY_IDENTIFIER in comment.body @translate_github_exception def get_pull_request(self, pr): diff --git a/lintly/backends/gitlab.py b/lintly/backends/gitlab.py index 2595c21..37769e4 100644 --- a/lintly/backends/gitlab.py +++ b/lintly/backends/gitlab.py @@ -7,7 +7,7 @@ import gitlab import requests -from lintly.constants import LINTLY_IDENTIFIER +from lintly.config import Config from .base import BaseGitBackend from .errors import ( @@ -136,7 +136,7 @@ def delete_pull_request_comments(self, pr): mr = project.mergerequests.list(iid=pr)[0] client = GitLabAPIClient(self.token, self.user, self.project) for note in mr.notes.list(all=True, per_page=DEFAULT_PER_PAGE): - if LINTLY_IDENTIFIER in note.body: + if Config.LINTLY_IDENTIFIER in note.body: url = '/projects/{project_id}/merge_requests/{mr_id}/notes/{note_id}'.format( project_id=project.id, mr_id=mr.id, note_id=note.id ) diff --git a/lintly/cli.py b/lintly/cli.py index 0b7d76c..97f4e7f 100644 --- a/lintly/cli.py +++ b/lintly/cli.py @@ -9,6 +9,7 @@ from .constants import FAIL_ON_ANY, FAIL_ON_NEW from .exceptions import NotPullRequestException from .parsers import PARSERS +from . import constants logger = logging.getLogger(__name__) @@ -59,6 +60,9 @@ default=True, help=('Whether Lintly should post a PR review with a body. ' 'The body is not removed after a re-run.')) +@click.option('--comment-tag', + default='', + help='A tag used to identify comments from a previous run that should be deleted') @click.option('--exit-zero/--no-exit-zero', default=False, help=('Whether Lintly should exit with error code indicating ' 'amount of violations or not. Default false')) diff --git a/lintly/config.py b/lintly/config.py index c5a9018..0307aef 100644 --- a/lintly/config.py +++ b/lintly/config.py @@ -7,8 +7,11 @@ class Config(object): """A Config object that knows how to return configuration from the CLI or Continuous Integration services""" + LINTLY_IDENTIFIER = '' + def __init__(self, cli_config): self.cli_config = cli_config + type(self).LINTLY_IDENTIFIER = type(self).LINTLY_IDENTIFIER % self.comment_tag def as_dict(self): return { @@ -22,6 +25,7 @@ def as_dict(self): 'request_changes': self.request_changes, 'github_check_run_id': self.github_check_run_id, 'review_body': self.review_body, + 'comment_tag': self.comment_tag, } @property @@ -64,6 +68,10 @@ def request_changes(self): def review_body(self): return self.cli_config['review_body'] + @property + def comment_tag(self): + return self.cli_config['comment_tag'] + @property def github_check_run_id(self): """The Check Run ID from GitHub Actions. diff --git a/lintly/constants.py b/lintly/constants.py index b3a0583..5c3fc42 100644 --- a/lintly/constants.py +++ b/lintly/constants.py @@ -1,11 +1,6 @@ FAIL_ON_ANY = 'any' FAIL_ON_NEW = 'new' -# Identifies that a comment came from Lintly. This is used to aid in automatically -# deleting old PR comments/reviews. This is valid Markdown that is hidden from -# users in GitHub and GitLab. -LINTLY_IDENTIFIER = '' - # These constants define the actions that lintly might take against a PR concerning reviews, ie. # not the commit status ACTION_REVIEW_USE_CHECKS = 'use_checks' diff --git a/lintly/formatters.py b/lintly/formatters.py index fdeb274..88f81a5 100644 --- a/lintly/formatters.py +++ b/lintly/formatters.py @@ -5,7 +5,7 @@ from jinja2 import Environment, FileSystemLoader -from .constants import LINTLY_IDENTIFIER +from .config import Config TEMPLATES_PATH = os.path.join(os.path.dirname(__file__), 'templates') @@ -23,7 +23,7 @@ def build_pr_comment(config, violations): :return: The comment """ template = env.get_template('pr_comment.txt') - return template.render(violations=violations, LINTLY_IDENTIFIER=LINTLY_IDENTIFIER) + return template.render(violations=violations, LINTLY_IDENTIFIER=Config.LINTLY_IDENTIFIER) def build_pr_review_line_comment(violation): @@ -32,7 +32,7 @@ def build_pr_review_line_comment(violation): :return: The comment """ template = env.get_template('pr_review_line_comment.txt') - return template.render(violation=violation, LINTLY_IDENTIFIER=LINTLY_IDENTIFIER) + return template.render(violation=violation, LINTLY_IDENTIFIER=Config.LINTLY_IDENTIFIER) def build_check_line_comment(violation): @@ -42,4 +42,4 @@ def build_check_line_comment(violation): def build_pr_review_body(violations): template = env.get_template('pr_review_body.txt') - return template.render(violations=violations, LINTLY_IDENTIFIER=LINTLY_IDENTIFIER) + return template.render(violations=violations, LINTLY_IDENTIFIER=Config.LINTLY_IDENTIFIER)