name: docs_update

on:
  push:
    branches:
      - "main"


env:
  USE_VARS: "${{ vars.USE_VARS }}"
  DBT_PROFILES_DIR: "${{ vars.DBT_PROFILES_DIR }}"
  DBT_VERSION: "${{ vars.DBT_VERSION }}"
  ACCOUNT: "${{ vars.ACCOUNT }}"
  ROLE: "${{ vars.ROLE }}"
  USER: "${{ vars.USER }}"
  PASSWORD: "${{ secrets.PASSWORD }}"
  REGION: "${{ vars.REGION }}"
  DATABASE: "${{ vars.DATABASE }}"
  WAREHOUSE: "${{ vars.WAREHOUSE }}"
  SCHEMA: "${{ vars.SCHEMA }}"

concurrency:
  group: ${{ github.workflow }}

jobs:
  run_dbt_jobs:
    runs-on: ubuntu-latest
    environment: 
      name: workflow_prod

    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-python@v4
        with:
          python-version: "3.10"
          cache: "pip"

      - name: install dependencies
        run: |
          pip install -r requirements.txt
          dbt deps
      
      - name: refresh ddl for datashare
        run: |
          cnt=$(dbt ls -m fsc_utils.datashare._datashare___create_gold | wc -l ); if [ $cnt -eq 1 ]; then dbt run -m fsc_utils.datashare._datashare___create_gold; fi; 
      - name: checkout docs branch
        run: |
          git checkout -B docs origin/main
      - name: generate dbt docs
        run: |
          dbt ls -t prod
          dbt docs generate --no-compile -t prod

      - name: move files to docs directory
        run: |
          mkdir -p ./docs
          cp target/{catalog.json,manifest.json,index.html} docs/
      - name: clean up target directory
        run: dbt clean

      - name: check for changes
        run: git status

      - name: stage changed files
        run: git add .

      - name: commit changed files
        run: |
          git config user.email "abc@xyz"
          git config user.name "github-actions"
          git commit -am "Auto-update docs"
      - name: push changes to docs
        run: |
          git push -f --set-upstream origin docs

  notify-failure:
    needs: [run_dbt_jobs]
    if: failure()
    uses: ./.github/workflows/slack_notify.yml
    secrets:
      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}