name: Validate

on:
  pull_request:
    branches:
      - master
      - develop
      - 'release/*'

jobs:
  openapi-spec:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Code
      uses: actions/checkout@v2
    - name: Setup Node v14
      uses: actions/setup-node@v2
      with:
        node-version: 14

    # https://github.com/actions/cache/blob/main/examples.md#node---yarn
    - name: Get yarn cache directory path
      id: yarn-cache-dir-path
      run: echo "::set-output name=dir::$(yarn cache dir)"

    - uses: actions/cache@v2
      id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
      with:
        path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
        key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
        restore-keys: |
          ${{ runner.os }}-yarn-

    - name: Setup Go v1.17.3
      uses: actions/setup-go@v2
      with:
        go-version: '^1.17.3'

    - name: Prebuild docs
      run: yarn prebuild:docs

    - name: Build OpenAPI 2.0 Spec
      run: yarn build:docs

    # Install dependencies globally to bypass installing all frontend deps
    - name: Install swagger2openapi and swagger-cli
      run: yarn global add swagger2openapi @apidevtools/swagger-cli

    # OpenAPI2.0 does not support multiple body params (which we utilise in some of our handlers).
    # OAS3.0 however does support multiple body params - hence its best to convert the generated OAS 2.0
    # to OAS 3.0 and validate the output of generated OAS 3.0 instead.
    - name: Convert OpenAPI 2.0 to OpenAPI 3.0 and validate spec
      run: yarn validate:docs
