Skip to content

Commit 169c672

Browse files
rufer7sunbrye
andauthored
Fix and improve example workflow (#36384)
Co-authored-by: Sunbrye Ly <56200261+sunbrye@users.noreply.github.com>
1 parent 2c30a1a commit 169c672

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

content/copilot/rolling-out-github-copilot-at-scale/reminding-inactive-users.md

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ If you no longer need access to {% data variables.product.prodname_copilot_short
5858

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

61-
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.
61+
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.
6262

6363
To use this workflow:
6464

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

8181
If you're new to {% data variables.product.prodname_actions %}, see [AUTOTITLE](/actions/writing-workflows/quickstart).
82-
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`.
82+
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`.
8383

8484
### Example workflow
8585

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

9090
``` yaml annotate
9191
# Name your workflow
92-
name: Remind inactive users about Copilot license
92+
name: Remind inactive users about GitHub Copilot license
9393

94-
# Run the workflow every day at 8am UTC
9594
on:
95+
# Run on demand (enables `Run workflow` button on the Actions tab to easily trigger a run manually)
96+
workflow_dispatch:
97+
# Run the workflow every day at 8am UTC
9698
schedule:
9799
- cron: '0 8 * * *'
98100

99101
jobs:
100102
context-log:
101103
runs-on: ubuntu-latest
104+
105+
# Modify the default permissions granted to GITHUB_TOKEN
106+
permissions:
107+
contents: read
108+
issues: write
109+
102110
steps:
103-
- name: Check Copilot Last Activity
111+
- name: Check last GitHub Copilot activity
104112
id: check-last-activity
105113
run: |
106-
# Call the user management API
114+
# List all GitHub Copilot seat assignments for an organization
107115
RESPONSE=$(gh api \
108116
-H "Accept: application/vnd.github+json" \
109117
-H "X-GitHub-Api-Version: 2022-11-28" \
110118
-H "Authorization: Bearer {% raw %}${{ secrets.COPILOT_LICENSE_READ }}{% endraw %}" \
111-
/orgs/$ORGANIZATION_VAR/copilot/billing/seats)
119+
/orgs/{% raw %}${{ github.repository_owner }}{% endraw %}/copilot/billing/seats)
112120
echo "Raw Response from gh api:"
113121
echo "$RESPONSE"
114122
115-
# Parse and check each user's `last_activity_at`
123+
# Parse and check each user's `last_activity_at` and `created_at`
116124
echo "$RESPONSE" | jq -c '.seats[]' | while read -r seat; do
117125
LOGIN=$(echo "$seat" | jq -r '.assignee.login')
118126
LAST_ACTIVITY=$(echo "$seat" | jq -r '.last_activity_at')
127+
CREATED_AT=$(echo "$seat" | jq -r '.created_at')
119128
120-
# Replace ORG/REPO with the repository name
121-
EXISTING_ISSUES=$(gh issue list --repo ORG/REPO --assignee $LOGIN --label 'copilot-reminder' --json id)
129+
# List all open issues with label `copilot-reminder`
130+
EXISTING_ISSUES=$(gh issue list --repo {% raw %}${{ github.repository }}{% endraw %} --assignee $LOGIN --label 'copilot-reminder' --json id)
122131
123-
# Convert dates to seconds since epoch for comparison
124-
LAST_ACTIVITY_DATE=$(date -d "$LAST_ACTIVITY" +%s)
132+
# Get last activity date and convert dates to seconds since epoch for comparison
133+
if [ "$LAST_ACTIVITY" = "null" ]; then
134+
LAST_ACTIVITY_DATE=$(date -d "$CREATED_AT" +%s)
135+
else
136+
LAST_ACTIVITY_DATE=$(date -d "$LAST_ACTIVITY" +%s)
137+
fi
125138
THIRTY_DAYS_AGO=$(date -d "30 days ago" +%s)
126139
127140
# Create issues for inactive users who don't have an existing open issue
128141
if [ "$LAST_ACTIVITY_DATE" -lt "$THIRTY_DAYS_AGO" ] && [ "$EXISTING_ISSUES" = "[]" ]; then
129142
echo "User $LOGIN has not been active in the last 30 days. Last activity: $LAST_ACTIVITY"
130143
131-
# Replace ORG/REPO with the repository name
132-
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')"
144+
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')"
133145
else
134146
echo "User $LOGIN is active or already has an assigned reminder issue. Last activity: $LAST_ACTIVITY"
135147
fi
136148
done
137149
138-
# Set the GITHUB_TOKEN, required for the `gh issue` commands
150+
# Set the GH_TOKEN, required for the 'gh issue' commands
139151
env:
140-
GITHUB_TOKEN: {% raw %}${{ github.token }}{% endraw %}
152+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
141153
```
142154
143155
<!-- markdownlint-enable GHD021 -->

0 commit comments

Comments
 (0)