跳至主要内容

提醒未使用的用户使用其 GitHub Copilot 许可证

使用 GitHub API 识别未活跃用户并帮助他们快速入门。

谁可以使用此功能?

组织所有者和计费管理员

GitHub Copilot Business 或 GitHub Copilot Enterprise

在企业部署 GitHub Copilot 时,重要的是跟踪哪些用户正在使用他们的 Copilot 许可证,以便通过重新分配未使用的许可证或帮助用户快速上手 Copilot 来有效响应。

您可以使用 列出组织中所有 Copilot 座位分配 API 端点来获取分配了许可证的每位用户的最近活动日期。然后,您可以通过筛选出在一定时间内未使用许可证的用户并向他们发送提醒来自动响应。

编写提醒信息

对未活跃用户的提醒应帮助他们克服 Copilot 常见的采用障碍。我们建议通过开展调查或访谈开发者来识别贵公司特定的阻碍因素。

例如,信息可以包含帮助用户的相关信息和链接

  • 在其环境中安装 Copilot。
  • 设置 Copilot 以配合公司代理或防火墙工作。
  • 在日常工作中充分利用 Copilot。

您还应明确说明如果许可证继续未使用,您将采取的后续措施,例如撤销用户的许可证。

示例提醒

在下一节中,我们将使用此信息在自动化流程中创建分配给每位未活跃用户的问题。

我们注意到您已30天未使用分配给您的 GitHub Copilot 许可证。以下资源可能帮助您快速入门

如果您不再需要 Copilot 访问权限,请在此问题中告知我们。如果您的许可证在后续 30 天内仍未使用,我们将撤销该许可证,以便为其他用户释放访问权限。

Markdown 示例提醒

Markdown
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.

使用 GitHub Actions 自动化提醒

以下示例工作流使用 API 识别组织中在 30 天内未使用许可证或自分配座位以来从未使用过的用户,然后为每位用户创建一个指派给他的 issue。这是一个简易示例,您可以根据需要进行调整。

使用此工作流的步骤

  1. 在将创建提醒 issue 的仓库中创建一个标签。将标签命名为 copilot-reminder。我们将使用该标签检查每位未活跃用户是否已有打开的提醒 issue。

    要创建标签,请参阅 管理标签

  2. 将您的提醒信息(例如在 Markdown 示例提醒 中提供的)保存为仓库或组织中的 GitHub Actions 变量。将变量命名为 COPILOT_REMINDER_MESSAGE

    要创建变量,请参阅 在变量中存储信息

  3. 创建一个具有调用 列出组织中所有 Copilot 座位分配 API 端点权限的个人访问令牌。例如,创建一个具有以下详细信息的细粒度令牌

    • 资源所有者:您要查找未活跃用户的组织。
    • 组织权限:GitHub Copilot Business(只读)。

    要创建令牌,请参阅 管理个人访问令牌

  4. 将访问令牌保存为仓库或组织中的 GitHub Actions secret。将 secret 命名为 COPILOT_LICENSE_READ

    要创建 secret,请参阅 在 GitHub Actions 中使用 secret

  5. 使用下面的示例,在您希望创建提醒 issue 的仓库中创建工作流。

    如果您是 GitHub Actions 新手,请参阅 GitHub Actions 快速入门

  6. 如果您希望在不同于工作流所在仓库的仓库中创建 issue,请在 gh 命令中将 ${{ github.repository }} 替换为目标仓库的名称。例如:octo-org/octo-repo

示例工作流

注意

此示例假设您通过组织分配许可证。企业账户和企业团队也有类似的 API 端点。参阅 Copilot 用户管理的 REST API 端点

YAML
name: Remind inactive users about GitHub Copilot license
on:

为工作流命名

  workflow_dispatch:

按需运行(在 Actions 选项卡启用 Run workflow 按钮,以便手动触发运行)

  schedule:
    - cron: '0 8 * * *'
jobs:
  context-log:
    runs-on: ubuntu-latest

每天 UTC 时间 08:00 运行工作流

    permissions:
      contents: read
      issues: write
    steps:
      - name: Check last GitHub Copilot activity
        id: check-last-activity
        run: |

修改授予 GITHUB_TOKEN 的默认权限

          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"

列出组织中所有 GitHub Copilot 座位分配

          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')

解析并检查每位用户的 last_activity_atcreated_at

            EXISTING_ISSUES=$(gh issue list --repo ${{ github.repository }} --assignee $LOGIN --label 'copilot-reminder' --json id)

列出所有带有标签 copilot-reminder 的打开 issue

            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)

获取最近活动日期并将日期转换为自 epoch 起的秒数以进行比较

            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

为没有已打开 issue 的未活跃用户创建 issue

        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

设置 GH_TOKEN,‘gh issue’ 命令所必需的

# 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 }}

延伸阅读

© . This site is unofficial and not affiliated with GitHub, Inc.