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
| name: CodeState PR Report | ||
| on: | ||
| push: | ||
| branches: [ main ] | ||
| pull_request: | ||
| types: [opened, synchronize, reopened, ready_for_review] | ||
| workflow_dispatch: | ||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
| jobs: | ||
| pr-report: | ||
| name: Generate PR Code Report | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
| - name: Fetch gh-pages branch (if exists) | ||
| uses: actions/checkout@v4 | ||
| continue-on-error: true | ||
| with: | ||
| ref: gh-pages | ||
| path: ghp | ||
| - name: Set up Python | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.x' | ||
| - name: Generate PR report | ||
| env: | ||
| BASE_SHA: ${{ github.event.pull_request.base.sha || '' }} | ||
| HEAD_SHA: ${{ github.sha }} | ||
| run: | | ||
| python tools/codestate_pr_report.py | ||
| - name: Build Pages content (index + history) | ||
| env: | ||
| PR_NUMBER: ${{ github.event.pull_request.number || '' }} | ||
| GITHUB_SHA: ${{ github.sha }} | ||
| run: | | ||
| set -e | ||
| SHORT_SHA=$(git rev-parse --short "$GITHUB_SHA") | ||
| COMMIT_TIME=$(git show -s --format=%cI "$GITHUB_SHA" || date -u +%Y-%m-%dT%H:%M:%SZ) | ||
| COMMIT_MSG=$(git show -s --format=%s "$GITHUB_SHA" || echo "") | ||
| # Truncate commit message for display (80 chars max) | ||
| COMMIT_MSG_SHORT=$(printf '%s' "$COMMIT_MSG" | head -c 80) | ||
| if [ ${#COMMIT_MSG} -gt 80 ]; then COMMIT_MSG_SHORT="$COMMIT_MSG_SHORT…"; fi | ||
| OUTDIR="site" | ||
| mkdir -p "$OUTDIR" | ||
| # Bring over existing reports so we can publish with keep_files=false safely | ||
| if [ -d ghp/reports ]; then | ||
| mkdir -p "$OUTDIR/reports" | ||
| cp -R ghp/reports/* "$OUTDIR/reports/" || true | ||
| fi | ||
| # Decide sub path | ||
| if [ -n "$PR_NUMBER" ]; then | ||
| SUBPATH="reports/pr-${PR_NUMBER}/sha-${SHORT_SHA}" | ||
| else | ||
| SUBPATH="reports/manual/sha-${SHORT_SHA}" | ||
| fi | ||
| mkdir -p "$OUTDIR/$SUBPATH" | ||
| # Copy report as index.html for direct open | ||
| if [ -f codestate_pr_report.html ]; then | ||
| cp codestate_pr_report.html "$OUTDIR/$SUBPATH/index.html" | ||
| else | ||
| echo "<html><body><h3>No report found</h3></body></html>" > "$OUTDIR/$SUBPATH/index.html" | ||
| fi | ||
| # Copy JSON for programmatic access | ||
| if [ -f codestate_pr_report.json ]; then | ||
| cp codestate_pr_report.json "$OUTDIR/$SUBPATH/codestate_pr_report.json" | ||
| fi | ||
| # Write metadata for this run | ||
| COMMIT_MSG_ESC=$(printf '%s' "$COMMIT_MSG" | sed 's/"/\\"/g') | ||
| cat > "$OUTDIR/$SUBPATH/metadata.json" << META | ||
| {"pr_number":"$PR_NUMBER","short_sha":"$SHORT_SHA","sha":"$GITHUB_SHA","time":"$COMMIT_TIME","message":"$COMMIT_MSG_ESC"} | ||
| META | ||
| # Build history list from existing content in OUTDIR (copied + current) | ||
| HISTORY_FILE_MD="$OUTDIR/history.md" | ||
| echo "# CodeState Report History" > "$HISTORY_FILE_MD" | ||
| echo >> "$HISTORY_FILE_MD" | ||
| echo "Browse all published reports. Newest first." >> "$HISTORY_FILE_MD" | ||
| echo >> "$HISTORY_FILE_MD" | ||
| # Collect existing entries | ||
| if [ -d "$OUTDIR/reports" ]; then | ||
| EXISTING_INDEXES=$(find "$OUTDIR/reports" -type f -name index.html | sort -r) | ||
| while IFS= read -r path; do | ||
| rel=${path#${OUTDIR}/} | ||
| # rel like reports/pr-123/sha-abc/index.html | ||
| dir=$(dirname "$rel") | ||
| base=$(basename "$dir") | ||
| parent=$(basename "$(dirname "$dir")") | ||
| if [[ "$parent" == pr-* ]]; then | ||
| prnum=${parent#pr-} | ||
| label="PR #$prnum ($base)" | ||
| else | ||
| label="Manual ($base)" | ||
| fi | ||
| # Try to read metadata for time and full sha | ||
| TIME=""; FULL_SHA=""; MSG=""; | ||
| if [ -f "$OUTDIR/$dir/metadata.json" ]; then | ||
| TIME=$(sed -n 's/.*"time"[[:space:]]*:[[:space:]]*"\(.*\)".*/\1/p' "$OUTDIR/$dir/metadata.json" | head -n1) | ||
| FULL_SHA=$(sed -n 's/.*"sha"[[:space:]]*:[[:space:]]*"\(.*\)".*/\1/p' "$OUTDIR/$dir/metadata.json" | head -n1) | ||
| MSG=$(sed -n 's/.*"message"[[:space:]]*:[[:space:]]*"\(.*\)".*/\1/p' "$OUTDIR/$dir/metadata.json" | head -n1) | ||
| fi | ||
| short=${base#sha-} | ||
| cmd="git checkout ${FULL_SHA:-$short}" | ||
| # Truncate message for display | ||
| MSG_SHORT=$(printf '%s' "$MSG" | head -c 80) | ||
| if [ ${#MSG} -gt 80 ]; then MSG_SHORT="$MSG_SHORT…"; fi | ||
| if [ -n "$TIME" ] && [ -n "$MSG_SHORT" ]; then | ||
| echo "- [$label]($dir/) — $TIME — \`$cmd\` — $MSG_SHORT" >> "$HISTORY_FILE_MD" | ||
| elif [ -n "$TIME" ]; then | ||
| echo "- [$label]($dir/) — $TIME — \`$cmd\`" >> "$HISTORY_FILE_MD" | ||
| else | ||
| echo "- [$label]($dir/) — \`$cmd\`" >> "$HISTORY_FILE_MD" | ||
| fi | ||
| done <<< "$EXISTING_INDEXES" | ||
| fi | ||
| # Add current run at the top | ||
| echo "- [Current Run]($SUBPATH/) — $COMMIT_TIME — \`git checkout $GITHUB_SHA\` — $COMMIT_MSG_SHORT" | cat - "$HISTORY_FILE_MD" > "$HISTORY_FILE_MD.tmp" && mv "$HISTORY_FILE_MD.tmp" "$HISTORY_FILE_MD" | ||
| # Build index.md from root README plus quick links | ||
| INDEX_MD="$OUTDIR/index.md" | ||
| echo "<!-- Auto-generated by workflow -->" > "$INDEX_MD" | ||
| echo "[Open latest report]($SUBPATH/) · [History](history.html)" >> "$INDEX_MD" | ||
| echo >> "$INDEX_MD" | ||
| echo >> "$INDEX_MD" | ||
| if [ -f README.md ]; then | ||
| cat README.md >> "$INDEX_MD" | ||
| else | ||
| echo "# CodeState\n\nSee [history](history.html) for all reports." >> "$INDEX_MD" | ||
| fi | ||
| - name: Publish to gh-pages | ||
| uses: peaceiris/actions-gh-pages@v3 | ||
| with: | ||
| github_token: ${{ secrets.GITHUB_TOKEN }} | ||
| publish_branch: gh-pages | ||
| publish_dir: ./site | ||
| enable_jekyll: true | ||
| keep_files: false | ||
| - name: Upload HTML artifact | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: codestate-pr-report | ||
| path: | | ||
| codestate_pr_report.html | ||
| codestate_pr_report.json | ||
| if-no-files-found: warn | ||
| - name: Create or update PR comment | ||
| if: github.event_name == 'pull_request' | ||
| uses: marocchino/sticky-pull-request-comment@v2 | ||
| with: | ||
| header: codestate-pr-report | ||
| path: codestate_pr_report.md | ||