name: 'Util: Update Node Popularity'

on:
  schedule:
    # Run every Monday at 00:00 UTC
    - cron: '0 0 * * 1'
  workflow_dispatch: # Allow manual trigger for testing

permissions:
  contents: write
  pull-requests: write

jobs:
  update-popularity:
    if: |
      github.event_name == 'workflow_dispatch' ||
      (github.event_name == 'schedule' && github.repository == 'n8n-io/n8n')
    runs-on: ubuntu-latest
    outputs:
      pull-request-number: ${{ steps.create-pr.outputs.pull-request-number }}
    steps:
      - name: Generate GitHub App Token
        id: generate-token
        uses: actions/create-github-app-token@29824e69f54612133e76f7eaac726eef6c875baf # v2.2.1
        with:
          app-id: ${{ secrets.N8N_ASSISTANT_APP_ID }}
          private-key: ${{ secrets.N8N_ASSISTANT_PRIVATE_KEY }}

      - name: Checkout repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          token: ${{ steps.generate-token.outputs.token }}

      - name: Setup Node.js and Dependencies
        uses: ./.github/actions/setup-nodejs
        with:
          build-command: '' # Skip build, we only need to fetch data

      - name: Fetch node popularity data
        run: |
          cd packages/frontend/editor-ui
          node scripts/fetch-node-popularity.mjs
        env:
          N8N_FAIL_ON_POPULARITY_FETCH_ERROR: 'false' # Don't fail if API is down

      - name: Format generated file
        run: pnpm biome format --write packages/frontend/editor-ui/data/node-popularity.json

      - name: Check for changes
        id: check-changes
        run: |
          if git diff --quiet packages/frontend/editor-ui/data/node-popularity.json; then
            echo "No changes to popularity data"
            echo "has_changes=false" >> "$GITHUB_OUTPUT"
          else
            echo "Popularity data has changed"
            echo "has_changes=true" >> "$GITHUB_OUTPUT"
          fi

      - name: Create Pull Request
        if: steps.check-changes.outputs.has_changes == 'true'
        id: create-pr
        uses: peter-evans/create-pull-request@c0f553fe549906ede9cf27b5156039d195d2ece0 # v8.1.0
        with:
          token: ${{ steps.generate-token.outputs.token }}
          commit-message: 'chore: Update node popularity data'
          labels: 'automation:scheduled-update'
          title: 'chore: Update node popularity data'
          body: |
            This automated PR updates the node popularity data used for sorting nodes in the node creator panel.

            The data is fetched weekly from the n8n telemetry endpoint to reflect current usage patterns.

            _Generated by the weekly node popularity update workflow._
          branch: update-node-popularity
          base: master
          delete-branch: true
          author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
          committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

  approve-and-automerge:
    needs: [update-popularity]
    if: |
      needs.update-popularity.outputs.pull-request-number != '' &&
      (github.event_name == 'workflow_dispatch' ||
      (github.event_name == 'schedule' && github.repository == 'n8n-io/n8n'))
    uses: ./.github/workflows/util-approve-and-set-automerge.yml
    secrets: inherit
    with:
      pull-request-number: ${{ needs.update-popularity.outputs.pull-request-number }}
