如何将GitHub问题限制为仅允许一个被分配者?

huangapple go评论65阅读模式
英文:

How to restrict GitHub issues to one assignee?

问题

如何防止一个问题分配多个人?

英文:

https://stackoverflow.com/q/61781949/14731 asks about assigning more than 1 person per issue. I would like to do the opposite. How do I prevent users from assigning more than 1 person per issue?

答案1

得分: 0

GitHub不支持此功能,您需要设置一种替代方法,例如GitHub Action。

该操作会在每次更新问题时检查问题上的受让人数。
如果受让人数超过一个,它可以自动删除额外的受让人并/或发布一条解释政策的评论。

您可以使用例如actions/github-script,它使您可以轻松地在工作流中快速编写一个使用GitHub API和工作流运行上下文的脚本。

例如,在您的存储库中的.github/workflows/restrict_assignees.yml

name: 限制单个受让人

on:
  issues:
    types: [assigned]

jobs:
  强制单个受让人:
    runs-on: ubuntu-latest
    steps:
    - name: 检出代码
      uses: actions/checkout@v2

    - name: 限制为单个受让人
      run: |
        assignees=$(gh api repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/assignees --jq '.[].login' | wc -l)
        if [[ "$assignees" -gt 1 ]]; then
          first_assignee=$(gh api repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/assignees --jq '.[0].login')
          gh api -X DELETE repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/assignees -f assignees="$first_assignee"
          echo "问题 #${{ github.event.issue.number }} 现在仅分配给 $first_assignee。"
        fi        

从理论上讲,您可能会遇到一个特殊情况:如果两个用户同时被分配(例如,在同一个API请求中),它将删除两者然后重新添加第一个。
它也不考虑第一个受让人在操作运行之前可能被取消分配的可能性。

但作为第一个草案,您可以尝试它,看它是否基本满足您的要求。

英文:

Since that does not seem to be natively supported by GitHub, you would need to set up an alternative way, like a GitHub Action.

Said action would checks the number of assignees on an issue whenever it is updated.
If the number of assignees exceeds one, it could automatically remove the additional assignees and/or post a comment explaining the policy.

You can use for instance actions/github-script, which makes it easy to quickly write a script in your workflow that uses the GitHub API and the workflow run context.

For instance, as .github/workflows/restrict_assignees.yml in your repository:

name: Restrict to Single Assignee

on:
  issues:
    types: [assigned]

jobs:
  enforce-single-assignee:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Restrict to Single Assignee
      run: |
        assignees=$(gh api repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/assignees --jq '.[].login' | wc -l)
        if [[ "$assignees" -gt 1 ]]; then
          first_assignee=$(gh api repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/assignees --jq '.[0].login')
          gh api -X DELETE repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/assignees -f assignees="$first_assignee"
          echo "Issue #${{ github.event.issue.number }} is now only assigned to $first_assignee."
        fi        

In theory, you could have a corner case: if two users are assigned at the same time (for example, in the same API request), it will remove both and then re-add the first.
It also doesn't account for the possibility that the first assignee might be unassigned before the action runs.

But as a first draft, you can try it out and see if it mostly addresses your requirement.

huangapple
  • 本文由 发表于 2023年5月21日 23:07:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定