Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and improve example workflow #36384

Merged
merged 5 commits into from
Feb 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ If you no longer need access to {% data variables.product.prodname_copilot_short

## Automating the reminder with {% data variables.product.prodname_actions %}

The following example workflow uses the API to identify users in an organization who haven't used their license for 30 days, then creates an issue assigned to each user. This is a simple example that you can adapt to meet your needs.
The following example workflow uses the API to identify users in an organization who haven't used their license for 30 days or haven't used it at all since the seat was assigned, then creates an issue assigned to each user. This is a simple example that you can adapt to meet your needs.

To use this workflow:

Expand All @@ -79,7 +79,7 @@ To use this workflow:
1. Using the example below, create the workflow in the repository where you want the reminder issues to be created.

If you're new to {% data variables.product.prodname_actions %}, see [AUTOTITLE](/actions/writing-workflows/quickstart).
1. In the workflow, replace the `ORG/REPO` placeholders in the `gh` commands with the name of the repository where you want the reminder issues to be created. For example: `octo-org/octo-repo`.
1. If you want to create the issues in a repository other than the one in which the workflow is located, replace `${{ github.repository }}` in the `gh` commands with the name of the repository where you want the reminder issues to be created. For example: `octo-org/octo-repo`.

### Example workflow

Expand All @@ -89,55 +89,67 @@ To use this workflow:

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

# Run the workflow every day at 8am UTC
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 Copilot Last Activity
- name: Check last GitHub Copilot activity
id: check-last-activity
run: |
# Call the user management API
# 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 {% raw %}${{ secrets.COPILOT_LICENSE_READ }}{% endraw %}" \
/orgs/$ORGANIZATION_VAR/copilot/billing/seats)
/orgs/{% raw %}${{ github.repository_owner }}{% endraw %}/copilot/billing/seats)
echo "Raw Response from gh api:"
echo "$RESPONSE"

# Parse and check each user's `last_activity_at`
# 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')

# Replace ORG/REPO with the repository name
EXISTING_ISSUES=$(gh issue list --repo ORG/REPO --assignee $LOGIN --label 'copilot-reminder' --json id)
# List all open issues with label `copilot-reminder`
EXISTING_ISSUES=$(gh issue list --repo {% raw %}${{ github.repository }}{% endraw %} --assignee $LOGIN --label 'copilot-reminder' --json id)

# Convert dates to seconds since epoch for comparison
LAST_ACTIVITY_DATE=$(date -d "$LAST_ACTIVITY" +%s)
# 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"

# Replace ORG/REPO with the repository name
NEW_ISSUE_URL="$(gh issue create --title "Reminder about your GitHub Copilot license" --body "{% raw %}${{ vars.COPILOT_REMINDER_MESSAGE }}{% endraw %}" --repo ORG/REPO --assignee $LOGIN --label 'copilot-reminder')"
NEW_ISSUE_URL="$(gh issue create --title "Reminder about your GitHub Copilot license" --body "{% raw %}${{ vars.COPILOT_REMINDER_MESSAGE }}{% endraw %}" --repo {% raw %}${{ github.repository }}{% endraw %} --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 GITHUB_TOKEN, required for the `gh issue` commands
# Set the GH_TOKEN, required for the 'gh issue' commands
env:
GITHUB_TOKEN: {% raw %}${{ github.token }}{% endraw %}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

<!-- markdownlint-enable GHD021 -->
Expand Down
Loading