name: Go Generate
on:
  workflow_dispatch:
    inputs:
      source_url:
        type: string
        description: "URL of the source for this workflow run"
      source_author:
        type: string
        description: "Username of the source for this workflow run"

env:
  INPUT_SOURCE_URL: ${{ github.event.inputs.source_url }}
  INPUT_SOURCE_AUTHOR: ${{ github.event.inputs.source_author }}

permissions:
  contents: write
  pull-requests: write
  id-token: write
jobs:
  go-generate:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
      - name: Run go-generate to fetch data.json
        run: make go-generate
      - name: Check for repository changes
        run: |
          if git diff --name-only --exit-code; then
            echo "No changes found in repository after 'go generate'"
            echo "changes_exist=false" >> $GITHUB_ENV
          else
            echo "Changes found in repository after 'go generate':"
            git diff --name-only
            echo "changes_exist=true" >> $GITHUB_ENV
          fi
      - name: Create branch, commit and push
        if: ${{ env.changes_exist == 'true' }}
        id: branch
        run: |
          BRANCH="githubaction-go-generate-$(date +%Y-%m-%d-%H-%M-%S)"
          echo "::set-output name=branch::$BRANCH"
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --global user.name "github-actions[bot]"
          git checkout -b "$BRANCH"
          git commit -a -m 'go generate'
          git push origin "$BRANCH"
      - name: Retrieve token from vault
        uses: rancher-eio/read-vault-secrets@main
        with:
          secrets: |
            secret/data/github/repo/${{ github.repository }}/github-token/credentials token | PAT_TOKEN ;
      - name: Create Pull Request
        if: ${{ env.changes_exist == 'true' }}
        id: cpr
        uses: actions/github-script@v7
        env:
          SOURCE_BRANCH: ${{ steps.branch.outputs.branch }}
        with:
          github-token: ${{ env.PAT_TOKEN }}
          script: |
            let body = 'Auto-generated by GitHub Actions\n\n'
            if  ( `${ process.env.INPUT_SOURCE_URL }` ) {
              body += `\nSource URL: ${ process.env.INPUT_SOURCE_URL }`
            }
            if  ( `${ process.env.INPUT_SOURCE_AUTHOR }` ) {
              body += `\nSource AUTHOR: @${ process.env.INPUT_SOURCE_AUTHOR}`
            }

            const { data: pr } = await github.rest.pulls.create({
              title: "[${{ github.ref_name }}] go generate",
              body: body,
              owner: context.repo.owner,
              repo: context.repo.repo,
              base: "${{ github.ref_name }}",
              head: `${ process.env.SOURCE_BRANCH }`
            });
            await github.rest.issues.addLabels({
              ...context.repo,
              issue_number: pr.number,
              labels: ["status/auto-created"],
            });
            if  ( `${ process.env.INPUT_SOURCE_AUTHOR }` ) {
              await github.rest.issues.addAssignees({
                ...context.repo,
                issue_number: pr.number,
                assignees: [`${ process.env.INPUT_SOURCE_AUTHOR}`],
              });
            }
            console.log('Created new pull request');
            return pr.html_url;
      - name: Check outputs
        if: ${{ env.changes_exist == 'true' }}
        run: |
          echo "Pull Request URL - ${{ steps.cpr.outputs.result }}"
