on:
    workflow_call:
        inputs:
            artifact-name:
                type: string
                required: true
                description: "The name of the artifact containing the deb to include"
        secrets:
            ELEMENT_BOT_TOKEN:
                required: true
            CF_R2_ACCESS_KEY_ID:
                required: true
            CF_R2_TOKEN:
                required: true
# Protect reprepro database using concurrency
concurrency: reprepro
jobs:
    reprepro:
        name: Deploy debian package
        environment: packages.element.io
        runs-on: ubuntu-latest
        env:
            R2_INCOMING_BUCKET: ${{ vars.R2_INCOMING_BUCKET }}
            R2_URL: ${{ vars.CF_R2_S3_API }}
        steps:
            - name: Download artifact
              uses: actions/download-artifact@v3
              with:
                  name: ${{ inputs.artifact-name }}
                  path: dist

            - name: Upload incoming deb
              id: upload
              run: |
                  deb="$(ls *.deb | tail -n1)"
                  echo "incoming=$deb" >> $GITHUB_OUTPUT
                  aws s3 cp "$deb" "s3://$R2_INCOMING_BUCKET" --endpoint-url "$R2_URL" --region auto
              working-directory: dist
              env:
                  AWS_ACCESS_KEY_ID: ${{ secrets.CF_R2_ACCESS_KEY_ID }}
                  AWS_SECRET_ACCESS_KEY: ${{ secrets.CF_R2_TOKEN }}

            - name: Notify packages.element.io of incoming deb
              uses: peter-evans/repository-dispatch@26b39ed245ab8f31526069329e112ab2fb224588 # v2
              with:
                  token: ${{ secrets.ELEMENT_BOT_TOKEN }}
                  repository: vector-im/packages.element.io
                  event-type: reprepro-incoming
                  client-payload: '{"incoming": "${{ steps.upload.outputs.incoming }}"}'