<commit_before>version: 2.1

commands:
  setup-home-local-bin:
    steps:
      - run:
          shell: /bin/bash -euo pipefail
          command: |
            mkdir -p "$HOME/.local/bin"
            line='export PATH="$HOME/.local/bin:$PATH"'
            if grep "$line" $BASH_ENV >/dev/null; then
              echo "$line" >> $BASH_ENV
            fi

  invoke-lazy-sh:
    parameters:
      subcommand:
        type: string
    steps:
      - run:
          environment:
            TERM: xterm
          command: ./lazy.sh <<parameters.subcommand>>

  apt-update-and-install-common-deps:
    steps:
      - run: sudo apt update && sudo apt install gawk make

  restore-cache-gomod:
    steps:
      - restore_cache:
          key: go-mod-v4-{{ checksum "go.sum" }}
  save-cache-gomod:
    steps:
      - save_cache:
          key: go-mod-v4-{{ checksum "go.sum" }}
          paths:
            - "/go/pkg/mod"

  install-godep:
    steps:
      - apt-update-and-install-common-deps
      - invoke-lazy-sh:
          subcommand: godep

  install-docdep:
    steps:
      - apt-update-and-install-common-deps
      - run: sudo apt install python3 python3-pip libgirepository1.0-dev
      - invoke-lazy-sh:
          subcommand: docdep

  download-and-install-minio-client:
    steps:
      - setup-home-local-bin
      - restore_cache:
          key: minio-client-v2
      - run:
          shell: /bin/bash -eo pipefail
          command: |
            if which mc; then exit 0; fi
            sudo curl -sSL https://dl.min.io/client/mc/release/linux-amd64/archive/mc.RELEASE.2020-08-20T00-23-01Z \
              -o "$HOME/.local/bin/mc"
            sudo chmod +x "$HOME/.local/bin/mc"
      - save_cache:
          key: minio-client-v2
          paths:
            - "$HOME/.local/bin/mc"

  upload-minio:
    parameters:
      src:
        type: string
      dst:
        type: string
    steps:
      - run:
          shell: /bin/bash -eo pipefail
          when: always
          command: |
            if [ -n "$CIRCLE_PR_NUMBER" ]; then # CIRCLE_PR_NUMBER is guaranteed to be only present in forked PRs (external)
              echo "Forked PR detected. Sry, can't trust you with credentials to external artifact store, use CircleCI's instead."
              exit 0
            fi
            set -u # from now on

            mc config host add --api s3v4 zrepl-minio https://minio.cschwarz.com ${MINIO_ACCESS_KEY} ${MINIO_SECRET_KEY}

            # keep in sync with set-github-minio-status
            jobprefix=zrepl-ci-artifacts/test/${CIRCLE_SHA1}/${CIRCLE_JOB}

            # Upload artifacts
            mkdir -p ./artifacts
            mc cp -r <<parameters.src>> "zrepl-minio/$jobprefix/<<parameters.dst>>"

  set-github-minio-status:
    parameters:
      context:
        type: string
      description:
        type: string
      minio-dst:
        type: string
    steps:
      - run:
          shell: /bin/bash -eo pipefail
          command: |
            if [ -n "$CIRCLE_PR_NUMBER" ]; then # CIRCLE_PR_NUMBER is guaranteed to be only present in forked PRs (external)
              echo "Forked PR detected. Sry, can't trust you with credentials to external artifact store, use CircleCI's instead."
              exit 0
            fi
            set -u # from now on

            # keep in sync with with upload-minio command
            jobprefix=zrepl-ci-artifacts/test/${CIRCLE_SHA1}/${CIRCLE_JOB}
            # Push Artifact Link to GitHub
            REPO="zrepl/zrepl"
            COMMIT="${CIRCLE_SHA1}"
            JOB_NAME="${CIRCLE_JOB}"
            CONTEXT="<<parameters.context>>"
            DESCRIPTION="<<parameters.description>>"
            TARGETURL=https://minio.cschwarz.com/minio/"$jobprefix"/"<<parameters.minio-dst>>"
            curl "https://api.github.com/repos/$REPO/statuses/$COMMIT" \
              -H "Content-Type: application/json" \
              -H "Authorization: token $GITHUB_COMMIT_STATUS_TOKEN" \
              -X POST \
              -d '{"context":"'"$CONTEXT"'", "state": "success", "description":"'"$DESCRIPTION"'", "target_url":"'"$TARGETURL"'"}'


parameters:

  do_ci:
    type: boolean
    default: true

  do_release:
    type: boolean
    default: false

  release_docker_baseimage_tag:
    type: string
    default: "latest"

workflows:
  version: 2

  ci:
    when: << pipeline.parameters.do_ci >>
    jobs:
      - quickcheck-docs
      - quickcheck-go: &quickcheck-go-smoketest
          name: quickcheck-go-amd64-linux-1.15
          goversion: &latest-go-release "1.15"
          goos: linux
          goarch: amd64
      - test-go-on-latest-go-release:
          goversion: *latest-go-release
      - quickcheck-go:
          requires:
            - quickcheck-go-amd64-linux-1.15 #quickcheck-go-smoketest.name
          matrix: &quickcheck-go-matrix
            alias: quickcheck-go-matrix
            parameters:
              goversion: [*latest-go-release, "1.11"]
              goos: ["linux", "freebsd"]
              goarch: ["amd64", "arm64"]
            exclude:
              # don't re-do quickcheck-go-smoketest<commit_msg>build: pin Go version in circleci<commit_after>version: 2.1

