-
Notifications
You must be signed in to change notification settings - Fork 15
75 lines (62 loc) · 2.23 KB
/
pr-labeler.yml
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
name: PR Labeler 🏷️
on: [pull_request]
permissions:
contents: read
pull-requests: write
issues: write
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
function getFileExtensions(files) {
return files.map(file => path.extname(file).toLowerCase()).filter(ext => ext !== '');
}
async function addLabels(extensions) {
let labels = [];
let hasContentChanges = false;
let hasCodeChanges = false;
extensions.forEach(ext => {
if (ext === '.mdx') {
hasContentChanges = true;
} else {
hasCodeChanges = true;
}
})
if (hasContentChanges) {
labels.push('CONTENT');
}
if (hasCodeChanges) {
labels.push('CODE');
}
// Get current labels
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
const currentLabelNames = currentLabels.map(label => label.name);
// cross-reference current PR labels with the up-to-date label list
if (labels.length > 0 && labels.some(label => !currentLabelNames.includes(label))) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: labels
});
}
}
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
const changedFiles = files.map(f => f.filename);
const extensions = getFileExtensions(changedFiles);
await addLabels(extensions);