# This file is automatically added by @npmcli/template-oss. Do not edit.

name: Pull Request

on:
  pull_request:
    types:
      - opened
      - reopened
      - edited
      - synchronize

jobs:
  commitlint:
    name: Lint Commits
    if: github.repository_owner == 'npm'
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Setup Git User
        run: |
          git config --global user.email "npm-cli+bot@github.com"
          git config --global user.name "npm CLI robot"
      - name: Setup Node
        uses: actions/setup-node@v3
        id: node
        with:
          node-version: 20.x
          check-latest: contains('20.x', '.x')

      - name: Install Latest npm
        shell: bash
        env:
          NODE_VERSION: ${{ steps.node.outputs.node-version }}
        run: |
          MATCH=""
          SPECS=("latest" "next-10" "next-9" "next-8" "next-7" "next-6")

          echo "node@$NODE_VERSION"

          for SPEC in ${SPECS[@]}; do
            ENGINES=$(npm view npm@$SPEC --json | jq -r '.engines.node')
            echo "Checking if node@$NODE_VERSION satisfies npm@$SPEC ($ENGINES)"

            if npx semver -r "$ENGINES" "$NODE_VERSION" > /dev/null; then
              MATCH=$SPEC
              echo "Found compatible version: npm@$MATCH"
              break
            fi  
          done

          if [ -z $MATCH ]; then
            echo "Could not find a compatible version of npm for node@$NODE_VERSION"
            exit 1
          fi

          npm i --prefer-online --no-fund --no-audit -g npm@$MATCH

      - name: npm Version
        run: npm -v
      - name: Install Dependencies
        run: npm i --ignore-scripts --no-audit --no-fund
      - name: Run Commitlint on Commits
        id: commit
        continue-on-error: true
        run: |
          npx --offline commitlint -V --from 'origin/${{ github.base_ref }}' --to ${{ github.event.pull_request.head.sha }}
      - name: Run Commitlint on PR Title
        if: steps.commit.outcome == 'failure'
        env:
          PR_TITLE: ${{ github.event.pull_request.title }}
        run: |
          echo "$PR_TITLE" | npx --offline commitlint -V
