-
Notifications
You must be signed in to change notification settings - Fork 0
ci(scan-logs): attach pipeline annotation if concerning logs are found #46
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
76b9a2f
ci(scan-logs): attach pipeline annotation if concerning logs are found
JonZeolla 28b209b
good bot
JonZeolla afd9753
Fix scaript name
JonZeolla fb67257
Merge branch 'main' into add-deprecated-monitor
JonZeolla b75fbdf
Fixup
JonZeolla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| # Check if run ID is provided as argument | ||
| if [[ $# -eq 1 ]]; then | ||
| GITHUB_RUN_ID="$1" | ||
| elif [[ -z "${GITHUB_RUN_ID:-}" ]]; then | ||
| echo "Usage: $0 <run_id>" | ||
| echo "Or set GITHUB_RUN_ID environment variable" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Ensure required environment variables | ||
| : "${GITHUB_REPOSITORY:?Missing GITHUB_REPOSITORY}" | ||
| : "${GITHUB_TOKEN:?Missing GITHUB_TOKEN}" | ||
|
|
||
| # Use the GH CLI with auth | ||
| export GH_TOKEN="$GITHUB_TOKEN" | ||
|
|
||
| # Create a temp dir for logs | ||
| tmpdir=$(mktemp -d) | ||
| trap 'rm -rf "$tmpdir"' EXIT | ||
|
|
||
| cd "$tmpdir" | ||
|
|
||
| # Get list of jobs in the run | ||
| echo "Fetching jobs for run $GITHUB_RUN_ID..." | ||
| jobs_json=$(gh api \ | ||
| -H "Accept: application/vnd.github+json" \ | ||
| "/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/jobs" \ | ||
| --paginate) | ||
|
|
||
| # Extract completed job IDs (excluding the current job if GITHUB_JOB is set and always excluding "Test Scripts") | ||
| if [[ -n "${GITHUB_JOB:-}" ]]; then | ||
| completed_jobs=$(echo "$jobs_json" | jq -r --arg current_job "$GITHUB_JOB" '.jobs[] | select(.status == "completed") | select(.name != $current_job) | select(.name != "Test Scripts") | .id') | ||
| else | ||
| completed_jobs=$(echo "$jobs_json" | jq -r '.jobs[] | select(.status == "completed") | select(.name != "Test Scripts") | .id') | ||
| fi | ||
|
|
||
| if [[ -z "$completed_jobs" ]]; then | ||
| echo "::warning::No completed jobs found to scan" | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Download logs for each completed job | ||
| echo "Downloading logs for completed jobs..." | ||
| for job_id in $completed_jobs; do | ||
| job_name=$(echo "$jobs_json" | jq -r --arg id "$job_id" '.jobs[] | select(.id == ($id | tonumber)) | .name') | ||
| echo " Downloading logs for job: $job_name (ID: $job_id)" | ||
|
|
||
| # Create directory for this job's logs | ||
| mkdir -p "$job_name" | ||
|
|
||
| # Download the job's log | ||
| if gh api \ | ||
| -H "Accept: application/vnd.github+json" \ | ||
| "/repos/${GITHUB_REPOSITORY}/actions/jobs/${job_id}/logs" \ | ||
| --method GET > "$job_name/log.txt" 2>/dev/null; then | ||
| echo " ✓ Downloaded $(wc -l < "$job_name/log.txt") lines" | ||
| else | ||
| echo " ✗ Failed to download logs" | ||
| rm -rf "$job_name" | ||
| fi | ||
| done | ||
|
|
||
| # Scan for lines with "deprecated", "warning:", or "error" | ||
| echo "Scanning logs for warnings, deprecations, and errors..." | ||
|
|
||
| # Process each log file and collect results | ||
| results_file=$(mktemp) | ||
|
|
||
| find . -type f -name "*.txt" | while IFS= read -r logfile; do | ||
| # Get the job name from the directory structure | ||
| job_name=$(dirname "$logfile" | sed 's|^\./||' | cut -d'/' -f1) | ||
|
|
||
| # Search for patterns with context | ||
| while IFS= read -r line; do | ||
| # Extract line number and content | ||
| line_num=$(echo "$line" | cut -d: -f2) | ||
| content=$(echo "$line" | cut -d: -f3-) | ||
|
|
||
| # Sanitize content to prevent command injection and log poisoning | ||
| sanitized_content=$(echo "$content" | tr -d '\n\r' | cut -c1-200) | ||
|
|
||
| # Determine the type of issue and output both annotation and count | ||
| if echo "$content" | grep -qiE '\berror\b'; then | ||
| echo "::error file=$job_name,line=$line_num::$sanitized_content" | ||
| echo "error" >> "$results_file" | ||
| elif echo "$content" | grep -qiE '\bwarning:'; then | ||
| echo "::warning file=$job_name,line=$line_num::$sanitized_content" | ||
| echo "warning" >> "$results_file" | ||
| elif echo "$content" | grep -qiE '\bdeprecated\b'; then | ||
| echo "::warning file=$job_name,line=$line_num::$sanitized_content" | ||
| echo "deprecated" >> "$results_file" | ||
| fi | ||
| done < <(grep -niE '(\berror\b|warning:|deprecated)' "$logfile" 2>/dev/null || true) | ||
| done | ||
|
|
||
| # Count results | ||
| error_count=$(grep -c "error" "$results_file" 2>/dev/null || echo 0) | ||
| warning_count=$(grep -c "warning" "$results_file" 2>/dev/null || echo 0) | ||
| deprecated_count=$(grep -c "deprecated" "$results_file" 2>/dev/null || echo 0) | ||
|
|
||
| # Summary | ||
| echo | ||
| echo "=== Log Scan Summary ===" | ||
| echo "Errors found: $error_count" | ||
| echo "Warnings found: $warning_count" | ||
| echo "Deprecation notices found: $deprecated_count" | ||
| echo "Total issues: $((error_count + warning_count + deprecated_count))" | ||
|
|
||
| # Clean up temp file | ||
| rm -f "$results_file" | ||
|
|
||
| # Exit with success even if issues were found | ||
| exit 0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
{{cookiecutter.project_name|replace(" ", "")}}/scripts/scan_workflow_logs.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| # Check if run ID is provided as argument | ||
| if [[ $# -eq 1 ]]; then | ||
| GITHUB_RUN_ID="$1" | ||
| elif [[ -z "${GITHUB_RUN_ID:-}" ]]; then | ||
| echo "Usage: $0 <run_id>" | ||
| echo "Or set GITHUB_RUN_ID environment variable" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Ensure required environment variables | ||
| : "${GITHUB_REPOSITORY:?Missing GITHUB_REPOSITORY}" | ||
| : "${GITHUB_TOKEN:?Missing GITHUB_TOKEN}" | ||
JonZeolla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Use the GH CLI with auth | ||
| export GH_TOKEN="$GITHUB_TOKEN" | ||
|
|
||
| # Create a temp dir for logs | ||
| tmpdir=$(mktemp -d) | ||
| trap 'rm -rf "$tmpdir"' EXIT | ||
|
|
||
| cd "$tmpdir" | ||
|
|
||
| # Get list of jobs in the run | ||
| echo "Fetching jobs for run $GITHUB_RUN_ID..." | ||
| jobs_json=$(gh api \ | ||
| -H "Accept: application/vnd.github+json" \ | ||
| "/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/jobs" \ | ||
| --paginate) | ||
|
|
||
| # Extract completed job IDs (excluding the current job if GITHUB_JOB is set and always excluding "Test Scripts") | ||
| if [[ -n "${GITHUB_JOB:-}" ]]; then | ||
| completed_jobs=$(echo "$jobs_json" | jq -r --arg current_job "$GITHUB_JOB" '.jobs[] | select(.status == "completed") | select(.name != $current_job) | select(.name != "Test Scripts") | .id') | ||
| else | ||
| completed_jobs=$(echo "$jobs_json" | jq -r '.jobs[] | select(.status == "completed") | select(.name != "Test Scripts") | .id') | ||
| fi | ||
|
|
||
| if [[ -z "$completed_jobs" ]]; then | ||
| echo "::warning::No completed jobs found to scan" | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Download logs for each completed job | ||
| echo "Downloading logs for completed jobs..." | ||
| for job_id in $completed_jobs; do | ||
| job_name=$(echo "$jobs_json" | jq -r --arg id "$job_id" '.jobs[] | select(.id == ($id | tonumber)) | .name') | ||
| echo " Downloading logs for job: $job_name (ID: $job_id)" | ||
|
|
||
| # Create directory for this job's logs | ||
| mkdir -p "$job_name" | ||
|
|
||
| # Download the job's log | ||
| if gh api \ | ||
| -H "Accept: application/vnd.github+json" \ | ||
| "/repos/${GITHUB_REPOSITORY}/actions/jobs/${job_id}/logs" \ | ||
| --method GET > "$job_name/log.txt" 2>/dev/null; then | ||
| echo " ✓ Downloaded $(wc -l < "$job_name/log.txt") lines" | ||
| else | ||
| echo " ✗ Failed to download logs" | ||
| rm -rf "$job_name" | ||
| fi | ||
| done | ||
|
|
||
| # Scan for lines with "deprecated", "warning:", or "error" | ||
| echo "Scanning logs for warnings, deprecations, and errors..." | ||
|
|
||
| # Process each log file and collect results | ||
| results_file=$(mktemp) | ||
|
|
||
| find . -type f -name "*.txt" | while IFS= read -r logfile; do | ||
| # Get the job name from the directory structure | ||
| job_name=$(dirname "$logfile" | sed 's|^\./||' | cut -d'/' -f1) | ||
|
|
||
| # Search for patterns with context | ||
| while IFS= read -r line; do | ||
| # Extract line number and content | ||
| line_num=$(echo "$line" | cut -d: -f2) | ||
| content=$(echo "$line" | cut -d: -f3-) | ||
|
|
||
| # Sanitize content to prevent command injection and log poisoning | ||
| sanitized_content=$(echo "$content" | tr -d '\n\r' | cut -c1-200) | ||
|
|
||
| # Determine the type of issue and output both annotation and count | ||
| if echo "$content" | grep -qiE '\berror\b'; then | ||
| echo "::error file=$job_name,line=$line_num::$sanitized_content" | ||
| echo "error" >> "$results_file" | ||
| elif echo "$content" | grep -qiE '\bwarning:'; then | ||
| echo "::warning file=$job_name,line=$line_num::$sanitized_content" | ||
| echo "warning" >> "$results_file" | ||
| elif echo "$content" | grep -qiE '\bdeprecated\b'; then | ||
| echo "::warning file=$job_name,line=$line_num::$sanitized_content" | ||
| echo "deprecated" >> "$results_file" | ||
| fi | ||
| done < <(grep -niE '(\berror\b|warning:|deprecated)' "$logfile" 2>/dev/null || true) | ||
| done | ||
|
|
||
| # Count results | ||
| error_count=$(grep -c "error" "$results_file" 2>/dev/null || echo 0) | ||
| warning_count=$(grep -c "warning" "$results_file" 2>/dev/null || echo 0) | ||
| deprecated_count=$(grep -c "deprecated" "$results_file" 2>/dev/null || echo 0) | ||
|
|
||
| # Summary | ||
| echo | ||
| echo "=== Log Scan Summary ===" | ||
| echo "Errors found: $error_count" | ||
| echo "Warnings found: $warning_count" | ||
| echo "Deprecation notices found: $deprecated_count" | ||
| echo "Total issues: $((error_count + warning_count + deprecated_count))" | ||
|
|
||
| # Clean up temp file | ||
| rm -f "$results_file" | ||
|
|
||
| # Exit with success even if issues were found | ||
| exit 0 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.