name: ⏰ Publish Docs

on:
  push:
    branches:
      - dev
  workflow_dispatch:

jobs:
  docs:
    runs-on: ubuntu-latest
    steps:
      - name: Check out code
        uses: actions/checkout@master
        with:
          persist-credentials: false
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}
 
      - name: "Set up Go"
        uses: actions/setup-go@v2
        with: 
          go-version: 1.17

      - name: Generate YAML Syntax Documentation
        id: generate-docs
        run: |
          if ! which dstdocgen > /dev/null; then
            echo -e "Command dstdocgen not found! Installing\c"
            go install github.com/projectdiscovery/yamldoc-go/cmd/docgen/dstdocgen@main
          fi
          go generate pkg/templates/templates.go
          go build -o "cmd/docgen/docgen" cmd/docgen/docgen.go 
          ./cmd/docgen/docgen ../SYNTAX-REFERENCE.md ../nuclei-jsonschema.json
          echo "::set-output name=changes::$(git status -s | wc -l)"
        working-directory: v2

      - name: Commit files
        if: steps.generate-docs.outputs.changes > 0
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add SYNTAX-REFERENCE.md nuclei-jsonschema.json
          git commit -m "Auto Generate Syntax Docs + JSONSchema [$(date)] :robot:" -a

      - name: Push changes
        if: steps.generate-docs.outputs.changes > 0
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
