name: CI

on:
  push:
    branches:
      - main
      - "v[0-9]+.[0-9]+.x"
    tags:
      - "v*"
  pull_request:
    branches:
      - "**"

concurrency:
  # Allow only one workflow per any non-`main` branch.
  group: ${{ github.workflow }}-${{ github.ref_name }}-${{ github.ref_name == 'main' && github.sha || 'anysha' }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always
  CARGO_INCREMENTAL: 0
  RUST_BACKTRACE: 1

jobs:
  style:
    timeout-minutes: 60
    name: Check formatting and spelling
    runs-on:
      - self-hosted
      - test
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          clean: false
          fetch-depth: 0

      - name: Remove untracked files
        run: git clean -df

      - name: Set up default .cargo/config.toml
        run: cp ./.cargo/ci-config.toml ~/.cargo/config.toml

      - name: Check spelling
        run: |
          if ! which typos > /dev/null; then
            cargo install typos-cli
          fi
          typos

      - name: Run style checks
        uses: ./.github/actions/check_style

      - name: Check unused dependencies
        uses: bnjbvr/cargo-machete@main

      - name: Check license generation
        run: script/generate-licenses /tmp/zed_licenses_output

      - name: Ensure fresh merge
        shell: bash -euxo pipefail {0}
        run: |
          if [ -z "$GITHUB_BASE_REF" ];
          then
            echo "BUF_BASE_BRANCH=$(git merge-base origin/main HEAD)" >> $GITHUB_ENV
          else
            git checkout -B temp
            git merge -q origin/$GITHUB_BASE_REF -m "merge main into temp"
            echo "BUF_BASE_BRANCH=$GITHUB_BASE_REF" >> $GITHUB_ENV
          fi

      - uses: bufbuild/buf-setup-action@v1
        with:
          version: v1.29.0
      - uses: bufbuild/buf-breaking-action@v1
        with:
          input: "crates/proto/proto/"
          against: "https://github.com/${GITHUB_REPOSITORY}.git#branch=${BUF_BASE_BRANCH},subdir=crates/proto/proto/"

  macos_tests:
    timeout-minutes: 60
    name: (macOS) Run Clippy and tests
    runs-on:
      - self-hosted
      - test
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          clean: false

      - name: cargo clippy
        run: cargo xtask clippy

      - name: Run tests
        uses: ./.github/actions/run_tests

      - name: Build collab
        run: cargo build -p collab

      - name: Build other binaries and features
        run: cargo build --workspace --bins --all-features; cargo check -p gpui --features "macos-blade"

  # todo(linux): Actually run the tests
  linux_tests:
    timeout-minutes: 60
    name: (Linux) Run Clippy and tests
    runs-on:
      - self-hosted
      - deploy
    steps:
      - name: Add Rust to the PATH
        run: echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          clean: false

      - name: cargo clippy
        run: cargo xtask clippy

      - name: Build Zed
        run: cargo build -p zed

  # todo(windows): Actually run the tests
  windows_tests:
    timeout-minutes: 60
    name: (Windows) Run Clippy and tests
    runs-on: hosted-windows-1
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          clean: false

      - name: Cache dependencies
        uses: swatinem/rust-cache@v2
        with:
          save-if: ${{ github.ref == 'refs/heads/main' }}

      - name: cargo clippy
        run: cargo xtask clippy

      - name: Build Zed
        run: cargo build -p zed

  bundle-mac:
    timeout-minutes: 60
    name: Create a macOS bundle
    runs-on:
      - self-hosted
      - bundle
    if: ${{ startsWith(github.ref, 'refs/tags/v') || contains(github.event.pull_request.labels.*.name, 'run-bundling') }}
    needs: [macos_tests]
    env:
      MACOS_CERTIFICATE: ${{ secrets.MACOS_CERTIFICATE }}
      MACOS_CERTIFICATE_PASSWORD: ${{ secrets.MACOS_CERTIFICATE_PASSWORD }}
      APPLE_NOTARIZATION_USERNAME: ${{ secrets.APPLE_NOTARIZATION_USERNAME }}
      APPLE_NOTARIZATION_PASSWORD: ${{ secrets.APPLE_NOTARIZATION_PASSWORD }}
      ZED_CLIENT_CHECKSUM_SEED: ${{ secrets.ZED_CLIENT_CHECKSUM_SEED }}
      DIGITALOCEAN_SPACES_ACCESS_KEY: ${{ secrets.DIGITALOCEAN_SPACES_ACCESS_KEY }}
      DIGITALOCEAN_SPACES_SECRET_KEY: ${{ secrets.DIGITALOCEAN_SPACES_SECRET_KEY }}
    steps:
      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version: "18"

      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          # We need to fetch more than one commit so that `script/draft-release-notes`
          # is able to diff between the current and previous tag.
          #
          # 25 was chosen arbitrarily.
          fetch-depth: 25
          clean: false

      - name: Limit target directory size
        run: script/clear-target-dir-if-larger-than 100

      - name: Determine version and release channel
        if: ${{ startsWith(github.ref, 'refs/tags/v') }}
        run: |
          set -eu

          version=$(script/get-crate-version zed)
          channel=$(cat crates/zed/RELEASE_CHANNEL)
          echo "Publishing version: ${version} on release channel ${channel}"
          echo "RELEASE_CHANNEL=${channel}" >> $GITHUB_ENV

          expected_tag_name=""
          case ${channel} in
            stable)
              expected_tag_name="v${version}";;
            preview)
              expected_tag_name="v${version}-pre";;
            nightly)
              expected_tag_name="v${version}-nightly";;
            *)
              echo "can't publish a release on channel ${channel}"
              exit 1;;
          esac
          if [[ $GITHUB_REF_NAME != $expected_tag_name ]]; then
            echo "invalid release tag ${GITHUB_REF_NAME}. expected ${expected_tag_name}"
            exit 1
          fi
          mkdir -p target/
          # Ignore any errors that occur while drafting release notes to not fail the build.
          script/draft-release-notes "$version" "$channel" > target/release-notes.md || true

      - name: Generate license file
        run: script/generate-licenses

      - name: Create macOS app bundle
        run: script/bundle-mac

      - name: Rename single-architecture binaries
        if: ${{ github.ref == 'refs/heads/main' }} || contains(github.event.pull_request.labels.*.name, 'run-bundling') }}
        run: |
          mv target/aarch64-apple-darwin/release/Zed.dmg target/aarch64-apple-darwin/release/Zed-aarch64.dmg
          mv target/x86_64-apple-darwin/release/Zed.dmg target/x86_64-apple-darwin/release/Zed-x86_64.dmg

      - name: Upload app bundle (universal) to workflow run if main branch or specific label
        uses: actions/upload-artifact@v4
        if: ${{ github.ref == 'refs/heads/main' }} || contains(github.event.pull_request.labels.*.name, 'run-bundling') }}
        with:
          name: Zed_${{ github.event.pull_request.head.sha || github.sha }}.dmg
          path: target/release/Zed.dmg
      - name: Upload app bundle (aarch64) to workflow run if main branch or specific label
        uses: actions/upload-artifact@v4
        if: ${{ github.ref == 'refs/heads/main' }} || contains(github.event.pull_request.labels.*.name, 'run-bundling') }}
        with:
          name: Zed_${{ github.event.pull_request.head.sha || github.sha }}-aarch64.dmg
          path: target/aarch64-apple-darwin/release/Zed-aarch64.dmg

      - name: Upload app bundle (x86_64) to workflow run if main branch or specific label
        uses: actions/upload-artifact@v4
        if: ${{ github.ref == 'refs/heads/main' }} || contains(github.event.pull_request.labels.*.name, 'run-bundling') }}
        with:
          name: Zed_${{ github.event.pull_request.head.sha || github.sha }}-x86_64.dmg
          path: target/x86_64-apple-darwin/release/Zed-x86_64.dmg

      - uses: softprops/action-gh-release@v1
        name: Upload app bundle to release
        if: ${{ env.RELEASE_CHANNEL == 'preview' || env.RELEASE_CHANNEL == 'stable' }}
        with:
          draft: true
          prerelease: ${{ env.RELEASE_CHANNEL == 'preview' }}
          files: |
            target/aarch64-apple-darwin/release/Zed-aarch64.dmg
            target/x86_64-apple-darwin/release/Zed-x86_64.dmg
            target/release/Zed.dmg
          body_file: target/release-notes.md
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  bundle-linux:
    timeout-minutes: 60
    name: Create a Linux bundle
    runs-on:
      - self-hosted
      - deploy
    if: ${{ startsWith(github.ref, 'refs/tags/v') || contains(github.event.pull_request.labels.*.name, 'run-bundling') }}
    needs: [linux_tests]
    env:
      ZED_CLIENT_CHECKSUM_SEED: ${{ secrets.ZED_CLIENT_CHECKSUM_SEED }}
    steps:
      - name: Add Rust to the PATH
        run: echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          clean: false

      - name: Limit target directory size
        run: script/clear-target-dir-if-larger-than 100

      - name: Determine version and release channel
        if: ${{ startsWith(github.ref, 'refs/tags/v') }}
        run: |
          set -eu

          version=$(script/get-crate-version zed)
          channel=$(cat crates/zed/RELEASE_CHANNEL)
          echo "Publishing version: ${version} on release channel ${channel}"
          echo "RELEASE_CHANNEL=${channel}" >> $GITHUB_ENV

          expected_tag_name=""
          case ${channel} in
            stable)
              expected_tag_name="v${version}";;
            preview)
              expected_tag_name="v${version}-pre";;
            nightly)
              expected_tag_name="v${version}-nightly";;
            *)
              echo "can't publish a release on channel ${channel}"
              exit 1;;
          esac
          if [[ $GITHUB_REF_NAME != $expected_tag_name ]]; then
            echo "invalid release tag ${GITHUB_REF_NAME}. expected ${expected_tag_name}"
            exit 1
          fi

      - name: Create and upload Linux .tar.gz bundle
        run: script/bundle-linux

      - name: Upload Linux bundle to workflow run if main branch or specific label
        uses: actions/upload-artifact@v4
        if: ${{ github.ref == 'refs/heads/main' }} || contains(github.event.pull_request.labels.*.name, 'run-bundling') }}
        with:
          name: zed-${{ github.event.pull_request.head.sha || github.sha }}-x86_64-unknown-linux-gnu.tar.gz
          path: zed-*.tar.gz

      - name: Upload app bundle to release
        uses: softprops/action-gh-release@v1
        if: ${{ env.RELEASE_CHANNEL == 'preview' }}
        with:
          draft: true
          prerelease: ${{ env.RELEASE_CHANNEL == 'preview' }}
          files: target/release/zed-linux-x86_64.tar.gz
          body: ""
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
