name: Internal - Main - Continuous Integration

on:
  push:
    branches: [main]
    tags: ["*"]

  workflow_dispatch:

  schedule:
    - cron: "25 8 * * 1"

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

permissions: {}

jobs:
  ci:
    uses: ./.github/workflows/__shared-ci.yml
    permissions:
      actions: read
      contents: read
      id-token: write
      packages: read
      pull-requests: write
      security-events: write
      statuses: write
    secrets: inherit

  prepare-docs:
    needs: ci
    if: github.event_name != 'schedule'
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      artifact-id: ${{ steps.upload-artifact.outputs.artifact-id }}
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: 📖 Generate documentation
        id: generate-documentation
        uses: hoverkraft-tech/ci-dokumentor@c46a1a108957237cf485103a80b060c35c7dba33 # 0.2.2
        with:
          source: action.yml
          extra-badges: |
            [
              {
                "label":"codecov",
                "url":"https://codecov.io/gh/hoverkraft-tech/compose-action/graph/badge.svg?token=90JXB7EIMA",
                "linkUrl":"https://codecov.io/gh/hoverkraft-tech/compose-action"
              }
            ]

      - uses: actions/create-github-app-token@f8d387b68d61c58ab83c6c016672934102569859 # v3.0.0
        id: generate-token
        with:
          app-id: ${{ vars.CI_BOT_APP_ID }}
          private-key: ${{ secrets.CI_BOT_APP_PRIVATE_KEY }}

      - uses: hoverkraft-tech/ci-github-common/actions/create-and-merge-pull-request@4b53189212d5810f710bed89711002626977215b # 0.33.0
        with:
          github-token: ${{ steps.generate-token.outputs.token }}
          branch: docs/actions-workflows-documentation-update
          title: "docs: update actions and workflows documentation"
          body: Update actions and workflows documentation
          commit-message: |
            docs: update actions and workflows documentation

            [skip ci]

      - id: upload-artifact
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: documentation-files-${{ github.run_id }}
          path: |
            ${{ steps.generate-documentation.outputs.destination }}
            .github/logo.svg

  sync-docs:
    needs: prepare-docs
    if: needs.prepare-docs.outputs.artifact-id
    uses: hoverkraft-tech/public-docs/.github/workflows/sync-docs-dispatcher.yml@c40c17f7d6a8090950b3ef4bfc70502707a6bb9f # 0.3.0
    permissions:
      contents: read
    with:
      artifact-id: ${{ needs.prepare-docs.outputs.artifact-id }}
      github-app-id: ${{ vars.CI_BOT_APP_ID }}
    secrets:
      github-app-key: ${{ secrets.CI_BOT_APP_PRIVATE_KEY }}
