Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option comment_on_pr #47

Merged
merged 6 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ and configure it as follows:
check_name: Unit Test Results
comment_title: Unit Test Statistics
hide_comments: all but latest
comment_on_pr: true
files: test-results/**/*.xml
report_individual_runs: true
deduplicate_classes_by_file_name: false
Expand All @@ -77,6 +78,9 @@ Setting the option to `orphaned commits` will hide comments for orphaned commits
These are commits that do no longer belong to the pull request (due to commit history rewrite).
Hiding comments can be disabled all together with value `off`.

To disable comments on pull requests completely, set the option `comment_on_pr` to `false`.
Pull request comments are enabled by default.

Files can be selected via the `files` variable, which is optional and defaults to the current working directory.
It supports wildcards like `*`, `**`, `?` and `[]`. The `**` wildcard matches
[directories recursively](https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob): `./`, `./*/`, `./*/*/`, etc.
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ inputs:
description: 'Configures which earlier comments of the action to hide: off - no comments, orphaned commits - comments for commits that are removed from the branch, all but latest - all comments but the latest'
required: false
default: 'all but latest'
comment_on_pr:
description: 'Control PR comments: false - disable PR comments, true - comment on PRs'
required: false
default: 'true'
log_level:
description: 'Action logging level'
required: false
Expand Down
35 changes: 22 additions & 13 deletions publish_unit_test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def get_annotations(case_results: Dict[str, Dict[str, List[Dict[Any, Any]]]], re
def publish(token: str, event: dict, repo_name: str, commit_sha: str,
stats: Dict[Any, Any], cases: Dict[str, Dict[str, List[Dict[Any, Any]]]],
check_name: str, comment_title: str, hide_comment_mode: str,
report_individual_runs: bool):
comment_on_pr: bool, report_individual_runs: bool):
from github import Github, PullRequest, Requester, MainClass
from githubext import Repository, Commit, IssueComment

Expand Down Expand Up @@ -744,7 +744,7 @@ def hide_all_but_latest_comments(pull: PullRequest) -> None:
if os.environ.get('GITHUB_ACTIONS') is None:
logger.warning('action not running on GitHub, skipping hiding comment')
for node_id in comment_ids:
logger.info('commend {} should be hidden'.format(node_id))
logger.info('comment {} should be hidden'.format(node_id))
return

# hide all those comments
Expand All @@ -755,15 +755,20 @@ def hide_all_but_latest_comments(pull: PullRequest) -> None:
logger.info('publishing results for commit {}'.format(commit_sha))
publish_check(stats, cases)

pull = get_pull(commit_sha)
if pull is not None:
publish_comment(comment_title, stats, pull)
if hide_comment_mode == hide_comments_mode_orphaned:
hide_orphaned_commit_comments(pull)
elif hide_comment_mode == hide_comments_mode_all_but_latest:
hide_all_but_latest_comments(pull)
if comment_on_pr:
pull = get_pull(commit_sha)
if pull is not None:
publish_comment(comment_title, stats, pull)
if hide_comment_mode == hide_comments_mode_orphaned:
hide_orphaned_commit_comments(pull)
elif hide_comment_mode == hide_comments_mode_all_but_latest:
hide_all_but_latest_comments(pull)
else:
logger.info('hide_comments disabled, not hiding any comments')
else:
logger.info('there is no pull request for commit {}'.format(commit_sha))
else:
logger.info('there is no pull request for commit {}'.format(commit_sha))
logger.info('comment_on_pr disabled, not commenting on any pull requests')


def write_stats_file(stats, filename) -> None:
Expand All @@ -774,7 +779,8 @@ def write_stats_file(stats, filename) -> None:

def main(token: str, event: dict, repo: str, commit: str, files_glob: str,
check_name: str, comment_title: str, hide_comment_mode: str,
report_individual_runs: bool, dedup_classes_by_file_name: bool) -> None:
comment_on_pr: bool, report_individual_runs: bool,
dedup_classes_by_file_name: bool) -> None:
files = [str(file) for file in pathlib.Path().glob(files_glob)]
logger.info('reading {}: {}'.format(files_glob, list(files)))

Expand All @@ -789,7 +795,8 @@ def main(token: str, event: dict, repo: str, commit: str, files_glob: str,
stats = get_stats(results)

# publish the delta stats
publish(token, event, repo, commit, stats, results['case_results'], check_name, comment_title, hide_comment_mode, report_individual_runs)
publish(token, event, repo, commit, stats, results['case_results'], check_name, comment_title, hide_comment_mode,
comment_on_pr, report_individual_runs)


def get_commit_sha(event: dict, event_name: str):
Expand Down Expand Up @@ -832,6 +839,7 @@ def check_var(var: str, name: str, label: str, allowed_values: Optional[List[str
report_individual_runs = get_var('REPORT_INDIVIDUAL_RUNS') == 'true'
dedup_classes_by_file_name = get_var('DEDUPLICATE_CLASSES_BY_FILE_NAME') == 'true'
hide_comment_mode = get_var('HIDE_COMMENTS') or 'all but latest'
comment_on_pr = get_var('COMMENT_ON_PR') != 'false'
commit = get_var('COMMIT') or get_commit_sha(event, event_name)
files = get_var('FILES')

Expand All @@ -841,4 +849,5 @@ def check_var(var: str, name: str, label: str, allowed_values: Optional[List[str
check_var(commit, 'COMMIT or event file', 'Commit SHA')
check_var(files, 'FILES', 'Files pattern')

main(token, event, repo, commit, files, check_name, comment_title, hide_comment_mode, report_individual_runs, dedup_classes_by_file_name)
main(token, event, repo, commit, files, check_name, comment_title, hide_comment_mode, comment_on_pr,
report_individual_runs, dedup_classes_by_file_name)