# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [lts/hydrogen]

    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}

      - name: Write VPN Files
        run: |
          echo "$CA_CRT" > .github/vpn/ca.crt
          echo "$USER_CRT" > .github/vpn/user.crt
          echo "$USER_KEY" > .github/vpn/user.key
          echo "$TLS_KEY" > .github/vpn/tls.key
        shell: bash
        env:
          CA_CRT: ${{ secrets.CA_CRT}}
          USER_CRT: ${{ secrets.USER_CRT }}
          USER_KEY: ${{ secrets.USER_KEY }}
          TLS_KEY: ${{ secrets.TLS_KEY }}

      - name: Install Open VPN
        run: |
          sudo apt install apt-transport-https
          sudo wget https://swupdate.openvpn.net/repos/openvpn-repo-pkg-key.pub
          sudo apt-key add openvpn-repo-pkg-key.pub
          sudo wget -O /etc/apt/sources.list.d/openvpn3.list https://swupdate.openvpn.net/community/openvpn3/repos/openvpn3-jammy.list
          sudo apt update
          sudo apt install openvpn3=17~betaUb22042+jammy
      - name: Start Open VPN 3
        run: openvpn3 session-start --config .github/vpn/config.ovpn
      - name: Install Doxygen
        run: sudo apt-get install doxygen

      - name: Install dependencies
        run: npm ci

      - name: Check code style (aborts if errors found)
        run: npx @sasjs/cli lint

      - name: Add client
        run: echo "CLIENT=${{secrets.SAS9_4GL_IO_CLIENT}}"> .env.server

      - name: Add access token
        run: echo "ACCESS_TOKEN=${{secrets.SAS9_4GL_IO_ACCESS_TOKEN}}" >> .env.server

      - name: Add refresh token
        run: echo "REFRESH_TOKEN=${{secrets.SAS9_4GL_IO_REFRESH_TOKEN}}" >> .env.server

      - name: Build & Deploy Project to SAS server
        run: npx @sasjs/cli cbd -t server

      - name: Run all tests
        run: npx @sasjs/cli test -t server
        env:
          CI: true
          CLIENT: ${{secrets.CLIENT}}
          SECRET: ${{secrets.SECRET}}
          SAS_USERNAME: ${{secrets.SAS_USERNAME}}
          SAS_PASSWORD: ${{secrets.SAS_PASSWORD}}
          ACCESS_TOKEN: ${{secrets.ACCESS_TOKEN}}
          REFRESH_TOKEN: ${{secrets.REFRESH_TOKEN}}
