<commit_before>version: 2.1

orbs:
  secrethub: secrethub/cli@1.1
  gh: circleci/github-cli@1.0.5

commands:
  compute-sanitized-branch-name:
    steps:
      - run:
          name: Compute a sanitized version of branch name
          command: |
            export BRANCH_ID=$(echo "$CIRCLE_BRANCH" | sed -E 's/[~^]+//g' | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+|-+$//g' | tr "[:upper:]" "[:lower:]" | cut -c -60)
            echo "export BRANCH_ID=$BRANCH_ID" >> $BASH_ENV

jobs:
  build:
    docker:
      - image: cimg/ruby:3.1
    environment:
      BUNDLE_PATH: ~/repo/vendor/bundle
    steps:
      - checkout
      - restore_cache:
          keys:
            - rubygems-v1-{{ checksum "Gemfile.lock" }}
            - rubygems-v1-fallback
      - run:
          name: Bundle Install
          command: bundle check || bundle install
      - save_cache:
          key: rubygems-v1-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle
      - compute-sanitized-branch-name
      - run:
          name: Jekyll build
          command: bundle exec jekyll build --baseurl $BRANCH_ID
      - persist_to_workspace:
          root: ./
          paths:
            - _site

  deploy-on-azure-storage:
    docker:
      - image: mcr.microsoft.com/azure-cli:latest
    resource_class: small
    steps:
      - attach_workspace:
          at: .
      - compute-sanitized-branch-name
      - run:
          name: Login into Azure Storage and upload dist
          command: |
            az login --service-principal -u $AZURE_APPLICATION_ID --tenant $AZURE_TENANT -p $AZURE_APPLICATION_SECRET
            az storage container create -n $BRANCH_ID --public-access blob
            az storage blob upload-batch -s _site -d $BRANCH_ID

  comment-pr-after-deployment:
    docker:
      - image: cimg/node:14.18
    resource_class: small
    steps:
      - checkout
      - gh/setup
      - compute-sanitized-branch-name
      - run:
          name: Edit Pull Request Description
          command: |
            # First check there is an associated pull request, otherwise just stop the job here
            if ! gh pr view --json title;
            then
              echo "No PR found for this branch, stopping the job here."
              exit 0
            fi

            # If PR state is different from OPEN
            if [ "$(gh pr view --json state --jq .state)" != "OPEN" ];
            then
              echo "PR is not opened, stopping the job here."
              exit 0
            fi

            export PR_BODY_UI_SECTION="
            <!-- UI placeholder -->
            🚀 CI was able to deploy the build of this PR, so you can now try it directly [here](https://graviteedocs.blob.core.windows.net/${BRANCH_ID}/index.html)
            <!-- UI placeholder end -->
            "

            export CLEAN_BODY=$(gh pr view --json body --jq .body | sed '/UI placeholder -->/,/UI placeholder end -->/d')

            gh pr edit --body "$CLEAN_BODY$PR_BODY_UI_SECTION"

  remove-old-containers:
    docker:
      - image: mcr.microsoft.com/azure-cli:latest
    resource_class: small
    steps:
      - run:
          name: install coreutils to manipulate dates
          command: |
            apk add coreutils
      - run:
          name: Login into Azure Storage and purge containers older than 30days
          command: |
            az login --service-principal -u $AZURE_APPLICATION_ID --tenant $AZURE_TENANT -p $AZURE_APPLICATION_SECRET

            lowerRange=date -d 'now - 30 days'

            for containerName in $(az storage container list  | jq -r '.[] | .name'); do
              lastModifiedDateString=$(az storage blob list -c $containerName --num-results 1 | jq -r '.[] | .properties.lastModified')
              lastModifiedDate=date -d $lastModifiedDateString
              if [ $lowerRange -g $lastModifiedDate ];
              then
                echo "Deleting container: $containerName"
                az storage container delete -n $containerName
              fi
            done

workflows:
  build:
    jobs:
      - build
      - deploy-on-azure-storage:
          filters:
            branches:
              ignore:
                - master
            tags:
              ignore:<commit_msg>ci: fix containers purge workflow (#697)<commit_after>version: 2.1

orbs:
  secrethub: secrethub/cli@1.1
  gh: circleci/github-cli@1.0.5

commands:
  compute-sanitized-branch-name:
    steps:
      - run:
          name: Compute a sanitized version of branch name
          command: |
            export BRANCH_ID=$(echo "$CIRCLE_BRANCH" | sed -E 's/[~^]+//g' | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+|-+$//g' | tr "[:upper:]" "[:lower:]" | cut -c -60)
            echo "export BRANCH_ID=$BRANCH_ID" >> $BASH_ENV

jobs:
  build:
    docker:
      - image: cimg/ruby:3.1
    environment:
      BUNDLE_PATH: ~/repo/vendor/bundle
    steps:
      - checkout
      - restore_cache:
          keys:
            - rubygems-v1-{{ checksum "Gemfile.lock" }}
            - rubygems-v1-fallback
      - run:
          name: Bundle Install
          command: bundle check || bundle install
      - save_cache:
          key: rubygems-v1-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle
      - compute-sanitized-branch-name
      - run:
          name: Jekyll build
          command: bundle exec jekyll build --baseurl $BRANCH_ID
      - persist_to_workspace:
          root: ./
          paths:
            - _site

  deploy-on-azure-storage:
    docker:
      - image: mcr.microsoft.com/azure-cli:latest
    resource_class: small
    steps:
      - attach_workspace:
          at: .
      - compute-sanitized-branch-name
      - run:
          name: Login into Azure Storage and upload dist
          command: |
            az login --service-principal -u $AZURE_APPLICATION_ID --tenant $AZURE_TENANT -p $AZURE_APPLICATION_SECRET
            az storage container create -n $BRANCH_ID --public-access blob
            az storage blob upload-batch -s _site -d $BRANCH_ID

  comment-pr-after-deployment:
    docker:
      - image: cimg/node:14.18
    resource_class: small
    steps:
      - checkout
      - gh/setup
      - compute-sanitized-branch-name
      - run:
          name: Edit Pull Request Description
          command: |
            # First check there is an associated pull request, otherwise just stop the job here
            if ! gh pr view --json title;
            then
              echo "No PR found for this branch, stopping the job here."
              exit 0
            fi

            # If PR state is different from OPEN
            if [ "$(gh pr view --json state --jq .state)" != "OPEN" ];
            then
              echo "PR is not opened, stopping the job here."
              exit 0
            fi

            export PR_BODY_UI_SECTION="
            <!-- UI placeholder -->
            🚀 CI was able to deploy the build of this PR, so you can now try it directly [here](https://graviteedocs.blob.core.windows.net/${BRANCH_ID}/index.html)
            <!-- UI placeholder end -->
            "

            export CLEAN_BODY=$(gh pr view --json body --jq .body | sed '/UI placeholder -->/,/UI placeholder end -->/d')

            gh pr edit --body "$CLEAN_BODY$PR_BODY_UI_SECTION"

  remove-old-containers:
    docker:
      - image: mcr.microsoft.com/azure-cli:latest
    resource_class: small
    steps:
      - run:
          name: install coreutils to manipulate dates
          command: |
            apk add coreutils
      - run:
          name: Login into Azure Storage and purge containers older than 30days
          command: |
            az login --service-principal -u $AZURE_APPLICATION_ID --tenant $AZURE_TENANT -p $AZURE_APPLICATION_SECRET

            lowerRange=date -d 'now - 30 days'

            for containerName in $(az storage container list  | jq -r '.[] | .name'); do
              lastModifiedDateString=$(az storage blob list -c $containerName --num-results 1 | jq -r '.[] | .properties.lastModified')
              lastModifiedDate=$(date -d $lastModifiedDateString)
              if [ $lowerRange -g $lastModifiedDate ];
              then
                echo "Deleting container: $containerName"
                az storage container delete -n $containerName
              fi
            done

workflows:
  build:
    jobs:
      - build
      - deploy-on-azure-storage:
          filters:
            branches:
              ignore:
                - master
            tags:
              ignore:
{'fraction_non_alphanumeric': 0.10290330133458206, 'fraction_numerical': 0.006672910325450714, 'mean_word_length': 1.6253841425937308, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3667876', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2492, 'n_words': 724}