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

name: Dispatch trybot
"on":
  repository_dispatch: {}
  push:
    branches:
      - ci/test
jobs:
  trybot:
    defaults:
      run:
        shell: bash --noprofile --norc -euo pipefail {0}
    permissions:
      contents: read
    runs-on: namespace-profile-linux-amd64;overrides.cache-tag=cue-dispatch-workflow
    if: ${{ ((github.ref == 'refs/heads/ci/test') && false) || github.event.client_payload.type == 'trybot' }}
    steps:
      - name: Write netrc file for cueckoo Gerrithub
        run: |-
          cat <<EOD > ~/.netrc
          machine cue.gerrithub.io
          login cueckoo
          password ${{ secrets.CUECKOO_GERRITHUB_PASSWORD }}
          EOD
          chmod 600 ~/.netrc
      - name: Write fake payload
        id: payload
        if: github.repository == 'cue-lang/cue' && (github.ref == 'refs/heads/ci/test')
        run: |-
          cat <<EOD >> $GITHUB_OUTPUT
          value<<DOE
          null
          DOE
          EOD
      - name: Trigger TryBot (fake data)
        if: github.event.client_payload.type != 'trybot'
        run: |-
          mkdir tmpgit
          cd tmpgit
          git init -b initialbranch
          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 origin  https://cue.gerrithub.io/a/cue-lang/cue

          git fetch origin ${{ fromJSON(steps.payload.outputs.value).ref }}
          git checkout -b local_${{ fromJSON(steps.payload.outputs.value).targetBranch }} FETCH_HEAD

          # Error if we already have dispatchTrailer according to git log logic.
          # See earlier check for GitHub expression logic check.
          x="$(git log -1 --pretty='%(trailers:key=Dispatch-Trailer,valueonly)')"
          if [[ "$x" != "" ]]
          then
          	 echo "Ref ${{ fromJSON(steps.payload.outputs.value).ref }} already has a Dispatch-Trailer"
          	 exit 1
          fi

          # Add the trailer because we don't have it yet. GitHub expressions do not have a
          # substitute or quote capability. So we do that in shell. We also strip out the
          # indenting added by toJSON. We ensure that the type field is first in order
          # that we can safely check for specific types of dispatch trailer.
          #
          # Use bash heredoc so that JSON's use of double quotes does
          # not get interpreted as shell.
          trailer="$(cat <<EOD | jq -r -c '{type} + .'
          ${{ toJSON(fromJSON(steps.payload.outputs.value)) }}
          EOD
          )"
          # --no-divider prevents a "---" line from marking the end of the commit message.
          # Here, we know that the input is exactly one commit message,
          # so don't do weird things if the commit message has a "---" line.
          git log -1 --format=%B | git interpret-trailers --no-divider --trailer "Dispatch-Trailer: $trailer" | git commit --amend -F -
          git log -1

          success=false
          for try in {1..20}; do
          	echo "Push to trybot try $try"
          	if git push -f https://github.com/cue-lang/cue-trybot local_${{ fromJSON(steps.payload.outputs.value).targetBranch }}:${{ fromJSON(steps.payload.outputs.value).targetBranch }}; then
          		success=true
          		break
          	fi
          	sleep 1
          done
          if ! $success; then
          	echo "Giving up"
          	exit 1
          fi
      - name: Trigger TryBot (repository_dispatch payload)
        if: github.event.client_payload.type == 'trybot'
        run: |-
          mkdir tmpgit
          cd tmpgit
          git init -b initialbranch
          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 origin  https://cue.gerrithub.io/a/cue-lang/cue

          git fetch origin ${{ github.event.client_payload.ref }}
          git checkout -b local_${{ github.event.client_payload.targetBranch }} FETCH_HEAD

          # Error if we already have dispatchTrailer according to git log logic.
          # See earlier check for GitHub expression logic check.
          x="$(git log -1 --pretty='%(trailers:key=Dispatch-Trailer,valueonly)')"
          if [[ "$x" != "" ]]
          then
          	 echo "Ref ${{ github.event.client_payload.ref }} already has a Dispatch-Trailer"
          	 exit 1
          fi

          # Add the trailer because we don't have it yet. GitHub expressions do not have a
          # substitute or quote capability. So we do that in shell. We also strip out the
          # indenting added by toJSON. We ensure that the type field is first in order
          # that we can safely check for specific types of dispatch trailer.
          #
          # Use bash heredoc so that JSON's use of double quotes does
          # not get interpreted as shell.
          trailer="$(cat <<EOD | jq -r -c '{type} + .'
          ${{ toJSON(github.event.client_payload) }}
          EOD
          )"
          # --no-divider prevents a "---" line from marking the end of the commit message.
          # Here, we know that the input is exactly one commit message,
          # so don't do weird things if the commit message has a "---" line.
          git log -1 --format=%B | git interpret-trailers --no-divider --trailer "Dispatch-Trailer: $trailer" | git commit --amend -F -
          git log -1

          success=false
          for try in {1..20}; do
          	echo "Push to trybot try $try"
          	if git push -f https://github.com/cue-lang/cue-trybot local_${{ github.event.client_payload.targetBranch }}:${{ github.event.client_payload.targetBranch }}; then
          		success=true
          		break
          	fi
          	sleep 1
          done
          if ! $success; then
          	echo "Giving up"
          	exit 1
          fi
