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

Publish results as job summary #266

Merged
merged 2 commits into from
May 12, 2022
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
2 changes: 1 addition & 1 deletion .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ jobs:
- name: Publish Unit Test Results
if: always()
run: |
docker run --workdir $GITHUB_WORKSPACE --rm -e INPUT_CHECK_NAME -e INPUT_FILES -e INPUT_TIME_UNIT -e INPUT_GITHUB_TOKEN -e INPUT_GITHUB_RETRIES -e INPUT_COMMIT -e INPUT_COMMENT_TITLE -e INPUT_FAIL_ON -e INPUT_REPORT_INDIVIDUAL_RUNS -e INPUT_DEDUPLICATE_CLASSES_BY_FILE_NAME -e INPUT_IGNORE_RUNS -e INPUT_HIDE_COMMENTS -e INPUT_COMMENT_ON_PR -e INPUT_COMMENT_MODE -e INPUT_COMPARE_TO_EARLIER_COMMIT -e INPUT_PULL_REQUEST_BUILD -e INPUT_EVENT_FILE -e INPUT_EVENT_NAME -e INPUT_TEST_CHANGES_LIMIT -e INPUT_CHECK_RUN_ANNOTATIONS -e INPUT_CHECK_RUN_ANNOTATIONS_BRANCH -e INPUT_SECONDS_BETWEEN_GITHUB_READS -e INPUT_SECONDS_BETWEEN_GITHUB_WRITES -e INPUT_JSON_FILE -e HOME -e GITHUB_JOB -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_REPOSITORY_OWNER -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_RETENTION_DAYS -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_SERVER_URL -e GITHUB_API_URL -e GITHUB_GRAPHQL_URL -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e GITHUB_ACTION_REPOSITORY -e GITHUB_ACTION_REF -e GITHUB_PATH -e GITHUB_ENV -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "$RUNNER_TEMP":"$RUNNER_TEMP" -v "$GITHUB_WORKSPACE":"$GITHUB_WORKSPACE" enricomi/publish-unit-test-result-action:latest
docker run --workdir $GITHUB_WORKSPACE --rm -e INPUT_CHECK_NAME -e INPUT_FILES -e INPUT_TIME_UNIT -e INPUT_GITHUB_TOKEN -e INPUT_GITHUB_RETRIES -e INPUT_COMMIT -e INPUT_COMMENT_TITLE -e INPUT_FAIL_ON -e INPUT_REPORT_INDIVIDUAL_RUNS -e INPUT_DEDUPLICATE_CLASSES_BY_FILE_NAME -e INPUT_IGNORE_RUNS -e INPUT_HIDE_COMMENTS -e INPUT_COMMENT_ON_PR -e INPUT_COMMENT_MODE -e INPUT_COMPARE_TO_EARLIER_COMMIT -e INPUT_PULL_REQUEST_BUILD -e INPUT_EVENT_FILE -e INPUT_EVENT_NAME -e INPUT_TEST_CHANGES_LIMIT -e INPUT_CHECK_RUN_ANNOTATIONS -e INPUT_CHECK_RUN_ANNOTATIONS_BRANCH -e INPUT_SECONDS_BETWEEN_GITHUB_READS -e INPUT_SECONDS_BETWEEN_GITHUB_WRITES -e INPUT_JSON_FILE -e INPUT_JOB_SUMMARY -e HOME -e GITHUB_JOB -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_REPOSITORY_OWNER -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_RETENTION_DAYS -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_SERVER_URL -e GITHUB_API_URL -e GITHUB_GRAPHQL_URL -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e GITHUB_ACTION_REPOSITORY -e GITHUB_ACTION_REF -e GITHUB_PATH -e GITHUB_ENV -e GITHUB_STEP_SUMMARY -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "$RUNNER_TEMP":"$RUNNER_TEMP" -v "$GITHUB_WORKSPACE":"$GITHUB_WORKSPACE" enricomi/publish-unit-test-result-action:latest
env:
INPUT_GITHUB_TOKEN: ${{ github.token }}
INPUT_CHECK_NAME: Unit Test Results (Docker Image)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ See the complete list of options below.
|`check_name`|`"Unit Test Results"`|An alternative name for the check result.|
|`comment_title`|same as `check_name`|An alternative name for the pull request comment.|
|`comment_mode`|`update last`|The action posts comments to a pull request that is associated with the commit. Set to `create new` to create a new comment on each commit, `update last` to create only one comment and update later on, `off` to not create pull request comments.|
|`job_summary`|`true`| Set to `true` to publish summary as Job Step Summary, can be used in addition or replacement of pull request decoration|
|`hide_comments`|`"all but latest"`|Configures which earlier comments in a pull request are hidden by the action:<br/>`"orphaned commits"` - comments for removed commits<br/>`"all but latest"` - all comments but the latest<br/>`"off"` - no hiding|
|`github_token`|`${{github.token}}`|An alternative GitHub token, other than the default provided by GitHub Actions runner.|
|`github_retries`|`10`|Requests to the GitHub API are retried this number of times. The value must be a positive integer or zero.|
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ inputs:
json_file:
description: 'Results are written to this JSON file.'
required: false
job_summary:
description: 'Set to `true` to publish summary as Job Step Summary, can be used in addition or replacement of pull request decoration'
required: false
lachaib marked this conversation as resolved.
Show resolved Hide resolved
default: 'true'
runs:
using: 'docker'
image: 'docker://ghcr.io/enricomi/publish-unit-test-result-action:v1.34'
Expand Down
5 changes: 5 additions & 0 deletions composite/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ inputs:
json_file:
description: 'Results are written to this JSON file.'
required: false
job_summary:
description: 'Set to `true` to publish summary as Job Step Summary, can be used in addition or replacement of pull request decoration'
required: false
default: 'true'
runs:
using: 'composite'
steps:
Expand Down Expand Up @@ -152,6 +156,7 @@ runs:
SECONDS_BETWEEN_GITHUB_READS: ${{ inputs.seconds_between_github_reads }}
SECONDS_BETWEEN_GITHUB_WRITES: ${{ inputs.seconds_between_github_writes }}
JSON_FILE: ${{ inputs.json_file }}
JOB_SUMMARY: ${{ inputs.job_summary }}
ROOT_LOG_LEVEL: ${{ inputs.root_log_level }}
LOG_LEVEL: ${{ inputs.log_level }}
shell: bash
Expand Down
12 changes: 12 additions & 0 deletions python/publish/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class Settings:
check_run_annotation: List[str]
seconds_between_github_reads: float
seconds_between_github_writes: float
job_summary: bool = True
job_summary_file: Optional[str] = None



