Allow users to set labels on PRs (#3936)

This commit is contained in:
Hind-M 2025-06-02 11:50:18 +02:00 committed by GitHub
parent 5789fe4f29
commit 703c8d7602
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,22 @@
# Description
<!-- Please include a summary of the changes and the related issue. -->
## Type of Change
<!-- Please skip this part if you are already using conventional commit keywords in the PR title. -->
- [ ] Bugfix
- [ ] Feature / enhancement
- [ ] CI / Documentation
- [ ] Maintenance
## Checklist
- [ ] My code follows the general style and conventions of the codebase, ensuring consistency
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] My changes generate no new warnings
- [ ] I have run `pre-commit run --all` locally in the source folder and confirmed that there are no linter errors.
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing tests pass locally with my changes

88
.github/workflows/set_pr_label.yml vendored Normal file
View File

@ -0,0 +1,88 @@
name: Set release label to PR based on title and description
on:
pull_request_target:
types:
- opened
- edited
permissions:
contents: read
issues: write
pull-requests: write
jobs:
set_label:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Assign label based on PR title
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pr_title = context.payload.pull_request.title;
// Define regex patterns for labels based on conventional commit keywords
const label_patterns_map = {
"release::bug_fixes": /^(fix)\b/i,
"release::enhancements": /^(feat|refactor|perf)\b/i,
"release::ci_docs": /^(ci|docs)\b/i,
"release::maintenance": /^(chore|style|test|build|revert)\b/i,
};
let assigned_label = null;
// Check each pattern
for (const [release_label, pattern] of Object.entries(label_patterns_map)) {
if (pattern.test(pr_title)) {
assigned_label = release_label;
break; // Assign first matching label
}
}
// If a label was matched, add it to PR
if (assigned_label) {
await github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: [assigned_label]
});
}
- name: Assign label based on PR description
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pr_body = context.payload.pull_request.body;
const label_map = {
"Bugfix": "release::bug_fixes",
"Feature / enhancement": "release::enhancements",
"CI / Documentation": "release::ci_docs",
"Maintenance": "release::maintenance"
};
let assigned_label = null;
for (const [checkbox_label, release_label] of Object.entries(label_map)) {
const checkbox_regex = new RegExp(`- \\[x\\] ${checkbox_label}`, 'mi');
if (checkbox_regex.test(pr_body)) {
assigned_label = release_label;
break; // Assign first matching label
}
}
// If a label was matched, add it to PR
if (assigned_label) {
await github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: [assigned_label]
});
}