name: Build remote branch
run-name: "Build remote branch: ${{ inputs.repo }}#${{ inputs.branch }}"

on:
  workflow_call:
    inputs:
      repo:
        description: 'Repository'
        type: string
        required: true
      branch:
        description: 'Branch'
        type: string
        required: true
    outputs:
      artifact:
        description: "Artifact name"
        value: ${{ jobs.build.outputs.name }}
    secrets:
      RETE_QA_PUBLIC_PULL:
        description: 'GitHub Personal access token'
        required: true
  workflow_dispatch:
    inputs:
      repo:
        description: 'Repository'
        required: true
      branch:
        description: 'Branch'
        required: true

jobs:
  build:
    name: Build ${{ inputs.repo }}#${{ inputs.branch }}
    runs-on: ubuntu-latest
    outputs:
      name: ${{ steps.package-info.outputs.name }}
    steps:
      - name: Info
        run: |
          echo "Repository: ${{ inputs.repo }}"
          echo "Branch: ${{ inputs.branch }}"
      - uses: actions/checkout@v3
        with:
          repository: ${{ inputs.repo }}
          token: ${{ secrets.RETE_QA_PUBLIC_PULL }}
          ref: ${{ inputs.branch }}
      - name: Use Node.js
        uses: actions/setup-node@v4
      - name: Install dependencies
        run: npm ci
      - name: Run build
        run: npm run build
      - name: Get npm package name
        id: package-info
        working-directory: dist
        run: |
          PACKAGE_NAME=$(node -p "require('./package.json').name")
          SANITIZED_NAME=$(echo $PACKAGE_NAME | sed 's/[@/]/-/g')
          echo "name=$SANITIZED_NAME" >> $GITHUB_OUTPUT
      - name: Print package info
        run: echo ${{ steps.package-info.outputs.name }}
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ steps.package-info.outputs.name }}
          path: dist
