name: 'HA Frontend - make SDIST'

on:
  workflow_dispatch:
    inputs:
      frontend_version:
        description: 'Home Assistant Frontend Version'
        required: true
        default: 'latest'

env:
  PYTHON_VERSION: 3.8
  NODE_VERSION: 14
  NODE_OPTIONS: --max_old_space_size=6144

jobs:
  make-sdist:
    runs-on: ubuntu-latest

    steps:
    - name: get-version
      id: get-version
      run: |
        if [ "${{ github.event.inputs.frontend_version }}" == "latest"  ];then
          wget -qO - https://api.github.com/repos/home-assistant/frontend/releases/latest|jq -r '.tag_name'
          echo "{version}=$(wget -qO - https://api.github.com/repos/home-assistant/frontend/releases/latest|jq -r '.tag_name') >> $GITHUB_OUTPUT"
        else
          echo "${{ github.event.inputs.frontend_version }}"
          echo "{version}=${{ github.event.inputs.frontend_version }} >> $GITHUB_OUTPUT"
        fi

    - name: Checkout frontend repository
      uses: actions/checkout@v3
      with:
        repository: home-assistant/frontend
        ref: ${{ steps.get-version.outputs.version }}

    - name: Set up Python ${{ env.PYTHON_VERSION }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ env.PYTHON_VERSION }}

    - name: Set up Node ${{ env.NODE_VERSION }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ env.NODE_VERSION }}
        cache: yarn

    - name: Install dependencies
      run: yarn install

   #- name: Download Translations
   #  run: ./script/translations_download
   #  env:
   #    LOKALISE_TOKEN: ${{ secrets.LOKALISE_TOKEN }}

    - name: Build and release package
      run: |
        python3 -m pip install build
        #export TWINE_USERNAME="__token__"
        #export TWINE_PASSWORD="${{ secrets.TWINE_TOKEN }}"
        #script/release
        script/build_frontend
        rm -rf dist home_assistant_frontend.egg-info
        python3 -m build

    - name: Upload build artifacts
      uses: actions/upload-artifact@v2
      with:
        name: ha-frontend-${{ steps.get-version.outputs.version }}
        path: ./dist/*.tar.gz

    - name: Upload SDIST to nextcloud
      run: |
        sudo add-apt-repository ppa:nextcloud-devs/client
        sudo apt update
        sudo apt install nextcloud-client
        rm dist/*.whl
        sudo nextcloudcmd -s -u ${{ secrets.NEXTCLOUD_USER }} -p ${{ secrets.NEXTCLOUD_PASSWORD }} --non-interactive dist/ ${{ secrets.NEXTCLOUD_URL }}

    - name: Create Issue
      uses: actions-ecosystem/action-create-issue@v1
      with:
        github_token: ${{ secrets.PAT_TOKEN }}
        title: new HA Frontend ${{ steps.get-version.outputs.version }}
        body: |
          SDIST uploaded to nextcloud
