# 当添加标签时对议题发表评论

你可以使用 GitHub Actions 在应用特定标签时自动对议题添加评论。

> \[!NOTE]
> GitHub Enterprise Server 目前不支持 GitHub 托管的运行器。

## 简介

本教程演示如何在应用特定标签时使用 GitHub CLI 注释问题。 例如，当 `help wanted` 标签添加到问题中后，可以添加注释来建议参与者处理该问题。 有关 GitHub CLI 的详细信息，请参阅 [在工作流中使用 GitHub CLI](/zh/enterprise-server@3.22/actions/how-tos/write-workflows/choose-what-workflows-do/use-github-cli)。

在本教程中，你将首先创建一个使用 `gh issue comment` 命令添加问题注释的工作流文件。 然后，您将自定义工作流以适应您的需要。

## 创建工作流程

1. 选择要应用此项目管理工作流程的仓库。 您可以使用您有写入权限的现有仓库，或者创建一个新的仓库。 有关创建仓库的详细信息，请参阅 [创建新仓库](/zh/enterprise-server@3.22/repositories/creating-and-managing-repositories/creating-a-new-repository)。

2. 在存储库中，创建一个名为 `.github/workflows/YOUR_WORKFLOW.yml` 的文件，将 `YOUR_WORKFLOW` 替换为你选择的名称。 这是一个工作流程文件。 有关在 GitHub 上创建新文件的详细信息，请参阅 [创建新文件](/zh/enterprise-server@3.22/repositories/working-with-files/managing-files/creating-new-files)。

3. 将以下 YAML 内容复制到工作流程文件中。

   ```yaml copy
   name: Add comment
   on:
     issues:
       types:
         - labeled
   jobs:
     add-comment:
       if: github.event.label.name == 'help wanted'
       runs-on: ubuntu-latest
       permissions:
         issues: write
       steps:
         - name: Add comment
           run: gh issue comment "$NUMBER" --body "$BODY"
           env:
             GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
             GH_REPO: ${{ github.repository }}
             NUMBER: ${{ github.event.issue.number }}
             BODY: >
               This issue is available for anyone to work on.
               **Make sure to reference this issue in your pull request.**
               :sparkles: Thank you for your contribution! :sparkles:
   ```

4. 自定义工作流程文件中的参数。
   * 将 `help wanted` 中的 `if: github.event.label.name == 'help wanted'` 替换为要处理的标签。 如果想要在多个标签上操作，请使用 `||` 分隔条件。 例如，`if: github.event.label.name == 'bug' || github.event.label.name == 'fix me'` 将在 `bug` 或 `fix me` 标签添加到问题时进行注释。
   * 将 `BODY` 的值更改为要添加的注释。 支持 GitHub 风格的 Markdown。 有关 Markdown 的详细信息，请参阅“[基本写作和格式语法](/zh/enterprise-server@3.22/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax)”。

5. 将工作流程文件提交到仓库的默认分支。 有关详细信息，请参阅“[创建新文件](/zh/enterprise-server@3.22/repositories/working-with-files/managing-files/creating-new-files)”。

## 测试工作流程

每当仓库中的问题被标记时，此工作流就会运行。 如果添加的标签是工作流文件中指定的标签之一，`gh issue comment` 命令将添加你为该问题指定的注释。

通过将指定的标签应用于议题来测试工作流程。

1. 在仓库中打开一个议题。 有关详细信息，请参阅“[创建议题](/zh/enterprise-server@3.22/issues/tracking-your-work-with-issues/using-issues/creating-an-issue)”。
2. 使用工作流程文件中的指定标签标记议题。 有关详细信息，请参阅“[管理标签](/zh/enterprise-server@3.22/issues/using-labels-and-milestones-to-track-work/managing-labels#applying-a-label)”。
3. 要查看通过标记议题所触发的工作流程运行，请查看工作流程运行的历史记录。 有关详细信息，请参阅“[查看工作流程运行历史记录](/zh/enterprise-server@3.22/actions/how-tos/monitor-workflows/view-workflow-run-history)”。
4. 当工作流程完成时，应在您标记的议题上添加一条评论。

## 后续步骤

* 若要详细了解可以使用 GitHub CLI 执行的其他作（如编辑现有注释），请访问 [GitHub CLI 手册](https://cli.github.com/manual/)。