name: Stable

on:
  push:
    tags:
      - '*'

jobs:

  # Builds the package and creates artifact with dist files
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
    - name: Set PHP version
      run: sudo update-alternatives --set php /usr/bin/php7.4
    - name: Checkout
      uses: actions/checkout@v2
    - name: Setup Node
      uses: actions/setup-node@v1
      with:
        node-version: '11'
    - name: Setup variables
      id: vars
      run: |
        echo "::set-output name=yarn-cache-path::$(yarn cache dir)"
        echo "::set-output name=composer-cache-path::$(composer config cache-files-dir)"
    - name: Cache Yarn
      uses: actions/cache@v1
      with:
        path: ${{ steps.vars.outputs.yarn-cache-path }}
        key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
        restore-keys: ${{ runner.os }}-yarn-
    - name: Cache Composer
      uses: actions/cache@v1
      with:
        path: ${{ steps.vars.outputs.composer-cache-path }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: ${{ runner.os }}-composer-
    - name: Install Node dependencies
      run: yarn install --no-progress
    - name: Install Composer dependencies
      run: composer install -o --no-dev --no-progress
    - name: Build static assets
      run: yarn build:production
    - name: Create build dir
      run: mkdir build
    - name: Copy files
      uses: Pendect/action-rsyncer@v1.1.0
      env:
        DEPLOY_KEY: ${{ secrets.PACKAGES_DEPLOY_KEY }}
      with:
        flags: '-avq'
        options: '--exclude-from=".build-excludes"'
        src: '.'
        dest: 'build'
    - name: Fix permissions
      run: sudo chown -R $USER:$USER ./
    - name: Upload artifact
      uses: actions/upload-artifact@v1
      with:
        name: build
        path: build

  # Creates the stable package artifact when tag is created
  pack:
    name: Pack
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Setup variables
        id: vars
        run: |
          echo "::set-output name=package-name::${{ secrets.SLUG }}-${GITHUB_REF#refs/tags/}"
      - name: Download artifact
        uses: actions/download-artifact@v1
        with:
          name: build
          path: ${{ secrets.SLUG }}
      - name: Create archive
        run: |
          zip -rq ${{ steps.vars.outputs.package-name }}.zip ${{ secrets.SLUG }}
      - name: Upload artifact
        uses: actions/upload-artifact@v1
        with:
          name: ${{ steps.vars.outputs.package-name }}
          path: ${{ steps.vars.outputs.package-name }}.zip

  # Uploads the stable package to internal repository
  upload:
    name: Upload to repo
    runs-on: ubuntu-latest
    needs: pack
    steps:
      - name: Setup variables
        id: vars
        run: |
          echo "::set-output name=package-name::${{ secrets.SLUG }}-${GITHUB_REF#refs/tags/}"
      - name: Download artifact
        uses: actions/download-artifact@v1
        with:
          name: ${{ steps.vars.outputs.package-name }}
          path: .
      - name: Upload to repo
        uses: Pendect/action-rsyncer@v1.1.0
        env:
          DEPLOY_KEY: ${{ secrets.PACKAGES_DEPLOY_KEY }}
        with:
          flags: '-avz'
          options: '--recursive '
          src: '.'
          dest: '${{ secrets.BRACKETSPACE_REPO_RSYNC_URL }}${{ secrets.SLUG }}/'

  # Release on GitHub
  github-release:
    name: Release on GitHub
    runs-on: ubuntu-latest
    needs: pack
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup variables
        id: vars
        run: |
          echo "::set-output name=version::${GITHUB_REF#refs/tags/}"
          echo "::set-output name=package-name::${{ secrets.SLUG }}-${GITHUB_REF#refs/tags/}"
      - name: Parse changelog
        run: |
          START="= ${{ steps.vars.outputs.version }} ="
          END="= [0-9]+.[0-9]+.[0-9]+ =|==|\$"
          grep -oPz "(?s)${START}.*?\n\K.*?(?=${END})" readme.txt > changelog.txt
          truncate -s-2 changelog.txt
      - name: Download artifact
        uses: actions/download-artifact@v1
        with:
          name: ${{ steps.vars.outputs.package-name }}
          path: .
      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          body_path: changelog.txt
          files: ${{ steps.vars.outputs.package-name }}.zip
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  # Deploys the stable tag to WordPress.org repository
  wordpress-org-release:
    name: Release on WordPress.org
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup Node
        uses: actions/setup-node@v1
        with:
          node-version: '11'
      - name: Setup variables
        id: vars
        run: |
          echo "::set-output name=yarn-cache-path::$(yarn cache dir)"
          echo "::set-output name=composer-cache-path::$(composer config cache-files-dir)"
      - name: Cache Yarn
        uses: actions/cache@v1
        with:
          path: ${{ steps.vars.outputs.yarn-cache-path }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: ${{ runner.os }}-yarn-
      - name: Cache Composer
        uses: actions/cache@v1
        with:
          path: ${{ steps.vars.outputs.composer-cache-path }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer-
      - name: Install Node dependencies
        run: yarn install --no-progress
      - name: Install Composer dependencies
        run: composer install -o --no-dev --no-progress
      - name: Build static assets
        run: yarn build:production
      - name: Create .distignore file
        run: cp .build-excludes .distignore
      - name: Deploy to WordPress repository
        uses: 10up/action-wordpress-plugin-deploy@1.4.0
        env:
          SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
          SVN_USERNAME: ${{ secrets.SVN_USERNAME }}
          SLUG: ${{ secrets.SLUG }}
