name: 🗒 Templates Stats

on:
  push:
    tags:
      - '*'
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    if: github.repository == 'projectdiscovery/nuclei-templates'
    steps:
      - uses: actions/checkout@v4
        with: 
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: 1.19

      - name: Installing Template Stats
        run: |
          go install github.com/projectdiscovery/templates-stats@main
        shell: bash

      - name: Markdown Stats
        run: |
          templates-stats -output TEMPLATES-STATS.md -path /home/runner/work/nuclei-templates/nuclei-templates/
        shell: bash

      - name: JSON Stats
        run: |
          templates-stats -output TEMPLATES-STATS.json -json -path /home/runner/work/nuclei-templates/nuclei-templates/
        shell: bash

      - name: Top 10 Stats
        run: |
          templates-stats -output TOP-10.md -top 10 -path /home/runner/work/nuclei-templates/nuclei-templates/
        shell: bash

      - name: Get statistical changes
        id: stats
        run: git status -s | wc -l | xargs -I {} echo CHANGES={} >> $GITHUB_OUTPUT

      - name: Commit files
        if: steps.stats.outputs.CHANGES > 0
        run: |
          git add TEMPLATES-STATS.*
          git add TOP-10.md
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git commit -m "Auto Generated Templates Stats [$(date)] :robot:" -a

      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}