|
| 1 | +name: PR Language Labeler |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [ opened, ready_for_review, synchronize ] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + label: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@v4 |
| 17 | + |
| 18 | + - name: Set up Node.js |
| 19 | + uses: actions/setup-node@v4 |
| 20 | + with: |
| 21 | + node-version: '20' |
| 22 | + |
| 23 | + - name: Create package.json |
| 24 | + run: echo '{}' > package.json |
| 25 | + |
| 26 | + - name: Install dependencies |
| 27 | + run: npm install @octokit/rest node-fetch |
| 28 | + |
| 29 | + - name: Detect languages and add labels |
| 30 | + env: |
| 31 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 32 | + PR_NUM: ${{ github.event.pull_request.number }} |
| 33 | + run: | |
| 34 | + node --input-type=module -e " |
| 35 | + import { Octokit } from '@octokit/rest'; |
| 36 | + import path from 'path'; |
| 37 | + import fetch from 'node-fetch'; |
| 38 | +
|
| 39 | + const octokit = new Octokit({ |
| 40 | + auth: process.env.GITHUB_TOKEN, |
| 41 | + request: { fetch } |
| 42 | + }); |
| 43 | +
|
| 44 | + const extensionsToLanguages = { |
| 45 | + js: 'js', |
| 46 | + ts: 'ts', |
| 47 | + py: 'py', |
| 48 | + java: 'java', |
| 49 | + kt: 'kotlin', |
| 50 | + cpp: 'c++', |
| 51 | + go: 'go', |
| 52 | + exs: 'elixir', |
| 53 | + swift: 'swift' |
| 54 | + // 필요한 다른 확장자와 언어 매핑 추가 |
| 55 | + }; |
| 56 | +
|
| 57 | + function getRandomColor() { |
| 58 | + return Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0'); |
| 59 | + } |
| 60 | +
|
| 61 | + async function run() { |
| 62 | + const { data: files } = await octokit.pulls.listFiles({ |
| 63 | + owner: process.env.GITHUB_REPOSITORY.split('/')[0], |
| 64 | + repo: process.env.GITHUB_REPOSITORY.split('/')[1], |
| 65 | + pull_number: process.env.PR_NUM, |
| 66 | + }); |
| 67 | +
|
| 68 | + const languages = new Set(); |
| 69 | + files.forEach(file => { |
| 70 | + const ext = path.extname(file.filename).slice(1); |
| 71 | + if (extensionsToLanguages[ext]) { |
| 72 | + languages.add(extensionsToLanguages[ext]); |
| 73 | + } |
| 74 | + }); |
| 75 | +
|
| 76 | + for (const language of languages) { |
| 77 | + try { |
| 78 | + // Check if the label already exists |
| 79 | + await octokit.issues.getLabel({ |
| 80 | + owner: process.env.GITHUB_REPOSITORY.split('/')[0], |
| 81 | + repo: process.env.GITHUB_REPOSITORY.split('/')[1], |
| 82 | + name: language, |
| 83 | + }); |
| 84 | + } catch (error) { |
| 85 | + if (error.status === 404) { // Label does not exist |
| 86 | + const color = getRandomColor(); |
| 87 | + await octokit.issues.createLabel({ |
| 88 | + owner: process.env.GITHUB_REPOSITORY.split('/')[0], |
| 89 | + repo: process.env.GITHUB_REPOSITORY.split('/')[1], |
| 90 | + name: language, |
| 91 | + color: color, |
| 92 | + }); |
| 93 | + } else { |
| 94 | + throw error; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +
|
| 99 | + if (languages.size > 0) { |
| 100 | + await octokit.issues.addLabels({ |
| 101 | + owner: process.env.GITHUB_REPOSITORY.split('/')[0], |
| 102 | + repo: process.env.GITHUB_REPOSITORY.split('/')[1], |
| 103 | + issue_number: process.env.PR_NUM, |
| 104 | + labels: Array.from(languages), |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | +
|
| 109 | + run().catch(err => console.error(err)); |
| 110 | + " |
0 commit comments