name: 'Publish release'
author: 'Forgejo authors'
description: |
  Publish release

inputs:
  forgejo:
    description: 'URL of the Forgejo instance where the release is uploaded (e.g. https://codeberg.org)'
    required: true
  from-owner:
    description: 'the owner from which a release is to be copied (e.g forgejo-integration)'
    required: true
  to-owner:
    description: 'the owner to which a release is to be copied (e.g. forgejo-experimental). It has be an organization in which doer has the required permissions. Or be the same as the doer'
    required: true
  repo:
    description: 'the repository from which a release is to be copied relative to from-owner and to-owner'
    default: 'forgejo'
  ref-name:
    description: 'ref_name of the tag of the release to be copied (e.g. github.ref_name)'
    required: true
  doer:
    description: 'Name of the user authoring the release (e.g. release-team). The user must be authorized to create packages in to-owner and releases in to-owner/repo'
    required: true
  token:
    description: 'application token created on forgejo by the doer, with a scope allowing it to create packages in to-owner and releases in to-owner/repo'
    required: true
  gpg-private-key:
    description: 'GPG Private Key to sign the release artifacts'
  gpg-passphrase:
    description: 'Passphrase of the GPG Private Key'
  verbose:
    description: 'Increase the verbosity level'
    default: 'false'

runs:
  using: "composite"
  steps:
    - id: hostport
      run: |
         url="${{ inputs.forgejo }}"
         hostport=${url##http*://}
         hostport=${hostport%%/}
         echo "value=$hostport" >> "$GITHUB_OUTPUT"    

    - id: tag-version
      run: |
        version="${{ inputs.ref-name }}"
        version=${version##*v}
        echo "value=$version" >> "$GITHUB_OUTPUT"

    - name: Create the release notes
        id: release-notes
        run: |
          anchor=${{ steps.tag-version.outputs.value }}
          anchor=${anchor//./-}
          cat >> "$GITHUB_OUTPUT" <<EOF
          value<<ENDVAR
          See https://codeberg.org/forgejo/forgejo/src/branch/forgejo/RELEASE-NOTES.md#$anchor
          ENDVAR
          EOF

    - name: apt-get install docker.io
      run: |
        DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -qq -y docker.io

    - name: download release
      uses: https://code.forgejo.org/actions/forgejo-release@v1
      with:
        url: ${{ inputs.forgejo }}
        repo: ${{ inputs.from-owner }}/${{ inputs.repo }}
        direction: download
        release-dir: release
        download-retry: 60
        token: ${{ inputs.token }}
        verbose: ${{ inputs.verbose }}

    - name: upload release
      uses: https://code.forgejo.org/actions/forgejo-release@v1
      with:
        url: ${{ inputs.forgejo }}
        repo: ${{ inputs.to-owner }}/${{ inputs.repo }}
        direction: upload
        release-dir: release
        release-notes: ${{ steps.release-notes.outputs.value }}
        token: ${{ inputs.token }}
        gpg-private-key: ${{ inputs.gpg-private-key }}
        gpg-passphrase: ${{ inputs.gpg-passphrase }}
        verbose: ${{ inputs.verbose }}

    - name: login to the registry
      uses: https://github.com/docker/login-action@v2
      with:
          registry: ${{ steps.hostport.outputs.value }}
          username: ${{ inputs.doer }}
          password: ${{ inputs.token }}

    - uses: https://code.forgejo.org/forgejo/forgejo-container-image@v1
      env:
        VERIFY: 'false'
      with:
        url: https://${{ steps.hostport.outputs.value }}
        destination-owner: ${{ inputs.to-owner }}
        owner: ${{ inputs.from-owner }}
        suffixes: '-rootless'
        project: ${{ inputs.repo }}
        tag: ${{ steps.tag-version.outputs.value }}
        doer: ${{ inputs.doer }}
        token: ${{ inputs.token }}
        verbose: ${{ inputs.verbose }}