-
Notifications
You must be signed in to change notification settings - Fork 126
110 lines (95 loc) ยท 3.33 KB
/
pr-language-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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: PR Language Labeler
on:
pull_request:
types: [ opened, ready_for_review, synchronize ]
permissions:
contents: write
pull-requests: write
jobs:
label:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Create package.json
run: echo '{}' > package.json
- name: Install dependencies
run: npm install @octokit/rest node-fetch
- name: Detect languages and add labels
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUM: ${{ github.event.pull_request.number }}
run: |
node --input-type=module -e "
import { Octokit } from '@octokit/rest';
import path from 'path';
import fetch from 'node-fetch';
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
request: { fetch }
});
const extensionsToLanguages = {
js: 'js',
ts: 'ts',
py: 'py',
java: 'java',
kt: 'kotlin',
cpp: 'c++',
go: 'go',
exs: 'elixir',
swift: 'swift'
// ํ์ํ ๋ค๋ฅธ ํ์ฅ์์ ์ธ์ด ๋งคํ ์ถ๊ฐ
};
function getRandomColor() {
return Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0');
}
async function run() {
const { data: files } = await octokit.pulls.listFiles({
owner: process.env.GITHUB_REPOSITORY.split('/')[0],
repo: process.env.GITHUB_REPOSITORY.split('/')[1],
pull_number: process.env.PR_NUM,
});
const languages = new Set();
files.forEach(file => {
const ext = path.extname(file.filename).slice(1);
if (extensionsToLanguages[ext]) {
languages.add(extensionsToLanguages[ext]);
}
});
for (const language of languages) {
try {
// Check if the label already exists
await octokit.issues.getLabel({
owner: process.env.GITHUB_REPOSITORY.split('/')[0],
repo: process.env.GITHUB_REPOSITORY.split('/')[1],
name: language,
});
} catch (error) {
if (error.status === 404) { // Label does not exist
const color = getRandomColor();
await octokit.issues.createLabel({
owner: process.env.GITHUB_REPOSITORY.split('/')[0],
repo: process.env.GITHUB_REPOSITORY.split('/')[1],
name: language,
color: color,
});
} else {
throw error;
}
}
}
if (languages.size > 0) {
await octokit.issues.addLabels({
owner: process.env.GITHUB_REPOSITORY.split('/')[0],
repo: process.env.GITHUB_REPOSITORY.split('/')[1],
issue_number: process.env.PR_NUM,
labels: Array.from(languages),
});
}
}
run().catch(err => console.error(err));
"