# This workflow validates basic pull request formatting requirements are met.
#
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: BSD-2-Clause-Patent
#

name: Validate Pull Request Formatting

on:
  pull_request_target:
    branches:
      - master
    types:
      - edited
      - opened
      - reopened
      - synchronize

concurrency:
  group: pr-validation-${{ github.event.number }}
  cancel-in-progress: true

jobs:
  validate_pr:
    name: Validate Pull Request Formatting
    # Do not run on draft PRs and only run on PRs in the tianocore org
    if: ${{ github.event.pull_request.draft == false && github.repository_owner == 'tianocore' }}
    runs-on: ubuntu-latest

    steps:
      - name: Generate Token
        id: app-token
        uses: actions/create-github-app-token@v2
        with:
          app-id: ${{ secrets.TIANOCORE_PR_AUTOMATION_APPLICATION_ID }}
          private-key: ${{ secrets.TIANOCORE_PR_AUTOMATION_APPLICATION_PRIVATE_KEY }}

      # Reduce checkout time with sparse-checkout
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
          sparse-checkout: |
            .github/scripts/

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'
          cache: 'pip'
          cache-dependency-path: '.github/scripts/requirements.txt'

      - name: Install PIP Modules
        run: pip install -r .github/scripts/requirements.txt

      - name: Validate PR Format
        id: validate
        run: python .github/scripts/validate_pr_formatting.py
        env:
          GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
          OWNER: ${{ github.repository_owner }}
          PR_NUMBER: ${{ github.event.number }}
          REPO: ${{ github.event.pull_request.base.repo.name }}

      - name: Post Validation Comment
        if: steps.validate.outputs.validation_error == 'true'
        run: python .github/scripts/validate_pr_formatting.py --post-comment
        env:
          GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
          PR_NUMBER: ${{ github.event.number }}

      - name: Check for Validation Errors
        if: steps.validate.outputs.validation_error == 'true'
        uses: actions/github-script@v7
        with:
          script: |
            core.setFailed('PR Formatting Validation Check Failed!')
