name: Create Changelog

on:
  workflow_dispatch:
    inputs:
      tag:
        description: Tag to checkout.
        required: false
      release_type:
        description: Release Type
        type: choice
        required: true
        default: 'Release'
        options:
          - 'Release'
          - 'Beta Release'
          - 'Alpha Release'
          - 'Cutting Edge Build'
      compare_tag:
        description: Tag(s) to compare against. If not set, will use the tag before `Tag`. If specifying multiple, seperate by commas. (Spaces allowed).
        required: false
      branch:
        description: Branch to push changelog to. If not set, changelog will just be uploaded as an artifact.
        required: false
      test:
        description: Whether to test commits. If true, then any parsing errors will throw an error.
        required: false
        type: boolean
        default: false

  workflow_call:
    inputs:
      tag:
        description: Tag to make changelog at.
        type: string
        required: false
      release_type:
        description: Release Type.
        type: string
        required: true
      compare_tag:
        description: Tag to compare against.
        type: string
        required: false
      branch:
        description: Branch to push changelog to. If not set, will not push. Will still be uploaded as artifact.
        type: string
        required: false
      test:
        description: Whether to test commits. If true, then any parsing errors will throw an error.
        required: false
        type: boolean
        default: false

jobs:
  deploy:
    name: Create Changelog (${{ inputs.tag }})
    runs-on: ubuntu-latest
    env:
      GITHUB_TAG: ${{ inputs.tag }}
      RELEASE_TYPE: ${{ inputs.release_type }}
    steps:
      - name: Get Token
        id: token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.APP_ID }}
          private-key: ${{ secrets.APP_KEY }}
          owner: Nomi-CEu

      - name: Checkout Repo
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{ inputs.tag }}
          token: ${{ steps.token.outputs.token }}

      - name: Restore NPM Cached Files
        uses: actions/cache@v4
        id: npm-cache
        with:
          path: |
            ~/.npm
            ./tools/node_modules
          key: ${{ runner.os }}-npm-${{ hashFiles('./tools/package-lock.json') }}
          restore-keys: ${{ runner.os }}-npm-

      - name: Setup NodeJS v20
        uses: actions/setup-node@v4
        with:
          node-version: 20
          check-latest: true

      - name: Setup NPM Packages
        if: steps.npm-cache.outputs.cache-hit != 'true'
        working-directory: ./tools
        run: npm ci

      - name: Create Changelog
        working-directory: ./tools
        run: npm run gulp createChangelog
        env:
          CFCORE_API_TOKEN: ${{ secrets.CFCORE_API_TOKEN }}
          COMPARE_TAG: ${{ inputs.compare_tag }}
          TEST_CHANGELOG: ${{ inputs.test }}


      - name: Commit and Push Changelog
        if: ${{ inputs.branch }}
        uses: "stefanzweifel/git-auto-commit-action@v5"
        id: "commit-template"
        with:
          commit_message: "Upload Changelogs for Release ${{ inputs.tag }}"
          commit_author: nomi-ceu-management[bot] <155502507+nomi-ceu-management[bot]@users.noreply.github.com>
          commit_user_name: nomi-ceu-management[bot]
          commit_user_email: 155502507+nomi-ceu-management[bot]@users.noreply.github.com
          branch: ${{ inputs.branch }}
          create_branch: true

      - name: Upload Changelog
        uses: actions/upload-artifact@v4
        with:
          name: Changelogs
          path: |
            ./CHANGELOG.md
            ./CHANGELOG_CF.md
          if-no-files-found: error
          compression-level: 9
