Mirror users.json with GitHub team #304
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Mirror users.json with GitHub team | |
on: | |
push: | |
branches: | |
- main | |
schedule: | |
- cron: '0 0 * * *' # Runs daily at midnight | |
workflow_dispatch: # Allows manual trigger | |
jobs: | |
mirror-users: | |
runs-on: ubuntu-latest | |
steps: | |
# This step sets up Node.js version 20 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: 20 | |
# This step installs the @octokit/action npm package | |
- run: npm install @octokit/[email protected] | |
# This step gets a GitHub App installation access token | |
- name: Get Token | |
id: get_workflow_token | |
uses: peter-murray/workflow-application-token-action@v3 | |
with: | |
application_id: ${{ secrets.APPLICATION_ID }} | |
application_private_key: ${{ secrets.APPLICATION_PRIVATE_KEY }} | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Collecting membership information | |
shell: pwsh | |
run: | | |
Write-Host "Authenticating with GitHub CLI" | |
echo ${{ steps.get_workflow_token.outputs.token }} | gh auth login --with-token | |
Write-Host "Getting users from file" | |
$users = (Get-Content users.json | ConvertFrom-Json).PSObject.Properties.Name | |
Write-Host "Users: $users" | |
Write-Host "Getting team members from Github API" | |
$team_members = gh api orgs/${{ github.repository_owner }}/teams/copilot-licenced-users/members | ConvertFrom-Json | ForEach-Object { $_.login } | |
Write-Host "Team members: $team_members" | |
# Add missing users to the team | |
Write-Host "Checking for missing users in team" | |
foreach ($user in $users) { | |
Write-Host "Checking $user" | |
if (-not $team_members.Contains($user)) { | |
Write-Host "Adding $user" | |
gh api --method PUT orgs/${{ github.repository_owner }}/teams/copilot-licenced-users/memberships/$user | |
} | |
} | |
# Remove users not in the list | |
Write-Host "Checking for users to remove from team" | |
foreach ($member in $team_members) { | |
Write-Host "Checking $member" | |
if (-not $users.Contains($member)) { | |
Write-Host "Removing $member" | |
gh api --method DELETE orgs/${{ github.repository_owner }}/teams/copilot-licenced-users/memberships/$member | |
} | |
} |