# Adds/removes a 'code-owner-approved' label when a component-specific # codeowner approves (or dismisses) a PR. # # Handles non-fork PRs directly. For fork PRs the GITHUB_TOKEN is read-only, # so label writes are deferred to codeowner-approved-label-update.yml which # triggers via workflow_run with write permissions. name: Codeowner Approved Label on: pull_request_review: types: [submitted, dismissed] permissions: issues: write pull-requests: read contents: read jobs: codeowner-approved: name: Run if: ${{ github.repository == 'esphome/esphome' }} runs-on: ubuntu-latest steps: - name: Checkout base branch uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 with: ref: ${{ github.event.pull_request.base.sha }} sparse-checkout: | .github/scripts/codeowners.js CODEOWNERS - name: Check codeowner approval and update label uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 env: PR_NUMBER: ${{ github.event.pull_request.number }} with: script: | const { loadCodeowners, determineLabelAction, LabelAction } = require('./.github/scripts/codeowners.js'); const owner = context.repo.owner; const repo = context.repo.repo; const pr_number = parseInt(process.env.PR_NUMBER, 10); const LABEL_NAME = 'code-owner-approved'; console.log(`Processing PR #${pr_number} for codeowner approval label`); const codeownersPatterns = loadCodeowners(); const action = await determineLabelAction( github, owner, repo, pr_number, codeownersPatterns, LABEL_NAME ); if (action === LabelAction.NONE) { console.log('No label change needed'); return; } try { if (action === LabelAction.ADD) { await github.rest.issues.addLabels({ owner, repo, issue_number: pr_number, labels: [LABEL_NAME] }); console.log(`Added '${LABEL_NAME}' label`); } else if (action === LabelAction.REMOVE) { await github.rest.issues.removeLabel({ owner, repo, issue_number: pr_number, name: LABEL_NAME }); console.log(`Removed '${LABEL_NAME}' label`); } } catch (error) { if (error.status === 403) { console.log('Fork PR: deferring label write to phase 2 workflow'); } else if (error.status === 404) { console.log('Label already removed'); } else { throw error; } }