1
0
mirror of https://github.com/community-scripts/ProxmoxVE.git synced 2025-03-08 13:19:05 +00:00
ProxmoxVE/.github/workflows/autolabeler.yml

91 lines
3.3 KiB
YAML
Raw Normal View History

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: runner-cluster-htl-set
2024-11-14 14:21:41 +01:00
permissions:
pull-requests: write
env:
2025-02-26 15:51:30 +02:00
CONFIG_PATH: .github/autolabeler-config.json
2024-11-14 14:21:41 +01:00
steps:
2025-02-10 14:50:04 +01:00
- name: Checkout repository
uses: actions/checkout@v4
- 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');
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);
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();
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,
});
const prFiles = prListFilesResponse.data;
2025-02-25 12:27:31 +02:00
2025-02-25 12:27:31 +02:00
// Apply labels based on file changes
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
return isFileStatusMatch && isIncludeGlobMatch && !isExcludeGlobMatch;
});
});
2024-11-14 14:21:41 +01:00
if (shouldAddLabel) {
labelsToAdd.add(label);
2024-11-14 14:21:41 +01:00
}
}
const templateLabelMappings = {
"🐞 **Bug fix**": "bugfix",
"✨ **New feature**": "feature",
"💥 **Breaking change**": "breaking change",
};
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);
}
}
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(", ")}`);
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),
});
2025-02-26 15:51:30 +02:00
}