name: "pr"
on: [pull_request]

jobs:
  build:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
      - uses: actions/cache@v3
        with:
          path: ~/.npm
          key: npm-${{ hashFiles('package-lock.json') }}
          restore-keys: |
            npm-
      - run: |
          npm ci
          npm run build
          npm run format-check
          npm run lint
          npm run package
          npm run test

      - name: Check for changes
        id: diff
        continue-on-error: true
        run: git diff --quiet dist

  self-check:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}

      - name: docker build
        id: build
        uses: ./
        with:
          file: __tests__/Dockerfile
          password: ${{ secrets.GCLOUD_SERVICE_ACCOUNT_KEY_NOBASE64 }}
          repository: eu.gcr.io/tradeshift-base/actions-docker

      - name: Check that push worked
        run: |
          docker pull ${{ steps.build.outputs.image }}

  self-check-custom-tag:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}

      - name: docker build
        id: build
        uses: ./
        with:
          file: __tests__/Dockerfile
          skip-default-tag: true
          password: ${{ secrets.GCLOUD_SERVICE_ACCOUNT_KEY_NOBASE64 }}
          repository: eu.gcr.io/tradeshift-base/actions-docker-custom
          tags: |
            eu.gcr.io/tradeshift-base/actions-docker-custom:my-tag-${{ github.event.pull_request.head.sha }}

      - name: Check that push worked
        run: |
          docker pull ${{ steps.build.outputs.image }}

      - name: Check that skip works
        run: |
          if docker pull eu.gcr.io/tradeshift-base/actions-docker-custom:${{ github.event.pull_request.head.sha }}; then
            echo "Tag found while expected to be missing";
            exit 1;
          fi;
          echo "Confirmed: Default tag not found"

  semantic-release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Dry-run release
        uses: tradeshift/actions-semantic-release@v1
        id: semantic-release
        with:
          dry_run: true
          check_name: Semantic release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Lint commit messages
        uses: tradeshift/commitlint-github-action@v5