name: Checks JDK version for minor updates

on:
  schedule:
    - cron: '0 0 1 * *' # run once a month at the first day of month

env:
  JDK_VERSION: '22.0.1+8'
  JDK_VENDOR: zulu

jobs:
  jdk-current:
    name: Check out current version
    runs-on: ubuntu-latest
    outputs:
      jdk-date: ${{ steps.get-data.outputs.jdk-date}}
    steps:
      - uses: actions/setup-java@v4
        with:
          java-version: ${{ env.JDK_VERSION }}
          distribution: ${{ env.JDK_VENDOR }}
          check-latest: false
      - name: Read JAVA_VERSION_DATE and store in env variable
        id: get-data
        run: |
          date=$(cat ${JAVA_HOME}/release | grep "JAVA_VERSION_DATE=\"" | awk -F'=' '{print $2}' | tr -d '"')
          echo "jdk-date=${date}" >> "$GITHUB_OUTPUT"
  jdk-latest:
    name: Checkout latest jdk version
    runs-on: ubuntu-latest
    outputs:
      jdk-date: ${{ steps.get-data.outputs.jdk-date}}
      jdk-version: ${{ steps.get-data.outputs.jdk-version}}
    steps:
      - uses: actions/setup-java@v4
        with:
          java-version: 21
          distribution: ${{ env.JDK_VENDOR }}
          check-latest: true
      - name: Read JAVA_VERSION_DATE and store in env variable
        id: get-data
        run: |
          date=$(cat ${JAVA_HOME}/release | grep "JAVA_VERSION_DATE=\"" | awk -F'=' '{print $2}' | tr -d '"')
          echo "jdk-date=${date}" >> "$GITHUB_OUTPUT"
          version=$(cat ${JAVA_HOME}/release | grep "JAVA_RUNTIME_VERSION=\"" | awk -F'=' '{print $2}' | tr -d '"')
          echo "jdk-version=${version}" >> "$GITHUB_OUTPUT"
  notify:
    name: Notifies for jdk update
    runs-on: ubuntu-latest
    needs: [jdk-current, jdk-latest]
    if: ${{ needs.jdk-latest.outputs.jdk-date }} > ${{ needs.jdk-current.outputs.jdk-date }}
    steps:
      - name: Slack Notification
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_USERNAME: 'Cryptobot'
          SLACK_ICON: false
          SLACK_ICON_EMOJI: ':bot:'
          SLACK_CHANNEL: 'cryptomator-desktop'
          SLACK_TITLE: "JDK update available"
          SLACK_MESSAGE: "Cryptomator-CI JDK can be upgraded to ${{ needs.jdk-latest.outputs.jdk-version }}. See https://github.com/cryptomator/cryptomator/wiki/How-to-update-the-build-JDK for instructions."
          SLACK_FOOTER: false
          MSG_MINIMAL: true