name: "Pull Request Labeler"
on:
  pull_request_target:
    types: [opened]

permissions: {}

jobs:
  pr-labeler:
    if: github.repository == 'numpy/numpy'
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write  # to add labels
    steps:
    - name: Label the PR
      continue-on-error: true
      uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3  # v9.0.0
      with:
        script: |
          const yaml = require('js-yaml');
          const {data} = await github.rest.repos.getContent({
            owner: context.repo.owner,
            repo: context.repo.repo,
            path: '.github/pr-prefix-labeler.yml',
          });
          const prefixToLabel = yaml.load(
            Buffer.from(data.content, data.encoding).toString()
          );
          const title = context.payload.pull_request.title;
          for (const [prefix, label] of Object.entries(prefixToLabel)) {
            if (title.startsWith(prefix)) {
              await github.rest.issues.addLabels({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: context.payload.pull_request.number,
                labels: [label],
              });
              break;
            }
          }
