# Code generated internal/ci/ci_tool.cue; DO NOT EDIT.

name: Evict caches
"on":
  schedule:
    - cron: 0 2 * * *
jobs:
  test:
    defaults:
      run:
        shell: bash --noprofile --norc -euo pipefail {0}
    if: ${{github.repository == 'cue-lang/cue'}}
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          fetch-depth: 0
      - name: Reset git directory modification times
        run: touch -t 202211302355 $(find * -type d)
      - name: Restore git file modification times
        uses: chetan/git-restore-mtime-action@075f9bc9d159805603419d50f794bd9f33252ebe
      - id: DispatchTrailer
        name: Try to extract Dispatch-Trailer
        run: |-
          x="$(git log -1 --pretty='%(trailers:key=Dispatch-Trailer,valueonly)')"
          if [[ "$x" == "" ]]
          then
             # Some steps rely on the presence or otherwise of the Dispatch-Trailer.
             # We know that we don't have a Dispatch-Trailer in this situation,
             # hence we use the JSON value null in order to represent that state.
             # This means that GitHub expressions can determine whether a Dispatch-Trailer
             # is present or not by checking whether the fromJSON() result of the
             # output from this step is the JSON value null or not.
             x=null
          fi
          echo "value<<EOD" >> $GITHUB_OUTPUT
          echo "$x" >> $GITHUB_OUTPUT
          echo "EOD" >> $GITHUB_OUTPUT
      - if: |-
          ((github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/release-branch.')) && (! (contains(github.event.head_commit.message, '
          Dispatch-Trailer: {"type":"')))) && (contains(github.event.head_commit.message, '
          Dispatch-Trailer: {"type":"'))
        name: Check we don't have Dispatch-Trailer on a protected branch
        run: |-
          echo "github.event.head_commit.message contains Dispatch-Trailer but we are on a protected branch"
          false
      - name: Delete caches
        run: |-
          echo ${{ secrets.CUECKOO_GITHUB_PAT }} | gh auth login --with-token
          for i in https://github.com/cue-lang/cue https://github.com/cue-lang/cue-trybot
          do
          	echo "Evicting caches for $i"
          	gh cache delete --repo $i --all --succeed-on-no-caches
          done
      - name: Trigger workflow runs to repopulate caches
        run: |-
          # Prepare git for pushes to trybot repo. Note
          # because we have already checked out code we don't
          # need origin. Fetch origin default branch for later use
          git config user.name cueckoo
          git config user.email cueckoo@gmail.com
          git config http.https://github.com/.extraheader "AUTHORIZATION: basic $(echo -n cueckoo:${{ secrets.CUECKOO_GITHUB_PAT }} | base64)"
          git remote add trybot https://github.com/cue-lang/cue-trybot

          # Now trigger the most recent workflow run on each of the default branches.
          # We do this by listing all the branches on the main repo and finding those
          # which match the protected branch patterns (globs).
          for j in $(curl -s -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.CUECKOO_GITHUB_PAT }}" -H "X-GitHub-Api-Version: 2022-11-28" -f https://api.github.com/repos/cue-lang/cue/branches | jq -r '.[] | .name')
          do
          	for i in master release-branch.*
          	do
          		if [[ "$j" != $i ]]; then
          			continue
          		fi

          		echo Branch: $j
          		sha=$(curl -s -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.CUECKOO_GITHUB_PAT }}" -H "X-GitHub-Api-Version: 2022-11-28" "https://api.github.com/repos/cue-lang/cue/commits/$j" | jq -r '.sha')
          		echo Latest commit: $sha

          		echo "Trigger workflow on cue-lang/cue"
          		curl -s -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.CUECKOO_GITHUB_PAT }}" -H "X-GitHub-Api-Version: 2022-11-28" --fail-with-body -X POST https://api.github.com/repos/cue-lang/cue/actions/workflows/trybot.yaml/dispatches -d "{\"ref\":\"$j\"}"

          		# Ensure that the trybot repo has the latest commit for
          		# this branch.  If the force-push results in a commit
          		# being pushed, that will trigger the trybot workflows
          		# so we don't need to do anything, otherwise we need to
          		# trigger the most recent commit on that branch
          		git remote -v
          		git fetch origin refs/heads/$j
          		git log -1 FETCH_HEAD

          		success=false
          		for try in {1..20}; do
          			echo "Push to trybot try $try"
          			exitCode=0; push="$(git push -f trybot FETCH_HEAD:$j 2>&1)" || exitCode=$?
          			echo "$push"
          			if [[ $exitCode -eq 0 ]]; then
          				success=true
          				break
          			fi
          			sleep 1
          		done
          		if ! $success; then
          			echo "Giving up"
          			exit 1
          		fi

          		if echo "$push" | grep up-to-date
          		then
          			# We are up-to-date, i.e. the push did nothing, hence we need to trigger a workflow_dispatch
          			# in the trybot repo.
          			echo "Trigger workflow on cue-lang/cue-trybot"
          			curl -s -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.CUECKOO_GITHUB_PAT }}" -H "X-GitHub-Api-Version: 2022-11-28" --fail-with-body -X POST https://api.github.com/repos/cue-lang/cue-trybot/actions/workflows/trybot.yaml/dispatches -d "{\"ref\":\"$j\"}"
          		else
          			echo "Force-push to cue-lang/cue-trybot did work; nothing to do"
          		fi
          	done
          done
