-
-
Notifications
You must be signed in to change notification settings - Fork 0
91 lines (83 loc) · 2.93 KB
/
label-pr-based-on-paths.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
name: Auto-label PR based on file paths
on:
pull_request:
types: [opened, synchronize]
jobs:
label:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
issues: write
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Label PR based on changes
uses: actions/github-script@v7
with:
script: |
const prFiles = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
const labelsToColor = {
main: 'f66a0a', // Orange
'common-components': '1f77b4', // Blue
layout: '8c564b', // Brown
service: '2ca02c', // Green
styles: '9467bd', // Purple
content: 'ffdd57', // Yellow
types: 'd62728', // Red
};
const labels = new Set();
prFiles.data.forEach(file => {
if (file.filename.startsWith('src/app')) {
labels.add('main');
}
if (file.filename.startsWith('src/components/layout')) {
labels.add('layout');
}
if (file.filename.startsWith('src/components/common')) {
labels.add('common-components');
}
if (file.filename.startsWith('src/service')) {
labels.add('service');
}
if (file.filename.startsWith('src/styles')) {
labels.add('styles');
}
if (file.filename.startsWith('src/posts')) {
labels.add('content');
}
if (file.filename.startsWith('src/types.d.ts')) {
labels.add('types');
}
});
if (labels.size > 0) {
for (const label of labels) {
try {
// Check if the label exists
await github.rest.issues.getLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: label
});
} catch (error) {
// If label doesn't exist, create it with the specified color
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: label,
color: labelsToColor[label] || 'b0b0b0', // Use default gray if no color specified
});
}
}
// Add labels to the PR
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: Array.from(labels)
});
}