|
| 1 | +name: Label PR by Folder |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize] |
| 6 | + |
| 7 | +jobs: |
| 8 | + label-by-folder: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + steps: |
| 12 | + - name: Checkout code |
| 13 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 14 | + |
| 15 | + - name: Get changed files |
| 16 | + id: changed-files |
| 17 | + uses: tj-actions/changed-files@ed68ef82c095e0d48ec87eccea555d944a631a4c # v46.0.5 |
| 18 | + with: |
| 19 | + separator: "|" |
| 20 | + |
| 21 | + - name: Label PR based on folders |
| 22 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 23 | + with: |
| 24 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 25 | + script: | |
| 26 | + const prNumber = context.payload.pull_request.number; |
| 27 | + const changedFiles = "${{ steps.changed-files.outputs.all_changed_files }}".split("|"); |
| 28 | + const folderLabels = new Set(); |
| 29 | + |
| 30 | + for (const file of changedFiles) { |
| 31 | + // Match top-level folders (e.g., "docker-host/folder/something.txt" → "docker-host") |
| 32 | + const match = file.match(/^([^\/]+)/); // Extract the top-level folder from the file path |
| 33 | + // Skip .github folder as it contains workflow files, not project-specific code |
| 34 | + if (match && match[1] !== '.github') { |
| 35 | + const folder = match[1]; // no need to sanitize, no slashes |
| 36 | + folderLabels.add(folder); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if (folderLabels.size === 0) { |
| 41 | + console.log("No folder-based labels required for this PR."); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const labelsToAdd = [...folderLabels]; |
| 46 | + console.log(`Adding the following folder-based labels to the PR: ${labelsToAdd.join(", ")}`); |
| 47 | + |
| 48 | + await github.rest.issues.addLabels({ |
| 49 | + ...context.repo, |
| 50 | + issue_number: prNumber, |
| 51 | + labels: labelsToAdd, |
| 52 | + }); |
0 commit comments