如何基于GitHub Issue Form下拉选项为Issue自动添加对应标签
实现方案
GitHub 原生的 YAML Issue 表单模板暂不支持直接根据用户选择的下拉选项动态分配标签,需要配合 GitHub Actions 完成该需求,具体配置步骤如下:
步骤1:确认Issue模板配置
你现有的 Bug 报告模板无需调整,保持id: bug-type的配置即可,方便后续 Actions 读取字段值。
# 你现有模板可直接沿用 name: Bug Report description: File a bug report labels: [triage] body: - type: dropdown id: bug-type attributes: label: Project multiple: true options: - API - WebUI validations: required: true
步骤2:新增自动打标签的GitHub Actions配置
在仓库的.github/workflows/目录下新建配置文件,命名为issue-auto-label.yml,填入以下内容:
name: Auto Label Issues by Project Type on: issues: types: [opened] jobs: add-labels: runs-on: ubuntu-latest permissions: issues: write steps: - name: Get dropdown selection value id: get-selection uses: actions/github-script@v7 with: script: | const issue = context.payload.issue; // 解析Issue表单内容,提取bug-type字段的值 const body = issue.body; const projectMatch = body.match(/### Project\s*\n\s*(.*?)\s*\n/); if (!projectMatch) return []; const selections = projectMatch[1].split(',').map(item => item.trim().toLowerCase()); return selections; - name: Add corresponding labels if: steps.get-selection.outputs.result != '[]' uses: actions/github-script@v7 with: script: | const labels = JSON.parse(process.env.SELECTIONS); if (labels.length === 0) return; github.rest.issues.addLabels({ owner: context.repo.owner, repo: context.repo.repo, issue_number: context.issue.number, labels: labels }); env: SELECTIONS: ${{ steps.get-selection.outputs.result }}
注意事项
- 提前在仓库的标签管理中创建
api、webui两个标签,大小写和配置中保持一致 - 如果后续下拉选项新增条目,只需要对应创建同名小写标签即可自动匹配,无需修改Actions配置
- 若下拉选项名称和标签名不匹配,可以在第一步的script中添加映射规则替换即可
内容的提问来源于stack exchange,提问作者MiHo
相关产品推荐
相关产品推荐

