name: Update AppVeyor Image

# Run chron daily Mon-Fri
on:
  workflow_dispatch:
  schedule:
    - cron: '0 8 * * 1-5' # runs 8:00 every business day (see https://crontab.guru)

permissions:
  contents: write
  pull-requests: write

jobs:
  bake-appveyor-image:
    name: Bake AppVeyor Image
    permissions:
      contents: write
      pull-requests: write  # to create a new PR with updated Appveyor images
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
      with:
        fetch-depth: 0
    - name: Yarn install
      run: |
        node script/yarn.js install --frozen-lockfile
    - name: Set Repo for Commit
      run: git config --global --add safe.directory $GITHUB_WORKSPACE
    - name: Check AppVeyor Image
      env:
        APPVEYOR_TOKEN: ${{ secrets.APPVEYOR_TOKEN }}
      run: |
        node ./script/prepare-appveyor
        if [ -f ./image_version.txt ]; then
          echo "APPVEYOR_IMAGE_VERSION="$(cat image_version.txt)"" >> $GITHUB_ENV
          rm image_version.txt
        fi
    - name: (Optionally) Update Appveyor Image
      if: ${{ env.APPVEYOR_IMAGE_VERSION }}
      uses: mikefarah/yq@bb66c9c872a7a4cf3d6846c2ff6d182c66ec3f77 # v4.40.7
      with:
        cmd: |
          yq '.image = "${{ env.APPVEYOR_IMAGE_VERSION }}"' "appveyor.yml" > "appveyor2.yml"
          yq '.image = "${{ env.APPVEYOR_IMAGE_VERSION }}"' "appveyor-woa.yml" > "appveyor-woa2.yml"
    - name: (Optionally) Generate Commit Diff
      if: ${{ env.APPVEYOR_IMAGE_VERSION }}
      run: |
        diff -w -B appveyor.yml appveyor2.yml > appveyor.diff || true
        patch -f appveyor.yml < appveyor.diff
        rm appveyor2.yml appveyor.diff
    - name: (Optionally) Generate Commit Diff for WOA
      if: ${{ env.APPVEYOR_IMAGE_VERSION }}
      run: |
        diff -w -B appveyor-woa.yml appveyor-woa2.yml > appveyor-woa.diff || true
        patch -f appveyor-woa.yml < appveyor-woa.diff
        rm appveyor-woa2.yml appveyor-woa.diff
    - name: (Optionally) Commit and Pull Request
      if: ${{ env.APPVEYOR_IMAGE_VERSION }}
      uses: peter-evans/create-pull-request@b1ddad2c994a25fbc81a28b3ec0e368bb2021c50 # v6.0.0
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        commit-message: 'build: update appveyor image to latest version'
        committer: GitHub <noreply@github.com>
        author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
        signoff: false
        branch: bump-appveyor-image
        delete-branch: true
        reviewers: electron/wg-releases
        title: 'build: update appveyor image to latest version'
        labels: semver/none,no-backport
        body: |
          This PR updates appveyor.yml to the latest baked image, ${{ env.APPVEYOR_IMAGE_VERSION }}.
          Notes: none
