Skip to content

Commit

Permalink
Merge pull request grantmcconnaughey#14 from 23andMe/add-linter-name-…
Browse files Browse the repository at this point in the history
…to-messages

Added context (which includes linter name, unless explicitly overridden via CLI flags) to PR comments.
  • Loading branch information
sarahc23 authored Mar 24, 2021
2 parents fe55e15 + def8897 commit bfe1d83
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lintly/backends/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,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(self.context, violation)
})

client = GitHubAPIClient(token=self.token)
Expand All @@ -212,7 +212,7 @@ def create_pull_request_review(self, pr, patch, all_violations, pr_review_action
# there are no pending comments to add then we send the request
if len(comments_batch) == GITHUB_PULL_REQUEST_COMMENT_LIMIT or not comments:
data = {
'body': build_pr_review_body(all_violations),
'body': build_pr_review_body(self.context, all_violations),
'event': self._get_event(pr_review_action),
'comments': comments_batch,
}
Expand Down
8 changes: 5 additions & 3 deletions lintly/builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ def __init__(self, config, linter_output):

self.project = Project(config.repo)

context = config.context or "Lintly/{0}".format(config.format)
self.git_client = GitHubBackend(token=config.api_key, project=self.project, context=context)
self.context = config.context or "Lintly/{0}".format(config.format)
self.git_client = GitHubBackend(
token=config.api_key, project=self.project, context=self.context
)

# All violations found from the linting output
self._all_violations = {}
Expand Down Expand Up @@ -178,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.context, self.violations)
self.git_client.create_pull_request_comment(self.config.pr, comment)

def get_result_description(self):
Expand Down
33 changes: 18 additions & 15 deletions lintly/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,41 @@
from .constants import LINTLY_IDENTIFIER


TEMPLATES_PATH = os.path.join(os.path.dirname(__file__), 'templates')
TEMPLATES_PATH = os.path.join(os.path.dirname(__file__), "templates")


env = Environment(
loader=FileSystemLoader(TEMPLATES_PATH),
autoescape=False
)
env = Environment(loader=FileSystemLoader(TEMPLATES_PATH), autoescape=False)


def build_pr_comment(config, violations):
def build_pr_comment(context, violations):
"""
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)
template = env.get_template("pr_comment.txt")
return template.render(
violations=violations, context=context, LINTLY_IDENTIFIER=LINTLY_IDENTIFIER
)


def build_pr_review_line_comment(violation):
def build_pr_review_line_comment(context, violation):
"""
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)
template = env.get_template("pr_review_line_comment.txt")
return template.render(
violation=violation, context=context, LINTLY_IDENTIFIER=LINTLY_IDENTIFIER
)


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


def build_pr_review_body(violations):
template = env.get_template('pr_review_body.txt')
return template.render(violations=violations, LINTLY_IDENTIFIER=LINTLY_IDENTIFIER)
def build_pr_review_body(context, violations):
template = env.get_template("pr_review_body.txt")
return template.render(
violations=violations, context=context, LINTLY_IDENTIFIER=LINTLY_IDENTIFIER
)
3 changes: 2 additions & 1 deletion lintly/templates/pr_comment.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### [Lintly](https://github.com/grantmcconnaughey/Lintly)
### [ttam-lintly](https://github.com/23andMe/Lintly)
#### {{ context }}

{% if len(violations) %}
The following code quality issues were introduced:
Expand Down
4 changes: 3 additions & 1 deletion lintly/templates/pr_review_body.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#### {{ context }}

{% if violations|length %}

[Lintly](https://github.com/grantmcconnaughey/Lintly) has detected code quality issues in this pull request.
[ttam-lintly](https://github.com/23andMe/Lintly) has detected code quality issues in this pull request.

{% else %}

Expand Down
2 changes: 1 addition & 1 deletion lintly/templates/pr_review_line_comment.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{ violation.code }}: {{ violation.message }} {{ LINTLY_IDENTIFIER }}
*({{ context }})* {{ violation.code }}: {{ violation.message }} {{ LINTLY_IDENTIFIER }}

0 comments on commit bfe1d83

Please sign in to comment.