name: Release

on:
  push:
    branches:
      - main

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
  release:
    name: Changesets Release
    # Prevents action from creating a PR on forks
    if: github.repository == 'apollographql/vscode-graphql'
    runs-on: ubuntu-latest
    # Permissions necessary for Changesets to push a new branch and open PRs
    # (for automated Version Packages PRs), and request the JWT for provenance.
    # More info: https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect#adding-permissions-settings
    permissions:
      contents: write
      pull-requests: write
      id-token: write
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          # Fetch entire git history so  Changesets can generate changelogs
          # with the correct commits
          fetch-depth: 0

      - name: Check for pre.json file existence
        id: check_files
        uses: andstor/file-existence-action@v2.0.0
        with:
          files: ".changeset/pre.json"

      - name: Setup Node.js 24.x
        uses: actions/setup-node@v3
        with:
          node-version: 24.x

      - name: Install dependencies
        run: npm ci

      - name: Create release PR or publish to npm + GitHub
        id: changesets
        if: steps.check_files.outputs.files_exists == 'false'
        uses: changesets/action@v1
        with:
          version: npm run changeset-version
          publish: npm run changeset-publish
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ""

      - name: Attach VSX to GitHub release
        if: steps.changesets.outcome == 'success' && steps.changesets.outputs.published == 'true'
        run: |
          npx -y @vscode/vsce package --out "vscode-apollo-$VERSION.vsix"
          gh release upload "v$VERSION" "vscode-apollo-$VERSION.vsix"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          VERSION: ${{ fromJson(steps.changesets.outputs.publishedPackages)[0].version }}

      - name: Publish to Open VSX Registry
        if: steps.changesets.outcome == 'success' && steps.changesets.outputs.published == 'true'
        uses: HaaLeo/publish-vscode-extension@v1
        with:
          pat: ${{ secrets.OPEN_VSX_TOKEN }}
          baseContentUrl: https://raw.githubusercontent.com/apollographql/vscode-graphql

      - name: Publish to Visual Studio Marketplace
        if: steps.changesets.outcome == 'success' && steps.changesets.outputs.published == 'true'
        uses: HaaLeo/publish-vscode-extension@v1
        with:
          pat: ${{ secrets.VS_MARKETPLACE_TOKEN }}
          registryUrl: https://marketplace.visualstudio.com
          baseContentUrl: https://raw.githubusercontent.com/apollographql/vscode-graphql

      - name: Send a Slack notification on publish
        if: steps.changesets.outcome == 'success' && steps.changesets.outputs.published == 'true'
        id: slack
        uses: slackapi/slack-github-action@v1.24.0
        with:
          # Slack channel id, channel name, or user id to post message
          # See also: https://api.slack.com/methods/chat.postMessage#channels
          # You can pass in multiple channels to post to by providing
          # a comma-delimited list of channel IDs
          channel-id: "C02J316U84V"
          payload: |
            {
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "A new version of `vscode-apollo` was released: <https://github.com/apollographql/vscode-graphql/releases/tag/v${{ fromJson(steps.changesets.outputs.publishedPackages)[0].version }}|v${{ fromJson(steps.changesets.outputs.publishedPackages)[0].version }}> :rocket:"
                  }
                }
              ]
            }
        env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
