2024-11-14 14:21:41 +01:00
|
|
|
name: Auto Label Pull Requests
|
|
|
|
|
|
|
|
on:
|
|
|
|
pull_request_target:
|
|
|
|
branches: ["main"]
|
|
|
|
types: [opened, synchronize, reopened, edited]
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
autolabeler:
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
permissions:
|
|
|
|
pull-requests: write
|
|
|
|
env:
|
|
|
|
CONFIG_PATH: .github/autolabeler-config.json
|
|
|
|
steps:
|
2025-02-10 14:50:04 +01:00
|
|
|
- name: Checkout repository
|
|
|
|
uses: actions/checkout@v4
|
|
|
|
|
2025-02-10 14:58:06 +01:00
|
|
|
- name: Install minimatch
|
|
|
|
run: npm install minimatch
|
|
|
|
|
2025-02-21 08:13:54 +01:00
|
|
|
- name: Label PR based on file changes and PR template
|
2024-11-14 14:21:41 +01:00
|
|
|
uses: actions/github-script@v7
|
|
|
|
with:
|
|
|
|
script: |
|
|
|
|
const fs = require('fs').promises;
|
2025-02-10 14:50:04 +01:00
|
|
|
const path = require('path');
|
2025-02-10 14:58:06 +01:00
|
|
|
const { minimatch } = require('minimatch');
|
2024-11-14 14:21:41 +01:00
|
|
|
|
2025-02-10 14:50:04 +01:00
|
|
|
const configPath = path.resolve(process.env.CONFIG_PATH);
|
2025-02-10 14:58:06 +01:00
|
|
|
const fileContent = await fs.readFile(configPath, 'utf-8');
|
|
|
|
const autolabelerConfig = JSON.parse(fileContent);
|
2025-02-21 08:13:54 +01:00
|
|
|
|
2024-11-14 14:21:41 +01:00
|
|
|
const prNumber = context.payload.pull_request.number;
|
2025-02-25 12:27:31 +02:00
|
|
|
const prBody = context.payload.pull_request.body.toLowerCase();
|
|
|
|
|
2025-02-21 08:13:54 +01:00
|
|
|
let labelsToAdd = new Set();
|
|
|
|
|
2025-02-10 14:58:06 +01:00
|
|
|
const prListFilesResponse = await github.rest.pulls.listFiles({
|
2024-11-14 14:21:41 +01:00
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
pull_number: prNumber,
|
2025-02-10 14:58:06 +01:00
|
|
|
});
|
|
|
|
const prFiles = prListFilesResponse.data;
|
2025-02-25 12:27:31 +02:00
|
|
|
|
|
|
|
const templateLabelMappings = {
|
|
|
|
"🐞 **bug fix**": "bugfix",
|
|
|
|
"✨ **new feature**": "feature",
|
|
|
|
"💥 **breaking change**": "breaking change",
|
|
|
|
"🆕 **new script**": "new script"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const [checkbox, label] of Object.entries(templateLabelMappings)) {
|
|
|
|
const escapedCheckbox = checkbox.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
|
|
|
|
const regex = new RegExp(`- \\[(x|X)\\]\\s*.*${escapedCheckbox}`, "i");
|
|
|
|
const match = prBody.match(regex);
|
|
|
|
if (match) {
|
|
|
|
console.log(`Match: ${match}`);
|
|
|
|
labelsToAdd.add(label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (labelsToAdd.size === 0) {
|
|
|
|
labelsToAdd.add("general");
|
|
|
|
}
|
2025-02-10 14:58:06 +01:00
|
|
|
|
2025-02-25 12:27:31 +02:00
|
|
|
// Apply labels based on file changes
|
2025-02-10 14:58:06 +01:00
|
|
|
for (const [label, rules] of Object.entries(autolabelerConfig)) {
|
|
|
|
const shouldAddLabel = prFiles.some((prFile) => {
|
|
|
|
return rules.some((rule) => {
|
|
|
|
const isFileStatusMatch = rule.fileStatus ? rule.fileStatus === prFile.status : true;
|
|
|
|
const isIncludeGlobMatch = rule.includeGlobs.some((glob) => minimatch(prFile.filename, glob));
|
|
|
|
const isExcludeGlobMatch = rule.excludeGlobs.some((glob) => minimatch(prFile.filename, glob));
|
2025-02-25 12:27:31 +02:00
|
|
|
|
2025-02-10 14:58:06 +01:00
|
|
|
return isFileStatusMatch && isIncludeGlobMatch && !isExcludeGlobMatch;
|
|
|
|
});
|
|
|
|
});
|
2024-11-14 14:21:41 +01:00
|
|
|
|
2025-02-10 14:58:06 +01:00
|
|
|
if (shouldAddLabel) {
|
2025-02-20 14:01:33 +01:00
|
|
|
labelsToAdd.add(label);
|
2024-11-14 14:21:41 +01:00
|
|
|
}
|
|
|
|
}
|
2025-02-25 12:27:31 +02:00
|
|
|
|
2025-02-21 10:04:16 +01:00
|
|
|
console.log(`Labels to add: ${Array.from(labelsToAdd).join(", ")}`);
|
|
|
|
|
2025-02-20 14:01:33 +01:00
|
|
|
if (labelsToAdd.size > 0) {
|
|
|
|
console.log(`Adding labels ${Array.from(labelsToAdd).join(", ")} to PR ${prNumber}`);
|
|
|
|
await github.rest.issues.addLabels({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
issue_number: prNumber,
|
|
|
|
labels: Array.from(labelsToAdd),
|
|
|
|
});
|
|
|
|
}
|