commands:
  setup-home-local-bin:
    steps:
      - run:
          shell: /bin/bash -euo pipefail
          command: |
            mkdir -p "$HOME/.local/bin"
            line='export PATH="$HOME/.local/bin:$PATH"'
            if grep "$line" $BASH_ENV >/dev/null; then
              echo "$line" >> $BASH_ENV
            fi

  invoke-lazy-sh:
    parameters:
      subcommand:
        type: string
    steps:
      - run:
          environment:
            TERM: xterm
          command: ./lazy.sh <<parameters.subcommand>>

  apt-update-and-install-common-deps:
    steps:
      - run: sudo apt update && sudo apt install gawk make

  restore-cache-gomod:
    steps:
      - restore_cache:
          key: go-mod-v4-{{ checksum "go.sum" }}
  save-cache-gomod:
    steps:
      - save_cache:
          key: go-mod-v4-{{ checksum "go.sum" }}
          paths:
            - "/go/pkg/mod"

  install-godep:
    steps:
      - apt-update-and-install-common-deps
      - invoke-lazy-sh:
          subcommand: godep

  install-docdep:
    steps:
      - apt-update-and-install-common-deps
      - run: sudo apt install python3 python3-pip libgirepository1.0-dev
      - invoke-lazy-sh:
          subcommand: docdep

  download-and-install-minio-client:
    steps:
      - setup-home-local-bin
      - restore_cache:
          key: minio-client-v2
      - run:
          shell: /bin/bash -eo pipefail
          command: |
            if which mc; then exit 0; fi
            sudo curl -sSL https://dl.min.io/client/mc/release/linux-amd64/archive/mc.RELEASE.2020-08-20T00-23-01Z \
              -o "$HOME/.local/bin/mc"
            sudo chmod +x "$HOME/.local/bin/mc"
      - save_cache:
          key: minio-client-v2
          paths:
            - "$HOME/.local/bin/mc"

  upload-minio:
    parameters:
      src:
        type: string
      dst:
        type: string
    steps:
      - run:
          shell: /bin/bash -eo pipefail
          when: always
          command: |
            if [ -n "$CIRCLE_PR_NUMBER" ]; then # CIRCLE_PR_NUMBER is guaranteed to be only present in forked PRs (external)
              echo "Forked PR detected. Sry, can't trust you with credentials to external artifact store, use CircleCI's instead."
              exit 0
            fi
            set -u # from now on

            mc config host add --api s3v4 zrepl-minio https://minio.cschwarz.com ${MINIO_ACCESS_KEY} ${MINIO_SECRET_KEY}

            # keep in sync with set-github-minio-status
            jobprefix=zrepl-ci-artifacts/test/${CIRCLE_SHA1}/${CIRCLE_JOB}

            # Upload artifacts
            mkdir -p ./artifacts
            mc cp -r <<parameters.src>> "zrepl-minio/$jobprefix/<<parameters.dst>>"

  set-github-minio-status:
    parameters:
      context:
        type: string
      description:
        type: string
      minio-dst:
        type: string
    steps:
      - run:
          shell: /bin/bash -eo pipefail
          command: |
            if [ -n "$CIRCLE_PR_NUMBER" ]; then # CIRCLE_PR_NUMBER is guaranteed to be only present in forked PRs (external)
              echo "Forked PR detected. Sry, can't trust you with credentials to external artifact store, use CircleCI's instead."
              exit 0
            fi
            set -u # from now on

            # keep in sync with with upload-minio command
            jobprefix=zrepl-ci-artifacts/test/${CIRCLE_SHA1}/${CIRCLE_JOB}
            # Push Artifact Link to GitHub
            REPO="zrepl/zrepl"
            COMMIT="${CIRCLE_SHA1}"
            JOB_NAME="${CIRCLE_JOB}"
            CONTEXT="<<parameters.context>>"
            DESCRIPTION="<<parameters.description>>"
            TARGETURL=https://minio.cschwarz.com/minio/"$jobprefix"/"<<parameters.minio-dst>>"
            curl "https://api.github.com/repos/$REPO/statuses/$COMMIT" \
              -H "Content-Type: application/json" \
              -H "Authorization: token $GITHUB_COMMIT_STATUS_TOKEN" \
              -X POST \
              -d '{"context":"'"$CONTEXT"'", "state": "success", "description":"'"$DESCRIPTION"'", "target_url":"'"$TARGETURL"'"}'


parameters:

  do_ci:
    type: boolean
    default: true

  do_release:
    type: boolean
    default: false

  release_docker_baseimage_tag:
    type: string
    default: "1.15"

workflows:
  version: 2

  ci:
    when: << pipeline.parameters.do_ci >>
    jobs:
      - quickcheck-docs
      - quickcheck-go: &quickcheck-go-smoketest
          name: quickcheck-go-amd64-linux-1.15
          goversion: &latest-go-release "1.15"
          goos: linux
          goarch: amd64
      - test-go-on-latest-go-release:
          goversion: *latest-go-release
      - quickcheck-go:
          requires:
            - quickcheck-go-amd64-linux-1.15 #quickcheck-go-smoketest.name
          matrix: &quickcheck-go-matrix
            alias: quickcheck-go-matrix
            parameters:
              goversion: [*latest-go-release, "1.11"]
              goos: ["linux", "freebsd"]
              goarch: ["amd64", "arm64"]
            exclude:
              # don't re-do quickcheck-go-smoketest
{'fraction_non_alphanumeric': 0.11934477379095164, 'fraction_numerical': 0.01140795631825273, 'mean_word_length': 1.7214115149907137, 'pattern_counts': {'":': 8, '<': 31, '<?xml version=': 0, '>': 37, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '469257', 'n_tokens_mistral': 3381, 'n_tokens_neox': 3225, 'n_words': 716}