name: Trigger Release
on:
  pull_request:
    types: [closed]
    branches:
      - master
jobs:
  get_pull_request:
    if: ${{ github.event.pull_request.merged == true }}
    runs-on: cai-standard-x86-linux
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
        with:
          token: ${{ secrets._GITHUB_TOKEN }}

      - name: Extract PR metadata from event payload
        id: get-merged-pull-request
        env:
          PR_TITLE: ${{ github.event.pull_request.title }}
          PR_BODY: ${{ github.event.pull_request.body }}
        run: |
          echo "labels=${{ join(github.event.pull_request.labels.*.name, ',') }}" >> $GITHUB_OUTPUT
          echo "title=${PR_TITLE}" >> $GITHUB_OUTPUT
          {
            echo "body<<ALKS_CLI_RELEASE_BODY"
            echo "${PR_BODY}"
            echo "ALKS_CLI_RELEASE_BODY"
          } >> $GITHUB_OUTPUT
    outputs:
      pull_request_labels: ${{ steps.get-merged-pull-request.outputs.labels }}
      pull_request_title: ${{ steps.get-merged-pull-request.outputs.title }}
      pull_request_body: ${{ steps.get-merged-pull-request.outputs.body }}

  bump_version:
    runs-on: cai-standard-x86-linux
    permissions:
      id-token: write
      contents: write
    needs:
      - get_pull_request
    if: ${{ contains(needs.get_pull_request.outputs.pull_request_labels, 'release/patch') || contains(needs.get_pull_request.outputs.pull_request_labels, 'release/minor') || contains(needs.get_pull_request.outputs.pull_request_labels, 'release/major') }}
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
        with:
          token: ${{ secrets._GITHUB_TOKEN }}
          ref: refs/heads/master

      - uses: actions/setup-node@v4
        with:
          node-version: 24

      - name: Set up Artifactory Auth with OIDC
        uses: jfrog/setup-jfrog-cli@v4
        env:
          JF_URL: https://artifactory.coxautoinc.com
        with:
          oidc-provider-name: ghes

      - name: Configure npm to use Artifactory
        run: jf npm-config --repo-resolve cai-npm --server-id-resolve setup-jfrog-cli-server

      - name: Set github user
        run: |
          git config --global user.name 'github-actions[bot]'
          git config --global user.email 'github-actions[bot]@users.noreply.github.com'

      - name: install dependencies
        run: jf npm ci

      - name: Get the release level (major/minor/patch) from the labels
        uses: actions-ecosystem/action-release-label@955bf130fba6be3d99d2c14457d0dc7f176bc563
        id: release-label
        if: ${{ needs.get_pull_request.outputs.pull_request_title != null }}
        with:
          labels: ${{ needs.get_pull_request.outputs.pull_request_labels }}

      - name: skip non-merge pushes
        if: ${{ needs.get_pull_request.outputs.pull_request_title == null }}
        run: echo "skipping build since this is not a merge of a pull request" && exit 1

      - name: skip merges without a release label
        if: ${{ steps.release-label.outputs.level == null }}
        run: echo "skipping build since there is no release label on the merged PR" && exit 1

      - name: update the date in changelog.txt (if needed)
        run: |
          echo "★ Release Notes: $(date +%Y-%m-%d) ★" > changelog.temp.txt
          tail -n +2 changelog.txt >> changelog.temp.txt
          if [[ ! $(cmp --silent changelog.txt changelog.temp.txt ) ]]; then
            mv changelog.temp.txt changelog.txt
            git add changelog.txt
            git commit -m 'updates changelog.txt' --allow-empty
          fi

      - name: Bump both git-tag and package.json version
        env:
          RELEASE_LEVEL: ${{ steps.release-label.outputs.level }}
          RELEASE_TITLE: ${{ needs.get_pull_request.outputs.pull_request_title }}
        run: npm version "$RELEASE_LEVEL" -m "$RELEASE_TITLE"

      - name: Temporarily disable enforce_admins branch protection
        if: always()
        run: |
          curl -s -X DELETE \
            -H "Authorization: token ${{ secrets._GITHUB_TOKEN }}" \
            -H "Accept: application/vnd.github.v3+json" \
            https://ghe.coxautoinc.com/api/v3/repos/ETS-CloudAutomation/alks-cli/branches/master/protection/enforce_admins

      - name: Commit & Push changes
        run: git push origin HEAD:master --follow-tags

      - name: Enable enforce_admins branch protection
        if: always()
        run: |
          curl -s -X POST \
            -H "Authorization: token ${{ secrets._GITHUB_TOKEN }}" \
            -H "Accept: application/vnd.github.v3+json" \
            https://ghe.coxautoinc.com/api/v3/repos/ETS-CloudAutomation/alks-cli/branches/master/protection/enforce_admins

      - name: Get the latest tag
        id: previoustag
        uses: WyriHaximus/github-action-get-previous-tag@8656db79964f6757c940965c172d371ceedc8a7f
        with:
          fallback: 0.0.1

      - uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.previoustag.outputs.tag }}
          release_name: ${{ steps.previoustag.outputs.tag }}
          body: ${{ needs.get_pull_request.outputs.pull_request_body }}

      - name: Build the project
        run: npm run build

      - name: Configure npm for npmjs publishing
        run: |
          echo "registry=https://registry.npmjs.org/" > .npmrc
          echo "//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}" >> .npmrc
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}

      - name: Publish to npmjs.com
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}

      - name: Refresh Artifactory OIDC before deploy (token minted at job start may have expired)
        id: arty_oidc
        continue-on-error: true
        uses: jfrog/setup-jfrog-cli@v4
        env:
          JF_URL: https://artifactory.coxautoinc.com
        with:
          oidc-provider-name: ghes

      - name: Configure npm for Artifactory deploy
        id: arty_config
        continue-on-error: true
        run: jf npm-config --repo-deploy cai-npm-local --server-id-deploy setup-jfrog-cli-server

      - name: Publish to Artifactory (mirror — npmjs release already succeeded above)
        id: arty_publish
        continue-on-error: true
        run: jf npm publish --access public

      - name: Flag Artifactory mirror failure
        if: steps.arty_oidc.outcome == 'failure' || steps.arty_config.outcome == 'failure' || steps.arty_publish.outcome == 'failure'
        run: echo "::warning::Release published to npmjs.com OK, but the Artifactory (cai-npm-local) mirror publish FAILED — version is on npmjs, not yet in Artifactory. Republish manually; do NOT treat the release as failed."
# ci: releases dual-publish to npmjs.com and the internal Artifactory registry (cai-npm-local); see the "Artifactory" steps above.
