wesnoth/.github/workflows/map-diff.yml
Gunter Labes 6f085ffa6d
Avoid indentation so the embedded html is parsed right
Use one html line per map.
2025-03-10 23:13:41 +01:00

85 lines
3.2 KiB
YAML

name: Map Diff
on:
pull_request_target:
paths:
- '**.map'
jobs:
comment-map-diff:
permissions:
pull-requests: write
continue-on-error: true
runs-on: ubuntu-latest
defaults:
run:
working-directory: utils/wesnoth-map-diff
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16'
- name: Package install
run: npm install
- name: Package build
run: npm run build:prod
- name: Get maps diff
id: get-maps-diff
env:
SHA: ${{ github.event.pull_request.head.sha }}
run: |
sudo apt-get -y -q install pngquant imagemagick
comment_body=""
log() { printf '\e[1m%s\e[m\n' "$*"; } # write log message in bold
err() { printf '\e[1;31m%s\e[m\n' "$*"; exit 1; } # write error message in bold red and exit
dim() { tr -cd ',\n' < "$1" | awk 'p {if (p!=length) f=1} {p=length} END {if (f) print "not a rectangle!"; w=p-1; h=NR-2; printf "%dx%d\n", w, h}'; }
## Get changed maps
git fetch --depth=1 origin "$SHA"
mapfile -t map_paths < <(git diff --name-only HEAD "$SHA" | grep '\.map$')
for map_path in "${map_paths[@]}"
do
## Get new map version
log "Check out $map_path from $SHA..."
new_map=${map_path##*/}
git show "$SHA":"$map_path" > "$new_map"
old_dim=$(dim "../../$map_path")
new_dim=$(dim "$new_map")
log "Generate map diff image for $map_path..."
node build/index.js "../../$map_path" "$new_map" diff_image.png
identify diff_image.png
log 'Compress image...'
pngquant --force --output diff_image.png diff_image.png
identify diff_image.png
log 'Uploading diff_image.png...'
# https://api.imgur.com/endpoints/image/#image-upload https://apidocs.imgur.com/#c85c9dfc-7487-4de2-9ecd-66f727cf3139
json=$(curl -s -F image=@diff_image.png https://api.imgur.com/3/upload | tee /dev/stderr)
log 'Generating HTML comment...'
# careful with leading spaces in the output since it is ultimately markdown (with embedded html) so the indentation may have meaning
html=$(printf %s "$json" | jq -rRs --arg path "$map_path" --arg dim "$old_dim ➔ $new_dim" '. as $input |
try fromjson catch ({error:$input}) | (if .data.link then "map diff image" else "image upload failed" end) as $alt |
@html "<h3>\($path)</h3><p>dimensions: \($dim)</p><img src=\"\(.data.link)\" alt=\"\($alt)\"/><br/><details><summary>imgur response</summary><p>\(tojson)</p></details>"')
comment_body+=$html$'\n'
done
[[ $comment_body ]] || err Failed to generate comment
printf '%s\n' "$comment_body" > body.html
- name: Find comment
uses: peter-evans/find-comment@v3
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
- name: Add comment
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.pull_request.number }}
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace
body-path: utils/wesnoth-map-diff/body.html