Skip to content

Commit

Permalink
CI: generate zip preview on edits and issue comments
Browse files Browse the repository at this point in the history
re-use comment-id if the zipfile sha1sum did not change to prevent duplicate comments
  • Loading branch information
theofficialgman committed Aug 5, 2023
1 parent a9a3669 commit 80e0288
Showing 1 changed file with 72 additions and 6 deletions.
78 changes: 72 additions & 6 deletions .github/workflows/zip-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@ name: Zip Preview

on:
issues:
types: opened
types: [opened, edited]
issue_comment:
types: [created, edited]

permissions:
issues: write

jobs:
zip_preview:
if: contains(github.event.issue.labels.*.name, 'Zip/PR included')
zip_preview_issue:
if: |
contains(github.event_name, 'issues') &&
contains(github.event.issue.labels.*.name, 'Zip/PR included')
runs-on: ubuntu-latest
steps:
- id: zip_preview
- name: Generate Preview Content
id: zip_preview
run: |
zip_url="$(echo "${{ github.event.issue.body }}" | sed -n '/### Upload file or Add PR Link/,/###/p' | grep -v "###" | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*.zip")"
if [ -z "$zip_url" ]; then
exit 0
else
wget -O download.zip "$zip_url"
echo "sha1=$(sha1sum download.zip)" >> $GITHUB_OUTPUT
unzip download.zip
rm -f download.zip
Expand All @@ -34,14 +40,74 @@ jobs:
echo '' >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
fi
- name: Add zip preview comment
- name: Find Comment
uses: peter-evans/find-comment@v2
if: steps.zip_preview.outputs.zip_preview
id: fc
with:
issue-number: ${{ github.event.issue.number }}
comment-author: 'github-actions[bot]'
body-includes: "The sha1sum of the zip was: ${{ steps.zip_preview.outputs.sha1 }}"
- name: Add zip preview comment for issue
uses: peter-evans/create-or-update-comment@v3
if: steps.zip_preview.outputs.zip_preview
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.issue.number }}
body: |
A zipfile was found in the body of your issue.
The sha1sum of the zip was: ${{ steps.zip_preview.outputs.sha1 }}
The contents can be previewed below:
${{ steps.zip_preview.outputs.zip_preview }}
zip_preview_issue_comment:
if: |
contains(github.event_name, 'issue_comment') &&
contains(github.event.issue.labels.*.name, 'Zip/PR included') &&
${{ !github.event.issue.pull_request }}
runs-on: ubuntu-latest
steps:
- name: Generate Preview Content
id: zip_preview
run: |
zip_url="$(echo "${{ github.event.comment.body }}" | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*.zip")"
if [ -z "$zip_url" ]; then
exit 0
else
wget -O download.zip "$zip_url"
echo "sha1=$(sha1sum download.zip)" >> $GITHUB_OUTPUT
unzip download.zip
rm -f download.zip
files="$(find ./ -not -name '*.png' -type f | sed 's|^./||')"
echo "zip_preview<<EOF" >> $GITHUB_OUTPUT
IFS=$'\n'
for file in $files; do
echo '`'"$file"'`' >> $GITHUB_OUTPUT
echo '```' >> $GITHUB_OUTPUT
cat "$file" >> $GITHUB_OUTPUT
echo $'\n''```' >> $GITHUB_OUTPUT
done
echo '' >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
fi
- name: Find Comment
uses: peter-evans/find-comment@v2
if: steps.zip_preview.outputs.zip_preview
id: fc
with:
issue-number: ${{ github.event.issue.number }}
comment-author: 'github-actions[bot]'
body-includes: "The sha1sum of the zip was: ${{ steps.zip_preview.outputs.sha1 }}"
- name: Add zip preview comment for issue comment
uses: peter-evans/create-or-update-comment@v3
if: steps.zip_preview.outputs.zip_preview
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.issue.number }}
body: |
A zipfile was found in the body of an issue comment.
The sha1sum of the zip was: ${{ steps.zip_preview.outputs.sha1 }}
The contents can be previewed below:
${{ steps.zip_preview.outputs.zip_preview }}

0 comments on commit 80e0288

Please sign in to comment.