Skip to content

Commit

Permalink
ci: add pr langauge labeler
Browse files Browse the repository at this point in the history
  • Loading branch information
bky373 committed Aug 17, 2024
1 parent 6639ebc commit 16ba899
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions .github/workflows/pr-language-labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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));
"
js, ts, py, java, kt, ts, cpp, go, exs, swift

0 comments on commit 16ba899

Please sign in to comment.