|
| 1 | +name: screenshot-comparison-comment |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: |
| 6 | + - screenshot-comparison |
| 7 | + types: |
| 8 | + - completed |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: ${{ github.workflow }}-${{ github.head_ref }}-${{ github.event.workflow_run.id }} |
| 12 | + cancel-in-progress: true |
| 13 | + |
| 14 | +jobs: |
| 15 | + screenshot-comparison-comment: |
| 16 | + if: > |
| 17 | + github.event.workflow_run.event == 'pull_request' && |
| 18 | + github.event.workflow_run.conclusion == 'success' |
| 19 | +
|
| 20 | + timeout-minutes: 2 |
| 21 | + |
| 22 | + permissions: |
| 23 | + actions: read # for downloading artifacts |
| 24 | + contents: write # for pushing screenshot-diff to companion branch |
| 25 | + pull-requests: write # for creating a comment on pull requests |
| 26 | + |
| 27 | + runs-on: ubuntu-latest |
| 28 | + |
| 29 | + steps: |
| 30 | + - uses: dawidd6/action-download-artifact@v11 |
| 31 | + with: |
| 32 | + name: pr |
| 33 | + run_id: ${{ github.event.workflow_run.id }} |
| 34 | + - id: get-pull-request-number |
| 35 | + name: Get pull request number |
| 36 | + shell: bash |
| 37 | + run: | |
| 38 | + echo "pull_request_number=$(cat NR)" >> "$GITHUB_OUTPUT" |
| 39 | + - name: main checkout |
| 40 | + id: checkout-main |
| 41 | + uses: actions/checkout@v4 |
| 42 | + with: |
| 43 | + ref: main |
| 44 | + - id: switch-companion-branch |
| 45 | + env: |
| 46 | + BRANCH_NAME: companion_${{ github.event.workflow_run.head_branch }} |
| 47 | + run: | |
| 48 | + # orphan means it will create no history branch |
| 49 | + git branch -D "$BRANCH_NAME" || true |
| 50 | + git checkout --orphan "$BRANCH_NAME" |
| 51 | + git rm -rf . |
| 52 | + - uses: dawidd6/action-download-artifact@v11 |
| 53 | + with: |
| 54 | + run_id: ${{ github.event.workflow_run.id }} |
| 55 | + name: screenshot-diff-reports |
| 56 | + path: screenshot-diff-reports |
| 57 | + - id: check-if-there-are-valid-files |
| 58 | + name: Check if there are valid files |
| 59 | + shell: bash |
| 60 | + run: | |
| 61 | + # Find all the files ending with _compare.png |
| 62 | + mapfile -d '' -t files_to_add < <(find . -type f -name "*_compare.png" -print0) |
| 63 | + |
| 64 | + # Check for invalid file names and add only valid ones |
| 65 | + exist_valid_files="false" |
| 66 | + for file in "${files_to_add[@]}"; do |
| 67 | + if [[ $file =~ ^([-0-9A-Z_a-z.\ \/]|\[|\])+$ ]]; then |
| 68 | + exist_valid_files="true" |
| 69 | + break |
| 70 | + fi |
| 71 | + done |
| 72 | + echo "exist_valid_files=$exist_valid_files" >> "$GITHUB_OUTPUT" |
| 73 | + - id: push-screenshot-diff |
| 74 | + shell: bash |
| 75 | + if: steps.check-if-there-are-valid-files.outputs.exist_valid_files == 'true' |
| 76 | + env: |
| 77 | + BRANCH_NAME: companion_${{ github.event.workflow_run.head_branch }} |
| 78 | + run: | |
| 79 | + # Find all the files ending with _compare.png |
| 80 | + mapfile -d '' -t files_to_add < <(find . -type f -name "*_compare.png" -print0) |
| 81 | + |
| 82 | + # Check for invalid file names and add only valid ones |
| 83 | + for file in "${files_to_add[@]}"; do |
| 84 | + if [[ "$file" =~ ^([-0-9A-Z_a-z.\ \/]|\[|\])+$ ]]; then |
| 85 | + git add "$file" |
| 86 | + fi |
| 87 | + done |
| 88 | + git config --global user.name ScreenshotBot |
| 89 | + git config --global user.email 41898282+github-actions[bot]@users.noreply.github.com |
| 90 | + git commit -m "Add screenshot diff" |
| 91 | + git push origin HEAD:"$BRANCH_NAME" -f |
| 92 | + - id: generate-diff-reports |
| 93 | + name: Generate diff reports |
| 94 | + if: steps.check-if-there-are-valid-files.outputs.exist_valid_files == 'true' |
| 95 | + env: |
| 96 | + BRANCH_NAME: companion_${{ github.event.workflow_run.head_branch }} |
| 97 | + shell: bash |
| 98 | + run: | |
| 99 | + # Find all the files ending with _compare.png in roborazzi folder |
| 100 | + mapfile -d '' -t files < <(find . -type f -name "*_compare.png" -print0) |
| 101 | + delimiter="$(openssl rand -hex 8)" |
| 102 | + { |
| 103 | + echo "reports<<${delimiter}" |
| 104 | + |
| 105 | + # Create markdown table header |
| 106 | + echo "Snapshot diff report" |
| 107 | + echo "| File name | Image |" |
| 108 | + echo "|-------|-------|" |
| 109 | + } >> "$GITHUB_OUTPUT" |
| 110 | + |
| 111 | + # Iterate over the files and create table rows |
| 112 | + for file in "${files[@]}"; do |
| 113 | + # Get the file name and insert newlines every 20 characters |
| 114 | + fileName=$(basename "$file" | sed -r 's/(.{20})/\1<br>/g') |
| 115 | + urlPart="${BRANCH_NAME//#/%23}/${file//#/%23}" |
| 116 | + urlPart="${urlPart//[/%5B}" |
| 117 | + urlPart="${urlPart//]/%5D}" |
| 118 | + urlPart="${urlPart// /%20}" |
| 119 | + echo "| [$fileName](https://github.com/${{ github.repository }}/blob/$urlPart) |  |" >> "$GITHUB_OUTPUT" |
| 120 | + done |
| 121 | + echo "${delimiter}" >> "$GITHUB_OUTPUT" |
| 122 | + - name: Find Comment |
| 123 | + uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3.1.0 |
| 124 | + id: fc |
| 125 | + if: steps.generate-diff-reports.outputs.reports != '' |
| 126 | + with: |
| 127 | + issue-number: ${{ steps.get-pull-request-number.outputs.pull_request_number }} |
| 128 | + comment-author: 'github-actions[bot]' |
| 129 | + body-includes: Snapshot diff report |
| 130 | + |
| 131 | + - name: Add or update comment on PR |
| 132 | + uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0 |
| 133 | + if: steps.generate-diff-reports.outputs.reports != '' |
| 134 | + with: |
| 135 | + comment-id: ${{ steps.fc.outputs.comment-id }} |
| 136 | + issue-number: ${{ steps.get-pull-request-number.outputs.pull_request_number }} |
| 137 | + body: ${{ steps.generate-diff-reports.outputs.reports }} |
| 138 | + edit-mode: replace |
| 139 | + |
| 140 | + - name: Cleanup outdated companion branches |
| 141 | + run: | |
| 142 | + # Find outdated companion branches with last commit date |
| 143 | + git branch -r --format="%(refname:lstrip=3)" | grep companion_ | while read -r branch; do |
| 144 | + last_commit_date_timestamp=$(git log -1 --format=%ct "origin/$branch") |
| 145 | + now_timestamp=$(date +%s) |
| 146 | + # Delete branch if it's older than 1 month |
| 147 | + # if [ $((now_timestamp - last_commit_date_timestamp)) -gt 2592000 ]; then |
| 148 | + # For testing purpose, delete branch if it's older than 1 second |
| 149 | + echo "branch: $branch now_timestamp: $now_timestamp last_commit_date_timestamp: $last_commit_date_timestamp" |
| 150 | + if [ $((now_timestamp - last_commit_date_timestamp)) -gt 1 ]; then |
| 151 | + # Comment out for demonstration purpose |
| 152 | + echo "Deleting $branch" |
| 153 | +
|
| 154 | + # git push origin --delete "$branch" |
| 155 | + fi |
| 156 | + done |
0 commit comments