Skip to content

Commit

Permalink
Handle case when test reports are missing timestamp and file (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmilosevic authored Nov 29, 2024
1 parent df3a078 commit eea8fd5
Show file tree
Hide file tree
Showing 6 changed files with 2,831 additions and 11 deletions.
16 changes: 9 additions & 7 deletions .github/actions/collect_data/download_workflow_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ set_up_dirs() {
mkdir -p generated/cicd/$workflow_run_id/logs
}

# Download artifacts for the given run id
# Test report artfacts must include "report" in their name
download_artifacts() {
if gh api --paginate /repos/$REPOSITORY/actions/runs/$RUN_ID/artifacts | jq '.artifacts[] | .name' | grep -q "test-reports-*"; then
echo "[Info] Downloading artifacts $attempt_number"
gh run download --repo $REPOSITORY -D generated/cicd/$RUN_ID/artifacts --pattern "test-reports-*" $RUN_ID
else
echo "[Warning] Test reports not found for workflow run $RUN_ID"
fi
for artifact in $(gh api --paginate /repos/$REPOSITORY/actions/runs/$RUN_ID/artifacts | jq '.artifacts[] | .name' | grep report); do
artifact=${artifact//\"/} # strip quotes
echo "[Info] Downloading artifacts $artifact"
gh run download --repo $REPOSITORY -D generated/cicd/$RUN_ID/artifacts --name $artifact $RUN_ID
done
}

download_logs_for_all_jobs() {
Expand All @@ -55,7 +56,8 @@ download_logs_for_all_jobs() {

set_up_dirs "$RUN_ID"
download_artifacts "$REPOSITORY" "$RUN_ID"
download_logs_for_all_jobs "$REPOSITORY" "$RUN_ID" "$ATTEMPT_NUMBER"
# Note: we don't need information from logs yet
# download_logs_for_all_jobs "$REPOSITORY" "$RUN_ID" "$ATTEMPT_NUMBER"
gh api /repos/$REPOSITORY/actions/runs/$RUN_ID/attempts/$ATTEMPT_NUMBER > generated/cicd/$RUN_ID/workflow.json
gh api /repos/$REPOSITORY/actions/runs/$RUN_ID/attempts/$ATTEMPT_NUMBER/jobs --paginate | jq -s '{total_count: .[0].total_count, jobs: map(.jobs) | add}' > generated/cicd/$RUN_ID/workflow_jobs.json

Expand Down
17 changes: 14 additions & 3 deletions .github/actions/collect_data/src/parsers/python_unittest_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0
import xmltodict
from loguru import logger
from pydantic_models import Test
from datetime import datetime, timedelta
from .parser import Parser
Expand All @@ -23,6 +24,15 @@ def get_tests(test_report_path):
data = f.read()
dict_data = xmltodict.parse(data)
previous_test_end_ts = None

# Workaround: If test info does not have timestamp, use current time
if "@timestamp" not in dict_data["testsuites"]["testsuite"][0]["testcase"][0]:
previous_test_end_ts = datetime.now().isoformat()
logger.warning("Timestamp not found in test report. Using current time.")

if "@file" not in dict_data["testsuites"]["testsuite"][0]["testcase"][0]:
logger.warning("Filepath not found in test report.")

for testsuite in dict_data["testsuites"]["testsuite"]:

# testcases can be dict or list
Expand All @@ -32,11 +42,12 @@ def get_tests(test_report_path):

for testcase in testcases:
message = None
test_start_ts = testcase["@timestamp"]
test_start_ts = testcase.get("@timestamp", previous_test_end_ts)
duration = testcase["@time"]
skipped = testcase.get("skipped", False)
error = testcase.get("error", False)
failure = testcase.get("failure", False)
file = testcase.get("@file", "")
if skipped:
message = testcase["skipped"]["@message"]
if error:
Expand All @@ -57,7 +68,7 @@ def get_tests(test_report_path):
test_start_ts=test_start_ts,
test_end_ts=test_end_ts,
test_case_name=testcase["@name"],
filepath=testcase["@file"],
filepath=file,
category=testcase["@classname"],
group="unittest",
owner=None,
Expand All @@ -69,7 +80,7 @@ def get_tests(test_report_path):
error_message=message,
success=not (error or failure),
skipped=bool(skipped),
full_test_name=f"{testcase['@file']}::{testcase['@name']}",
full_test_name=f"{file}::{testcase['@name']}",
config=None,
tags=None,
)
Expand Down
Loading

0 comments on commit eea8fd5

Please sign in to comment.