# Generated from xtask::workflows::extension_workflow_rollout
# Rebuild with `cargo xtask workflows`.
name: extension_workflow_rollout
env:
  CARGO_TERM_COLOR: always
on:
  workflow_dispatch: {}
jobs:
  fetch_extension_repos:
    runs-on: namespace-profile-2x4-ubuntu-2404
    steps:
    - id: list-repos
      name: extension_workflow_rollout::fetch_extension_repos::get_repositories
      uses: actions/github-script@v7
      with:
        script: |
          const repos = await github.paginate(github.rest.repos.listForOrg, {
              org: 'zed-extensions',
              type: 'public',
              per_page: 100,
          });

          const filteredRepos = repos
              .filter(repo => !repo.archived)
              .map(repo => repo.name);

          console.log(`Found ${filteredRepos.length} extension repos`);
          return filteredRepos;
        result-encoding: json
    outputs:
      repos: ${{ steps.list-repos.outputs.result }}
    timeout-minutes: 5
  rollout_workflows_to_extension:
    needs:
    - fetch_extension_repos
    if: needs.fetch_extension_repos.outputs.repos != '[]'
    runs-on: namespace-profile-2x4-ubuntu-2404
    strategy:
      matrix:
        repo: ${{ fromJson(needs.fetch_extension_repos.outputs.repos) }}
      fail-fast: false
      max-parallel: 5
    steps:
    - id: generate-token
      name: extension_bump::generate_token
      uses: actions/create-github-app-token@v2
      with:
        app-id: ${{ secrets.ZED_ZIPPY_APP_ID }}
        private-key: ${{ secrets.ZED_ZIPPY_APP_PRIVATE_KEY }}
        owner: zed-extensions
        repositories: ${{ matrix.repo }}
        permission-pull-requests: write
        permission-contents: write
        permission-workflows: write
    - name: checkout_zed_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
        path: zed
        fetch-depth: '0'
    - name: steps::checkout_repo_with_token
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
        token: ${{ steps.generate-token.outputs.token }}
        repository: zed-extensions/${{ matrix.repo }}
        path: extension
    - id: prev-tag
      name: extension_workflow_rollout::rollout_workflows_to_extension::get_previous_tag_commit
      run: |
        PREV_COMMIT=$(git rev-parse "extension-workflows^{commit}" 2>/dev/null || echo "")
        if [ -z "$PREV_COMMIT" ]; then
            echo "::error::No previous rollout tag 'extension-workflows' found. Cannot determine file changes."
            exit 1
        fi
        echo "Found previous rollout at commit: $PREV_COMMIT"
        echo "prev_commit=$PREV_COMMIT" >> "$GITHUB_OUTPUT"
      working-directory: zed
    - id: calc-changes
      name: extension_workflow_rollout::rollout_workflows_to_extension::get_removed_files
      run: |
        PREV_COMMIT="${{ steps.prev-tag.outputs.prev_commit }}"

        if [ "${{ matrix.repo }}" = "workflows" ]; then
            WORKFLOW_DIR="extensions/workflows"
        else
            WORKFLOW_DIR="extensions/workflows/shared"
        fi

        echo "Calculating changes from $PREV_COMMIT to HEAD for $WORKFLOW_DIR"

        # Get deleted files (status D) and renamed files (status R - old name needs removal)
        # Using -M to detect renames, then extracting files that are gone from their original location
        REMOVED_FILES=$(git diff --name-status -M "$PREV_COMMIT" HEAD -- "$WORKFLOW_DIR" | \
            awk '/^D/ { print $2 } /^R/ { print $2 }' | \
            xargs -I{} basename {} 2>/dev/null | \
            tr '\n' ' ' || echo "")

        REMOVED_FILES=$(echo "$REMOVED_FILES" | xargs)

        echo "Files to remove: $REMOVED_FILES"
        echo "removed_files=$REMOVED_FILES" >> "$GITHUB_OUTPUT"
      working-directory: zed
    - name: extension_workflow_rollout::rollout_workflows_to_extension::sync_workflow_files
      run: |
        REMOVED_FILES="${{ steps.calc-changes.outputs.removed_files }}"

        mkdir -p extension/.github/workflows
        cd extension/.github/workflows

        if [ -n "$REMOVED_FILES" ]; then
            for file in $REMOVED_FILES; do
                if [ -f "$file" ]; then
                    rm -f "$file"
                fi
            done
        fi

        cd - > /dev/null

        if [ "${{ matrix.repo }}" = "workflows" ]; then
            cp zed/extensions/workflows/*.yml extension/.github/workflows/
        else
            cp zed/extensions/workflows/shared/*.yml extension/.github/workflows/
        fi
    - id: short-sha
      name: extension_workflow_rollout::rollout_workflows_to_extension::get_short_sha
      run: |
        echo "sha_short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
      working-directory: zed
    - id: create-pr
      name: extension_workflow_rollout::rollout_workflows_to_extension::create_pull_request
      uses: peter-evans/create-pull-request@v7
      with:
        path: extension
        title: Update CI workflows to `zed@${{ steps.short-sha.outputs.sha_short }}`
        body: |
          This PR updates the CI workflow files from the main Zed repository
          based on the commit zed-industries/zed@${{ github.sha }}
        commit-message: Update CI workflows to `zed@${{ steps.short-sha.outputs.sha_short }}`
        branch: update-workflows
        committer: zed-zippy[bot] <234243425+zed-zippy[bot]@users.noreply.github.com>
        author: zed-zippy[bot] <234243425+zed-zippy[bot]@users.noreply.github.com>
        base: main
        delete-branch: true
        token: ${{ steps.generate-token.outputs.token }}
        sign-commits: true
    - name: extension_workflow_rollout::rollout_workflows_to_extension::enable_auto_merge
      run: |
        PR_NUMBER="${{ steps.create-pr.outputs.pull-request-number }}"
        if [ -n "$PR_NUMBER" ]; then
            cd extension
            gh pr merge "$PR_NUMBER" --auto --squash
        fi
      env:
        GH_TOKEN: ${{ steps.generate-token.outputs.token }}
    timeout-minutes: 10
  create_rollout_tag:
    needs:
    - rollout_workflows_to_extension
    runs-on: namespace-profile-2x4-ubuntu-2404
    steps:
    - id: generate-token
      name: extension_bump::generate_token
      uses: actions/create-github-app-token@v2
      with:
        app-id: ${{ secrets.ZED_ZIPPY_APP_ID }}
        private-key: ${{ secrets.ZED_ZIPPY_APP_PRIVATE_KEY }}
        permission-contents: write
    - name: steps::checkout_repo_with_token
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
        token: ${{ steps.generate-token.outputs.token }}
        fetch-depth: '0'
    - name: extension_workflow_rollout::create_rollout_tag::configure_git
      run: |
        git config user.name "zed-zippy[bot]"
        git config user.email "234243425+zed-zippy[bot]@users.noreply.github.com"
    - name: extension_workflow_rollout::create_rollout_tag::update_rollout_tag
      run: |
        if git rev-parse "extension-workflows" >/dev/null 2>&1; then
            git tag -d "extension-workflows"
            git push origin ":refs/tags/extension-workflows" || true
        fi

        echo "Creating new tag 'extension-workflows' at $(git rev-parse --short HEAD)"
        git tag "extension-workflows"
        git push origin "extension-workflows"
    timeout-minutes: 1
defaults:
  run:
    shell: bash -euxo pipefail {0}
