update #8
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") | |
| 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 | |
| # 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 | |
| echo "- [$label](/$dir/)" >> "$HISTORY_FILE_MD" | |
| done <<< "$EXISTING_INDEXES" | |
| fi | |
| # Add current run at the top | |
| echo "- [Current Run](/$SUBPATH/)" | 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) 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 |