From 551e3974c972aed2947632ca365e176c7abd43d9 Mon Sep 17 00:00:00 2001 From: bky373 Date: Sat, 17 Aug 2024 21:36:35 +0900 Subject: [PATCH 1/4] ci: add pr langauge labeler --- .github/workflows/pr-language-labeler.yml | 110 ++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 .github/workflows/pr-language-labeler.yml diff --git a/.github/workflows/pr-language-labeler.yml b/.github/workflows/pr-language-labeler.yml new file mode 100644 index 000000000..68b6d714e --- /dev/null +++ b/.github/workflows/pr-language-labeler.yml @@ -0,0 +1,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)); + " From cfcb10315526865268fc4b2d864eb82c6b6c5d84 Mon Sep 17 00:00:00 2001 From: bky373 Date: Sat, 17 Aug 2024 22:10:32 +0900 Subject: [PATCH 2/4] rename job --- .github/workflows/pr-language-labeler.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-language-labeler.yml b/.github/workflows/pr-language-labeler.yml index 68b6d714e..b530323bd 100644 --- a/.github/workflows/pr-language-labeler.yml +++ b/.github/workflows/pr-language-labeler.yml @@ -9,7 +9,7 @@ permissions: pull-requests: write jobs: - label: + labeling-languages: runs-on: ubuntu-latest steps: - name: Checkout code From 2ccc4a4657ec87699214be0064b4fc4e011ef9f9 Mon Sep 17 00:00:00 2001 From: bky373 Date: Tue, 20 Aug 2024 12:32:30 +0900 Subject: [PATCH 3/4] apply reviews (Integrate into integration workflow) --- .github/workflows/integration.yaml | 74 +++++++++++++++ .github/workflows/pr-language-labeler.yml | 110 ---------------------- 2 files changed, 74 insertions(+), 110 deletions(-) delete mode 100644 .github/workflows/pr-language-labeler.yml diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index cd481d9a9..36537c593 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -9,3 +9,77 @@ jobs: steps: - uses: actions/checkout@v4 - uses: fernandrone/linelint@0.0.6 + + labeling-languages: + runs-on: ubuntu-latest + continue-on-error: true + 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: ${{ github.token }} + PR_NUM: ${{ github.event.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' + // 필요한 다른 확장자와 언어 매핑 추가 + }; + + 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]); + } + }); + + 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)); + " diff --git a/.github/workflows/pr-language-labeler.yml b/.github/workflows/pr-language-labeler.yml deleted file mode 100644 index b530323bd..000000000 --- a/.github/workflows/pr-language-labeler.yml +++ /dev/null @@ -1,110 +0,0 @@ -name: PR Language Labeler - -on: - pull_request: - types: [ opened, ready_for_review, synchronize ] - -permissions: - contents: write - pull-requests: write - -jobs: - labeling-languages: - 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)); - " From 5726380434c9124445f1424c79f47c263ccff6cd Mon Sep 17 00:00:00 2001 From: bky373 Date: Tue, 20 Aug 2024 22:09:49 +0900 Subject: [PATCH 4/4] Rename job --- .github/workflows/integration.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index 36537c593..cff96df39 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -10,7 +10,7 @@ jobs: - uses: actions/checkout@v4 - uses: fernandrone/linelint@0.0.6 - labeling-languages: + label-lang: runs-on: ubuntu-latest continue-on-error: true steps: