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 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bugale committed Jul 11, 2022
1 parent a19bf0d commit 86d65e1
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 30 deletions.
6 changes: 3 additions & 3 deletions lintly/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create_pull_request_comment(self, pr, comment):
"""
raise NotImplementedError

def delete_pull_request_comments(self, pr):
def delete_pull_request_comments(self, pr, comment_tag):
"""
Deletes all pull request comments for the bot account.
"""
Expand All @@ -39,13 +39,13 @@ def get_pr_diff(self, pr):
"""
raise NotImplementedError

def create_pull_request_review(self, pr, patch, all_violations, pr_review_action, has_body):
def create_pull_request_review(self, pr, patch, all_violations, pr_review_action, has_body, comment_tag):
"""
Creates a pull request review for the given build.
"""
raise NotImplementedError

def delete_pull_request_review_comments(self, pr):
def delete_pull_request_review_comments(self, pr, comment_tag):
"""
Deletes all pull request review comments for the bot account.
"""
Expand Down
6 changes: 3 additions & 3 deletions lintly/backends/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def get_pull_request(self, pr):
def create_pull_request_comment(self, pr, comment):
pass

def delete_pull_request_comments(self, pr):
def delete_pull_request_comments(self, pr, comment_tag):
pass

def create_pull_request_review(self, pr, patch, all_violations, pr_review_action, has_body):
def create_pull_request_review(self, pr, patch, all_violations, pr_review_action, has_body, comment_tag):
pass

def delete_pull_request_review_comments(self, pr):
def delete_pull_request_review_comments(self, pr, comment_tag):
pass

def post_status(self, state, description, sha, target_url):
Expand Down
18 changes: 9 additions & 9 deletions lintly/backends/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def __init__(self, token, project, context):
self.client = Github(token, user_agent=GITHUB_USER_AGENT, per_page=DEFAULT_PER_PAGE)
self.context = context

def _should_delete_comment(self, comment):
return LINTLY_IDENTIFIER in comment.body
def _should_delete_comment(self, comment, comment_tag):
return (LINTLY_IDENTIFIER % comment_tag) in comment.body

@translate_github_exception
def get_pull_request(self, pr):
Expand Down Expand Up @@ -152,11 +152,11 @@ def create_pull_request_comment(self, pr, comment):
)

@translate_github_exception
def delete_pull_request_comments(self, pr):
def delete_pull_request_comments(self, pr, comment_tag):
repo = self.client.get_repo(self.project.full_name)
pull_request = repo.get_issue(int(pr))
for comment in pull_request.get_comments():
if self._should_delete_comment(comment):
if self._should_delete_comment(comment, comment_tag):
comment.delete()

def get_pr_diff(self, pr):
Expand All @@ -180,7 +180,7 @@ def _get_event(self, review_action):
elif review_action == ACTION_REVIEW_APPROVE:
return 'APPROVE'

def create_pull_request_review(self, pr, patch, all_violations, pr_review_action, has_body):
def create_pull_request_review(self, pr, patch, all_violations, pr_review_action, has_body, comment_tag):
comments = []
for file_path in all_violations:
violations = all_violations[file_path]
Expand All @@ -192,7 +192,7 @@ def create_pull_request_review(self, pr, patch, all_violations, pr_review_action
comments.append({
'path': file_path,
'position': patch_position,
'body': build_pr_review_line_comment(violation)
'body': build_pr_review_line_comment(violation, self, comment_tag)
})

client = GitHubAPIClient(token=self.token)
Expand All @@ -213,7 +213,7 @@ def create_pull_request_review(self, pr, patch, all_violations, pr_review_action
'comments': comments_batch,
}
if has_body:
data['body'] = build_pr_review_body(all_violations)
data['body'] = build_pr_review_body(all_violations, comment_tag)

url = '/repos/{owner}/{repo_name}/pulls/{pr_number}/reviews'.format(
owner=self.project.owner_login,
Expand All @@ -225,11 +225,11 @@ def create_pull_request_review(self, pr, patch, all_violations, pr_review_action
comments_batch.clear()

@translate_github_exception
def delete_pull_request_review_comments(self, pr):
def delete_pull_request_review_comments(self, pr, comment_tag):
repo = self.client.get_repo(self.project.full_name)
pull_request = repo.get_pull(int(pr))
for comment in pull_request.get_review_comments():
if self._should_delete_comment(comment):
if self._should_delete_comment(comment, comment_tag):
comment.delete()

def post_status(self, state, description, sha, target_url=''):
Expand Down
8 changes: 4 additions & 4 deletions lintly/backends/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,23 @@ def create_pull_request_comment(self, pr, comment):
mr.notes.create({'body': comment})

@translate_gitlab_exception
def delete_pull_request_comments(self, pr):
def delete_pull_request_comments(self, pr, comment_tag):
project = self.client.projects.get(self.project.full_name)
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 (LINTLY_IDENTIFIER % comment_tag) 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
)
client.delete(url)

@translate_gitlab_exception
def create_pull_request_review(self, pr, patch, all_violations, pr_review_action, has_body):
def create_pull_request_review(self, pr, patch, all_violations, pr_review_action, has_body, comment_tag):
raise NotSupportedError()

@translate_gitlab_exception
def delete_pull_request_review_comments(self, pr):
def delete_pull_request_review_comments(self, pr, comment_tag):
raise NotSupportedError()

@translate_gitlab_exception
Expand Down
9 changes: 5 additions & 4 deletions lintly/builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ def get_pr_patch(self, diff):

def cleanup_previous_comments(self):
logger.info('Deleting old PR review comments')
self.git_client.delete_pull_request_review_comments(self.config.pr)
self.git_client.delete_pull_request_review_comments(self.config.pr, self.config.comment_tag)

logger.info('Deleting old PR comment')
self.git_client.delete_pull_request_comments(self.config.pr)
self.git_client.delete_pull_request_comments(self.config.pr, self.config.comment_tag)

def find_diff_violations(self, patch):
"""
Expand Down Expand Up @@ -165,7 +165,8 @@ def submit_pr_review(self, patch, pr_review_action):
patch,
self._diff_violations,
pr_review_action,
self.config.review_body
self.config.review_body,
self.config.comment_tag
)
post_pr_comment = False
except GitClientError as e:
Expand All @@ -179,7 +180,7 @@ def submit_pr_review(self, patch, pr_review_action):

if post_pr_comment and pr_review_action in (ACTION_REVIEW_COMMENT, ACTION_REVIEW_REQUEST_CHANGES):
logger.info('Creating PR comment')
comment = build_pr_comment(self.config, self.violations)
comment = build_pr_comment(self.violations, self.config.comment_tag)
self.git_client.create_pull_request_comment(self.config.pr, comment)

def get_result_description(self):
Expand Down
3 changes: 3 additions & 0 deletions lintly/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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
4 changes: 4 additions & 0 deletions lintly/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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
2 changes: 1 addition & 1 deletion lintly/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# 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 -->'
LINTLY_IDENTIFIER = '<!-- Automatically posted by Lintly %s -->'

# These constants define the actions that lintly might take against a PR concerning reviews, ie.
# not the commit status
Expand Down
12 changes: 6 additions & 6 deletions lintly/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@
)


def build_pr_comment(config, violations):
def build_pr_comment(violations, comment_tag):
"""
Creates a Markdown representation of the comment to be posted to a pull request.
: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=(LINTLY_IDENTIFIER % comment_tag))


def build_pr_review_line_comment(violation):
def build_pr_review_line_comment(violation, comment_tag):
"""
Creates a Markdown representation of the comment to be posted to a pull request.
: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=(LINTLY_IDENTIFIER % comment_tag))


def build_check_line_comment(violation):
template = env.get_template('check_line_comment.txt')
return template.render(violation=violation)


def build_pr_review_body(violations):
def build_pr_review_body(violations, comment_tag):
template = env.get_template('pr_review_body.txt')
return template.render(violations=violations, LINTLY_IDENTIFIER=LINTLY_IDENTIFIER)
return template.render(violations=violations, LINTLY_IDENTIFIER=(LINTLY_IDENTIFIER % comment_tag))

0 comments on commit 86d65e1

Please sign in to comment.