---
name: Build

on:
  push:
    branches-ignore:
      - 'l10n_master'
  workflow_dispatch:
    inputs: {}


jobs:
  cloc:
    name: CLOC
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout repo
        uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579

      - name: Set up cloc
        run: |
          sudo apt update
          sudo apt -y install cloc
      - name: Print lines of code
        run: cloc --include-lang TypeScript,JavaScript,HTML,Sass,CSS --vcs git

  lint:
    name: Lint
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout repo
        uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579

      - name: Set up Node
        uses: actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561
        with:
          node-version: '16'

      - name: Run linter
        run: |
          npm ci
          npm run lint

  setup:
    name: Setup
    runs-on: ubuntu-20.04
    outputs:
      repo_url: ${{ steps.gen_vars.outputs.repo_url }}
      adj_build_number: ${{ steps.gen_vars.outputs.adj_build_number }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579

      - name: Get Package Version
        id: gen_vars
        run: |
          repo_url=https://github.com/$GITHUB_REPOSITORY.git
          adj_build_num=${GITHUB_SHA:0:7}

          echo "::set-output name=repo_url::$repo_url"
          echo "::set-output name=adj_build_number::$adj_build_num"

  locales-test:
    name: Locales Test
    runs-on: ubuntu-20.04
    needs:
      - setup
      - lint
    steps:
      - name: Checkout repo
        uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579

      - name: Testing locales - extName length
        run: |
         found_error=false

         echo "Locales Test"
         echo "============"
         echo "extName string must be 40 characters or less"
         echo
         for locale in $(ls src/_locales/); do
           string_length=$(jq '.extName.message | length' src/_locales/$locale/messages.json)
           if [[ $string_length -gt 40 ]]; then
             echo "$locale: $string_length"
             found_error=true
           fi
         done

         if $found_error; then
           echo
           echo "Please fix 'extName' for the locales listed above."
           exit 1
         else
           echo "Test passed!"
         fi

  build:
    name: Build
    runs-on: windows-2019
    needs:
      - setup
      - locales-test
    env:
      _BUILD_NUMBER: ${{ needs.setup.outputs.adj_build_number }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579

      - name: Set up Node
        uses: actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561
        with:
          node-version: '16'

      - name: Print environment
        run: |
          node --version
          npm --version

      - name: NPM setup & test
        run: |
          npm ci
          npm run dist
          npm run test

      - name: Gulp
        run: gulp ci

      - name: Build sources for reviewers
        shell: cmd
        run: |
          mkdir dist\Source
          call git clone %GITHUB_SERVER_URL%/%GITHUB_REPOSITORY% dist\Source
          cd dist\Source
          call git checkout %GITHUB_SHA%
          call git submodule update --init --recursive
          cd ../
          del /S/Q "Source\.git\objects\pack\*"
          call 7z a browser-source.zip "Source\*"

      - name: Upload Opera artifact
        uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2
        with:
          name: dist-opera-${{ env._BUILD_NUMBER }}.zip
          path: dist/dist-opera.zip
          if-no-files-found: error

      - name: Upload Chrome artifact
        uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2
        with:
          name: dist-chrome-${{ env._BUILD_NUMBER }}.zip
          path: dist/dist-chrome.zip
          if-no-files-found: error

      - name: Upload Firefox artifact
        uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2
        with:
          name: dist-firefox-${{ env._BUILD_NUMBER }}.zip
          path: dist/dist-firefox.zip
          if-no-files-found: error

      - name: Upload Edge artifact
        uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2
        with:
          name: dist-edge-${{ env._BUILD_NUMBER }}.zip
          path: dist/dist-edge.zip
          if-no-files-found: error

      - name: Upload browser source
        uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2
        with:
          name: browser-source-${{ env._BUILD_NUMBER }}.zip
          path: dist/browser-source.zip
          if-no-files-found: error

      - name: Upload coverage artifact
        if: false
        uses: actions/upload-artifact@82c141cc518b40d92cc801eee768e7aafc9c2fa2
        with:
          name: coverage-${{ env._BUILD_NUMBER }}.zip
          path: coverage/coverage-${{ env._BUILD_NUMBER }}.zip
          if-no-files-found: error


  crowdin-push:
    name: Crowdin Push
    if: github.ref == 'refs/heads/master'
    runs-on: ubuntu-20.04
    needs:
      - build
    env:
      _CROWDIN_PROJECT_ID: "268134"
    steps:
      - name: Checkout repo
        uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579

      - name: Login to Azure
        uses: Azure/login@1f63701bf3e6892515f1b7ce2d2bf1708b46beaf
        with:
          creds: ${{ secrets.AZURE_PROD_KV_CREDENTIALS }}

      - name: Retrieve secrets
        id: retrieve-secrets
        uses: Azure/get-keyvault-secrets@80ccd3fafe5662407cc2e55f202ee34bfff8c403
        with:
          keyvault: "bitwarden-prod-kv"
          secrets: "crowdin-api-token"

      - name: Upload Sources
        uses: crowdin/github-action@b92956d48336795b1e5e6a353fdef213ada949c5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CROWDIN_API_TOKEN: ${{ steps.retrieve-secrets.outputs.crowdin-api-token }}
        with:
          config: crowdin.yml
          crowdin_branch_name: master
          upload_sources: true
          upload_translations: false


  check-failures:
    name: Check for failures
    if: always()
    runs-on: ubuntu-20.04
    needs:
      - cloc
      - setup
      - locales-test
      - build
      - crowdin-push
      - lint
    steps:
      - name: Check if any job failed
        if: ${{ (github.ref == 'refs/heads/master') || (github.ref == 'refs/heads/rc') }}
        env:
          CLOC_STATUS: ${{ needs.cloc.result }}
          SETUP_STATUS: ${{ needs.setup.result }}
          LOCALES_TEST_STATUS: ${{ needs.locales-test.result }}
          BUILD_STATUS: ${{ needs.build.result }}
          CROWDIN_PUSH_STATUS: ${{ needs.crowdin-push.result }}
          LINT_STATUS: ${{ needs.lint.result }}
        run: |
          if [ "$CLOC_STATUS" = "failure" ]; then
              exit 1
          elif [ "$SETUP_STATUS" = "failure" ]; then
              exit 1
          elif [ "$LOCALES_TEST_STATUS" = "failure" ]; then
              exit 1
          elif [ "$BUILD_STATUS" = "failure" ]; then
              exit 1
          elif [ "$CROWDIN_PUSH_STATUS" = "failure" ]; then
              exit 1
          elif [ "LINT_STATUS" = "failure" ]; then
              exit 1
          fi

      - name: Login to Azure - Prod Subscription
        uses: Azure/login@1f63701bf3e6892515f1b7ce2d2bf1708b46beaf
        if: failure()
        with:
          creds: ${{ secrets.AZURE_PROD_KV_CREDENTIALS }}

      - name: Retrieve secrets
        id: retrieve-secrets
        uses: Azure/get-keyvault-secrets@80ccd3fafe5662407cc2e55f202ee34bfff8c403
        if: failure()
        with:
          keyvault: "bitwarden-prod-kv"
          secrets: "devops-alerts-slack-webhook-url"

      - name: Notify Slack on failure
        uses: act10ns/slack@da3191ebe2e67f49b46880b4633f5591a96d1d33
        if: failure()
        env:
          SLACK_WEBHOOK_URL: ${{ steps.retrieve-secrets.outputs.devops-alerts-slack-webhook-url }}
        with:
          status: ${{ job.status }}
