name: Footprint Tracking

# Run every 12 hours and on tags
on:
  schedule:
    - cron: '50 1/12 * * *'
  push:
    paths:
      - 'VERSION'
      - '.github/workflows/footprint-tracking.yml'
    branches:
      - main
      - v*-branch
    tags:
      # only publish v* tags, do not care about zephyr-v* which point to the
      # same commit
      - 'v*'

concurrency:
  group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

jobs:
  footprint-tracking:
    runs-on:
      group: zephyr-runner-v2-linux-x64-4xlarge
    if: github.repository_owner == 'zephyrproject-rtos'
    container:
      image: ghcr.io/zephyrproject-rtos/ci-repo-cache:v0.27.4.20241026
      options: '--entrypoint /bin/bash'
    defaults:
      run:
        shell: bash
    strategy:
      fail-fast: false
    env:
      ZEPHYR_TOOLCHAIN_VARIANT: zephyr
    steps:
      - name: Apply container owner mismatch workaround
        run: |
          # FIXME: The owner UID of the GITHUB_WORKSPACE directory may not
          #        match the container user UID because of the way GitHub
          #        Actions runner is implemented. Remove this workaround when
          #        GitHub comes up with a fundamental fix for this problem.
          git config --global --add safe.directory ${GITHUB_WORKSPACE}

      - name: Print cloud service information
        run: |
          echo "ZEPHYR_RUNNER_CLOUD_PROVIDER = ${ZEPHYR_RUNNER_CLOUD_PROVIDER}"
          echo "ZEPHYR_RUNNER_CLOUD_NODE = ${ZEPHYR_RUNNER_CLOUD_NODE}"
          echo "ZEPHYR_RUNNER_CLOUD_POD = ${ZEPHYR_RUNNER_CLOUD_POD}"

      - name: Update PATH for west
        run: |
          echo "$HOME/.local/bin" >> $GITHUB_PATH

      - name: Install packages
        run: |
          sudo apt-get update
          sudo apt-get install -y python3-venv
          pip install -U gitpython

      - name: checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          fetch-depth: 0

      - name: Environment Setup
        run: |
          echo "ZEPHYR_SDK_INSTALL_DIR=/opt/toolchains/zephyr-sdk-$( cat SDK_VERSION )" >> $GITHUB_ENV

      - name: west setup
        run: |
          west init -l . || true
          west config --global update.narrow true
          west update 2>&1 1> west.update.log || west update 2>&1 1> west.update2.log

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ vars.AWS_TESTING_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_TESTING_SECRET_ACCESS_KEY }}
          aws-region: us-east-1

      - name: Record Footprint
        env:
          BASE_REF: ${{ github.base_ref }}
        run: |
          export ZEPHYR_BASE=${PWD}
          ./scripts/footprint/track.py  -p scripts/footprint/plan.txt

      - name: Upload footprint data
        run: |
          python3 -m venv .venv
          . .venv/bin/activate
          pip install awscli
          aws s3 sync  --quiet footprint_data/ s3://testing.zephyrproject.org/footprint_data/

      - name: Transform Footprint data to Twister JSON reports
        run: |
          shopt -s globstar
          export ZEPHYR_BASE=${PWD}
          python3 ./scripts/footprint/pack_as_twister.py -vvv \
            --plan ./scripts/footprint/plan.txt \
            --test-name='name.feature' \
            ./footprint_data/**/

      - name: Upload to ElasticSearch
        env:
          ELASTICSEARCH_KEY: ${{ secrets.ELASTICSEARCH_KEY }}
          ELASTICSEARCH_SERVER: "https://elasticsearch.zephyrproject.io:443"
          ELASTICSEARCH_INDEX: ${{ vars.FOOTPRINT_TRACKING_INDEX }}
        run: |
          shopt -s globstar
          pip install -U elasticsearch
          run_date=`date --iso-8601=minutes`
          python3 ./scripts/ci/upload_test_results_es.py -r ${run_date} \
            --flatten footprint \
            --flatten-list-names "{'children':'name'}" \
            --transform "{ 'footprint_name': '^(?P<footprint_area>([^\/]+\/){0,2})(?P<footprint_path>([^\/]*\/)*)(?P<footprint_symbol>[^\/]*)$' }" \
            --run-id "${{ github.run_id }}" \
            --run-attempt "${{ github.run_attempt }}" \
            --run-workflow "footprint-tracking:${{ github.event_name }}" \
            --run-branch "${{ github.ref_name }}" \
            -i ${ELASTICSEARCH_INDEX} \
            ./footprint_data/**/twister_footprint.json
        #
