name: SonarQube Analysis

on:
  pull_request:
    types: [opened, synchronize, reopened]
    branches:
      - develop
  push:
    branches:
      - develop

env:
  PROJECT_NAME: qppa-repos-sonarqube-analysis

jobs:
  build:
    name: Quality Gate
    runs-on: ubuntu-latest
    timeout-minutes: 5

    permissions:
      id-token: write       # for IAM role via OIDC
      contents: read        # for actions/checkout

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Print branch name
        run: |
          echo "Current branch: ${GITHUB_HEAD_REF}"

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v6
        with:
          role-to-assume: arn:aws:iam::730335206644:role/delegatedadmin/developer/qppa-github-actions-role-1
          aws-region: us-east-1

      - name: Start SonarQube CodeBuild scan
        id: start_build
        run: |
          aws codebuild start-build \
            --project-name "${PROJECT_NAME}" \
            --source-type-override "GITHUB" \
            --source-location-override "${{ github.event.repository.html_url }}" \
            --source-version "${GITHUB_HEAD_REF}" \
            --environment-variables-override \
              name=GITHUB_REPO_NAME,value=${{ github.event.repository.name }},type=PLAINTEXT
