Update autolabeler.yml

This commit is contained in:
CanbiZ 2025-02-10 13:08:31 +01:00 committed by GitHub
parent 920245bba1
commit 69ff934a42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,50 +13,57 @@ jobs:
env: env:
CONFIG_PATH: .github/autolabeler-config.json CONFIG_PATH: .github/autolabeler-config.json
steps: steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install minimatch
run: npm install minimatch
- name: Label PR based on config rules - name: Label PR based on config rules
uses: actions/github-script@v7 uses: actions/github-script@v7
with: with:
script: | script: |
const fs = require('fs').promises; const fs = require('fs').promises;
const path = require('path');
const { minimatch } = require('minimatch'); const { minimatch } = require('minimatch');
const configPath = path.resolve(process.env.CONFIG_PATH); const configPath = process.env.CONFIG_PATH;
const fileContent = await fs.readFile(configPath, 'utf-8'); let config;
const autolabelerConfig = JSON.parse(fileContent); try {
const fileContent = await fs.readFile(configPath, 'utf-8');
config = JSON.parse(fileContent);
} catch (error) {
console.error(`❌ Fehler beim Laden der Konfigurationsdatei: ${error.message}`);
return;
}
const prNumber = context.payload.pull_request.number; const prNumber = context.payload.pull_request.number;
const prListFilesResponse = await github.rest.pulls.listFiles({ const prFiles = (await github.rest.pulls.listFiles({
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
pull_number: prNumber, pull_number: prNumber,
}); })).data;
const prFiles = prListFilesResponse.data;
for (const [label, rules] of Object.entries(autolabelerConfig)) { let labelsToAdd = new Set();
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;
});
});
if (shouldAddLabel) { for (const [label, rules] of Object.entries(config)) {
console.log(`Adding label ${label} to PR ${prNumber}`); if (prFiles.some(prFile =>
await github.rest.issues.addLabels({ rules.some(rule =>
owner: context.repo.owner, (!rule.fileStatus || rule.fileStatus === prFile.status) &&
repo: context.repo.repo, rule.includeGlobs.some(glob => minimatch(prFile.filename, glob)) &&
issue_number: prNumber, !rule.excludeGlobs.some(glob => minimatch(prFile.filename, glob))
labels: [label], )
}); )) {
labelsToAdd.add(label);
} }
} }
const existingLabels = new Set((await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
})).data.map(l => l.name));
labelsToAdd = [...labelsToAdd].filter(label => !existingLabels.has(label));
if (labelsToAdd.length > 0) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: labelsToAdd,
});
}