name: PR Settings

on:
  pull_request:
    types: [ready_for_review]
    branches:
      - main

permissions:
  actions: write
  checks: write
  contents: write
  deployments: write
  id-token: write
  issues: write
  discussions: write
  packages: write
  pages: write
  pull-requests: write
  repository-projects: write
  security-events: write
  statuses: write

jobs:
  labels:
    runs-on: namespace-profile-btp-lite
    steps:
      - name: Configure 1Password
        uses: 1password/load-secrets-action/configure@581a835fb51b8e7ec56b71cf2ffddd7e68bb25e0 # v2
        with:
          service-account-token: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}

      - name: Load secrets
        uses: 1password/load-secrets-action@581a835fb51b8e7ec56b71cf2ffddd7e68bb25e0 # v2
        env:
          PR_SLACK_WEBHOOK_URL: op://platform/slack-webhooks/pr-webhook

      - name: Send GitHub Action trigger data to Slack workflow with reviewers
        if: ${{ tojson(github.event.pull_request.requested_reviewers) != '[]' && !github.event.pull_request.draft }}
        uses: slackapi/slack-github-action@485a9d42d3a73031f12ec201c457e2162c45d02d # v2.0.0
        with:
          webhook: ${{ env.PR_SLACK_WEBHOOK_URL }}
          webhook-type: webhook-trigger
          payload: |
            {
              "message": "${{ github.event.pull_request.title }} (by ${{ github.event.pull_request.user.login }} and to be checked by ${{ join(github.event.pull_request.requested_reviewers.*.login, ', ') }}) -> ${{ github.event.pull_request.html_url }}"
            }

      - name: Send GitHub Action trigger data to Slack workflow without reviewers
        if: ${{ !github.event.pull_request.draft }}
        uses: slackapi/slack-github-action@485a9d42d3a73031f12ec201c457e2162c45d02d # v2.0.0
        with:
          webhook: ${{ env.PR_SLACK_WEBHOOK_URL }}
          webhook-type: webhook-trigger
          payload: |
            {
              "message": "${{ github.event.pull_request.title }} (by ${{ github.event.pull_request.user.login }}) -> ${{ github.event.pull_request.html_url }}"
            }
