<commit_before>on:
  push:
    branches-ignore:
      - 'no-ci-*'
      - 'wip-*'
      - 'x-*'
    tags:
      - 'releases/*'
      - 'prereleases/*'

name: Build MAGE Server and Optionally Release

env:
  MAGE_NODE_VERSIONS: 14 16

jobs:

  config:
    runs-on: ubuntu-latest
    outputs:
      # '' to disable tests, 'true' to enable
      tests_enabled: 'true'
      node_versions-all: ${{ steps.node_versions.outputs.all }}
      node_versions-all-json: ${{ steps.node_versions.outputs.all-json }}
      node_versions-latest: ${{ steps.node_versions.outputs.latest }}
      node_versions-pre_latest: ${{ steps.node_versions.outputs.pre_latest }}
      node_versions-pre_latest-json: ${{ steps.node_versions.outputs.pre_latest-json }}
      release-is_release: ${{ steps.release_info.outputs.is_release }}
      release-is_prerelease: ${{ steps.release_info.outputs.is_prerelease }}
      release-name: ${{ steps.release_info.outputs.version }}

    steps:
      - id: sort_node_versions
        name: sort versions
        run: |
          echo 'node_versions_lines<<EOF' >> $GITHUB_ENV
          echo ${MAGE_NODE_VERSIONS} | tr ' ' '\n' | sort -V >> $GITHUB_ENV
          echo 'EOF' >> $GITHUB_ENV
      - id: node_versions
        name: main
        run: |
          node_versions_arr=($(echo "${node_versions_lines}"))
          node_versions_latest=${node_versions_arr[-1]}
          node_versions_pre_latest=${node_versions_arr[@]:0:${#node_versions_arr[@]}-1}
          node_versions_pre_latest=(${node_versions_arr[@]:0:-1})
          node_versions_json=$(printf '"%s",' ${node_versions_arr[@]})
          node_versions_json="[ ${node_versions_json:0:-1} ]"
          node_versions_pre_latest_json=$(printf '"%s",' ${node_versions_pre_latest[@]})
          node_versions_pre_latest_json="[ ${node_versions_pre_latest_json:0:-1} ]"
          echo "node versions arr: ${node_versions_arr[@]}"
          echo "node versions json: ${node_versions_json}"
          echo "node versions latest: ${node_versions_latest}"
          echo "node versions pre_latest: ${node_versions_pre_latest}"
          echo "node versions pre_latest json: ${node_versions_pre_latest_json}"
          echo ::set-output name=all::${node_versions_arr}
          echo ::set-output name=all-json::${node_versions_json}
          echo ::set-output name=latest::${node_versions_latest}
          echo ::set-output name=pre_latest::${node_versions_pre_latest}
          echo ::set-output name=pre_latest-json::${node_versions_pre_latest_json}
      - id: release_info
        name: get release info
        run: |
          mage_version=$(echo ${GITHUB_REF_NAME} | sed -E 's/^(pre)?releases\///i')
          [[ -z ${mage_version} ]] && mage_version=${GITHUB_REF}
          echo ::set-output name=is_release::${{ (github.ref_type == 'tag' && contains(github.ref_name, 'releases/')) || '' }}
          echo ::set-output name=is_prerelease::${{ startsWith(github.ref_name, 'prereleases/') || '' }}
          echo ::set-output name=version::${mage_version}

  build_service:
    name: build mage service
    needs: config
    runs-on: ubuntu-latest
    steps:
      - name: install native dev libs
        run: |
          sudo apt-get install build-essential
      - name: checkout
        uses: actions/checkout@v2
      - name: setup node
        uses: actions/setup-node@v2
        with:
          node-version: ${{ needs.config.outputs.node_versions-latest }}
          cache: npm
          cache-dependency-path: |
            service/package-lock.json
            plugins/nga-msi/package-lock.json
            plugins/image/service/package-lock.json
      - name: build service
        run: |
          cd service
          npm ci
          npm run build
      - name: pack service
        run: npm pack ./service
      - name: build plugin nga-msi
        run: |
          cd plugins/nga-msi
          npm ci
          npm run build
      - name: pack nga-msi
        run: npm pack ./plugins/nga-msi
      - name: build plugin image.service
        run: |
          cd plugins/image/service
          npm ci
          npm run build
      - name: pack plugin image.service
        run: npm pack ./plugins/image/service
      - name: upload packages
        uses: actions/upload-artifact@v2
        with:
          name: mage_service_packages
          path: |
            ngageoint-mage.*.tgz

  test_service:
    name: test mage service
    needs: [ config, build_service ]
    if: ${{ needs.config.outputs.tests_enabled }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node: ${{ fromJSON(needs.config.outputs.node_versions-all-json) }}
    steps:
      - name: checkout
        uses: actions/checkout@v2
      - name: setup node
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node }}
          cache: npm
          cache-dependency-path: |
            service/package-lock.json
      - name: install service dependencies
        run: |
          npm ci --prefix service
      - name: download service package
        uses: actions/download-artifact@v2
        with:
          name: mage_service_packages
      - name: extract service lib
        run: |
          tar xzf $(ls -1 ngageoint-mage.service-*.tgz) package/lib
          mv package/lib ./service
          rmdir package
      - name: test service with node ${{ matrix.node }}
        run: |
          cd service
          npm run ci:test
      # TODO: restore coveralls test coverage report - see buildAndTest.yml

  # build_image_plugin:
  #   name: build image plugin
  #   needs: [ config, test_service ]
  #   runs-on: ubuntu-latest
  #   steps:
  #     - name: checkout
  #       uses: actions/checkout@v2
  #     - name: setup node
  #       uses: actions/setup-node@v2
  #       with:
  #         node-version: ${{ needs.config.outputs.node_versions-latest }}
  #         cache: npm
  #         cache-dependency-path: |
  #           service/package-lock.json
  #           plugins/image/service/package-lock.json
  #     - name: download service packages
  #       uses: actions/download-artifact@v2
  #       with:
  #         name: mage_service_packages

  # build_msi_plugin:
  #   name: build nga-msi plugin
  #   needs: [ config, test_service ]
  #   runs-on: ubuntu-latest

  build_web-app:
    name: build mage web app
    needs: config
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v2
      - name: setup node
        uses: actions/setup-node@v2
        with:
          # TODO: angular 9 build does not run on node > 14
          node-version: 14
          cache: npm
          cache-dependency-path: web-app/package-lock.json
      - name: build web-app
        run: |
          cd web-app
          npm ci
          npm run build
        env:
          NODE_OPTIONS: "--max_old_space_size=4096"
      - name: test web-app
        if: ${{ needs.config.outputs.tests_enabled }}
        run: npm run --prefix web-app ci:test
      - name: pack web-app
        run: |
          npm pack ./web-app/dist/core-lib
          npm pack ./web-app/dist/app
      - name: upload packages
        uses: actions/upload-artifact@v2
        with:
          name: mage_web-app_packages
          path: |
            ngageoint-mage.*.tgz

  create_release_stub:
    # so the matrix job does not race to create the release
    name: release - create empty release
    needs: [ config, build_service, build_web-app ]
    runs-on: ubuntu-latest
    steps:
      - name: main
        if: ${{ needs.config.outputs.release-is_release }}
        uses: softprops/action-gh-release@v1
        with:
          name: ${{ needs.config.outputs.release-name }}
          prerelease: ${{ needs.config.outputs.release-is_prerelease }}

  release_packages:
    name:  release - add npm package tarballs
    needs: [ config, create_release_stub ]
    if: ${{ needs.config.outputs.release-is_release }}
    runs-on: ubuntu-latest
    steps:
      - name: setup node
        uses: actions/setup-node@v2
        with:
          node-version: ${{ needs.config.outputs.node_versions-latest }}
      - name: download service packages
        uses: actions/download-artifact@v2
        with:
          name: mage_service_packages
      - name: download web-app packages
        uses: actions/download-artifact@v2
        with:
          name: mage_web-app_packages
      - name: publish to gitlab
        env:
          NPM_CONFIG_USERCONFIG: "${{ runner.temp }}/gitlab.npmrc"
        run: |
          npm config set @ngageoint:registry https://gitlab.gs.mil/api/v4/projects/14746/packages/npm/
          npm config set -- '//gitlab.gs.mil/api/v4/projects/14746/packages/npm/:_authToken' ${{ secrets.GITLAB_PACKAGE_REGISTRY_TOKEN }}
          npm publish $(ls -1 ngageoint-mage.service-*.tgz)
          npm publish $(ls -1 ngageoint-mage.web-core-lib-*.tgz)
          npm publish $(ls -1 ngageoint-mage.web-app-*.tgz)
          check_version=$(npm pkg --prefix ./plugins/image/service get version | tr -d '"')
          echo "checking @ngageoint/mage.image.service@${check_version}"
          [[ -n $(npm view @ngageoint/mage.image.service@${check_version} version) ]] || npm publish $(ls -1 ngageoint-mage.image.service-*.tgz)
          check_version=$(npm pkg --prefix ./plugins/nga-msi get version | tr -d '"')
          echo "checking @ngageoint/mage.nga-msi@${check_version}"
          [[ -n $(npm view @ngageoint/mage.nga-msi@${check_version} version) ]] || npm publish $(ls -1 ngageoint-mage.nga-msi-*.tgz)
      - name: add package tarballs to release
        uses: softprops/action-gh-release@v1
        with:
          name: ${{ needs.config.outputs.release-name }}
          files: |
            package.json
            ngageoint-mage.*.tgz

  # release_prebuilt_platform_instance:
  #   name:  release - add pre-built mage instance tarballs<commit_msg>[ci] test version check before publishing plugin packages<commit_after>on:
  push:
    branches-ignore:
      - 'no-ci-*'
      - 'wip-*'
      - 'x-*'
    tags:
      - 'releases/*'
      - 'prereleases/*'

name: Build MAGE Server and Optionally Release

env:
  MAGE_NODE_VERSIONS: 14 16

jobs:

  config:
    runs-on: ubuntu-latest
    outputs:
      # '' to disable tests, 'true' to enable
      tests_enabled: 'true'
      node_versions-all: ${{ steps.node_versions.outputs.all }}
      node_versions-all-json: ${{ steps.node_versions.outputs.all-json }}
      node_versions-latest: ${{ steps.node_versions.outputs.latest }}
      node_versions-pre_latest: ${{ steps.node_versions.outputs.pre_latest }}
      node_versions-pre_latest-json: ${{ steps.node_versions.outputs.pre_latest-json }}
      release-is_release: ${{ steps.release_info.outputs.is_release }}
      release-is_prerelease: ${{ steps.release_info.outputs.is_prerelease }}
      release-name: ${{ steps.release_info.outputs.version }}

    steps:
      - id: sort_node_versions
        name: sort versions
        run: |
          echo 'node_versions_lines<<EOF' >> $GITHUB_ENV
          echo ${MAGE_NODE_VERSIONS} | tr ' ' '\n' | sort -V >> $GITHUB_ENV
          echo 'EOF' >> $GITHUB_ENV
      - id: node_versions
        name: main
        run: |
          node_versions_arr=($(echo "${node_versions_lines}"))
          node_versions_latest=${node_versions_arr[-1]}
          node_versions_pre_latest=${node_versions_arr[@]:0:${#node_versions_arr[@]}-1}
          node_versions_pre_latest=(${node_versions_arr[@]:0:-1})
          node_versions_json=$(printf '"%s",' ${node_versions_arr[@]})
          node_versions_json="[ ${node_versions_json:0:-1} ]"
          node_versions_pre_latest_json=$(printf '"%s",' ${node_versions_pre_latest[@]})
          node_versions_pre_latest_json="[ ${node_versions_pre_latest_json:0:-1} ]"
          echo "node versions arr: ${node_versions_arr[@]}"
          echo "node versions json: ${node_versions_json}"
          echo "node versions latest: ${node_versions_latest}"
          echo "node versions pre_latest: ${node_versions_pre_latest}"
          echo "node versions pre_latest json: ${node_versions_pre_latest_json}"
          echo ::set-output name=all::${node_versions_arr}
          echo ::set-output name=all-json::${node_versions_json}
          echo ::set-output name=latest::${node_versions_latest}
          echo ::set-output name=pre_latest::${node_versions_pre_latest}
          echo ::set-output name=pre_latest-json::${node_versions_pre_latest_json}
      - id: release_info
        name: get release info
        run: |
          mage_version=$(echo ${GITHUB_REF_NAME} | sed -E 's/^(pre)?releases\///i')
          [[ -z ${mage_version} ]] && mage_version=${GITHUB_REF}
          echo ::set-output name=is_release::${{ (github.ref_type == 'tag' && contains(github.ref_name, 'releases/')) || '' }}
          echo ::set-output name=is_prerelease::${{ startsWith(github.ref_name, 'prereleases/') || '' }}
          echo ::set-output name=version::${mage_version}

  build_service:
    name: build mage service
    needs: config
    runs-on: ubuntu-latest
    steps:
      - name: install native dev libs
        run: |
          sudo apt-get install build-essential
      - name: checkout
        uses: actions/checkout@v2
      - name: setup node
        uses: actions/setup-node@v2
        with:
          node-version: ${{ needs.config.outputs.node_versions-latest }}
          cache: npm
          cache-dependency-path: |
            service/package-lock.json
            plugins/nga-msi/package-lock.json
            plugins/image/service/package-lock.json
      - name: build service
        run: |
          cd service
          npm ci
          npm run build
      - name: pack service
        run: npm pack ./service
      - name: build plugin nga-msi
        run: |
          cd plugins/nga-msi
          npm ci
          npm run build
      - name: pack nga-msi
        run: npm pack ./plugins/nga-msi
      - name: build plugin image.service
        run: |
          cd plugins/image/service
          npm ci
          npm run build
      - name: pack plugin image.service
        run: npm pack ./plugins/image/service
      - name: upload packages
        uses: actions/upload-artifact@v2
        with:
          name: mage_service_packages
          path: |
            ngageoint-mage.*.tgz

  test_service:
    name: test mage service
    needs: [ config, build_service ]
    if: ${{ needs.config.outputs.tests_enabled }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node: ${{ fromJSON(needs.config.outputs.node_versions-all-json) }}
    steps:
      - name: checkout
        uses: actions/checkout@v2
      - name: setup node
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node }}
          cache: npm
          cache-dependency-path: |
            service/package-lock.json
      - name: install service dependencies
        run: |
          npm ci --prefix service
      - name: download service package
        uses: actions/download-artifact@v2
        with:
          name: mage_service_packages
      - name: extract service lib
        run: |
          tar xzf $(ls -1 ngageoint-mage.service-*.tgz) package/lib
          mv package/lib ./service
          rmdir package
      - name: test service with node ${{ matrix.node }}
        run: |
          cd service
          npm run ci:test
      # TODO: restore coveralls test coverage report - see buildAndTest.yml

  # build_image_plugin:
  #   name: build image plugin
  #   needs: [ config, test_service ]
  #   runs-on: ubuntu-latest
  #   steps:
  #     - name: checkout
  #       uses: actions/checkout@v2
  #     - name: setup node
  #       uses: actions/setup-node@v2
  #       with:
  #         node-version: ${{ needs.config.outputs.node_versions-latest }}
  #         cache: npm
  #         cache-dependency-path: |
  #           service/package-lock.json
  #           plugins/image/service/package-lock.json
  #     - name: download service packages
  #       uses: actions/download-artifact@v2
  #       with:
  #         name: mage_service_packages

  # build_msi_plugin:
  #   name: build nga-msi plugin
  #   needs: [ config, test_service ]
  #   runs-on: ubuntu-latest

  build_web-app:
    name: build mage web app
    needs: config
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v2
      - name: setup node
        uses: actions/setup-node@v2
        with:
          # TODO: angular 9 build does not run on node > 14
          node-version: 14
          cache: npm
          cache-dependency-path: web-app/package-lock.json
      - name: build web-app
        run: |
          cd web-app
          npm ci
          npm run build
        env:
          NODE_OPTIONS: "--max_old_space_size=4096"
      - name: test web-app
        if: ${{ needs.config.outputs.tests_enabled }}
        run: npm run --prefix web-app ci:test
      - name: pack web-app
        run: |
          npm pack ./web-app/dist/core-lib
          npm pack ./web-app/dist/app
      - name: upload packages
        uses: actions/upload-artifact@v2
        with:
          name: mage_web-app_packages
          path: |
            ngageoint-mage.*.tgz

  create_release_stub:
    # so the matrix job does not race to create the release
    name: release - create empty release
    needs: [ config, build_service, build_web-app ]
    runs-on: ubuntu-latest
    steps:
      - name: main
        if: ${{ needs.config.outputs.release-is_release }}
        uses: softprops/action-gh-release@v1
        with:
          name: ${{ needs.config.outputs.release-name }}
          prerelease: ${{ needs.config.outputs.release-is_prerelease }}

  release_packages:
    name:  release - add npm package tarballs
    needs: [ config, create_release_stub ]
    if: ${{ needs.config.outputs.release-is_release }}
    runs-on: ubuntu-latest
    steps:
      - name: setup node
        uses: actions/setup-node@v2
        with:
          node-version: ${{ needs.config.outputs.node_versions-latest }}
      - name: install json
        run: npm i -g json
      - name: download service packages
        uses: actions/download-artifact@v2
        with:
          name: mage_service_packages
      - name: download web-app packages
        uses: actions/download-artifact@v2
        with:
          name: mage_web-app_packages
      - name: publish to gitlab
        env:
          NPM_CONFIG_USERCONFIG: "${{ runner.temp }}/gitlab.npmrc"
        run: |
          npm config set @ngageoint:registry https://gitlab.gs.mil/api/v4/projects/14746/packages/npm/
          npm config set -- '//gitlab.gs.mil/api/v4/projects/14746/packages/npm/:_authToken' ${{ secrets.GITLAB_PACKAGE_REGISTRY_TOKEN }}
          npm publish $(ls -1 ngageoint-mage.service-*.tgz)
          npm publish $(ls -1 ngageoint-mage.web-core-lib-*.tgz)
          npm publish $(ls -1 ngageoint-mage.web-app-*.tgz)
          check_version=$(tar xzOf ngageoint-mage.image.service-*.tgz package/package.json | json version)
          echo "checking @ngageoint/mage.image.service@${check_version}"
          [[ -n $(npm view @ngageoint/mage.image.service@${check_version} version) ]] || npm publish $(ls -1 ngageoint-mage.image.service-*.tgz)
          check_version=$(tar xzOf ngageoint-mage.nga-msi-*.tgz package/package.json | json version)
          echo "checking @ngageoint/mage.nga-msi@${check_version}"
          [[ -n $(npm view @ngageoint/mage.nga-msi@${check_version} version) ]] || npm publish $(ls -1 ngageoint-mage.nga-msi-*.tgz)
      - name: add package tarballs to release
        uses: softprops/action-gh-release@v1
        with:
          name: ${{ needs.config.outputs.release-name }}
          files: |
            package.json
            ngageoint-mage.*.tgz

  # release_prebuilt_platform_instance:
  #   name:  release - add pre-built mage instance tarballs
{'fraction_non_alphanumeric': 0.11861680739847205, 'fraction_numerical': 0.005729794933655006, 'mean_word_length': 2.145770750988142, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2636047', 'n_tokens_mistral': 6347, 'n_tokens_neox': 6020, 'n_words': 1320}