Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
Issue grantmcconnaughey#60 - Add support for custom comment tag
Browse files Browse the repository at this point in the history
  • Loading branch information
bugale committed Jul 11, 2022
1 parent c63e77f commit 809380e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lintly/backends/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions lintly/backends/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
)
Expand Down
4 changes: 4 additions & 0 deletions lintly/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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'))
Expand Down
8 changes: 8 additions & 0 deletions lintly/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<!-- Automatically posted by Lintly %s -->'

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 {
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 0 additions & 5 deletions lintly/constants.py
Original file line number Diff line number Diff line change
@@ -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 = '<!-- Automatically posted by Lintly -->'

# 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'
Expand Down
8 changes: 4 additions & 4 deletions lintly/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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)

0 comments on commit 809380e

Please sign in to comment.