-
Notifications
You must be signed in to change notification settings - Fork 2
61 lines (51 loc) · 2.15 KB
/
pr-labeller.yaml
File metadata and controls
61 lines (51 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
name: Label PR by Folder
on:
pull_request:
types: [opened, synchronize]
permissions:
contents: read
pull-requests: write
jobs:
label-by-folder:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@ed68ef82c095e0d48ec87eccea555d944a631a4c # v46.0.5
with:
separator: "|"
- name: Label PR based on folders
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = context.payload.pull_request.number;
const changedFiles = "${{ steps.changed-files.outputs.all_changed_files }}".split("|");
const folderLabels = new Set();
for (const file of changedFiles) {
// Match top-level folders (e.g., "docker-host/folder/something.txt" → "docker-host")
const match = file.match(/^([^\/]+)/); // Extract the top-level folder from the file path
// Skip .github folder as it contains workflow files, not project-specific code
if (match && match[1] !== '.github') {
const folder = match[1]; // no need to sanitize, no slashes
folderLabels.add(folder);
}
}
if (folderLabels.size === 0) {
console.log("No folder-based labels required for this PR.");
return;
}
const labelsToAdd = [...folderLabels];
console.log(`Adding the following folder-based labels to the PR: ${labelsToAdd.join(", ")}`);
try {
await github.rest.issues.addLabels({
...context.repo,
issue_number: prNumber,
labels: labelsToAdd,
});
} catch (error) {
console.error(`Failed to add labels to PR #${prNumber}:`, error);
throw error; // Rethrow the error to ensure the workflow fails if this is critical
}