# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

name: Sample Run and Dependabot Auto-merge
on:
  push:
    branches: [ master ]

permissions:
  id-token: write
  contents: write
  pull-requests: write
  statuses: write

jobs:
  sample-run:
    timeout-minutes: 8
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        shell: bash

    strategy:
      fail-fast: false
      matrix:
        node-version: [ '18.x', '20.x', '21.x' ]
        jdk-version: [ "8", "11", "17", "21", "24" ]
        os: [ ubuntu-latest, macOS-latest, windows-latest ]

    steps:
      - name: Checkout working directory
        uses: actions/checkout@v4

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-region: us-east-1
          role-to-assume: arn:aws:iam::751999266872:role/GitHubNodejs
          role-session-name: myGitHubActionsNodejs

      - name: Set up JDK ${{ matrix.jdk-version }}
        uses: actions/setup-java@v4
        with:
          java-version: ${{ matrix.jdk-version }}
          distribution: 'corretto'

      - name: Set up Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'

      - name: npm clean install, build, and test
        run: |
          npm ci
          npm run build --if-present
          npm test

      - name: Install dependencies
        run: |
          npm install

      - name: Running data producer
        run: |
          cd samples/basic_sample/producer
          node sample_kinesis_producer_app.js

      - name: Running KCL consumer (windows or ubuntu)
        if: matrix.os != 'macOS-latest'
        run: |
          cd samples/basic_sample/consumer
          timeout 45 ../../../bin/kcl-bootstrap -e -p ./sample.properties || status="$?"; if (( status == 124 )); then exit 0; else exit 1; fi; exit "$status"

      - name: Running KCL consumer (macOS)
        if: matrix.os == 'macOS-latest'
        run: |
          brew install coreutils
          cd samples/basic_sample/consumer
          gtimeout 45 ../../../bin/kcl-bootstrap --java /usr/bin/java -e -p ./sample.properties || status="$?"; if (( status == 124 )); then exit 0; else exit 1; fi; exit "$status"

  auto-merge-dependabot:
    needs: [ sample-run ]
    runs-on: ubuntu-latest
    if: github.actor == 'dependabot[bot]' && github.event.pull_request.user.login == 'dependabot[bot]'
    steps:
      - name: Fetch Dependabot metadata
        id: metadata
        uses: dependabot/fetch-metadata@v2
        with:
          alert-lookup: true
          github-token: "${{ secrets.GITHUB_TOKEN }}"

#      - name: Approve PR
#        if: steps.metadata.outputs.update-type != 'version-update:semver-major'
#        run: gh pr review --approve "$PR_URL"
#        env:
#          PR_URL: ${{github.event.pull_request.html_url}}
#          GH_TOKEN: ${{secrets.GITHUB_TOKEN}}

#      - name: Enable auto-merge for Dependabot PRs
#        if: steps.metadata.outputs.update-type != 'version-update:semver-major'
#        run: gh pr merge --auto --merge "$PR_URL"
#        env:
#          PR_URL: ${{github.event.pull_request.html_url}}
#          GH_TOKEN: ${{secrets.GITHUB_TOKEN}}