# This is a basic workflow that is manually triggered

name: Update Site Version Number

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

permissions: {}

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

    permissions:
      contents: read

    steps:
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
      with:
        repository: FreeTubeApp/FreeTubeApp.io
        token: ${{ secrets.FLATHUB_TOKEN }}
        persist-credentials: true

    - name: Get Repo Release List
      uses: moustacheful/github-api-exec-action@2135aaccb1220f81e6fa4f14c90cc20efba069fe #v0
      id: list_results
      with:
        # Command to execute, (e.g: `pulls.create`), see https://octokit.github.io/rest.js/ for available commands
        command: repos.listReleases
        payload: >
            {
              "owner": "FreeTubeApp",
              "repo": "FreeTube"
            }
      env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    - name: Create Current Version Variable
      uses: bluwy/substitute-string-action@70ff0b17357670ffd3fee68e95b6de9963b66bad #v3.0.0
      id: current
      with:
        _input-text: ${{ fromJson(steps.list_results.outputs.result)[0].tag_name }}
        -beta: ''
        v: ''
    - name: Create Previous Version Variable
      uses: bluwy/substitute-string-action@70ff0b17357670ffd3fee68e95b6de9963b66bad #v3.0.0
      id: previous
      with:
        _input-text: ${{ fromJson(steps.list_results.outputs.result)[1].tag_name }}
        -beta: ''
        v: ''
    - name: Set Master Branch
    # Currently the default branch is master, but if that changes later, then this acts as a failsafe.
      shell: bash
      run: |
        git checkout master
    - name: Update index.php
      shell: bash
      env:
        CURRENT: ${{ steps.current.outputs.result }}
        PREVIOUS: ${{ steps.previous.outputs.result }}
      run: |
        sed -i "s/${PREVIOUS}/${CURRENT}/g" src/index.php
    - name: Commit Files
      uses: stefanzweifel/git-auto-commit-action@04702edda442b2e678b25b537cec683a1493fcb9 #v7.1.0
      with:
        # Optional but recommended
        # Defaults to "Apply automatic changes"
        commit_message: Update version number to v${{ steps.current.outputs.result }}

        # 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
