name: Release Creation

on: 
  release:
    types: [published]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2

    # get part of the tag after the `v`
    - name: Extract tag version number
      id: get_version
      uses: battila7/get-version-action@v2
   
    # Substitute the Manifest and Download URLs in the module.json
    - name: Substitute Manifest and Download Links For Versioned Ones
      id: sub_manifest_link_version
      uses: microsoft/variable-substitution@v1
      with:
        files: 'src/module.json'
      env:
        version: ${{steps.get_version.outputs.version-without-v}}
        url: https://gitea.ascorrea.com/${{github.repository}}
        manifest: https://gitea.ascorrea.com/${{github.repository}}/releases/latest/download/module.json
        download: https://gitea.ascorrea.com/${{github.repository}}/releases/download/${{github.event.release.tag_name}}/module.zip

    # Create a zip file with all files required by the module to add to the release
    - name: Create files (module.zip, module.json) for release
      working-directory: ./src
      run: > 
        mkdir -p ../dist  
        && zip -r ../dist/module.zip .
        && cp module.json ../dist/ 
        && echo \"Done! Zipped module.json and copied module.js.\"
    
    - name: Setting commit message variable
      id: commit-message
      # Per 
      # https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#example-of-a-multiline-string
      run: |
        {
          echo 'COMMIT_MESSAGE<<EOF'
          git log -1 --pretty=%B
          echo EOF
        } >> "$GITHUB_OUTPUT"

    # Create a release for this specific version
    - name: Update Release with Files
      id: create_version_release
      uses: akkuman/gitea-release-action@v1
      with:
        files: |-
            ./dist/module.json
            ./dist/module.zip
        body: |-
          ${{ steps.commit-message.outputs.COMMIT_MESSAGE }}