name: Test and Release

# Run this job on all pushes and pull requests
# as well as tags with a semantic version
on:
    push:
        branches:
            - "*"
        tags:
            # normal versions
            - "v[0-9]+.[0-9]+.[0-9]+"
            # pre-releases
            - "v[0-9]+.[0-9]+.[0-9]+-**"
    pull_request: {}

jobs:
    # Performs quick checks before the expensive test runs
    check-and-lint:
        if: contains(github.event.head_commit.message, '[skip ci]') == false

        runs-on: ubuntu-latest

        strategy:
            matrix:
                node-version: [14.x]

        steps:
            - name: Checkout code
              uses: actions/checkout@v2

            - name: Use Node.js ${{ matrix.node-version }}
              uses: actions/setup-node@v1
              with:
                  node-version: ${{ matrix.node-version }}

            - name: Install Dependencies
              run: npm ci
              
            - name: Lint TypeScript code
              run: npm run lint
            
            - name: Test package files
              run: npm run test:package

# Deploys the final package to NPM
    deploy:
        needs: [check-and-lint]

        # Trigger this step only when a commit on master is tagged with a version number
        if: |
            contains(github.event.head_commit.message, '[skip ci]') == false &&
            github.event_name == 'push' &&
            github.event.base_ref == 'refs/heads/master' &&
            startsWith(github.ref, 'refs/tags/v')

        runs-on: ubuntu-latest
        strategy:
            matrix:
                node-version: [14.x]

        steps:
            - name: Checkout code
              uses: actions/checkout@v2

            - name: Use Node.js ${{ matrix.node-version }}
              uses: actions/setup-node@v1
              with:
                  node-version: ${{ matrix.node-version }}

            - name: Extract the version and commit body from the tag
              id: extract_release
              # The body may be multiline, therefore newlines and % need to be escaped
              run: |
                  VERSION="${{ github.ref }}"
                  VERSION=${VERSION##*/v}
                  echo "::set-output name=VERSION::$VERSION"
                  BODY=$(git show -s --format=%b)
                  BODY="${BODY//'%'/'%25'}"
                  BODY="${BODY//$'\n'/'%0A'}"
                  BODY="${BODY//$'\r'/'%0D'}"
                  echo "::set-output name=BODY::$BODY"

            - name: Publish package to npm
              run: |
                  npm config set //registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}
                  npm whoami
                  npm publish

            - name: Create Github Release
              uses: actions/create-release@v1
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              with:
                  tag_name: ${{ github.ref }}
                  release_name: Release v${{ steps.extract_release.outputs.VERSION }}
                  draft: false
                  # Prerelease versions create prereleases on Github
                  prerelease: ${{ contains(steps.extract_release.outputs.VERSION, '-') }}
                  body: ${{ steps.extract_release.outputs.BODY }}
