# 提醒非活动用户使用其GitHub Copilot 许可证

使用 GitHub API 识别非活动用户并帮助他们入门。

在企业中推出 GitHub Copilot 时，请务必跟踪哪些用户使用其 Copilot 许可证，以便通过重新分配未使用的许可证或帮助用户开始使用 Copilot来有效响应。

可以使用[列出组织的所有 Copilot 席位分配](/zh/rest/copilot/copilot-user-management#list-all-copilot-seat-assignments-for-an-organization) API 终结点来查找组织中分配有许可证的每个用户的最后活动日期。 然后，可以通过筛选一定时间内未使用其许可证的用户并向这些用户发送提醒来自动做出响应。

## 编写提醒消息

向非活跃用户发送的提醒应帮助用户克服采用 Copilot 时常见的障碍。 建议通过开展调查或采访开发人员来识别公司的特定阻碍因素。

例如，该消息可以包含帮助用户的信息和链接：

* 将 Copilot 安装到其环境中
* 将 Copilot 设置为可与贵公司的代理或防火墙配合使用。
* 在日常工作中充分利用 Copilot。

你还应明确传达在许可证仍不被使用的情况下你将采取的任何进一步的行动，例如撤销用户的许可证。

### 示例提醒

在下一部分中，我们将在自动化中使用此消息创建一个问题，并将其分配给每个非活动用户。

> 我们注意到，分配给您的 GitHub Copilot 许可证已经有 30 天未使用。 下面是一些可能帮助您入门的资源：
>
> * 如果您尚未在您的环境中设置好 Copilot，请参阅 [为自己设置GitHub Copilot](/zh/copilot/how-tos/set-up/set-up-for-self) 或 [排除GitHub Copilot的常见故障问题](/zh/copilot/how-tos/troubleshoot-copilot/troubleshoot-common-issues)。
> * 有关入门的最佳实践和建议，请参阅 [使用 GitHub Copilot 的最佳做法](/zh/copilot/get-started/best-practices) 或 [GitHub Copilot 对话助手的提示设计](/zh/copilot/concepts/prompting/prompt-engineering)。
> * 有关特定任务的示例，请参阅 [GitHub Copilot 指南](/zh/copilot/tutorials/copilot-cookbook)。
>
> 如果您不再需要对 Copilot 的访问权限，请在此问题中告知我们。 如果许可证在 30 天内保持非活动状态，我们将撤销该许可证，以便为其他用户释放access。

#### Markdown 提醒示例

<!-- markdownlint-disable search-replace -->

```markdown copy
We noticed you haven't used your assigned license for GitHub Copilot in 30 days. Here are some resources that might help you get started:

* If you haven't yet set up Copilot in your environment, see [Setting up GitHub Copilot for yourself](https://docs.github.com/en/copilot/setting-up-github-copilot/setting-up-github-copilot-for-yourself) or [Troubleshooting common issues with GitHub Copilot](https://docs.github.com/en/copilot/troubleshooting-github-copilot/troubleshooting-common-issues-with-github-copilot).
* For best practices and advice on getting started, see [Best practices for using GitHub Copilot](https://docs.github.com/en/copilot/using-github-copilot/best-practices-for-using-github-copilot) or [Prompt engineering for GitHub Copilot](https://docs.github.com/en/copilot/using-github-copilot/prompt-engineering-for-github-copilot).
* For examples related to specific tasks, see [Copilot Chat Cookbook](https://docs.github.com/en/copilot/example-prompts-for-github-copilot-chat).

If you no longer need access to Copilot, please let us know in this issue. If your license remains inactive for a further 30 days, we'll revoke it to free up access for another user.
```

<!-- markdownlint-enable search-replace -->

## 使用 GitHub Actions 将提醒自动化

以下示例工作流使用 API 识别组织中 30 天内未使用过其许可证或自分配席位以来根本没有使用许可证的用户，然后创建分配给每个用户的议题。 这是一个简单的示例，你可以对其进行调整来满足你的需求。

要使用此工作流，请：

1. 在将创建提醒问题的存储库中创建标签。 调用标签 `copilot-reminder`。 我们将使用此标签来检查每个非活动用户是否都已有处于打开状态的提醒问题。

   若要创建标签，请参阅“[管理标签](/zh/issues/using-labels-and-milestones-to-track-work/managing-labels#creating-a-label)”。
2. 将提醒消息（例如 [Markdown 中的提醒示例](#example-reminder-in-markdown)中提供的消息）保存为存储库或组织中的 GitHub Actions 变量。 调用变量 `COPILOT_REMINDER_MESSAGE`。

   若要创建变量，请参阅“[在变量中存储信息](/zh/actions/how-tos/write-workflows/choose-what-workflows-do/use-variables#creating-configuration-variables-for-a-repository)”。
3. 创建一个 personal access token，它应有权调用[列出组织中的所有 Copilot 席位分配](/zh/rest/copilot/copilot-user-management#list-all-copilot-seat-assignments-for-an-organization) API 终结点。 例如，创建具有下列详细信息的精细化的令牌：

   * **资源所有者**：用于查找非活动用户的组织。
   * **组织权限**： GitHub Copilot Business （只读）。

   若要创建令牌，请参阅 [管理个人访问令牌](/zh/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token)。
4. 将访问令牌保存为 GitHub Actions 存储库或组织中的机密。 调用密钥 `COPILOT_LICENSE_READ`。

   若要创建机密，请参阅 [在 GitHub Actions 中使用机密](/zh/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets#creating-secrets-for-a-repository)。
5. 通过使用以下示例，在要在其中创建提醒议题的仓库中创建工作流。

   如果你不熟悉 GitHub Actions，请参阅 [GitHub Actions快速入门](/zh/actions/get-started/quickstart)。
6. 如果要在不属于工作流所在的存储库中创建问题，请将 `${{ github.repository }}` 命令中的 `${{ github.repository }}` 替换为要在其中创建提醒问题的存储库的名称。 例如：`octo-org/octo-repo`。

### 示例工作流

> \[!NOTE] 此示例假定你通过组织分配许可证。 企业帐户和企业团队也存在类似的 API 终结点。 请参阅“[用于Copilot用户管理的 REST API 终结点](/zh/rest/copilot/copilot-user-management)”。

<!-- markdownlint-disable GHD021 -->

```yaml annotate
# Name your workflow
name: Remind inactive users about GitHub Copilot license

on:
  # Run on demand (enables `Run workflow` button on the Actions tab to easily trigger a run manually)
  workflow_dispatch:
  # Run the workflow every day at 8am UTC
  schedule:
    - cron: '0 8 * * *'

jobs:
  context-log:
    runs-on: ubuntu-latest

    # Modify the default permissions granted to GITHUB_TOKEN
    permissions:
      contents: read
      issues: write

    steps:
      - name: Check last GitHub Copilot activity
        id: check-last-activity
        run: |
          # List all GitHub Copilot seat assignments for an organization
          RESPONSE=$(gh api \
            -H "Accept: application/vnd.github+json" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            -H "Authorization: Bearer ${{ secrets.COPILOT_LICENSE_READ }}" \
            /orgs/${{ github.repository_owner }}/copilot/billing/seats)
          echo "Raw Response from gh api:"
          echo "$RESPONSE"

          # Parse and check each user's `last_activity_at` and `created_at`
          echo "$RESPONSE" | jq -c '.seats[]' | while read -r seat; do
            LOGIN=$(echo "$seat" | jq -r '.assignee.login')
            LAST_ACTIVITY=$(echo "$seat" | jq -r '.last_activity_at')
            CREATED_AT=$(echo "$seat" | jq -r '.created_at')

            # List all open issues with label `copilot-reminder`
            EXISTING_ISSUES=$(gh issue list --repo ${{ github.repository }} --assignee $LOGIN --label 'copilot-reminder' --json id)

            # Get last activity date and convert dates to seconds since epoch for comparison
            if [ "$LAST_ACTIVITY" = "null" ]; then
              LAST_ACTIVITY_DATE=$(date -d "$CREATED_AT" +%s)
            else
              LAST_ACTIVITY_DATE=$(date -d "$LAST_ACTIVITY" +%s)
            fi
            THIRTY_DAYS_AGO=$(date -d "30 days ago" +%s)

            # Create issues for inactive users who don't have an existing open issue
            if [ "$LAST_ACTIVITY_DATE" -lt "$THIRTY_DAYS_AGO" ] && [ "$EXISTING_ISSUES" = "[]" ]; then
              echo "User $LOGIN has not been active in the last 30 days. Last activity: $LAST_ACTIVITY"

              NEW_ISSUE_URL="$(gh issue create --title "Reminder about your GitHub Copilot license" --body "${{ vars.COPILOT_REMINDER_MESSAGE }}" --repo ${{ github.repository }} --assignee $LOGIN --label 'copilot-reminder')"
            else
              echo "User $LOGIN is active or already has an assigned reminder issue. Last activity: $LAST_ACTIVITY"
            fi
          done

        # Set the GH_TOKEN, required for the 'gh issue' commands
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

<!-- markdownlint-enable GHD021 -->

## 其他阅读材料

* [GitHub Copilot的指标数据属性](/zh/copilot/reference/metrics-data#last_activity_at)
* [推动您的公司采用GitHub Copilot](/zh/copilot/tutorials/roll-out-at-scale/enable-developers/drive-adoption)
* [使用 Copilot 使用情况指标跟踪许可证激活和初始使用情况](/zh/copilot/tutorials/roll-out-at-scale/assign-licenses/track-usage-and-adoption)