|
| 1 | +# Checks for image diffs in a Pull Request and adds a GitHub comment showing the diff |
| 2 | +name: DVC image diff |
| 3 | + |
| 4 | +on: |
| 5 | + pull_request: |
| 6 | + paths: |
| 7 | + - 'pygmt/tests/baseline/*.png.dvc' |
| 8 | + |
| 9 | +jobs: |
| 10 | + dvc-diff: |
| 11 | + name: DVC image diff |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout |
| 16 | + |
| 17 | + with: |
| 18 | + # fetch all history so that dvc diff works |
| 19 | + fetch-depth: 0 |
| 20 | + |
| 21 | + - name: Setup data version control (DVC) |
| 22 | + |
| 23 | + |
| 24 | + - name: Setup continuous machine learning (CML) |
| 25 | + |
| 26 | + |
| 27 | + # Produce the markdown diff report, which should look like: |
| 28 | + # ## Summary of changed images |
| 29 | + # |
| 30 | + # This is an auto-generated report of images that have changed on the DVC remote |
| 31 | + # |
| 32 | + # | Status | Path | |
| 33 | + # |----------|-------------------------------------| |
| 34 | + # | added | pygmt/tests/baseline/test_image.png | |
| 35 | + - name: Put list of images that were added or changed into report |
| 36 | + run: | |
| 37 | + echo -e "## Summary of changed images\n" > report.md |
| 38 | + echo -e "This is an auto-generated report of images that have changed on the DVC remote\n" >> report.md |
| 39 | + dvc diff --show-md master HEAD >> report.md |
| 40 | + cat report.md |
| 41 | +
|
| 42 | + - name: Pull image data from cloud storage |
| 43 | + run: dvc pull --remote upstream |
| 44 | + |
| 45 | + - name: Put image diff(s) into report |
| 46 | + env: |
| 47 | + repo_token: ${{ secrets.GITHUB_TOKEN }} |
| 48 | + id: image-diff |
| 49 | + run: | |
| 50 | + # Get just the filename of the changed image from the report |
| 51 | + awk 'NR>=7 {print $4}' report.md > diff_files.txt |
| 52 | +
|
| 53 | + # Append each image to the markdown report |
| 54 | + echo -e "## Image diff(s)\n" >> report.md |
| 55 | + echo -e "<details>\n" >> report.md |
| 56 | +
|
| 57 | + while IFS= read -r line; do |
| 58 | + echo -e "- $line \n" >> report.md |
| 59 | + cml-publish --title $line --md "$line" >> report.md |
| 60 | + done < diff_files.txt |
| 61 | +
|
| 62 | + echo -e "</details>\n" >> report.md |
| 63 | +
|
| 64 | + # Mention git commit SHA in the report |
| 65 | + echo -e "Report last updated at commit ${{ github.event.pull_request.head.sha }}" >> report.md |
| 66 | +
|
| 67 | + # Format report to escape newlines before publishing as GitHub comment |
| 68 | + report=$(cat report.md) |
| 69 | + report="${report//'%'/'%25'}" |
| 70 | + report="${report//$'\n'/'%0A'}" |
| 71 | + report="${report//$'\r'/'%0D'}" |
| 72 | + echo ::set-output name=report::$report |
| 73 | +
|
| 74 | + - name: Find comment with image diff report |
| 75 | + uses: peter-evans/[email protected] |
| 76 | + id: fc |
| 77 | + with: |
| 78 | + issue-number: ${{ github.event.pull_request.number }} |
| 79 | + comment-author: 'github-actions[bot]' |
| 80 | + body-includes: 'This is an auto-generated report of images that have changed on the DVC remote' |
| 81 | + |
| 82 | + - name: Create comment with image diff report |
| 83 | + if: steps.fc.outputs.comment-id == '' |
| 84 | + uses: peter-evans/[email protected] |
| 85 | + with: |
| 86 | + issue-number: ${{ github.event.pull_request.number }} |
| 87 | + body: ${{ steps.image-diff.outputs.report }} |
| 88 | + |
| 89 | + - name: Update comment with new image diff report |
| 90 | + if: steps.fc.outputs.comment-id != '' |
| 91 | + uses: peter-evans/[email protected] |
| 92 | + with: |
| 93 | + comment-id: ${{ steps.fc.outputs.comment-id }} |
| 94 | + body: ${{ steps.image-diff.outputs.report }} |
| 95 | + edit-mode: replace |
0 commit comments