ProxmoxVE/.github/workflows/autolabeler.yml

101 lines
3.6 KiB
YAML
Raw Normal View History

2024-11-14 13:21:41 +00: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 13:50:04 +00:00
- name: Checkout repository
uses: actions/checkout@v4
- name: Install minimatch
run: npm install minimatch
- name: Label PR based on file changes
2024-11-14 13:21:41 +00:00
uses: actions/github-script@v7
with:
script: |
const fs = require('fs').promises;
2025-02-10 13:50:04 +00:00
const path = require('path');
const { minimatch } = require('minimatch');
2024-11-14 13:21:41 +00:00
2025-02-10 13:50:04 +00:00
const configPath = path.resolve(process.env.CONFIG_PATH);
const fileContent = await fs.readFile(configPath, 'utf-8');
const autolabelerConfig = JSON.parse(fileContent);
2024-11-14 13:21:41 +00:00
const prNumber = context.payload.pull_request.number;
const prListFilesResponse = await github.rest.pulls.listFiles({
2024-11-14 13:21:41 +00:00
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
const prFiles = prListFilesResponse.data;
let labelsToAdd = new Set();
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 13:21:41 +00:00
if (shouldAddLabel) {
labelsToAdd.add(label);
2024-11-14 13:21:41 +00: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),
});
}
- name: Label PR based on PR template selections
uses: actions/github-script@v7
with:
script: |
const prBody = context.payload.pull_request.body.toLowerCase();
const prNumber = context.payload.pull_request.number;
const labelMappings = {
"🐞 bug fix": "bug fix",
"✨ new feature": "new feature",
"💥 breaking change": "breaking change",
"🆕 new script": "new script"
};
let labelsToAdd = new Set();
for (const [checkbox, label] of Object.entries(labelMappings)) {
const regex = new RegExp(`- \\[(.*?)\\] ${checkbox}`, "i");
if (regex.test(prBody)) {
labelsToAdd.add(label);
}
}
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),
});
}