# This is a basic workflow that is manually triggered

name: Create Flatpak PR

# Controls when the action will run. Workflow runs when manually triggered using the UI
# or API.
on:
  workflow_dispatch:
  release:
    types: [published]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
      with:
        repository: flathub/io.freetubeapp.FreeTube
        token: ${{ secrets.FLATHUB_TOKEN }}
    - name: GitHub API exec action
      uses: moustacheful/github-api-exec-action@v0
      id: api_results
      with:
        # Command to execute, (e.g: `pulls.create`), see https://octokit.github.io/rest.js/ for available commands
        command: repos.getRelease
        payload: >
            {
              "owner": "FreeTubeApp",
              "repo": "FreeTube",
              "release_id": ${{ secrets.UPLOAD_ID }}
            }
      env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    - name: Install xmlstarlet
      run: sudo apt -y install xmlstarlet
    - name: Create Version Variable
      uses: bluwy/substitute-string-action@v2
      id: sub
      with:
        _input-text: ${{ fromJson(steps.api_results.outputs.result).tag_name }}
        -beta: ''
        v: ''
    - name: Create Release Branch
      run: |
        git checkout -b release-v${{ steps.sub.outputs.result }}
        git push --set-upstream origin release-v${{ steps.sub.outputs.result }}
    - name: Download x64 Release
      uses: fabriciobastian/download-release-asset-action@v1.0.6
      with:
        version: v${{ steps.sub.outputs.result }}-beta
        repository: FreeTubeApp/FreeTube
        file: freetube-${{ steps.sub.outputs.result }}-linux-portable-x64.zip
    - name: Download ARM Release
      uses: fabriciobastian/download-release-asset-action@v1.0.6
      with:
        version: v${{ steps.sub.outputs.result }}-beta
        repository: FreeTubeApp/FreeTube
        file: freetube-${{ steps.sub.outputs.result }}-linux-portable-arm64.zip
    - name: Set x64 Hash Variable
      id: hash-x64
      run: |
        echo 'HASH_X64<<EOF' >> $GITHUB_ENV
        sha256sum freetube-${{ steps.sub.outputs.result }}-linux-portable-x64.zip | awk '{print $1}' >> $GITHUB_ENV
        echo 'EOF' >> $GITHUB_ENV
    - name: Set ARM Hash Variable
      id: hash-arm64
      run: |
        echo 'HASH_ARM64<<EOF' >> $GITHUB_ENV
        sha256sum freetube-${{ steps.sub.outputs.result }}-linux-portable-arm64.zip | awk '{print $1}' >> $GITHUB_ENV
        echo 'EOF' >> $GITHUB_ENV
    - name: Set Date Variable
      id: current-date
      run: |
        echo 'CURRENT_DATE<<EOF' >> $GITHUB_ENV
        date +"%Y-%m-%d" >> $GITHUB_ENV
        echo 'EOF' >> $GITHUB_ENV
    - name: Update x64 File Location in yml File
      uses: mikefarah/yq@4.0.0-beta1
      with:
        # The Command which should be run
        cmd: yq w -i io.freetubeapp.FreeTube.yml modules[0].sources[0].url 'https://github.com/FreeTubeApp/FreeTube/releases/download/v${{ steps.sub.outputs.result }}-beta/freetube-${{ steps.sub.outputs.result }}-linux-portable-x64.zip'
    - name: Update x64 Hash in yml File
      uses: mikefarah/yq@4.0.0-beta1
      with:
        # The Command which should be run
        cmd: yq w -i io.freetubeapp.FreeTube.yml modules[0].sources[0].sha256 ${{ env.HASH_X64 }}
    - name: Update ARM File Location in yml File
      uses: mikefarah/yq@4.0.0-beta1
      with:
        # The Command which should be run
        cmd: yq w -i io.freetubeapp.FreeTube.yml modules[0].sources[1].url 'https://github.com/FreeTubeApp/FreeTube/releases/download/v${{ steps.sub.outputs.result }}-beta/freetube-${{ steps.sub.outputs.result }}-linux-portable-arm64.zip'
    - name: Update ARM Hash in yml File
      uses: mikefarah/yq@4.0.0-beta1
      with:
        # The Command which should be run
        cmd: yq w -i io.freetubeapp.FreeTube.yml modules[0].sources[1].sha256 ${{ env.HASH_ARM64 }}
    - name: Add Patch Notes to XML File
      run: xmlstarlet ed -L -i /application/releases/release[1] -t elem -n releaseTMP -v "" -i //releaseTMP -t attr -n version -v "${{ steps.sub.outputs.result }} Beta" -i //releaseTMP -t attr -n date -v "${{ env.CURRENT_DATE }}" -s //releaseTMP -t elem -n url -v "" -s //releaseTMP/url -t text -n "" -v "https://github.com/FreeTubeApp/FreeTube/releases/tag/v${{ steps.sub.outputs.result }}-beta" -r //releaseTMP -v "release" io.freetubeapp.FreeTube.metainfo.xml
    - name: Remove Release Files
      run: |
        rm freetube-${{ steps.sub.outputs.result }}-linux-portable-x64.zip
        rm freetube-${{ steps.sub.outputs.result }}-linux-portable-arm64.zip
    - name: Commit Files
      uses: stefanzweifel/git-auto-commit-action@v5
      with:
        # Optional but recommended
        # Defaults to "Apply automatic changes"
        commit_message: Update files for v${{ steps.sub.outputs.result }}
        token: ${{ secrets.FLATHUB_TOKEN }}

        # Optional options appended to `git-commit`
        # See https://git-scm.com/docs/git-commit for a list of available options
        commit_options: '--no-verify --signoff'

        # Optional: Disable dirty check and always try to create a commit and push
        skip_dirty_check: true
    - name: Create PR
      run: |
        echo ${{ secrets.FLATHUB_TOKEN }} >> auth.txt
        gh auth login --with-token < auth.txt
        rm auth.txt
        gh pr create --title "Release v${{ steps.sub.outputs.result }}" --body "This is an automated PR for the v${{ steps.sub.outputs.result }} release. This PR will be updated and merged once testing is complete."
