name: CI
on: [push, pull_request]

jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        node-version: ['23', '22', '20', '18', '16', '14']
        os: ['windows-latest', 'ubuntu-24.04', 'ubuntu-latest', 'macos-latest', 'macos-13']
        exclude:
          # exclude node14 test on arm due to nodejs 14 release is not available for darwin-arm
          - node-version: '14'
            os: 'macos-latest'
    runs-on: ${{ matrix.os }}
    name: Node ${{ matrix.node-version }} sample (${{ matrix.os }})
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}

      - name: Get NPM Cache Directory
        id: npm-cache
        run: |
          echo "::set-output name=dir::$(npm config get cache)"

      - name: Cache NPM
        uses: actions/cache@v4
        with:
          path: ${{ steps.npm-cache.outputs.dir }}
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Install NPM Dependencies
        run: npm ci

      - name: Setup Python for Github-Markup
        if: ${{ runner.os != 'Windows' }}
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'

      - name: Install docutils for Github-Markup
        if: ${{ runner.os != 'Windows' }}
        run: |
          python -m pip install --upgrade pip
          pip install docutils

      - name: Setup Licensee, Linguist, and Github-Markup Dependencies (MacOS)
        if: ${{ runner.os == 'macOS' }}
        run: brew install cmake pkg-config icu4c libidn

      - name: Setup Licensee, Linguist, and Github-Markup Dependencies (Linux)
        if: ${{ runner.os == 'Linux' }}
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential libicu-dev libcurl4-openssl-dev libldap2-dev libidn11-dev

      - name: Run Test
        run: npm test

  test-docker:
    runs-on: ubuntu-latest
    name: Docker sample
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Cache Docker layers
        uses: actions/cache@v4
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Docker Build
        uses: docker/build-push-action@v6
        with:
          tags: todogroup/repolinter:latest
          load: true
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache

      - name: Docker Run
        run: docker run -t -v ${{ github.workspace }}:/src -w /src todogroup/repolinter:latest .

  publish-release:
    name: Publish a Release
    if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
    needs: [test, test-docker]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Semantic Release
        id: semantic
        uses: cycjimmy/semantic-release-action@v4
        with:
          extra_plugins: |
            @semantic-release/git
            @semantic-release/changelog
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Cache Docker layers
        if: steps.semantic.outputs.new_release_published == 'true'
        uses: actions/cache@v4
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-

      - name: Set up Docker Buildx
        if: steps.semantic.outputs.new_release_published == 'true'
        uses: docker/setup-buildx-action@v1

      - name: Login to GitHub Container Registry
        if: steps.semantic.outputs.new_release_published == 'true'
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Gather Docker Labels
        if: steps.semantic.outputs.new_release_published == 'true'
        id: docker_meta
        uses: crazy-max/ghaction-docker-meta@v5
        with:
          images: ghcr.io/${{ github.repository }}

      - name: Build and Push to GitHub Container Registry
        if: steps.semantic.outputs.new_release_published == 'true'
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          labels: ${{ steps.docker_meta.outputs.labels }}
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache
          tags: >-
            ghcr.io/${{ github.repository }}:latest,
            ghcr.io/${{ github.repository }}:v${{ steps.semantic.outputs.new_release_major_version }}.${{ steps.semantic.outputs.new_release_minor_version }},
            ghcr.io/${{ github.repository }}:v${{ steps.semantic.outputs.new_release_major_version }}.${{ steps.semantic.outputs.new_release_minor_version }}.${{ steps.semantic.outputs.new_release_patch_version }}
