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

93 lines
3.5 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: 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
- 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-21 08:13:54 +01:00
const prBody = context.payload.pull_request.body.toLowerCase();
2025-02-21 10:04:16 +01:00
// Label-Sammlung (um doppelte API-Calls zu vermeiden)
2025-02-21 08:13:54 +01:00
let labelsToAdd = new Set();
2025-02-21 10:04:16 +01:00
// Prüfe Datei-Änderungen
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;
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));
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
}
}
2025-02-21 10:04:16 +01:00
// Prüfe PR-Template Checkboxen mit den korrekten Labels
2025-02-21 08:13:54 +01:00
const templateLabelMappings = {
2025-02-21 08:15:38 +01:00
"🐞 bug fix": "bugfix",
"✨ new feature": "feature",
"💥 breaking change": "breaking change",
"🆕 new script": "new script"
};
2025-02-21 08:13:54 +01:00
for (const [checkbox, label] of Object.entries(templateLabelMappings)) {
const regex = new RegExp(`- \\[(.*?)\\] ${checkbox}`, "i");
2025-02-21 08:13:54 +01:00
const match = prBody.match(regex);
if (match && match[1].trim() !== "") { // Checkbox ist gesetzt
labelsToAdd.add(label);
}
}
2025-02-21 10:04:16 +01:00
// Debugging: Anzeigen, welche Labels tatsächlich erkannt wurden
console.log(`Labels to add: ${Array.from(labelsToAdd).join(", ")}`);
// Labels setzen, falls neue erkannt wurden
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),
});
}