Comment on the pull request #862
This file contains 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: Comment on the pull request | |
on: | |
workflow_run: | |
workflows: | |
- ScreenShotTest | |
types: | |
- completed | |
jobs: | |
comment: | |
runs-on: ubuntu-latest | |
if: > | |
github.event.workflow_run.event == 'pull_request' && | |
github.event.workflow_run.conclusion == 'success' | |
steps: | |
- name: 'Download artifact' | |
uses: actions/[email protected] | |
with: | |
script: | | |
var artifacts = await github.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: ${{github.event.workflow_run.id }}, | |
}); | |
var matchArtifact = artifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == "screenshot-test-results" | |
})[0]; | |
var download = await github.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}); | |
var fs = require('fs'); | |
fs.writeFileSync('${{github.workspace}}/screenshot-test-results.zip', Buffer.from(download.data)); | |
- run: | | |
mkdir -p preview-screenshots/out/failures | |
unzip screenshot-test-results.zip -d preview-screenshots/out/failures | |
- name: New checkout for the companion branch | |
id: checkout_docs | |
if: ${{ hashFiles('preview-screenshots/out/failures/*.png') != '' }} | |
uses: actions/checkout@v3 | |
with: | |
path: temp_docs | |
- name: Commit the screenshot to the branch | |
id: docs_images | |
if: steps.checkout_docs.outcome == 'success' | |
continue-on-error: true # https://github.com/DroidKaigi/conference-app-2022/issues/497 | |
env: | |
BRANCH_NAME: companion_${{ github.event.workflow_run.head_branch }} | |
run: | | |
cd $GITHUB_WORKSPACE/temp_docs/ | |
git switch -C $BRANCH_NAME | |
mkdir -p docs/screenshots/ | |
cp -a ../preview-screenshots/out/failures/delta* docs/screenshots/ | |
git add . | |
git config --global user.name ScreenshotBot | |
git config --global push.default current | |
git config --global remote.pushDefault origin | |
git commit -m "Upload screenshots to github page." | |
git push -f | |
cd $GITHUB_WORKSPACE/preview-screenshots/out/failures | |
echo ::set-output name=images::$(ls -d delta* | jq -R -s -c 'split("\n")[:-1]' | jq -r --arg IMAGE_PATH "https://raw.githubusercontent.com/droidkaigi/conference-app-2022/$BRANCH_NAME/docs/screenshots/" '.[] |= $IMAGE_PATH + .') | |
cd $GITHUB_WORKSPACE/ | |
- name: Build PR Comment with Preview | |
id: pr_comment | |
if: steps.docs_images.outcome == 'success' | |
continue-on-error: true # https://github.com/DroidKaigi/conference-app-2022/issues/497 | |
env: | |
ALL_SCREENSHOTS: ${{ steps.docs_images.outputs.images }} | |
# Build a comment message with the image from snapshot images for demo purpose. | |
run: | | |
echo "There are differences in Compose previews:" > report.md | |
echo >> report.md # A blank line. | |
echo "$ALL_SCREENSHOTS" | jq -r '.[]' | while read -r image; do | |
echo "![]($image)" >> report.md | |
done | |
echo ::set-output name=comment::$(cat report.md) | |
- name: 'Comment on PR' | |
if: steps.pr_comment.outcome == 'success' | |
uses: actions/github-script@v3 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
var fs = require('fs'); | |
var issue_number = Number(fs.readFileSync('./preview-screenshots/out/failures/NR')); | |
await github.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue_number, | |
body: `${{ steps.pr_comment.outputs.comment }}` | |
}); | |
- name: Delete old branch saving screenshots | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
EXPIRED_TERM: 14 day ago | |
REMOTE_REPO: origin | |
TARGET_BRANCH_PREFIX: companion_ | |
DATE_FORMAT: '%Y%m%d' | |
run: | | |
cd $GITHUB_WORKSPACE/temp_docs/ | |
expired_date=`date --date="$EXPIRED_TERM" "+${DATE_FORMAT}"` | |
git fetch --all | |
git branch -r | grep "[^* ]+" -Eo | grep "${REMOTE_REPO}/${TARGET_BRANCH_PREFIX}.+" -Eo | | |
while read branch; do | |
commit_date=`git show -s $branch --format='%cd' --date=format:"${DATE_FORMAT}"` | |
if [[ $commit_date -le $expired_date ]]; then | |
short_branch=`echo $branch | sed "s/${REMOTE_REPO}\///g"` | |
git push ${REMOTE_REPO} --delete $short_branch | |
fi | |
done |