@dataclasses.dataclass(frozen=True)
Expand Down Expand Up @@ -252,6 +255,7 @@ def publish_check(self,

title = get_short_summary(stats)
summary = get_long_summary_md(stats_with_delta)
self.publish_summary(summary)

# create full json
data = PublishData(
Expand Down Expand Up @@ -305,6 +309,14 @@ def publish_json(self, data: PublishData):
# provide a reduced version to Github actions
self._gha.set_output('json', json.dumps(data.reduced(), ensure_ascii=False))

def publish_summary(self, summary: str):
if self._settings.job_summary_file:
try:
with open(self._settings.job_summary_file, 'a', encoding='utf-8') as summary_file:
summary_file.write(summary)
except Exception as e:
self._gha.error(f'Failed to write summary file {self._settings.job_summary_file}: {str(e)}')

@staticmethod
def get_test_lists_from_check_run(check_run: Optional[CheckRun]) -> Tuple[Optional[List[str]], Optional[List[str]]]:
if check_run is None:
Expand Down
7 changes: 6 additions & 1 deletion python/publish_unit_test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ def get_settings(options: dict, gha: Optional[GithubAction] = None) -> Settings:
ignore_runs=get_bool_var('IGNORE_RUNS', options, default=False, gha=gha),
check_run_annotation=annotations,
seconds_between_github_reads=float(seconds_between_github_reads),
seconds_between_github_writes=float(seconds_between_github_writes)
seconds_between_github_writes=float(seconds_between_github_writes),
job_summary=get_bool_var('JOB_SUMMARY', options, default=True, gha=gha),
job_summary_file=options.get('GITHUB_STEP_SUMMARY')
)

check_var(settings.token, 'GITHUB_TOKEN', 'GitHub token')
lachaib marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -312,6 +314,9 @@ def get_settings(options: dict, gha: Optional[GithubAction] = None) -> Settings:

deprecate_var(get_var('COMMENT_ON_PR', options) or None, 'COMMENT_ON_PR', 'Instead, use option "comment_mode" with values "off", "create new", or "update last".', gha)

if settings.job_summary:
check_var_condition(settings.job_summary_file, f'GITHUB_STEP_SUMMARY must point to a writeable file when JOB_SUMMARY is true')

return settings


Expand Down
8 changes: 6 additions & 2 deletions python/test/test_action_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ def get_settings(token='token',
ignore_runs=ignore_runs,
check_run_annotation=check_run_annotation.copy(),
seconds_between_github_reads=seconds_between_github_reads,
seconds_between_github_writes=seconds_between_github_writes
seconds_between_github_writes=seconds_between_github_writes,
job_summary=False,
job_summary_file=None,
)

def test_get_settings(self):
Expand Down Expand Up @@ -450,6 +452,7 @@ def do_test_get_settings(self,
# annotations config tested in test_get_annotations_config*
SECONDS_BETWEEN_GITHUB_READS='1.5',
SECONDS_BETWEEN_GITHUB_WRITES='2.5',
JOB_SUMMARY='false'
)
options.update(**kwargs)
for arg in kwargs:
Expand Down Expand Up @@ -791,7 +794,8 @@ def test_main_fork_pr_check(self):
GITHUB_EVENT_PATH=file.name,
GITHUB_EVENT_NAME='pull_request',
GITHUB_REPOSITORY='repo',
EVENT_FILE=None
EVENT_FILE=None,
JOB_SUMMARY="false"
), gha)
finally:
if sys.platform == 'win32':
Expand Down
24 changes: 22 additions & 2 deletions python/test/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def create_settings(comment_mode=comment_mode_create,
event_name: str = 'event name',
json_file: Optional[str] = None,
pull_request_build: str = pull_request_build_mode_merge,
test_changes_limit: Optional[int] = 5):
test_changes_limit: Optional[int] = 5,
job_summary: Optional[bool] = False,
job_summary_file: Optional[str] = None):
return Settings(
token=None,
api_url='https://the-github-api-url',
Expand Down Expand Up @@ -81,7 +83,9 @@ def create_settings(comment_mode=comment_mode_create,
ignore_runs=False,
check_run_annotation=check_run_annotation,
seconds_between_github_reads=1.5,
seconds_between_github_writes=2.5
seconds_between_github_writes=2.5,
job_summary=job_summary,
job_summary_file=job_summary_file
)

stats = UnitTestRunResults(
Expand Down Expand Up @@ -1448,6 +1452,22 @@ def test_publish_json(self):
}
gha.set_output.assert_called_once_with('json', json.dumps(expected))

def test_publish_summary(self):
with tempfile.TemporaryDirectory() as path:
filepath = os.path.join(path, 'summary.md')
settings = self.create_settings(job_summary=True, job_summary_file=filepath)
gh, gha, req, repo, commit = self.create_mocks(digest=self.base_digest, check_names=[settings.check_name])
publisher = Publisher(settings, gh, gha)

summary = 'Well, this summary is not very interesting'

publisher.publish_summary(summary)

with open(filepath, 'r') as summary_file:
contents = summary_file.read()
self.assertEqual(contents, summary)


def test_publish_comment(self):
settings = self.create_settings(event={'pull_request': {'base': {'sha': 'commit base'}}}, event_name='pull_request')
base_commit = 'base-commit'
Expand Down