[HOWTO] Identify and remind inactive GitHub Copilot users

Recently, I was looking for a way to identify users who have a GitHub Copilot Business license seat assigned but are not actively using GitHub Copilot. To avoid unnecessary costs, I wanted to ask these users whether they still need the seat or whether it can be reassigned to another user who wants to actively use it.

Fortunately, a great new article was added to GitHub docs about 2 weeks ago:

Reminding inactive users to use their GitHub Copilot license

After reading through the article, I was thrilled because the example workflow completely covered my requirements and it is a pragmatic solution that can be implemented quickly. After several attempts and troubleshooting, I may have to revise the “can be implemented quickly” 😉

Unfortunately I had to deal with the following errors.

  • gh: Not Found (HTTP 404)
    This was the first and as well the easiest to solve. $ORGANIZATION_VAR is not defined in the example workflow. I just replaced it with ${{ github.repository_owner }} (information from GitHub context)
  • date: invalid date ‘null’
    The workflow run again failed because one of the users with a GitHub Copilot Business license seat assigned never used it and therefore last_activity_at was null. This I fixed by using created_at in such cases instead.
  • GraphQL: Resource not accessible by integration (repository.defaultBranchRef)
    This one was the hardest one to fix. But after some research I replaced the GITHUB_TOKEN environment variable with GH_TOKEN according to one of the official examples

After fixing the above mentioned errors, it worked! To avoid that others have to go through the same struggles, I opened an issue and a pull request to improve the example workflow.

Anyway, I share my working version below.

name: Remind inactive users about GitHub Copilot license

# Run the workflow every day at 8AM UTC
on:
  workflow_dispatch:
  schedule:
    - cron: '0 8 * * *'

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

    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 (https://docs.github.com/en/rest/copilot/copilot-user-management?apiVersion=2022-11-28#list-all-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')

            EXISTING_ISSUES=$(gh issue list --repo ${{ github.repository }} --assignee $LOGIN --label 'github-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 'github-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 }}

While I was looking for such a solution, I also stumbled across the GitHub Copilot Metrics Viewer for Power BI repository at github.com which I want to try out in a next step. I keep you posted!

Leave a comment

Website Powered by WordPress.com.

Up ↑