Grant Permissions #33
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: Remove Existing & Add New Collaborators | |
on: | |
schedule: | |
- cron: '0 16 * * *' | |
workflow_dispatch: | |
inputs: | |
usernames: | |
description: 'Usernames to grant permissions (comma-separated)' | |
required: true | |
jobs: | |
add-collaborator: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Remove existing collaborators | |
if: github.actor == 'texas-egads' | |
run: | | |
REPO_OWNER="EGaDSAustin" | |
REPO_NAME="JJ-0" | |
GH_TOKEN="${{ secrets.GH_TOKEN }}" | |
# Get the current collaborators with "write" permission | |
CURRENT_WRITE_COLLABORATORS=$(curl -s -H "Authorization: token $GH_TOKEN" \ | |
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/collaborators" | jq -r '.[] | select(.permissions.write == true and .collaborator == true) | .login') | |
# Remove each current write collaborator | |
for COLLABORATOR in $CURRENT_WRITE_COLLABORATORS; do | |
curl -X DELETE -H "Authorization: token $GH_TOKEN" \ | |
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/collaborators/$COLLABORATOR" | |
echo "Removed write collaborator: $COLLABORATOR" | |
done | |
env: | |
GH_TOKEN: ${{ secrets.GH_TOKEN }} | |
- name: Add collaborators | |
if: github.actor == 'texas-egads' | |
run: | | |
REPO_OWNER="EGaDSAustin" | |
REPO_NAME="JJ-0" | |
GH_TOKEN="${{ secrets.GH_TOKEN }}" | |
# Split comma-separated list of usernames into an array | |
IFS=',' read -ra USERNAMES <<< "${{ github.event.inputs.usernames }}" | |
for USERNAME in "${USERNAMES[@]}"; do | |
# Get the user's ID | |
USER_ID=$(curl -s -H "Authorization: token $GH_TOKEN" \ | |
"https://api.github.com/users/$USERNAME" | jq -r .id) | |
# Add the user as a collaborator | |
curl -X PUT -H "Authorization: token $GH_TOKEN" \ | |
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/collaborators/$USERNAME" \ | |
-d "{\"permission\":\"write\"}" | |
# Optionally, you can add some logging for each user | |
echo "Added write collaborator: $USERNAME" | |
done | |
env: | |
GH_TOKEN: ${{ secrets.GH_TOKEN }} |