|
1 | | -name: Convert PNG to WebP, Update Markdown, and Comment |
2 | | - |
| 1 | +name: Convert PNG to WebP, Update Markdown Refs, and Comment |
3 | 2 | on: |
4 | 3 | push: |
5 | 4 | branches: ['**'] |
| 5 | + workflow_dispatch: |
6 | 6 | pull_request: |
7 | | - |
8 | 7 | jobs: |
9 | 8 | convert-images: |
10 | 9 | runs-on: ubuntu-latest |
11 | | - |
12 | 10 | steps: |
13 | 11 | - name: Checkout code |
14 | 12 | uses: actions/checkout@v4 |
15 | | - |
16 | 13 | - name: Install cwebp |
17 | 14 | run: sudo apt-get update && sudo apt-get install -y webp |
18 | | - |
19 | 15 | - name: Convert and Replace |
20 | 16 | id: convert |
21 | 17 | run: | |
22 | 18 | echo "converted_images=" >> $GITHUB_OUTPUT |
23 | 19 | echo "updated_docs=" >> $GITHUB_OUTPUT |
24 | | -
|
25 | 20 | mkdir -p .tmp_logs |
26 | 21 | touch .tmp_logs/converted.txt .tmp_logs/updated.txt |
27 | | -
|
28 | 22 | find . -type f -name "*.png" ! -name "*.webp.png" | while read img; do |
29 | 23 | webp="${img%.png}.webp" |
30 | 24 | echo "Converting $img → $webp" |
31 | 25 | cwebp -q 80 "$img" -o "$webp" && rm "$img" && echo "$img → $webp (deleted PNG)" >> .tmp_logs/converted.txt |
32 | | -
|
33 | 26 | escaped_img=$(printf '%s\n' "$img" | sed 's|[][\.*^$(){}+?|]|\\&|g') |
34 | 27 | escaped_webp=$(printf '%s\n' "$webp" | sed 's|[][\.*^$(){}+?|]|\\&|g') |
35 | | -
|
36 | 28 | grep -rl --include="*.md" "$img" . | while read md; do |
37 | 29 | sed -i "s|$escaped_img|$escaped_webp|g" "$md" && echo "$md" >> .tmp_logs/updated.txt |
38 | 30 | done |
39 | 31 | done |
40 | | -
|
41 | 32 | echo "converted_images<<EOF" >> $GITHUB_OUTPUT |
42 | 33 | cat .tmp_logs/converted.txt >> $GITHUB_OUTPUT |
43 | 34 | echo "EOF" >> $GITHUB_OUTPUT |
44 | | -
|
45 | 35 | echo "updated_docs<<EOF" >> $GITHUB_OUTPUT |
46 | 36 | sort -u .tmp_logs/updated.txt >> $GITHUB_OUTPUT |
47 | 37 | echo "EOF" >> $GITHUB_OUTPUT |
48 | | -
|
49 | 38 | - name: Commit changes |
50 | 39 | run: | |
51 | 40 | git config user.name "github-actions" |
52 | 41 | git config user.email "[email protected]" |
53 | 42 | git add '**/*.webp' '**/*.md' |
54 | 43 | git commit -m "Convert PNGs to WebP, delete originals, and update markdown references" || echo "No changes to commit" |
55 | 44 | git push || echo "No changes to push" |
56 | | -
|
57 | 45 | - name: Comment on PR or commit |
58 | 46 | env: |
59 | 47 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
60 | 48 | run: | |
61 | 49 | CONVERTED="${{ steps.convert.outputs.converted_images }}" |
62 | 50 | UPDATED="${{ steps.convert.outputs.updated_docs }}" |
63 | | -
|
64 | | - BODY="### 🖼️ PNG to WebP Conversion Report |
65 | | -
|
66 | | -**Converted & Deleted:** |
67 | | -\`\`\` |
68 | | -$CONVERTED |
69 | | -\`\`\` |
70 | | - |
71 | | -**Markdown Files Updated:** |
72 | | -\`\`\` |
73 | | -$UPDATED |
74 | | -\`\`\` |
75 | | -" |
76 | | -
|
| 51 | + |
| 52 | + # Create comment body using heredoc |
| 53 | + BODY=$(cat <<'EOF' |
| 54 | + ### 🖼️ PNG to WebP Conversion Report |
| 55 | + |
| 56 | + **Converted & Deleted:** |
| 57 | + ``` |
| 58 | + CONVERTED_PLACEHOLDER |
| 59 | + ``` |
| 60 | + |
| 61 | + **Markdown Files Updated:** |
| 62 | + ``` |
| 63 | + UPDATED_PLACEHOLDER |
| 64 | + ``` |
| 65 | + EOF |
| 66 | + ) |
| 67 | + |
| 68 | + # Replace placeholders with actual values |
| 69 | + BODY="${BODY//CONVERTED_PLACEHOLDER/$CONVERTED}" |
| 70 | + BODY="${BODY//UPDATED_PLACEHOLDER/$UPDATED}" |
| 71 | + |
77 | 72 | if [ "${{ github.event_name }}" = "pull_request" ]; then |
78 | 73 | PR_URL="${{ github.event.pull_request.comments_url }}" |
79 | 74 | else |
80 | 75 | PR_URL="https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}/comments" |
81 | 76 | fi |
82 | | - |
| 77 | + |
83 | 78 | echo "$BODY" | jq -Rs '{body: .}' | curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/json" -d @- "$PR_URL" |
0 commit comments