name: Update Translation Files

on:
  push:
    branches:
      - master

jobs:
  build:

    runs-on: ubuntu-latest

    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      INVENTREE_DB_NAME: './test_db.sqlite'
      INVENTREE_DB_ENGINE: django.db.backends.sqlite3
      INVENTREE_DEBUG: info
      INVENTREE_MEDIA_ROOT: ./media
      INVENTREE_STATIC_ROOT: ./static

    steps:
    - uses: actions/checkout@v2
    - name: Get Current Translations
      run: |
        git fetch
        git checkout origin/l10 -- `git ls-tree origin/l10 -r --name-only | grep ".po"`
        git reset
    - name: Set up Python 3.7
      uses: actions/setup-python@v1
      with:
        python-version: 3.7
    - name: Install Dependencies
      run: |
          sudo apt-get update
          sudo apt-get install -y gettext
          pip3 install invoke
          invoke install
    - name: Make Translations
      run: |
        invoke translate
    - name: stash changes
      run: |
        git stash
    - name: Checkout Translation Branch
      uses: actions/checkout@v2.3.4
      with:
        ref: l10
    - name: Commit files
      run: |
        git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
        git config --local user.name "github-actions[bot]"
        git checkout stash -- .
        git reset
        git add "*.po"
        git commit -m "updated translation base"
    - name: Push changes
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: l10
