name: LLVM Compatibility

on:
  workflow_dispatch:
  schedule:
    - cron: '0 00 * * 1' # Every Monday @ 00:00 UTC

jobs:
  build_test:
    name: Build & test the Docker image with latest LLVM
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive
          token: ${{ secrets.GH_TOKEN }}

      - name: Rebase LLVM
        run: |
          cd llvm-project
          git remote add base git@github.com:llvm/llvm-project.git
          git fetch base
          git rebase base/main

      - name: Log LLVM commit
        run: echo "LLVM commit" && cd ${{ github.workspace }}/llvm-project && git log -1

      - name: login
        run: echo "${{ secrets.GHCR_PASSWORD }}" | docker login -u ${{ secrets.GHCR_LOGIN }} --password-stdin ghcr.io

      - name: Build
        run: docker image build --no-cache -t compiler-latest-llvm -f builders/Dockerfile.concrete-compiler-env .

      - name: Test compiler with latest LLVM
        uses: addnab/docker-run-action@v3
        with:
          image: compiler-latest-llvm
          run: |
            cd /compiler
            pip install pytest
            make BUILD_DIR=/build run-tests

      - name: Update Custom LLVM
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GH_TOKEN }}
          branch: main
          force: true
          repository: zama-ai/concrete-compiler-internal-llvm-project

      - name: Update LLVM submodule
        id: update-llvm
        run: |
          git submodule update --remote
          echo "::set-output name=commit::$(cd llvm-project && git rev-parse --short HEAD)"

      - name: Commit latest LLVM version
        uses: EndBug/add-and-commit@v7
        with:
          add: ./llvm-project
          default_author: github_actions
          message: "chore: update LLVM to ${{ steps.update-llvm.outputs.commit }}"

      - name: Send Slack Notification
        if: ${{ always() }}
        continue-on-error: true
        uses: rtCamp/action-slack-notify@12e36fc18b0689399306c2e0b3e0f2978b7f1ee7
        env:
          SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }}
          SLACK_ICON: https://pbs.twimg.com/profile_images/1274014582265298945/OjBKP9kn_400x400.png
          SLACK_COLOR: ${{ job.status }}
          SLACK_MESSAGE: "Compatibility check with latest LLVM finished with status ${{ job.status }} \
            (${{ env.ACTION_RUN_URL }})"
          SLACK_USERNAME: ${{ secrets.BOT_USERNAME }}
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
